From 815d30fc9254ce6e1eaccb5dfc4edd3c1d6ff161 Mon Sep 17 00:00:00 2001 From: Cristian Necula Date: Fri, 3 Mar 2017 17:18:42 +0200 Subject: [PATCH 1/3] tests/render-in-iframe: added failing test for rendering in another document --- test/generator/render-in-iframe/_config.js | 21 +++++++++++++++++++++ test/generator/render-in-iframe/main.html | 6 ++++++ 2 files changed, 27 insertions(+) create mode 100644 test/generator/render-in-iframe/_config.js create mode 100644 test/generator/render-in-iframe/main.html diff --git a/test/generator/render-in-iframe/_config.js b/test/generator/render-in-iframe/_config.js new file mode 100644 index 000000000000..190a2b57dead --- /dev/null +++ b/test/generator/render-in-iframe/_config.js @@ -0,0 +1,21 @@ +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/render-in-iframe/main.html b/test/generator/render-in-iframe/main.html new file mode 100644 index 000000000000..c67978fa51f7 --- /dev/null +++ b/test/generator/render-in-iframe/main.html @@ -0,0 +1,6 @@ +

Just some static HTML

+ From 2bc8a7ebef7cdf49ccd003af1a93c2535ef17b9e Mon Sep 17 00:00:00 2001 From: Cristian Necula Date: Fri, 3 Mar 2017 17:18:58 +0200 Subject: [PATCH 2/3] generators/dom: add styles to the root component's document 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 --- src/generators/dom/index.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/generators/dom/index.js b/src/generators/dom/index.js index 709b5d84000d..cf7e5bd117b1 100644 --- a/src/generators/dom/index.js +++ b/src/generators/dom/index.js @@ -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; } ` ); } @@ -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 ) { @@ -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} ` ); From 7dce2a50e67e4fe79bcb6c0b8ca35f9ba68797da Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Fri, 10 Mar 2017 15:45:52 -0500 Subject: [PATCH 3/3] move test to new location --- test/generator/{ => samples}/render-in-iframe/_config.js | 0 test/generator/{ => samples}/render-in-iframe/main.html | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename test/generator/{ => samples}/render-in-iframe/_config.js (100%) rename test/generator/{ => samples}/render-in-iframe/main.html (100%) diff --git a/test/generator/render-in-iframe/_config.js b/test/generator/samples/render-in-iframe/_config.js similarity index 100% rename from test/generator/render-in-iframe/_config.js rename to test/generator/samples/render-in-iframe/_config.js diff --git a/test/generator/render-in-iframe/main.html b/test/generator/samples/render-in-iframe/main.html similarity index 100% rename from test/generator/render-in-iframe/main.html rename to test/generator/samples/render-in-iframe/main.html