diff --git a/src/generators/dom/index.ts b/src/generators/dom/index.ts index f131b840b9..1c810ddeb7 100644 --- a/src/generators/dom/index.ts +++ b/src/generators/dom/index.ts @@ -147,9 +147,6 @@ export default function dom( .join(',\n')} }`; - const target = generator.customElement ? `this.shadowRoot` : `options.target`; - const anchor = generator.customElement ? `null` : `options.anchor || null`; - const constructorBody = deindent` ${options.dev && !generator.customElement && `if ( !options || (!options.target && !options._root) ) throw new Error( "'target' is a required option" );`} @@ -210,7 +207,7 @@ export default function dom( this._fragment = @create_main_fragment( this._state, this ); - if ( !options._root ) { + if ( options.target ) { ${generator.hydratable ? deindent` var nodes = @children( options.target ); @@ -221,7 +218,9 @@ export default function dom( ${options.dev && `if ( options.hydrate ) throw new Error( 'options.hydrate only works if the component was compiled with the \`hydratable: true\` option' );`} this._fragment.create(); `} - this._fragment.${block.hasIntroMethod ? 'intro' : 'mount'}( ${target}, ${anchor} ); + ${generator.customElement ? + `this._mount( options.target, options.anchor || null );` : + `this._fragment.${block.hasIntroMethod ? 'intro' : 'mount'}( options.target, options.anchor || null );`} ${(generator.hasComponents || generator.hasComplexBindings || templateProperties.oncreate || generator.hasIntroTransitions) && deindent` ${generator.hasComponents && `this._lock = true;`} @@ -257,6 +256,13 @@ export default function dom( } `).join('\n\n')} + ${generator.slots.size && deindent` + connectedCallback() { + Object.keys(this._slotted).forEach(key => { + this.appendChild(this._slotted[key]); + }); + }`} + attributeChangedCallback ( attr, oldValue, newValue ) { this.set({ [attr]: newValue }); } @@ -265,7 +271,7 @@ export default function dom( customElements.define('${generator.tag}', ${name}); @assign( ${prototypeBase}, ${proto}, { _mount(target, anchor) { - this._fragment.mount(this.shadowRoot, null); + this._fragment.${block.hasIntroMethod ? 'intro' : 'mount'}(this.shadowRoot, null); target.insertBefore(this, anchor); }, diff --git a/src/generators/dom/visitors/Element/Element.ts b/src/generators/dom/visitors/Element/Element.ts index a230b52cd3..1b72dd3a85 100644 --- a/src/generators/dom/visitors/Element/Element.ts +++ b/src/generators/dom/visitors/Element/Element.ts @@ -8,6 +8,7 @@ import visitEventHandler from './EventHandler'; import visitBinding from './Binding'; import visitRef from './Ref'; import * as namespaces from '../../../../utils/namespaces'; +import getStaticAttributeValue from '../../../shared/getStaticAttributeValue'; import addTransitions from './addTransitions'; import { DomGenerator } from '../../index'; import Block from '../../Block'; @@ -44,8 +45,13 @@ export default function visitElement( return meta[node.name](generator, block, node); } - if (node.name === 'slot' && !generator.customElement) { - return visitSlot(generator, block, state, node, elementStack, componentStack); + if (node.name === 'slot') { + if (generator.customElement) { + const slotName = getStaticAttributeValue(node, 'name') || 'default'; + generator.slots.add(slotName); + } else { + return visitSlot(generator, block, state, node, elementStack, componentStack); + } } if (generator.components.has(node.name) || node.name === ':Self') { diff --git a/test/js/samples/collapses-text-around-comments/expected-bundle.js b/test/js/samples/collapses-text-around-comments/expected-bundle.js index ae148c67cc..58ee113019 100644 --- a/test/js/samples/collapses-text-around-comments/expected-bundle.js +++ b/test/js/samples/collapses-text-around-comments/expected-bundle.js @@ -246,7 +246,7 @@ function SvelteComponent ( options ) { this._fragment = create_main_fragment( this._state, this ); - if ( !options._root ) { + if ( options.target ) { this._fragment.create(); this._fragment.mount( options.target, options.anchor || null ); } diff --git a/test/js/samples/collapses-text-around-comments/expected.js b/test/js/samples/collapses-text-around-comments/expected.js index deb4d4d303..88dd6ab34f 100644 --- a/test/js/samples/collapses-text-around-comments/expected.js +++ b/test/js/samples/collapses-text-around-comments/expected.js @@ -71,7 +71,7 @@ function SvelteComponent ( options ) { this._fragment = create_main_fragment( this._state, this ); - if ( !options._root ) { + if ( options.target ) { this._fragment.create(); this._fragment.mount( options.target, options.anchor || null ); } diff --git a/test/js/samples/computed-collapsed-if/expected-bundle.js b/test/js/samples/computed-collapsed-if/expected-bundle.js index af71c37d15..2d23af3ba8 100644 --- a/test/js/samples/computed-collapsed-if/expected-bundle.js +++ b/test/js/samples/computed-collapsed-if/expected-bundle.js @@ -193,7 +193,7 @@ function SvelteComponent ( options ) { this._fragment = create_main_fragment( this._state, this ); - if ( !options._root ) { + if ( options.target ) { this._fragment.create(); this._fragment.mount( options.target, options.anchor || null ); } diff --git a/test/js/samples/computed-collapsed-if/expected.js b/test/js/samples/computed-collapsed-if/expected.js index 6bb4ebaf9e..340622cd28 100644 --- a/test/js/samples/computed-collapsed-if/expected.js +++ b/test/js/samples/computed-collapsed-if/expected.js @@ -42,7 +42,7 @@ function SvelteComponent ( options ) { this._fragment = create_main_fragment( this._state, this ); - if ( !options._root ) { + if ( options.target ) { this._fragment.create(); this._fragment.mount( options.target, options.anchor || null ); } diff --git a/test/js/samples/css-media-query/expected-bundle.js b/test/js/samples/css-media-query/expected-bundle.js index d743041461..a62ee67ef9 100644 --- a/test/js/samples/css-media-query/expected-bundle.js +++ b/test/js/samples/css-media-query/expected-bundle.js @@ -228,7 +228,7 @@ function SvelteComponent ( options ) { this._fragment = create_main_fragment( this._state, this ); - if ( !options._root ) { + if ( options.target ) { this._fragment.create(); this._fragment.mount( options.target, options.anchor || null ); } diff --git a/test/js/samples/css-media-query/expected.js b/test/js/samples/css-media-query/expected.js index 23839e6c0d..86167afeb1 100644 --- a/test/js/samples/css-media-query/expected.js +++ b/test/js/samples/css-media-query/expected.js @@ -57,7 +57,7 @@ function SvelteComponent ( options ) { this._fragment = create_main_fragment( this._state, this ); - if ( !options._root ) { + if ( options.target ) { this._fragment.create(); this._fragment.mount( options.target, options.anchor || null ); } diff --git a/test/js/samples/custom-element-basic/expected-bundle.js b/test/js/samples/custom-element-basic/expected-bundle.js index 876bff4ece..abbc95a50f 100644 --- a/test/js/samples/custom-element-basic/expected-bundle.js +++ b/test/js/samples/custom-element-basic/expected-bundle.js @@ -224,9 +224,9 @@ class SvelteComponent extends HTMLElement { this._fragment = create_main_fragment( this._state, this ); - if ( !options._root ) { + if ( options.target ) { this._fragment.create(); - this._fragment.mount( this.shadowRoot, null ); + this._mount( options.target, options.anchor || null ); } } diff --git a/test/js/samples/custom-element-basic/expected.js b/test/js/samples/custom-element-basic/expected.js index 82dc7f2bb5..71db8c0cc8 100644 --- a/test/js/samples/custom-element-basic/expected.js +++ b/test/js/samples/custom-element-basic/expected.js @@ -53,9 +53,9 @@ class SvelteComponent extends HTMLElement { this._fragment = create_main_fragment( this._state, this ); - if ( !options._root ) { + if ( options.target ) { this._fragment.create(); - this._fragment.mount( this.shadowRoot, null ); + this._mount( options.target, options.anchor || null ); } } diff --git a/test/js/samples/custom-element-slot/expected-bundle.js b/test/js/samples/custom-element-slot/expected-bundle.js index 8ec67565ce..6a23c19f62 100644 --- a/test/js/samples/custom-element-slot/expected-bundle.js +++ b/test/js/samples/custom-element-slot/expected-bundle.js @@ -232,14 +232,17 @@ class SvelteComponent extends HTMLElement { this._root = options._root || this; this._yield = options._yield; this._bind = options._bind; + this._slotted = options.slots || {}; this.attachShadow({ mode: 'open' }); + this.slots = {}; + this._fragment = create_main_fragment( this._state, this ); - if ( !options._root ) { + if ( options.target ) { this._fragment.create(); - this._fragment.mount( this.shadowRoot, null ); + this._mount( options.target, options.anchor || null ); } } @@ -249,6 +252,12 @@ class SvelteComponent extends HTMLElement { + connectedCallback() { + Object.keys(this._slotted).forEach(key => { + this.appendChild(this._slotted[key]); + }); + } + attributeChangedCallback ( attr, oldValue, newValue ) { this.set({ [attr]: newValue }); } diff --git a/test/js/samples/custom-element-slot/expected.js b/test/js/samples/custom-element-slot/expected.js index c0e2ef136b..4e40cc559f 100644 --- a/test/js/samples/custom-element-slot/expected.js +++ b/test/js/samples/custom-element-slot/expected.js @@ -57,14 +57,17 @@ class SvelteComponent extends HTMLElement { this._root = options._root || this; this._yield = options._yield; this._bind = options._bind; + this._slotted = options.slots || {}; this.attachShadow({ mode: 'open' }); + this.slots = {}; + this._fragment = create_main_fragment( this._state, this ); - if ( !options._root ) { + if ( options.target ) { this._fragment.create(); - this._fragment.mount( this.shadowRoot, null ); + this._mount( options.target, options.anchor || null ); } } @@ -74,6 +77,12 @@ class SvelteComponent extends HTMLElement { + connectedCallback() { + Object.keys(this._slotted).forEach(key => { + this.appendChild(this._slotted[key]); + }); + } + attributeChangedCallback ( attr, oldValue, newValue ) { this.set({ [attr]: newValue }); } diff --git a/test/js/samples/custom-element-styled/expected-bundle.js b/test/js/samples/custom-element-styled/expected-bundle.js index ae41cfcccb..95ce6cdbac 100644 --- a/test/js/samples/custom-element-styled/expected-bundle.js +++ b/test/js/samples/custom-element-styled/expected-bundle.js @@ -225,9 +225,9 @@ class SvelteComponent extends HTMLElement { this._fragment = create_main_fragment( this._state, this ); - if ( !options._root ) { + if ( options.target ) { this._fragment.create(); - this._fragment.mount( this.shadowRoot, null ); + this._mount( options.target, options.anchor || null ); } } diff --git a/test/js/samples/custom-element-styled/expected.js b/test/js/samples/custom-element-styled/expected.js index f0b0e38020..e3b4769a03 100644 --- a/test/js/samples/custom-element-styled/expected.js +++ b/test/js/samples/custom-element-styled/expected.js @@ -54,9 +54,9 @@ class SvelteComponent extends HTMLElement { this._fragment = create_main_fragment( this._state, this ); - if ( !options._root ) { + if ( options.target ) { this._fragment.create(); - this._fragment.mount( this.shadowRoot, null ); + this._mount( options.target, options.anchor || null ); } } diff --git a/test/js/samples/each-block-changed-check/expected-bundle.js b/test/js/samples/each-block-changed-check/expected-bundle.js index 9f3048a816..b4b5d6f354 100644 --- a/test/js/samples/each-block-changed-check/expected-bundle.js +++ b/test/js/samples/each-block-changed-check/expected-bundle.js @@ -338,7 +338,7 @@ function SvelteComponent ( options ) { this._fragment = create_main_fragment( this._state, this ); - if ( !options._root ) { + if ( options.target ) { this._fragment.create(); this._fragment.mount( options.target, options.anchor || null ); } diff --git a/test/js/samples/each-block-changed-check/expected.js b/test/js/samples/each-block-changed-check/expected.js index c4cde082d8..7dcdb66696 100644 --- a/test/js/samples/each-block-changed-check/expected.js +++ b/test/js/samples/each-block-changed-check/expected.js @@ -154,7 +154,7 @@ function SvelteComponent ( options ) { this._fragment = create_main_fragment( this._state, this ); - if ( !options._root ) { + if ( options.target ) { this._fragment.create(); this._fragment.mount( options.target, options.anchor || null ); } diff --git a/test/js/samples/event-handlers-custom/expected-bundle.js b/test/js/samples/event-handlers-custom/expected-bundle.js index 472e46c8ac..7489f51cba 100644 --- a/test/js/samples/event-handlers-custom/expected-bundle.js +++ b/test/js/samples/event-handlers-custom/expected-bundle.js @@ -237,7 +237,7 @@ function SvelteComponent ( options ) { this._fragment = create_main_fragment( this._state, this ); - if ( !options._root ) { + if ( options.target ) { this._fragment.create(); this._fragment.mount( options.target, options.anchor || null ); } diff --git a/test/js/samples/event-handlers-custom/expected.js b/test/js/samples/event-handlers-custom/expected.js index 1af3570e3b..e713056e73 100644 --- a/test/js/samples/event-handlers-custom/expected.js +++ b/test/js/samples/event-handlers-custom/expected.js @@ -66,7 +66,7 @@ function SvelteComponent ( options ) { this._fragment = create_main_fragment( this._state, this ); - if ( !options._root ) { + if ( options.target ) { this._fragment.create(); this._fragment.mount( options.target, options.anchor || null ); } diff --git a/test/js/samples/if-block-no-update/expected-bundle.js b/test/js/samples/if-block-no-update/expected-bundle.js index 30fcae061a..e390e11fef 100644 --- a/test/js/samples/if-block-no-update/expected-bundle.js +++ b/test/js/samples/if-block-no-update/expected-bundle.js @@ -279,7 +279,7 @@ function SvelteComponent ( options ) { this._fragment = create_main_fragment( this._state, this ); - if ( !options._root ) { + if ( options.target ) { this._fragment.create(); this._fragment.mount( options.target, options.anchor || null ); } diff --git a/test/js/samples/if-block-no-update/expected.js b/test/js/samples/if-block-no-update/expected.js index f36c614c95..ca082058f4 100644 --- a/test/js/samples/if-block-no-update/expected.js +++ b/test/js/samples/if-block-no-update/expected.js @@ -104,7 +104,7 @@ function SvelteComponent ( options ) { this._fragment = create_main_fragment( this._state, this ); - if ( !options._root ) { + if ( options.target ) { this._fragment.create(); this._fragment.mount( options.target, options.anchor || null ); } diff --git a/test/js/samples/if-block-simple/expected-bundle.js b/test/js/samples/if-block-simple/expected-bundle.js index 9ac38ae5fe..efca4b8673 100644 --- a/test/js/samples/if-block-simple/expected-bundle.js +++ b/test/js/samples/if-block-simple/expected-bundle.js @@ -255,7 +255,7 @@ function SvelteComponent ( options ) { this._fragment = create_main_fragment( this._state, this ); - if ( !options._root ) { + if ( options.target ) { this._fragment.create(); this._fragment.mount( options.target, options.anchor || null ); } diff --git a/test/js/samples/if-block-simple/expected.js b/test/js/samples/if-block-simple/expected.js index eaf0415502..1eedecd147 100644 --- a/test/js/samples/if-block-simple/expected.js +++ b/test/js/samples/if-block-simple/expected.js @@ -80,7 +80,7 @@ function SvelteComponent ( options ) { this._fragment = create_main_fragment( this._state, this ); - if ( !options._root ) { + if ( options.target ) { this._fragment.create(); this._fragment.mount( options.target, options.anchor || null ); } diff --git a/test/js/samples/inline-style-optimized-multiple/expected-bundle.js b/test/js/samples/inline-style-optimized-multiple/expected-bundle.js new file mode 100644 index 0000000000..c9f7b3729a --- /dev/null +++ b/test/js/samples/inline-style-optimized-multiple/expected-bundle.js @@ -0,0 +1,221 @@ +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 insertNode(node, target, anchor) { + target.insertBefore(node, anchor); +} + +function detachNode(node) { + node.parentNode.removeChild(node); +} + +function createElement(name) { + return document.createElement(name); +} + +function setStyle(node, key, value) { + node.style.setProperty(key, value); +} + +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 div; + + return { + create: function () { + div = createElement( 'div' ); + this.hydrate(); + }, + + hydrate: function ( nodes ) { + setStyle(div, 'color', state.color); + setStyle(div, 'transform', "translate(" + state.x + "px," + state.y + "px)"); + }, + + mount: function ( target, anchor ) { + insertNode( div, target, anchor ); + }, + + update: function ( changed, state ) { + if ( changed.color ) { + setStyle(div, 'color', state.color); + } + + if ( changed.x || changed.y ) { + setStyle(div, 'transform', "translate(" + state.x + "px," + state.y + "px)"); + } + }, + + unmount: function () { + detachNode( div ); + }, + + destroy: noop + }; +} + +function SvelteComponent ( options ) { + 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._fragment = create_main_fragment( this._state, this ); + + if ( options.target ) { + this._fragment.create(); + this._fragment.mount( options.target, options.anchor || null ); + } +} + +assign( SvelteComponent.prototype, proto ); + +export default SvelteComponent; diff --git a/test/js/samples/inline-style-optimized-multiple/expected.js b/test/js/samples/inline-style-optimized-multiple/expected.js new file mode 100644 index 0000000000..3d48ec09a8 --- /dev/null +++ b/test/js/samples/inline-style-optimized-multiple/expected.js @@ -0,0 +1,64 @@ +import { assign, createElement, detachNode, insertNode, noop, proto, setStyle } from "svelte/shared.js"; + +function create_main_fragment ( state, component ) { + var div; + + return { + create: function () { + div = createElement( 'div' ); + this.hydrate(); + }, + + hydrate: function ( nodes ) { + setStyle(div, 'color', state.color); + setStyle(div, 'transform', "translate(" + state.x + "px," + state.y + "px)"); + }, + + mount: function ( target, anchor ) { + insertNode( div, target, anchor ); + }, + + update: function ( changed, state ) { + if ( changed.color ) { + setStyle(div, 'color', state.color); + } + + if ( changed.x || changed.y ) { + setStyle(div, 'transform', "translate(" + state.x + "px," + state.y + "px)"); + } + }, + + unmount: function () { + detachNode( div ); + }, + + destroy: noop + }; +} + +function SvelteComponent ( options ) { + 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._fragment = create_main_fragment( this._state, this ); + + if ( options.target ) { + this._fragment.create(); + this._fragment.mount( options.target, options.anchor || null ); + } +} + +assign( SvelteComponent.prototype, proto ); + +export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/inline-style-optimized-url/expected-bundle.js b/test/js/samples/inline-style-optimized-url/expected-bundle.js new file mode 100644 index 0000000000..e3793b4fe1 --- /dev/null +++ b/test/js/samples/inline-style-optimized-url/expected-bundle.js @@ -0,0 +1,216 @@ +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 insertNode(node, target, anchor) { + target.insertBefore(node, anchor); +} + +function detachNode(node) { + node.parentNode.removeChild(node); +} + +function createElement(name) { + return document.createElement(name); +} + +function setStyle(node, key, value) { + node.style.setProperty(key, value); +} + +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 div; + + return { + create: function () { + div = createElement( 'div' ); + this.hydrate(); + }, + + hydrate: function ( nodes ) { + setStyle(div, 'background', "url(data:image/png;base64," + state.data + ")"); + }, + + mount: function ( target, anchor ) { + insertNode( div, target, anchor ); + }, + + update: function ( changed, state ) { + if ( changed.data ) { + setStyle(div, 'background', "url(data:image/png;base64," + state.data + ")"); + } + }, + + unmount: function () { + detachNode( div ); + }, + + destroy: noop + }; +} + +function SvelteComponent ( options ) { + 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._fragment = create_main_fragment( this._state, this ); + + if ( options.target ) { + this._fragment.create(); + this._fragment.mount( options.target, options.anchor || null ); + } +} + +assign( SvelteComponent.prototype, proto ); + +export default SvelteComponent; diff --git a/test/js/samples/inline-style-optimized-url/expected.js b/test/js/samples/inline-style-optimized-url/expected.js new file mode 100644 index 0000000000..b02e794bb8 --- /dev/null +++ b/test/js/samples/inline-style-optimized-url/expected.js @@ -0,0 +1,59 @@ +import { assign, createElement, detachNode, insertNode, noop, proto, setStyle } from "svelte/shared.js"; + +function create_main_fragment ( state, component ) { + var div; + + return { + create: function () { + div = createElement( 'div' ); + this.hydrate(); + }, + + hydrate: function ( nodes ) { + setStyle(div, 'background', "url(data:image/png;base64," + state.data + ")"); + }, + + mount: function ( target, anchor ) { + insertNode( div, target, anchor ); + }, + + update: function ( changed, state ) { + if ( changed.data ) { + setStyle(div, 'background', "url(data:image/png;base64," + state.data + ")"); + } + }, + + unmount: function () { + detachNode( div ); + }, + + destroy: noop + }; +} + +function SvelteComponent ( options ) { + 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._fragment = create_main_fragment( this._state, this ); + + if ( options.target ) { + this._fragment.create(); + this._fragment.mount( options.target, options.anchor || null ); + } +} + +assign( SvelteComponent.prototype, proto ); + +export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/inline-style-optimized/expected-bundle.js b/test/js/samples/inline-style-optimized/expected-bundle.js new file mode 100644 index 0000000000..98713a9e1b --- /dev/null +++ b/test/js/samples/inline-style-optimized/expected-bundle.js @@ -0,0 +1,216 @@ +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 insertNode(node, target, anchor) { + target.insertBefore(node, anchor); +} + +function detachNode(node) { + node.parentNode.removeChild(node); +} + +function createElement(name) { + return document.createElement(name); +} + +function setStyle(node, key, value) { + node.style.setProperty(key, value); +} + +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 div; + + return { + create: function () { + div = createElement( 'div' ); + this.hydrate(); + }, + + hydrate: function ( nodes ) { + setStyle(div, 'color', state.color); + }, + + mount: function ( target, anchor ) { + insertNode( div, target, anchor ); + }, + + update: function ( changed, state ) { + if ( changed.color ) { + setStyle(div, 'color', state.color); + } + }, + + unmount: function () { + detachNode( div ); + }, + + destroy: noop + }; +} + +function SvelteComponent ( options ) { + 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._fragment = create_main_fragment( this._state, this ); + + if ( options.target ) { + this._fragment.create(); + this._fragment.mount( options.target, options.anchor || null ); + } +} + +assign( SvelteComponent.prototype, proto ); + +export default SvelteComponent; diff --git a/test/js/samples/inline-style-optimized/expected.js b/test/js/samples/inline-style-optimized/expected.js new file mode 100644 index 0000000000..2de1d2b084 --- /dev/null +++ b/test/js/samples/inline-style-optimized/expected.js @@ -0,0 +1,59 @@ +import { assign, createElement, detachNode, insertNode, noop, proto, setStyle } from "svelte/shared.js"; + +function create_main_fragment ( state, component ) { + var div; + + return { + create: function () { + div = createElement( 'div' ); + this.hydrate(); + }, + + hydrate: function ( nodes ) { + setStyle(div, 'color', state.color); + }, + + mount: function ( target, anchor ) { + insertNode( div, target, anchor ); + }, + + update: function ( changed, state ) { + if ( changed.color ) { + setStyle(div, 'color', state.color); + } + }, + + unmount: function () { + detachNode( div ); + }, + + destroy: noop + }; +} + +function SvelteComponent ( options ) { + 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._fragment = create_main_fragment( this._state, this ); + + if ( options.target ) { + this._fragment.create(); + this._fragment.mount( options.target, options.anchor || null ); + } +} + +assign( SvelteComponent.prototype, proto ); + +export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/inline-style-unoptimized/expected-bundle.js b/test/js/samples/inline-style-unoptimized/expected-bundle.js new file mode 100644 index 0000000000..f463764157 --- /dev/null +++ b/test/js/samples/inline-style-unoptimized/expected-bundle.js @@ -0,0 +1,227 @@ +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 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 div, text, div_1, div_1_style_value; + + return { + create: function () { + div = createElement( 'div' ); + text = createText( "\n" ); + div_1 = createElement( 'div' ); + this.hydrate(); + }, + + hydrate: function ( nodes ) { + div.style.cssText = state.style; + div_1.style.cssText = div_1_style_value = "" + ( state.key ) + ": " + ( state.value ); + }, + + mount: function ( target, anchor ) { + insertNode( div, target, anchor ); + insertNode( text, target, anchor ); + insertNode( div_1, target, anchor ); + }, + + update: function ( changed, state ) { + if ( changed.style ) { + div.style.cssText = state.style; + } + + if ( ( changed.key || changed.value ) && div_1_style_value !== ( div_1_style_value = "" + ( state.key ) + ": " + ( state.value ) ) ) { + div_1.style.cssText = div_1_style_value; + } + }, + + unmount: function () { + detachNode( div ); + detachNode( text ); + detachNode( div_1 ); + }, + + destroy: noop + }; +} + +function SvelteComponent ( options ) { + 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._fragment = create_main_fragment( this._state, this ); + + if ( options.target ) { + this._fragment.create(); + this._fragment.mount( options.target, options.anchor || null ); + } +} + +assign( SvelteComponent.prototype, proto ); + +export default SvelteComponent; diff --git a/test/js/samples/inline-style-unoptimized/expected.js b/test/js/samples/inline-style-unoptimized/expected.js new file mode 100644 index 0000000000..74f9ddca2a --- /dev/null +++ b/test/js/samples/inline-style-unoptimized/expected.js @@ -0,0 +1,70 @@ +import { assign, createElement, createText, detachNode, insertNode, noop, proto } from "svelte/shared.js"; + +function create_main_fragment ( state, component ) { + var div, text, div_1, div_1_style_value; + + return { + create: function () { + div = createElement( 'div' ); + text = createText( "\n" ); + div_1 = createElement( 'div' ); + this.hydrate(); + }, + + hydrate: function ( nodes ) { + div.style.cssText = state.style; + div_1.style.cssText = div_1_style_value = "" + ( state.key ) + ": " + ( state.value ); + }, + + mount: function ( target, anchor ) { + insertNode( div, target, anchor ); + insertNode( text, target, anchor ); + insertNode( div_1, target, anchor ); + }, + + update: function ( changed, state ) { + if ( changed.style ) { + div.style.cssText = state.style; + } + + if ( ( changed.key || changed.value ) && div_1_style_value !== ( div_1_style_value = "" + ( state.key ) + ": " + ( state.value ) ) ) { + div_1.style.cssText = div_1_style_value; + } + }, + + unmount: function () { + detachNode( div ); + detachNode( text ); + detachNode( div_1 ); + }, + + destroy: noop + }; +} + +function SvelteComponent ( options ) { + 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._fragment = create_main_fragment( this._state, this ); + + if ( options.target ) { + this._fragment.create(); + this._fragment.mount( options.target, options.anchor || null ); + } +} + +assign( SvelteComponent.prototype, proto ); + +export default SvelteComponent; \ No newline at end of file diff --git a/test/js/samples/legacy-input-type/expected-bundle.js b/test/js/samples/legacy-input-type/expected-bundle.js index 0a2fa851fe..189bfddb89 100644 --- a/test/js/samples/legacy-input-type/expected-bundle.js +++ b/test/js/samples/legacy-input-type/expected-bundle.js @@ -213,7 +213,7 @@ function SvelteComponent ( options ) { this._fragment = create_main_fragment( this._state, this ); - if ( !options._root ) { + if ( options.target ) { this._fragment.create(); this._fragment.mount( options.target, options.anchor || null ); } diff --git a/test/js/samples/legacy-input-type/expected.js b/test/js/samples/legacy-input-type/expected.js index 46277e3c98..33a277bb4a 100644 --- a/test/js/samples/legacy-input-type/expected.js +++ b/test/js/samples/legacy-input-type/expected.js @@ -44,7 +44,7 @@ function SvelteComponent ( options ) { this._fragment = create_main_fragment( this._state, this ); - if ( !options._root ) { + if ( options.target ) { this._fragment.create(); this._fragment.mount( options.target, options.anchor || null ); } diff --git a/test/js/samples/non-imported-component/expected-bundle.js b/test/js/samples/non-imported-component/expected-bundle.js index 7d90b2cf53..1bdf45a773 100644 --- a/test/js/samples/non-imported-component/expected-bundle.js +++ b/test/js/samples/non-imported-component/expected-bundle.js @@ -235,7 +235,7 @@ function SvelteComponent ( options ) { this._fragment = create_main_fragment( this._state, this ); - if ( !options._root ) { + if ( options.target ) { this._fragment.create(); this._fragment.mount( options.target, options.anchor || null ); diff --git a/test/js/samples/non-imported-component/expected.js b/test/js/samples/non-imported-component/expected.js index 3d5c223e7d..71d4e0b355 100644 --- a/test/js/samples/non-imported-component/expected.js +++ b/test/js/samples/non-imported-component/expected.js @@ -72,7 +72,7 @@ function SvelteComponent ( options ) { this._fragment = create_main_fragment( this._state, this ); - if ( !options._root ) { + if ( options.target ) { this._fragment.create(); this._fragment.mount( options.target, options.anchor || null ); diff --git a/test/js/samples/onrender-onteardown-rewritten/expected-bundle.js b/test/js/samples/onrender-onteardown-rewritten/expected-bundle.js index 400cca8ff5..4002cb8394 100644 --- a/test/js/samples/onrender-onteardown-rewritten/expected-bundle.js +++ b/test/js/samples/onrender-onteardown-rewritten/expected-bundle.js @@ -200,7 +200,7 @@ function SvelteComponent ( options ) { this._fragment = create_main_fragment( this._state, this ); - if ( !options._root ) { + if ( options.target ) { this._fragment.create(); this._fragment.mount( options.target, options.anchor || null ); diff --git a/test/js/samples/onrender-onteardown-rewritten/expected.js b/test/js/samples/onrender-onteardown-rewritten/expected.js index 02cef79f50..a50372f91b 100644 --- a/test/js/samples/onrender-onteardown-rewritten/expected.js +++ b/test/js/samples/onrender-onteardown-rewritten/expected.js @@ -49,7 +49,7 @@ function SvelteComponent ( options ) { this._fragment = create_main_fragment( this._state, this ); - if ( !options._root ) { + if ( options.target ) { this._fragment.create(); this._fragment.mount( options.target, options.anchor || null ); diff --git a/test/js/samples/setup-method/expected-bundle.js b/test/js/samples/setup-method/expected-bundle.js index 40e4565a9f..4c3b5db7aa 100644 --- a/test/js/samples/setup-method/expected-bundle.js +++ b/test/js/samples/setup-method/expected-bundle.js @@ -202,7 +202,7 @@ function SvelteComponent ( options ) { this._fragment = create_main_fragment( this._state, this ); - if ( !options._root ) { + if ( options.target ) { this._fragment.create(); this._fragment.mount( options.target, options.anchor || null ); } diff --git a/test/js/samples/setup-method/expected.js b/test/js/samples/setup-method/expected.js index 6b554eb781..9e33fb3921 100644 --- a/test/js/samples/setup-method/expected.js +++ b/test/js/samples/setup-method/expected.js @@ -51,7 +51,7 @@ function SvelteComponent ( options ) { this._fragment = create_main_fragment( this._state, this ); - if ( !options._root ) { + if ( options.target ) { this._fragment.create(); this._fragment.mount( options.target, options.anchor || null ); } diff --git a/test/js/samples/use-elements-as-anchors/expected-bundle.js b/test/js/samples/use-elements-as-anchors/expected-bundle.js index d8cf0a2ad3..c93fbe1a47 100644 --- a/test/js/samples/use-elements-as-anchors/expected-bundle.js +++ b/test/js/samples/use-elements-as-anchors/expected-bundle.js @@ -439,7 +439,7 @@ function SvelteComponent ( options ) { this._fragment = create_main_fragment( this._state, this ); - if ( !options._root ) { + if ( options.target ) { this._fragment.create(); this._fragment.mount( options.target, options.anchor || null ); } diff --git a/test/js/samples/use-elements-as-anchors/expected.js b/test/js/samples/use-elements-as-anchors/expected.js index 7bc5165be0..f65d1fe288 100644 --- a/test/js/samples/use-elements-as-anchors/expected.js +++ b/test/js/samples/use-elements-as-anchors/expected.js @@ -264,7 +264,7 @@ function SvelteComponent ( options ) { this._fragment = create_main_fragment( this._state, this ); - if ( !options._root ) { + if ( options.target ) { this._fragment.create(); this._fragment.mount( options.target, options.anchor || null ); }