use helper to encapsulate styles, rather than being repetitious (fixes #375)

pull/733/head
Rich Harris 8 years ago
parent 71047c2961
commit 70a99925a9

@ -25,6 +25,8 @@ export class DomGenerator extends Generator {
hasOutroTransitions: boolean; hasOutroTransitions: boolean;
hasComplexBindings: boolean; hasComplexBindings: boolean;
needsEncapsulateHelper: boolean;
constructor( constructor(
parsed: Parsed, parsed: Parsed,
source: string, source: string,
@ -38,6 +40,7 @@ export class DomGenerator extends Generator {
this.readonly = new Set(); this.readonly = new Set();
this.hydratable = options.hydratable; this.hydratable = options.hydratable;
this.needsEncapsulateHelper = false;
// initial values for e.g. window.innerWidth, if there's a <:Window> meta tag // initial values for e.g. window.innerWidth, if there's a <:Window> meta tag
this.metaBindings = []; this.metaBindings = [];
@ -131,6 +134,14 @@ export default function dom(
builder.addBlock(`[✂${parsed.js.content.start}-${parsed.js.content.end}✂]`); builder.addBlock(`[✂${parsed.js.content.start}-${parsed.js.content.end}✂]`);
} }
if (generator.needsEncapsulateHelper) {
builder.addBlock(deindent`
function @encapsulateStyles ( node ) {
@setAttribute( node, '${generator.stylesheet.id}', '' );
}
`);
}
if (generator.stylesheet.hasStyles && options.css !== false) { if (generator.stylesheet.hasStyles && options.css !== false) {
const { css, cssMap } = generator.stylesheet.render(options.filename); const { css, cssMap } = generator.stylesheet.render(options.filename);

@ -83,8 +83,9 @@ export default function visitElement(
// add CSS encapsulation attribute // add CSS encapsulation attribute
// TODO add a helper for this, rather than repeating it // TODO add a helper for this, rather than repeating it
if (node._needsCssAttribute) { if (node._needsCssAttribute) {
generator.needsEncapsulateHelper = true;
block.builders.hydrate.addLine( block.builders.hydrate.addLine(
`@setAttribute( ${name}, '${generator.stylesheet.id}', '' );` `@encapsulateStyles( ${name} );`
); );
} }

@ -143,6 +143,10 @@ var template = (function () {
}; };
}()); }());
function encapsulateStyles ( node ) {
setAttribute( node, 'svelte-3590263702', '' );
}
function add_css () { function add_css () {
var style = createElement( 'style' ); var style = createElement( 'style' );
style.id = 'svelte-3590263702-style'; style.id = 'svelte-3590263702-style';
@ -161,7 +165,7 @@ function create_main_fragment ( state, component ) {
}, },
hydrate: function ( nodes ) { hydrate: function ( nodes ) {
setAttribute( p, 'svelte-3590263702', '' ); encapsulateStyles( p );
}, },
mount: function ( target, anchor ) { mount: function ( target, anchor ) {

@ -8,6 +8,10 @@ var template = (function () {
}; };
}()); }());
function encapsulateStyles ( node ) {
setAttribute( node, 'svelte-3590263702', '' );
}
function add_css () { function add_css () {
var style = createElement( 'style' ); var style = createElement( 'style' );
style.id = 'svelte-3590263702-style'; style.id = 'svelte-3590263702-style';
@ -26,7 +30,7 @@ function create_main_fragment ( state, component ) {
}, },
hydrate: function ( nodes ) { hydrate: function ( nodes ) {
setAttribute( p, 'svelte-3590263702', '' ); encapsulateStyles( p );
}, },
mount: function ( target, anchor ) { mount: function ( target, anchor ) {

@ -131,6 +131,10 @@ var proto = {
set: set set: set
}; };
function encapsulateStyles ( node ) {
setAttribute( node, 'svelte-2363328337', '' );
}
function add_css () { function add_css () {
var style = createElement( 'style' ); var style = createElement( 'style' );
style.id = 'svelte-2363328337-style'; style.id = 'svelte-2363328337-style';
@ -148,7 +152,7 @@ function create_main_fragment ( state, component ) {
}, },
hydrate: function ( nodes ) { hydrate: function ( nodes ) {
setAttribute( div, 'svelte-2363328337', '' ); encapsulateStyles( div );
}, },
mount: function ( target, anchor ) { mount: function ( target, anchor ) {

@ -1,5 +1,9 @@
import { appendNode, assign, createElement, detachNode, dispatchObservers, insertNode, noop, proto, setAttribute } from "svelte/shared.js"; import { appendNode, assign, createElement, detachNode, dispatchObservers, insertNode, noop, proto, setAttribute } from "svelte/shared.js";
function encapsulateStyles ( node ) {
setAttribute( node, 'svelte-2363328337', '' );
}
function add_css () { function add_css () {
var style = createElement( 'style' ); var style = createElement( 'style' );
style.id = 'svelte-2363328337-style'; style.id = 'svelte-2363328337-style';
@ -17,7 +21,7 @@ function create_main_fragment ( state, component ) {
}, },
hydrate: function ( nodes ) { hydrate: function ( nodes ) {
setAttribute( div, 'svelte-2363328337', '' ); encapsulateStyles( div );
}, },
mount: function ( target, anchor ) { mount: function ( target, anchor ) {

Loading…
Cancel
Save