pull/366/merge
Cristian Necula 9 years ago committed by GitHub
commit 75aacff703

@ -207,8 +207,19 @@ export default function dom ( parsed, source, options, names ) {
_set: new CodeBuilder() _set: new CodeBuilder()
}; };
if ( parsed.css && options.css !== false ) { const includeCss = parsed.css && options.css !== false;
generator.current.builders.mount.addLine( `if ( !target.ownerDocument.__sveltecss_${parsed.hash} ) addCss( target.ownerDocument );` );
if ( includeCss ) {
generator.current.builders.mount.addBlock( deindent`
var _addCss = function() {
if ( !target.ownerDocument.__sveltecss_${parsed.hash} ) addCss( target.ownerDocument );
}
if ( component._root ) {
component._root._renderHooks.push({ fn: _addCss, context: this });
} else {
component._renderHooks.push({ fn: _addCss, context: this })
}
` );
builders.main.addBlock( deindent` builders.main.addBlock( deindent`
function addCss ( document ) { function addCss ( document ) {
@ -272,7 +283,7 @@ export default function dom ( parsed, source, options, names ) {
builders.init.addLine( `this._torndown = false;` ); builders.init.addLine( `this._torndown = false;` );
if ( generator.hasComponents ) { if ( generator.hasComponents || includeCss ) {
builders.init.addLine( `this._renderHooks = [];` ); builders.init.addLine( `this._renderHooks = [];` );
} }
@ -292,12 +303,14 @@ export default function dom ( parsed, source, options, names ) {
` ); ` );
} }
if ( generator.hasComponents ) { if ( generator.hasComponents || includeCss ) {
const statement = `this._flush();`; const statement = `this._flush();`;
builders.init.addBlock( statement ); builders.init.addBlock( statement );
if ( generator.hasComponents ) {
builders._set.addBlock( statement ); builders._set.addBlock( statement );
} }
}
if ( templateProperties.oncreate ) { if ( templateProperties.oncreate ) {
builders.init.addBlock( deindent` builders.init.addBlock( deindent`

@ -0,0 +1,7 @@
<p>test</p>
<style>
p {
color: red;
}
</style>

@ -1,21 +1,19 @@
export default { export default {
test ( assert, component, target, window ) { test ( assert, component, target, window ) {
const testStyles = ([control, test]) => {
assert.equal( window.getComputedStyle( control ).color, 'blue' );
assert.equal( window.getComputedStyle( test ).color, 'red' );
};
const iframe = window.document.createElement('iframe'); const iframe = window.document.createElement('iframe');
window.document.body.appendChild(iframe); window.document.body.appendChild(iframe);
const otherTarget = iframe.contentWindow.document.body; const otherTarget = iframe.contentWindow.document.body;
new component.constructor({ new component.constructor({
target: otherTarget target: otherTarget
}); });
assert.equal( testStyles(target.querySelectorAll( 'p' ));
window.getComputedStyle(target.querySelector('h1')).color, testStyles(otherTarget.querySelectorAll( 'p' ));
'rgb(255, 0, 0)'
);
assert.equal(
window.getComputedStyle(otherTarget.querySelector('h1')).color,
'rgb(255, 0, 0)'
);
} }
}; };

@ -1,6 +1,18 @@
<h1>Just some static HTML</h1> <p>control</p>
<div>
<Widget/>
</div>
<style> <style>
h1 { p {
color: rgb(255, 0, 0); color: blue;
} }
</style> </style>
<script>
import Widget from './Widget.html';
export default {
components: { Widget }
};
</script>

Loading…
Cancel
Save