Skip to content

Commit

Permalink
generators/dom: add styles to the root component's document
Browse files Browse the repository at this point in the history
1. store the _root's ownerDocument and inherit it in all child components
2. use this document when adding the styles
3. store a css presence flag on the document
  • Loading branch information
cristinecula committed Mar 3, 2017
1 parent 815d30f commit 2bc8a7e
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/generators/dom/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,13 +247,12 @@ export default function dom ( parsed, source, options, names ) {

if ( parsed.css && options.css !== false ) {
builders.main.addBlock( deindent`
let addedCss = false;
function addCss () {
function addCss (document) {
var style = ${generator.helper( 'createElement' )}( 'style' );
style.textContent = ${JSON.stringify( processCss( parsed, generator.code ) )};
${generator.helper( 'appendNode' )}( style, document.head );
addedCss = true;
document.__sveltecss_${parsed.hash} = true;
}
` );
}
Expand All @@ -264,7 +263,7 @@ export default function dom ( parsed, source, options, names ) {
builders.init.addLine( `this._torndown = false;` );

if ( parsed.css && options.css !== false ) {
builders.init.addLine( `if ( !addedCss ) addCss();` );
builders.init.addLine( `if ( !this._document.__sveltecss_${parsed.hash} ) addCss(this._document);` );
}

if ( generator.hasComponents ) {
Expand Down Expand Up @@ -342,6 +341,10 @@ export default function dom ( parsed, source, options, names ) {
this._root = options._root;
this._yield = options._yield;
this._document = this._root
? this._root._document
: options.target ? options.target.ownerDocument : document;
${builders.init}
` );

Expand Down

0 comments on commit 2bc8a7e

Please sign in to comment.