Skip to content

Commit

Permalink
replace <svelte:window> with <svelte:body> (#1846)
Browse files Browse the repository at this point in the history
  • Loading branch information
Conduitry committed Jan 5, 2019
1 parent 88b6a26 commit 82c06e4
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 26 deletions.
4 changes: 2 additions & 2 deletions src/compile/nodes/Document.ts → src/compile/nodes/Body.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import Node from './shared/Node';
import EventHandler from './EventHandler';

export default class Document extends Node {
type: 'Document';
export default class Body extends Node {
type: 'Body';
handlers: EventHandler[];

constructor(component, parent, scope, info) {
Expand Down
6 changes: 3 additions & 3 deletions src/compile/nodes/shared/mapChildren.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import AwaitBlock from '../AwaitBlock';
import Body from '../Body';
import Comment from '../Comment';
import Document from '../Document';
import EachBlock from '../EachBlock';
import Element from '../Element';
import Head from '../Head';
Expand All @@ -19,8 +19,8 @@ import Node from './Node';
function getConstructor(type): typeof Node {
switch (type) {
case 'AwaitBlock': return AwaitBlock;
case 'Body': return Body;
case 'Comment': return Comment;
case 'Document': return Document;
case 'EachBlock': return EachBlock;
case 'Element': return Element;
case 'Head': return Head;
Expand Down Expand Up @@ -50,4 +50,4 @@ export default function mapChildren(component, parent, scope, children: any[]) {

return node;
});
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import Block from '../Block';
import Wrapper from './shared/Wrapper';
import deindent from '../../../utils/deindent';
import Document from '../../nodes/Document';
import Body from '../../nodes/Body';

export default class DocumentWrapper extends Wrapper {
node: Document;
export default class BodyWrapper extends Wrapper {
node: Body;

render(block: Block, parentNode: string, parentNodes: string) {
this.node.handlers.forEach(handler => {
const snippet = handler.render(block);

block.builders.init.addBlock(deindent`
document.addEventListener("${handler.name}", ${snippet});
document.body.addEventListener("${handler.name}", ${snippet});
`);

block.builders.destroy.addBlock(deindent`
document.removeEventListener("${handler.name}", ${snippet});
document.body.removeEventListener("${handler.name}", ${snippet});
`);
});
}
}
}
6 changes: 3 additions & 3 deletions src/compile/render-dom/wrappers/Fragment.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Wrapper from './shared/Wrapper';
import AwaitBlock from './AwaitBlock';
import Body from './Body';
import DebugTag from './DebugTag';
import Document from './Document';
import EachBlock from './EachBlock';
import Element from './Element';
import Head from './Head';
Expand All @@ -21,8 +21,8 @@ import Block from '../Block';

const wrappers = {
AwaitBlock,
Body,
Comment: null,
Document,
DebugTag,
EachBlock,
Element,
Expand Down Expand Up @@ -140,4 +140,4 @@ export default class FragmentWrapper {
this.nodes[i].render(block, parentNode, parentNodes);
}
}
}
}
4 changes: 2 additions & 2 deletions src/compile/render-ssr/Renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const handlers: Record<string, Handler> = {
AwaitBlock,
Comment,
DebugTag,
Document: noop,
Body: noop,
EachBlock,
Element,
Head,
Expand Down Expand Up @@ -64,4 +64,4 @@ export default class Renderer {
handler(node, this, options);
});
}
}
}
6 changes: 3 additions & 3 deletions src/parse/state/tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const metaTags = new Map([
['svelte:head', 'Head'],
['svelte:meta', 'Meta'],
['svelte:window', 'Window'],
['svelte:document', 'Document']
['svelte:body', 'Body']
]);

const valid_meta_tags = [...metaTags.keys(), 'svelte:self', 'svelte:component'];
Expand Down Expand Up @@ -101,7 +101,7 @@ export default function tag(parser: Parser) {
const slug = metaTags.get(name).toLowerCase();
if (isClosingTag) {
if (
(name === 'svelte:window' || name === 'svelte:document') &&
(name === 'svelte:window' || name === 'svelte:body') &&
parser.current().children.length
) {
parser.error({
Expand Down Expand Up @@ -540,4 +540,4 @@ function readSequence(parser: Parser, done: () => boolean) {
code: `unexpected-eof`,
message: `Unexpected end of input`
});
}
}
4 changes: 2 additions & 2 deletions test/parser/samples/error-svelte-selfdestructive/error.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"code": "invalid-tag-name",
"message": "Valid <svelte:...> tag names are svelte:head, svelte:meta, svelte:window, svelte:document, svelte:self or svelte:component",
"message": "Valid <svelte:...> tag names are svelte:head, svelte:meta, svelte:window, svelte:body, svelte:self or svelte:component",
"pos": 10,
"start": {
"character": 10,
"line": 2,
"column": 2
}
}
}
8 changes: 4 additions & 4 deletions test/runtime/samples/document-event/_config.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
export default {
test({ assert, component, target, window }) {
test({ assert, component, window }) {
assert.deepEqual(component.events, []);

const event1 = new window.Event('mouseenter');
window.document.dispatchEvent(event1);
window.document.body.dispatchEvent(event1);
assert.deepEqual(component.events, ['enter']);

const event2 = new window.Event('mouseleave');
window.document.dispatchEvent(event2);
window.document.body.dispatchEvent(event2);
assert.deepEqual(component.events, ['enter', 'leave']);
},
};
};
2 changes: 1 addition & 1 deletion test/runtime/samples/document-event/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
}
</script>

<svelte:document on:mouseenter='{() => log("enter")}' on:mouseleave='{() => log("leave")}'/>
<svelte:body on:mouseenter='{() => log("enter")}' on:mouseleave='{() => log("leave")}'/>

0 comments on commit 82c06e4

Please sign in to comment.