diff --git a/src/compiler/compile/render_dom/index.ts b/src/compiler/compile/render_dom/index.ts index 6c0070e635..d92e5981a4 100644 --- a/src/compiler/compile/render_dom/index.ts +++ b/src/compiler/compile/render_dom/index.ts @@ -44,8 +44,8 @@ export default function dom( if (should_add_css) { body.push(b` - function ${add_css}(target) { - @append_style_if_not_present(target, "${component.stylesheet.id.replace('svelte-', '')}", "${styles}"); + function ${add_css}(options) { + @add_css_to_component(options, "${component.stylesheet.id.replace('svelte-', '')}", "${styles}"); } `); } @@ -529,7 +529,7 @@ export default function dom( constructor(options) { super(${options.dev && 'options'}); - ${should_add_css && b`@add_css_to_component(this, ${add_css}, options);`} + ${should_add_css && b`${add_css}(options);`} @init(this, options, ${definition}, ${has_create_fragment ? 'create_fragment' : 'null'}, ${not_equal}, ${prop_indexes}, ${dirty}); ${options.dev && b`@dispatch_dev("SvelteRegisterComponent", { component: this, tagName: "${name.name}", options, id: create_fragment.name });`} diff --git a/src/runtime/internal/Component.ts b/src/runtime/internal/Component.ts index cd7c68a3eb..334c58c33c 100644 --- a/src/runtime/internal/Component.ts +++ b/src/runtime/internal/Component.ts @@ -1,7 +1,7 @@ import { add_render_callback, flush, schedule_update, dirty_components } from './scheduler'; import { current_component, set_current_component } from './lifecycle'; import { blank_object, is_empty, is_function, run, run_all, noop } from './utils'; -import { children, detach } from './dom'; +import { append, children, detach, element } from './dom'; import { transition_in } from './transitions'; interface Fragment { @@ -100,9 +100,20 @@ function make_dirty(component, i) { let appendStylesTo = document.head; -export function add_css_to_component(add_css, { customStyleTag }) { - if (customStyleTag) appendStylesTo = customStyleTag; - add_css(appendStylesTo); +export function add_css_to_component( + { customStyleTag }, + styleSheetId: string, + styles: string, + styleId:string = `svelte-${styleSheetId}-style`) { + + if (customStyleTag) appendStylesTo = customStyleTag; + + if (!appendStylesTo.querySelector('#' + styleId)) { + const style = element('style'); + style.id = styleId; + style.textContent = styles; + append(appendStylesTo, style); + } } export function init(component, options, instance, create_fragment, not_equal, props, dirty = [-1]) { diff --git a/src/runtime/internal/dom.ts b/src/runtime/internal/dom.ts index 4d4da3f7bd..91e575ebf6 100644 --- a/src/runtime/internal/dom.ts +++ b/src/runtime/internal/dom.ts @@ -1,18 +1,5 @@ import { has_prop } from './utils'; -export function append_style_if_not_present( - target: Element, - styleSheetId: string, - styles: string, - styleId:string = `svelte-${styleSheetId}-style`) { - if (!target.querySelector('#' + styleId)) { - const style = element('style'); - style.id = styleId; - style.textContent = styles; - append(target, style); - } -} - export function append(target: Node, node: Node) { target.appendChild(node); }