add <svelte:document>

pull/7149/head
Henrik Giesel 5 years ago
parent 0580c2d90d
commit bb349a9dd7

@ -0,0 +1,26 @@
import Node from './shared/Node';
import EventHandler from './EventHandler';
import Action from './Action';
import Component from '../Component';
import TemplateScope from './shared/TemplateScope';
import { Element } from '../../interfaces';
export default class Document extends Node {
type: 'Document';
handlers: EventHandler[] = [];
actions: Action[] = [];
constructor(component: Component, parent: Node, scope: TemplateScope, info: Element) {
super(component, parent, scope, info);
info.attributes.forEach((node) => {
if (node.type === 'EventHandler') {
this.handlers.push(new EventHandler(component, this, scope, node));
} else if (node.type === 'Action') {
this.actions.push(new Action(component, this, scope, node));
} else {
// TODO there shouldn't be anything else here...
}
});
}
}

@ -12,6 +12,7 @@ import StyleDirective from './StyleDirective';
import Comment from './Comment'; import Comment from './Comment';
import ConstTag from './ConstTag'; import ConstTag from './ConstTag';
import DebugTag from './DebugTag'; import DebugTag from './DebugTag';
import Document from './Document';
import EachBlock from './EachBlock'; import EachBlock from './EachBlock';
import Element from './Element'; import Element from './Element';
import ElseBlock from './ElseBlock'; import ElseBlock from './ElseBlock';
@ -47,6 +48,7 @@ export type INode = Action
| Comment | Comment
| ConstTag | ConstTag
| DebugTag | DebugTag
| Document
| EachBlock | EachBlock
| Element | Element
| ElseBlock | ElseBlock

@ -3,6 +3,7 @@ import Body from '../Body';
import ConstTag from '../ConstTag'; import ConstTag from '../ConstTag';
import Comment from '../Comment'; import Comment from '../Comment';
import EachBlock from '../EachBlock'; import EachBlock from '../EachBlock';
import Document from '../Document';
import Element from '../Element'; import Element from '../Element';
import Head from '../Head'; import Head from '../Head';
import IfBlock from '../IfBlock'; import IfBlock from '../IfBlock';
@ -28,6 +29,7 @@ function get_constructor(type) {
case 'Body': return Body; case 'Body': return Body;
case 'Comment': return Comment; case 'Comment': return Comment;
case 'ConstTag': return ConstTag; case 'ConstTag': return ConstTag;
case 'Document': return Document;
case 'EachBlock': return EachBlock; case 'EachBlock': return EachBlock;
case 'Element': return Element; case 'Element': return Element;
case 'Head': return Head; case 'Head': return Head;

@ -0,0 +1,25 @@
import Block from '../Block';
import Wrapper from './shared/Wrapper';
import { x } from 'code-red';
import Document from '../../nodes/Document';
import { Identifier } from 'estree';
import EventHandler from './Element/EventHandler';
import add_event_handlers from './shared/add_event_handlers';
import { TemplateNode } from '../../../interfaces';
import Renderer from '../Renderer';
import add_actions from './shared/add_actions';
export default class DocumentWrapper extends Wrapper {
node: Document;
handlers: EventHandler[];
constructor(renderer: Renderer, block: Block, parent: Wrapper, node: TemplateNode) {
super(renderer, block, parent, node);
this.handlers = this.node.handlers.map(handler => new EventHandler(handler, this));
}
render(block: Block, _parent_node: Identifier, _parent_nodes: Identifier) {
add_event_handlers(block, x`@_document`, this.handlers);
add_actions(block, x`@_document`, this.node.actions);
}
}

@ -2,6 +2,7 @@ import Wrapper from './shared/Wrapper';
import AwaitBlock from './AwaitBlock'; import AwaitBlock from './AwaitBlock';
import Body from './Body'; import Body from './Body';
import DebugTag from './DebugTag'; import DebugTag from './DebugTag';
import Document from './Document';
import EachBlock from './EachBlock'; import EachBlock from './EachBlock';
import Element from './Element/index'; import Element from './Element/index';
import Head from './Head'; import Head from './Head';
@ -27,6 +28,7 @@ const wrappers = {
Body, Body,
Comment: null, Comment: null,
DebugTag, DebugTag,
Document,
EachBlock, EachBlock,
Element, Element,
Head, Head,

@ -27,6 +27,7 @@ const handlers: Record<string, Handler> = {
Body: noop, Body: noop,
Comment, Comment,
DebugTag, DebugTag,
Document: noop,
EachBlock, EachBlock,
Element, Element,
Head, Head,

@ -63,7 +63,7 @@ interface BaseExpressionDirective extends BaseDirective {
} }
export interface Element extends BaseNode { export interface Element extends BaseNode {
type: 'InlineComponent' | 'SlotTemplate' | 'Title' | 'Slot' | 'Element' | 'Head' | 'Options' | 'Window' | 'Body'; type: 'InlineComponent' | 'SlotTemplate' | 'Title' | 'Slot' | 'Element' | 'Head' | 'Options' | 'Window' | 'Document' | 'Body';
attributes: Array<BaseDirective | Attribute | SpreadAttribute>; attributes: Array<BaseDirective | Attribute | SpreadAttribute>;
name: string; name: string;
} }

@ -16,6 +16,7 @@ const meta_tags = new Map([
['svelte:head', 'Head'], ['svelte:head', 'Head'],
['svelte:options', 'Options'], ['svelte:options', 'Options'],
['svelte:window', 'Window'], ['svelte:window', 'Window'],
['svelte:document', 'Document'],
['svelte:body', 'Body'] ['svelte:body', 'Body']
]); ]);

Loading…
Cancel
Save