mirror of https://github.com/sveltejs/svelte
replace <svelte:document> with <svelte:body> (#1846)
parent
88b6a26a35
commit
f8d69e296f
@ -1,8 +1,8 @@
|
|||||||
import Node from './shared/Node';
|
import Node from './shared/Node';
|
||||||
import EventHandler from './EventHandler';
|
import EventHandler from './EventHandler';
|
||||||
|
|
||||||
export default class Document extends Node {
|
export default class Body extends Node {
|
||||||
type: 'Document';
|
type: 'Body';
|
||||||
handlers: EventHandler[];
|
handlers: EventHandler[];
|
||||||
|
|
||||||
constructor(component, parent, scope, info) {
|
constructor(component, parent, scope, info) {
|
@ -1,21 +1,21 @@
|
|||||||
import Block from '../Block';
|
import Block from '../Block';
|
||||||
import Wrapper from './shared/Wrapper';
|
import Wrapper from './shared/Wrapper';
|
||||||
import deindent from '../../../utils/deindent';
|
import deindent from '../../../utils/deindent';
|
||||||
import Document from '../../nodes/Document';
|
import Body from '../../nodes/Body';
|
||||||
|
|
||||||
export default class DocumentWrapper extends Wrapper {
|
export default class BodyWrapper extends Wrapper {
|
||||||
node: Document;
|
node: Body;
|
||||||
|
|
||||||
render(block: Block, parentNode: string, parentNodes: string) {
|
render(block: Block, parentNode: string, parentNodes: string) {
|
||||||
this.node.handlers.forEach(handler => {
|
this.node.handlers.forEach(handler => {
|
||||||
const snippet = handler.render(block);
|
const snippet = handler.render(block);
|
||||||
|
|
||||||
block.builders.init.addBlock(deindent`
|
block.builders.init.addBlock(deindent`
|
||||||
document.addEventListener("${handler.name}", ${snippet});
|
document.body.addEventListener("${handler.name}", ${snippet});
|
||||||
`);
|
`);
|
||||||
|
|
||||||
block.builders.destroy.addBlock(deindent`
|
block.builders.destroy.addBlock(deindent`
|
||||||
document.removeEventListener("${handler.name}", ${snippet});
|
document.body.removeEventListener("${handler.name}", ${snippet});
|
||||||
`);
|
`);
|
||||||
});
|
});
|
||||||
}
|
}
|
@ -1,13 +1,13 @@
|
|||||||
export default {
|
export default {
|
||||||
test({ assert, component, target, window }) {
|
test({ assert, component, window }) {
|
||||||
assert.deepEqual(component.events, []);
|
assert.deepEqual(component.events, []);
|
||||||
|
|
||||||
const event1 = new window.Event('mouseenter');
|
const event1 = new window.Event('mouseenter');
|
||||||
window.document.dispatchEvent(event1);
|
window.document.body.dispatchEvent(event1);
|
||||||
assert.deepEqual(component.events, ['enter']);
|
assert.deepEqual(component.events, ['enter']);
|
||||||
|
|
||||||
const event2 = new window.Event('mouseleave');
|
const event2 = new window.Event('mouseleave');
|
||||||
window.document.dispatchEvent(event2);
|
window.document.body.dispatchEvent(event2);
|
||||||
assert.deepEqual(component.events, ['enter', 'leave']);
|
assert.deepEqual(component.events, ['enter', 'leave']);
|
||||||
},
|
},
|
||||||
};
|
};
|
Loading…
Reference in new issue