Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CSS to each document a component is rendered to #362

Merged
merged 4 commits into from
Mar 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 18 additions & 21 deletions src/generators/dom/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,16 +201,30 @@ 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' ) {
Expand Down Expand Up @@ -253,28 +267,11 @@ 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 = [];` );
}
Expand Down
21 changes: 21 additions & 0 deletions test/generator/samples/render-in-iframe/_config.js
Original file line number Diff line number Diff line change
@@ -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)'
);
}
};
6 changes: 6 additions & 0 deletions test/generator/samples/render-in-iframe/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1>Just some static HTML</h1>
<style>
h1 {
color: rgb(255, 0, 0);
}
</style>