-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
nodejs-dom.ts
128 lines (115 loc) · 4.18 KB
/
nodejs-dom.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { IDom } from './dom';
import { IGlobal } from './global';
declare module './global' {
interface IGlobal {
window: any;
document: any;
}
}
/**
* Represents the core APIs of the DOM.
*/
export class NodeJsDom implements IDom {
constructor(public global: IGlobal) {
this.Element = global.Element;
this.NodeList = global.NodeList;
this.SVGElement = global.SVGElement || class SVGElement extends global.Element { };
}
Element: typeof Element;
NodeList: typeof NodeList;
SVGElement: typeof SVGElement /* until a proper implementation is ready: */ | any;
boundary: string = 'aurelia-dom-boundary';
title: string = "";
activeElement: Element = null;
addEventListener(eventName: string, callback: EventListenerOrEventListenerObject, capture: boolean): void {
return this.global.document.addEventListener(eventName, callback, capture);
}
removeEventListener(eventName: string, callback: EventListenerOrEventListenerObject, capture: boolean): void {
return this.global.document.removeEventListener(eventName, callback, capture);
}
createElement<T extends keyof HTMLElementTagNameMap>(tagName: T): HTMLElementTagNameMap[T];
createElement(tagName: string): Element {
return this.global.document.createElement(tagName);
}
createAttribute(name: string): Attr {
return this.global.document.createAttribute(name);
}
createTextNode(text: string): Text {
return this.global.document.createTextNode(text);
}
createComment(text: string): Comment {
return this.global.document.createComment(text);
}
createDocumentFragment(): DocumentFragment {
return this.global.document.createDocumentFragment();
}
createTemplateElement(): HTMLTemplateElement {
return this.global.document.createElement('template');
}
createMutationObserver(callback: (changes: MutationRecord[], instance: MutationObserver) => void): MutationObserver {
return new ((<any>this.global.window).MutationObserver)(callback);
}
createCustomEvent(eventType: string, options?: Object): CustomEvent {
return new this.global.CustomEvent(eventType, options);
}
dispatchEvent(evt: Event): void {
this.global.window.dispatchEvent(evt);
}
getComputedStyle(element: Element): CSSStyleDeclaration {
return this.global.window.getComputedStyle(element);
}
getElementById(id: string): Element {
return this.global.document.getElementById(id);
}
querySelector<E extends Element = Element>(query: string): E | null {
return this.global.document.querySelector(query);
}
querySelectorAll<E extends Element = Element>(query: string): NodeListOf<E> {
return this.global.document.querySelectorAll(query);
}
nextElementSibling(element: Element): Element {
return element.nextElementSibling;
}
createTemplateFromMarkup(markup: string): HTMLTemplateElement {
let parser = this.global.document.createElement('div');
parser.innerHTML = markup;
let temp = parser.firstElementChild;
if (!temp || temp.nodeName !== 'TEMPLATE') {
throw new Error('Template markup must be wrapped in a <template> element e.g. <template> <!-- markup here --> </template>');
}
return temp;
}
injectStyles(styles: string, destination?: Element, prepend?: boolean): Node {
let node = this.global.document.createElement('style');
node.innerHTML = styles;
node.type = 'text/css';
destination = destination || this.global.document.head;
if (prepend && destination.childNodes.length > 0) {
destination.insertBefore(node, destination.childNodes[0]);
} else {
destination.appendChild(node);
}
return node;
}
adoptNode(node: Node): Node {
return this.global.document.adoptNode(node);
}
appendNode(newNode: Node, parentNode?: Node): void {
(parentNode || this.global.document.body).appendChild(newNode);
}
replaceNode(newNode: Node, node: Node, parentNode?: Node): void {
if (node.parentNode) {
node.parentNode.replaceChild(newNode, node);
} else {
parentNode.replaceChild(newNode, node);
}
}
removeNode(node: Node, parentNode?: Node): void {
if (node.parentNode) {
node.parentNode.removeChild(node);
}
else {
parentNode.removeChild(node);
}
}
}