Skip to content

Commit

Permalink
Merge pull request #946 from sveltejs/gh-943
Browse files Browse the repository at this point in the history
remove whitespace around <:Window/>
  • Loading branch information
Rich-Harris committed Nov 23, 2017
2 parents 4c06226 + dc15498 commit 5823150
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 21 deletions.
10 changes: 10 additions & 0 deletions src/generators/dom/preprocess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -502,6 +511,7 @@ function preprocessChildren(
}

node.children = cleaned;
if (windowComponent) cleaned.unshift(windowComponent);
}

export default function preprocess(
Expand Down
8 changes: 4 additions & 4 deletions src/generators/dom/visitors/Element/meta/Window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export default function visitWindow(
block.builders.init.addBlock(deindent`
function ${handlerName}(event) {
${handlerBody}
};
}
window.addEventListener("${attribute.name}", ${handlerName});
`);

Expand Down Expand Up @@ -121,7 +121,7 @@ export default function visitWindow(
block.builders.init.addBlock(deindent`
function ${handlerName}(event) {
${handlerBody}
};
}
window.addEventListener("${event}", ${handlerName});
`);

Expand All @@ -144,7 +144,7 @@ export default function visitWindow(
? `#component.get("${bindings.scrollY}")`
: `window.scrollY`};
window.scrollTo(x, y);
};
}
`);

if (bindings.scrollX)
Expand Down Expand Up @@ -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});
`);
Expand Down
13 changes: 5 additions & 8 deletions test/js/samples/window-binding-scroll/expected-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
},

Expand Down
15 changes: 6 additions & 9 deletions test/js/samples/window-binding-scroll/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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);
},

Expand Down

0 comments on commit 5823150

Please sign in to comment.