scoped styles

pull/811/head
Rich Harris 8 years ago
parent 29fc3e5169
commit f5f35b5a50

@ -337,7 +337,7 @@ export default class Stylesheet {
}
}
render(cssOutputFilename: string) {
render(cssOutputFilename: string, shouldTransformSelectors: boolean) {
if (!this.hasStyles) {
return { css: null, cssMap: null };
}
@ -351,9 +351,11 @@ export default class Stylesheet {
}
});
this.children.forEach((child: (Atrule|Rule)) => {
child.transform(code, this.id, this.keyframes, this.cascade);
});
if (shouldTransformSelectors) {
this.children.forEach((child: (Atrule|Rule)) => {
child.transform(code, this.id, this.keyframes, this.cascade);
});
}
let c = 0;
this.children.forEach(child => {

@ -389,7 +389,9 @@ export default class Generator {
addString(finalChunk);
addString('\n\n' + getOutro(format, name, options, this.imports));
const { css, cssMap } = this.stylesheet.render(options.cssOutputFilename);
const { css, cssMap } = this.customElement ?
{ css: null, cssMap: null } :
this.stylesheet.render(options.cssOutputFilename, true);
return {
ast: this.ast,

@ -111,18 +111,17 @@ export default function dom(
`);
}
if (generator.stylesheet.hasStyles && options.css !== false) {
const { css, cssMap } = generator.stylesheet.render(options.filename);
const textContent = stringify(options.dev ?
`${css}\n/*# sourceMappingURL=${cssMap.toUrl()} */` :
css, { onlyEscapeAtSymbol: true });
const { css, cssMap } = generator.stylesheet.render(options.filename, !generator.customElement);
const styles = generator.stylesheet.hasStyles && stringify(options.dev ?
`${css}\n/*# sourceMappingURL=${cssMap.toUrl()} */` :
css, { onlyEscapeAtSymbol: true });
if (styles && generator.options.css !== false && !generator.customElement) {
builder.addBlock(deindent`
function @add_css () {
var style = @createElement( 'style' );
style.id = '${generator.stylesheet.id}-style';
style.textContent = ${textContent};
style.textContent = ${styles};
@appendNode( style, document.head );
}
`);
@ -148,7 +147,7 @@ export default function dom(
.join(',\n')}
}`;
const target = generator.customElement ? `this.attachShadow({ mode: 'open' })` : `options.target`;
const target = generator.customElement ? `this.shadowRoot` : `options.target`;
const anchor = generator.customElement ? `null` : `options.anchor || null`;
const constructorBody = deindent`
@ -184,9 +183,14 @@ export default function dom(
this._bind = options._bind;
${generator.slots.size && `this._slotted = options.slots || {};`}
${generator.stylesheet.hasStyles &&
options.css !== false &&
`if ( !document.getElementById( '${generator.stylesheet.id}-style' ) ) @add_css();`}
${generator.customElement ?
deindent`
this.attachShadow({ mode: 'open' });
${css && `this.shadowRoot.innerHTML = '<style>${options.dev ? `${css}\n/*# sourceMappingURL=${cssMap.toUrl()} */` : css}</style>';`}
` :
(generator.stylesheet.hasStyles && options.css !== false &&
`if ( !document.getElementById( '${generator.stylesheet.id}-style' ) ) @add_css();`)
}
${templateProperties.oncreate && `var oncreate = @template.oncreate.bind( this );`}

@ -88,7 +88,7 @@ export default function visitElement(
// add CSS encapsulation attribute
// TODO add a helper for this, rather than repeating it
if (node._needsCssAttribute) {
if (node._needsCssAttribute && !generator.customElement) {
generator.needsEncapsulateHelper = true;
block.builders.hydrate.addLine(
`@encapsulateStyles( ${name} );`

@ -94,7 +94,9 @@ export default function ssr(
visit(generator, mainBlock, node);
});
const { css, cssMap } = generator.stylesheet.render(options.filename);
const { css, cssMap } = generator.customElement ?
{ css: null, cssMap: null } :
generator.stylesheet.render(options.filename, true);
const result = deindent`
${hasJs && `[✂${parsed.js.content.start}-${parsed.js.content.end}✂]`}

@ -236,7 +236,7 @@ function SvelteComponent ( options ) {
this._fragment = create_main_fragment( this._state, this );
if ( options.target ) {
if ( !options._root ) {
this._fragment.create();
this._fragment.mount( options.target, options.anchor || null );
}

@ -71,7 +71,7 @@ function SvelteComponent ( options ) {
this._fragment = create_main_fragment( this._state, this );
if ( options.target ) {
if ( !options._root ) {
this._fragment.create();
this._fragment.mount( options.target, options.anchor || null );
}

@ -183,7 +183,7 @@ function SvelteComponent ( options ) {
this._fragment = create_main_fragment( this._state, this );
if ( options.target ) {
if ( !options._root ) {
this._fragment.create();
this._fragment.mount( options.target, options.anchor || null );
}

@ -42,7 +42,7 @@ function SvelteComponent ( options ) {
this._fragment = create_main_fragment( this._state, this );
if ( options.target ) {
if ( !options._root ) {
this._fragment.create();
this._fragment.mount( options.target, options.anchor || null );
}

@ -218,7 +218,7 @@ function SvelteComponent ( options ) {
this._fragment = create_main_fragment( this._state, this );
if ( options.target ) {
if ( !options._root ) {
this._fragment.create();
this._fragment.mount( options.target, options.anchor || null );
}

@ -57,7 +57,7 @@ function SvelteComponent ( options ) {
this._fragment = create_main_fragment( this._state, this );
if ( options.target ) {
if ( !options._root ) {
this._fragment.create();
this._fragment.mount( options.target, options.anchor || null );
}

@ -210,11 +210,13 @@ class SvelteComponent extends HTMLElement {
this._yield = options._yield;
this._bind = options._bind;
this.attachShadow({ mode: 'open' });
this._fragment = create_main_fragment( this._state, this );
if ( !options._root ) {
this._fragment.create();
this._fragment.mount( this.attachShadow({ mode: 'open' }), null );
this._fragment.mount( this.shadowRoot, null );
}
}

@ -49,11 +49,13 @@ class SvelteComponent extends HTMLElement {
this._yield = options._yield;
this._bind = options._bind;
this.attachShadow({ mode: 'open' });
this._fragment = create_main_fragment( this._state, this );
if ( !options._root ) {
this._fragment.create();
this._fragment.mount( this.attachShadow({ mode: 'open' }), null );
this._fragment.mount( this.shadowRoot, null );
}
}

@ -223,11 +223,13 @@ class SvelteComponent extends HTMLElement {
this._yield = options._yield;
this._bind = options._bind;
this.attachShadow({ mode: 'open' });
this._fragment = create_main_fragment( this._state, this );
if ( !options._root ) {
this._fragment.create();
this._fragment.mount( this.attachShadow({ mode: 'open' }), null );
this._fragment.mount( this.shadowRoot, null );
}
}

@ -58,11 +58,13 @@ class SvelteComponent extends HTMLElement {
this._yield = options._yield;
this._bind = options._bind;
this.attachShadow({ mode: 'open' });
this._fragment = create_main_fragment( this._state, this );
if ( !options._root ) {
this._fragment.create();
this._fragment.mount( this.attachShadow({ mode: 'open' }), null );
this._fragment.mount( this.shadowRoot, null );
}
}

@ -0,0 +1,5 @@
export default {
options: {
customElement: true
}
};

@ -0,0 +1,245 @@
function noop() {}
function assign(target) {
var k,
source,
i = 1,
len = arguments.length;
for (; i < len; i++) {
source = arguments[i];
for (k in source) target[k] = source[k];
}
return target;
}
function appendNode(node, target) {
target.appendChild(node);
}
function insertNode(node, target, anchor) {
target.insertBefore(node, anchor);
}
function detachNode(node) {
node.parentNode.removeChild(node);
}
function createElement(name) {
return document.createElement(name);
}
function createText(data) {
return document.createTextNode(data);
}
function destroy(detach) {
this.destroy = noop;
this.fire('destroy');
this.set = this.get = noop;
if (detach !== false) this._fragment.unmount();
this._fragment.destroy();
this._fragment = this._state = null;
}
function differs(a, b) {
return a !== b || ((a && typeof a === 'object') || typeof a === 'function');
}
function dispatchObservers(component, group, changed, newState, oldState) {
for (var key in group) {
if (!changed[key]) continue;
var newValue = newState[key];
var oldValue = oldState[key];
var callbacks = group[key];
if (!callbacks) continue;
for (var i = 0; i < callbacks.length; i += 1) {
var callback = callbacks[i];
if (callback.__calling) continue;
callback.__calling = true;
callback.call(component, newValue, oldValue);
callback.__calling = false;
}
}
}
function get(key) {
return key ? this._state[key] : this._state;
}
function fire(eventName, data) {
var handlers =
eventName in this._handlers && this._handlers[eventName].slice();
if (!handlers) return;
for (var i = 0; i < handlers.length; i += 1) {
handlers[i].call(this, data);
}
}
function observe(key, callback, options) {
var group = options && options.defer
? this._observers.post
: this._observers.pre;
(group[key] || (group[key] = [])).push(callback);
if (!options || options.init !== false) {
callback.__calling = true;
callback.call(this, this._state[key]);
callback.__calling = false;
}
return {
cancel: function() {
var index = group[key].indexOf(callback);
if (~index) group[key].splice(index, 1);
}
};
}
function on(eventName, handler) {
if (eventName === 'teardown') return this.on('destroy', handler);
var handlers = this._handlers[eventName] || (this._handlers[eventName] = []);
handlers.push(handler);
return {
cancel: function() {
var index = handlers.indexOf(handler);
if (~index) handlers.splice(index, 1);
}
};
}
function set(newState) {
this._set(assign({}, newState));
if (this._root._lock) return;
this._root._lock = true;
callAll(this._root._beforecreate);
callAll(this._root._oncreate);
callAll(this._root._aftercreate);
this._root._lock = false;
}
function _set(newState) {
var oldState = this._state,
changed = {},
dirty = false;
for (var key in newState) {
if (differs(newState[key], oldState[key])) changed[key] = dirty = true;
}
if (!dirty) return;
this._state = assign({}, oldState, newState);
this._recompute(changed, this._state, oldState, false);
if (this._bind) this._bind(changed, this._state);
dispatchObservers(this, this._observers.pre, changed, this._state, oldState);
this._fragment.update(changed, this._state);
dispatchObservers(this, this._observers.post, changed, this._state, oldState);
}
function callAll(fns) {
while (fns && fns.length) fns.pop()();
}
var proto = {
destroy: destroy,
get: get,
fire: fire,
observe: observe,
on: on,
set: set,
teardown: destroy,
_recompute: noop,
_set: _set
};
function create_main_fragment ( state, component ) {
var h1, text, text_1, text_2;
return {
create: function () {
h1 = createElement( 'h1' );
text = createText( "Hello " );
text_1 = createText( state.name );
text_2 = createText( "!" );
},
mount: function ( target, anchor ) {
insertNode( h1, target, anchor );
appendNode( text, h1 );
appendNode( text_1, h1 );
appendNode( text_2, h1 );
},
update: function ( changed, state ) {
if ( changed.name ) {
text_1.data = state.name;
}
},
unmount: function () {
detachNode( h1 );
},
destroy: noop
};
}
class SvelteComponent extends HTMLElement {
constructor(options = {}) {
super();
this.options = options;
this._state = options.data || {};
this._observers = {
pre: Object.create( null ),
post: Object.create( null )
};
this._handlers = Object.create( null );
this._root = options._root || this;
this._yield = options._yield;
this._bind = options._bind;
this.attachShadow({ mode: 'open' });
this.shadowRoot.innerHTML = '<style>h1{color:red}</style>';
this._fragment = create_main_fragment( this._state, this );
if ( !options._root ) {
this._fragment.create();
this._fragment.mount( this.shadowRoot, null );
}
}
static get observedAttributes() {
return ["name"];
}
get name() {
return this.get('name');
}
set name(value) {
this.set({ name: value });
}
attributeChangedCallback ( attr, oldValue, newValue ) {
this.set({ [attr]: newValue });
}
}
customElements.define('custom-element', SvelteComponent);
assign( SvelteComponent.prototype, proto );
export default SvelteComponent;

@ -0,0 +1,84 @@
import { appendNode, assign, createElement, createText, detachNode, insertNode, noop, proto } from "svelte/shared.js";
function create_main_fragment ( state, component ) {
var h1, text, text_1, text_2;
return {
create: function () {
h1 = createElement( 'h1' );
text = createText( "Hello " );
text_1 = createText( state.name );
text_2 = createText( "!" );
},
mount: function ( target, anchor ) {
insertNode( h1, target, anchor );
appendNode( text, h1 );
appendNode( text_1, h1 );
appendNode( text_2, h1 );
},
update: function ( changed, state ) {
if ( changed.name ) {
text_1.data = state.name;
}
},
unmount: function () {
detachNode( h1 );
},
destroy: noop
};
}
class SvelteComponent extends HTMLElement {
constructor(options = {}) {
super();
this.options = options;
this._state = options.data || {};
this._observers = {
pre: Object.create( null ),
post: Object.create( null )
};
this._handlers = Object.create( null );
this._root = options._root || this;
this._yield = options._yield;
this._bind = options._bind;
this.attachShadow({ mode: 'open' });
this.shadowRoot.innerHTML = '<style>h1{color:red}</style>';
this._fragment = create_main_fragment( this._state, this );
if ( !options._root ) {
this._fragment.create();
this._fragment.mount( this.shadowRoot, null );
}
}
static get observedAttributes() {
return ["name"];
}
get name() {
return this.get('name');
}
set name(value) {
this.set({ name: value });
}
attributeChangedCallback ( attr, oldValue, newValue ) {
this.set({ [attr]: newValue });
}
}
customElements.define('custom-element', SvelteComponent);
assign( SvelteComponent.prototype, proto );
export default SvelteComponent;

@ -0,0 +1,13 @@
<h1>Hello {{name}}!</h1>
<style>
h1 {
color: red;
}
</style>
<script>
export default {
tag: 'custom-element'
};
</script>

@ -328,7 +328,7 @@ function SvelteComponent ( options ) {
this._fragment = create_main_fragment( this._state, this );
if ( options.target ) {
if ( !options._root ) {
this._fragment.create();
this._fragment.mount( options.target, options.anchor || null );
}

@ -154,7 +154,7 @@ function SvelteComponent ( options ) {
this._fragment = create_main_fragment( this._state, this );
if ( options.target ) {
if ( !options._root ) {
this._fragment.create();
this._fragment.mount( options.target, options.anchor || null );
}

@ -227,7 +227,7 @@ function SvelteComponent ( options ) {
this._fragment = create_main_fragment( this._state, this );
if ( options.target ) {
if ( !options._root ) {
this._fragment.create();
this._fragment.mount( options.target, options.anchor || null );
}

@ -66,7 +66,7 @@ function SvelteComponent ( options ) {
this._fragment = create_main_fragment( this._state, this );
if ( options.target ) {
if ( !options._root ) {
this._fragment.create();
this._fragment.mount( options.target, options.anchor || null );
}

@ -269,7 +269,7 @@ function SvelteComponent ( options ) {
this._fragment = create_main_fragment( this._state, this );
if ( options.target ) {
if ( !options._root ) {
this._fragment.create();
this._fragment.mount( options.target, options.anchor || null );
}

@ -104,7 +104,7 @@ function SvelteComponent ( options ) {
this._fragment = create_main_fragment( this._state, this );
if ( options.target ) {
if ( !options._root ) {
this._fragment.create();
this._fragment.mount( options.target, options.anchor || null );
}

@ -245,7 +245,7 @@ function SvelteComponent ( options ) {
this._fragment = create_main_fragment( this._state, this );
if ( options.target ) {
if ( !options._root ) {
this._fragment.create();
this._fragment.mount( options.target, options.anchor || null );
}

@ -80,7 +80,7 @@ function SvelteComponent ( options ) {
this._fragment = create_main_fragment( this._state, this );
if ( options.target ) {
if ( !options._root ) {
this._fragment.create();
this._fragment.mount( options.target, options.anchor || null );
}

@ -203,7 +203,7 @@ function SvelteComponent ( options ) {
this._fragment = create_main_fragment( this._state, this );
if ( options.target ) {
if ( !options._root ) {
this._fragment.create();
this._fragment.mount( options.target, options.anchor || null );
}

@ -44,7 +44,7 @@ function SvelteComponent ( options ) {
this._fragment = create_main_fragment( this._state, this );
if ( options.target ) {
if ( !options._root ) {
this._fragment.create();
this._fragment.mount( options.target, options.anchor || null );
}

@ -225,7 +225,7 @@ function SvelteComponent ( options ) {
this._fragment = create_main_fragment( this._state, this );
if ( options.target ) {
if ( !options._root ) {
this._fragment.create();
this._fragment.mount( options.target, options.anchor || null );
}

@ -72,7 +72,7 @@ function SvelteComponent ( options ) {
this._fragment = create_main_fragment( this._state, this );
if ( options.target ) {
if ( !options._root ) {
this._fragment.create();
this._fragment.mount( options.target, options.anchor || null );
}

@ -190,7 +190,7 @@ function SvelteComponent ( options ) {
this._fragment = create_main_fragment( this._state, this );
if ( options.target ) {
if ( !options._root ) {
this._fragment.create();
this._fragment.mount( options.target, options.anchor || null );
}

@ -49,7 +49,7 @@ function SvelteComponent ( options ) {
this._fragment = create_main_fragment( this._state, this );
if ( options.target ) {
if ( !options._root ) {
this._fragment.create();
this._fragment.mount( options.target, options.anchor || null );
}

@ -192,7 +192,7 @@ function SvelteComponent ( options ) {
this._fragment = create_main_fragment( this._state, this );
if ( options.target ) {
if ( !options._root ) {
this._fragment.create();
this._fragment.mount( options.target, options.anchor || null );
}

@ -51,7 +51,7 @@ function SvelteComponent ( options ) {
this._fragment = create_main_fragment( this._state, this );
if ( options.target ) {
if ( !options._root ) {
this._fragment.create();
this._fragment.mount( options.target, options.anchor || null );
}

@ -429,7 +429,7 @@ function SvelteComponent ( options ) {
this._fragment = create_main_fragment( this._state, this );
if ( options.target ) {
if ( !options._root ) {
this._fragment.create();
this._fragment.mount( options.target, options.anchor || null );
}

@ -264,7 +264,7 @@ function SvelteComponent ( options ) {
this._fragment = create_main_fragment( this._state, this );
if ( options.target ) {
if ( !options._root ) {
this._fragment.create();
this._fragment.mount( options.target, options.anchor || null );
}

Loading…
Cancel
Save