From dc154987dae66140ae95b9166135406b7f893993 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Thu, 23 Nov 2017 11:44:25 -0500 Subject: [PATCH] remove whitespace around <:Window/> - fixes #943 --- src/generators/dom/preprocess.ts | 10 ++++++++++ .../dom/visitors/Element/meta/Window.ts | 8 ++++---- .../window-binding-scroll/expected-bundle.js | 13 +++++-------- test/js/samples/window-binding-scroll/expected.js | 15 ++++++--------- 4 files changed, 25 insertions(+), 21 deletions(-) diff --git a/src/generators/dom/preprocess.ts b/src/generators/dom/preprocess.ts index ef5147c920..5778258155 100644 --- a/src/generators/dom/preprocess.ts +++ b/src/generators/dom/preprocess.ts @@ -449,9 +449,18 @@ function preprocessChildren( const cleaned: Node[] = []; let lastChild: Node; + let windowComponent; + node.children.forEach((child: Node) => { if (child.type === 'Comment') return; + // special case — this is an easy way to remove whitespace surrounding + // <:Window/>. lil hacky but it works + if (child.type === 'Element' && child.name === ':Window') { + windowComponent = child; + return; + } + if (child.type === 'Text' && lastChild && lastChild.type === 'Text') { lastChild.data += child.data; lastChild.end = child.end; @@ -502,6 +511,7 @@ function preprocessChildren( } node.children = cleaned; + if (windowComponent) cleaned.unshift(windowComponent); } export default function preprocess( diff --git a/src/generators/dom/visitors/Element/meta/Window.ts b/src/generators/dom/visitors/Element/meta/Window.ts index f5cfb23e80..4dc8a23a93 100644 --- a/src/generators/dom/visitors/Element/meta/Window.ts +++ b/src/generators/dom/visitors/Element/meta/Window.ts @@ -61,7 +61,7 @@ export default function visitWindow( block.builders.init.addBlock(deindent` function ${handlerName}(event) { ${handlerBody} - }; + } window.addEventListener("${attribute.name}", ${handlerName}); `); @@ -121,7 +121,7 @@ export default function visitWindow( block.builders.init.addBlock(deindent` function ${handlerName}(event) { ${handlerBody} - }; + } window.addEventListener("${event}", ${handlerName}); `); @@ -144,7 +144,7 @@ export default function visitWindow( ? `#component.get("${bindings.scrollY}")` : `window.scrollY`}; window.scrollTo(x, y); - }; + } `); if (bindings.scrollX) @@ -172,7 +172,7 @@ export default function visitWindow( block.builders.init.addBlock(deindent` function ${handlerName}(event) { #component.set({ ${bindings.online}: navigator.onLine }); - }; + } window.addEventListener("online", ${handlerName}); window.addEventListener("offline", ${handlerName}); `); diff --git a/test/js/samples/window-binding-scroll/expected-bundle.js b/test/js/samples/window-binding-scroll/expected-bundle.js index c9348f5c68..6c357ef732 100644 --- a/test/js/samples/window-binding-scroll/expected-bundle.js +++ b/test/js/samples/window-binding-scroll/expected-bundle.js @@ -189,7 +189,7 @@ var proto = { /* generated by Svelte vX.Y.Z */ function create_main_fragment(state, component) { - var window_updating = false, text, p, text_1, text_2; + var window_updating = false, p, text, text_1; function onwindowscroll(event) { window_updating = true; @@ -208,27 +208,24 @@ function create_main_fragment(state, component) { return { c: function create() { - text = createText("\n\n"); p = createElement("p"); - text_1 = createText("scrolled to "); - text_2 = createText(state.y); + text = createText("scrolled to "); + text_1 = createText(state.y); }, m: function mount(target, anchor) { - insertNode(text, target, anchor); insertNode(p, target, anchor); + appendNode(text, p); appendNode(text_1, p); - appendNode(text_2, p); }, p: function update(changed, state) { if (changed.y) { - text_2.data = state.y; + text_1.data = state.y; } }, u: function unmount() { - detachNode(text); detachNode(p); }, diff --git a/test/js/samples/window-binding-scroll/expected.js b/test/js/samples/window-binding-scroll/expected.js index 7950ec458b..0387b24a05 100644 --- a/test/js/samples/window-binding-scroll/expected.js +++ b/test/js/samples/window-binding-scroll/expected.js @@ -2,7 +2,7 @@ import { appendNode, assign, createElement, createText, detachNode, init, insertNode, proto } from "svelte/shared.js"; function create_main_fragment(state, component) { - var window_updating = false, text, p, text_1, text_2; + var window_updating = false, p, text, text_1; function onwindowscroll(event) { window_updating = true; @@ -11,7 +11,7 @@ function create_main_fragment(state, component) { y: this.scrollY }); window_updating = false; - }; + } window.addEventListener("scroll", onwindowscroll); component.observe("y", function(y) { @@ -21,27 +21,24 @@ function create_main_fragment(state, component) { return { c: function create() { - text = createText("\n\n"); p = createElement("p"); - text_1 = createText("scrolled to "); - text_2 = createText(state.y); + text = createText("scrolled to "); + text_1 = createText(state.y); }, m: function mount(target, anchor) { - insertNode(text, target, anchor); insertNode(p, target, anchor); + appendNode(text, p); appendNode(text_1, p); - appendNode(text_2, p); }, p: function update(changed, state) { if (changed.y) { - text_2.data = state.y; + text_1.data = state.y; } }, u: function unmount() { - detachNode(text); detachNode(p); },