From ca9643495e742c1ae8045d3d172bf58d83526810 Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Wed, 15 Mar 2017 17:15:23 -0400 Subject: [PATCH] revert to previous addCss behaviour (#372) --- src/generators/dom/index.js | 39 ++++++++++--------- .../samples/render-in-iframe/_config.js | 21 ---------- .../samples/render-in-iframe/main.html | 6 --- 3 files changed, 21 insertions(+), 45 deletions(-) delete mode 100644 test/generator/samples/render-in-iframe/_config.js delete mode 100644 test/generator/samples/render-in-iframe/main.html diff --git a/src/generators/dom/index.js b/src/generators/dom/index.js index 51067279ac..26795ebff5 100644 --- a/src/generators/dom/index.js +++ b/src/generators/dom/index.js @@ -201,30 +201,16 @@ export default function dom ( parsed, source, options, names ) { getUniqueName: generator.getUniqueNameMaker() }); + parsed.html.children.forEach( node => generator.visit( node ) ); + + generator.addRenderer( generator.pop() ); + const builders = { main: new CodeBuilder(), init: new CodeBuilder(), _set: new CodeBuilder() }; - if ( parsed.css && options.css !== false ) { - generator.current.builders.mount.addLine( `if ( !target.ownerDocument.__sveltecss_${parsed.hash} ) addCss( target.ownerDocument );` ); - - builders.main.addBlock( deindent` - function addCss ( document ) { - var style = ${generator.helper( 'createElement' )}( 'style' ); - style.textContent = ${JSON.stringify( processCss( parsed, generator.code ) )}; - ${generator.helper( 'appendNode' )}( style, document.head ); - - document.__sveltecss_${parsed.hash} = true; - } - ` ); - } - - parsed.html.children.forEach( node => generator.visit( node ) ); - - generator.addRenderer( generator.pop() ); - if ( options.dev ) { builders._set.addBlock ( deindent` if ( typeof newState !== 'object' ) { @@ -267,11 +253,28 @@ export default function dom ( parsed, source, options, names ) { builders.main.addBlock( `[✂${parsed.js.content.start}-${parsed.js.content.end}✂]` ); } + if ( parsed.css && options.css !== false ) { + builders.main.addBlock( deindent` + var addedCss = false; + function addCss () { + var style = ${generator.helper( 'createElement' )}( 'style' ); + style.textContent = ${JSON.stringify( processCss( parsed, generator.code ) )}; + ${generator.helper( 'appendNode' )}( style, document.head ); + + addedCss = true; + } + ` ); + } + let i = generator.renderers.length; while ( i-- ) builders.main.addBlock( generator.renderers[i] ); builders.init.addLine( `this._torndown = false;` ); + if ( parsed.css && options.css !== false ) { + builders.init.addLine( `if ( !addedCss ) addCss();` ); + } + if ( generator.hasComponents ) { builders.init.addLine( `this._renderHooks = [];` ); } diff --git a/test/generator/samples/render-in-iframe/_config.js b/test/generator/samples/render-in-iframe/_config.js deleted file mode 100644 index 190a2b57de..0000000000 --- a/test/generator/samples/render-in-iframe/_config.js +++ /dev/null @@ -1,21 +0,0 @@ -export default { - test ( assert, component, target, window ) { - const iframe = window.document.createElement('iframe'); - window.document.body.appendChild(iframe); - - const otherTarget = iframe.contentWindow.document.body; - - new component.constructor({ - target: otherTarget - }); - - assert.equal( - window.getComputedStyle(target.querySelector('h1')).color, - 'rgb(255, 0, 0)' - ); - assert.equal( - window.getComputedStyle(otherTarget.querySelector('h1')).color, - 'rgb(255, 0, 0)' - ); - } -}; diff --git a/test/generator/samples/render-in-iframe/main.html b/test/generator/samples/render-in-iframe/main.html deleted file mode 100644 index c67978fa51..0000000000 --- a/test/generator/samples/render-in-iframe/main.html +++ /dev/null @@ -1,6 +0,0 @@ -

Just some static HTML

-