Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
titaniumnetwork-dev
GitHub Repository: titaniumnetwork-dev/Ultraviolet
Path: blob/main/src/client/dom/node.js
305 views
1
import EventEmitter from "events";
2
import HookEvent from "../hook.js";
3
4
/**
5
* @typedef {import('../index').default} UVClient
6
*/
7
8
class NodeApi extends EventEmitter {
9
/**
10
*
11
* @param {UVClient} ctx
12
*/
13
constructor(ctx) {
14
super();
15
this.ctx = ctx;
16
this.window = ctx.window;
17
this.Node = ctx.window.Node || {};
18
this.nodeProto = this.Node.prototype || {};
19
this.compareDocumentPosition = this.nodeProto.compareDocumentPosition;
20
this.contains = this.nodeProto.contains;
21
this.insertBefore = this.nodeProto.insertBefore;
22
this.replaceChild = this.nodeProto.replaceChild;
23
this.append = this.nodeProto.append;
24
this.appendChild = this.nodeProto.appendChild;
25
this.removeChild = this.nodeProto.removeChild;
26
27
this.textContent = ctx.nativeMethods.getOwnPropertyDescriptor(
28
this.nodeProto,
29
"textContent"
30
);
31
this.parentNode = ctx.nativeMethods.getOwnPropertyDescriptor(
32
this.nodeProto,
33
"parentNode"
34
);
35
this.parentElement = ctx.nativeMethods.getOwnPropertyDescriptor(
36
this.nodeProto,
37
"parentElement"
38
);
39
this.childNodes = ctx.nativeMethods.getOwnPropertyDescriptor(
40
this.nodeProto,
41
"childNodes"
42
);
43
this.baseURI = ctx.nativeMethods.getOwnPropertyDescriptor(
44
this.nodeProto,
45
"baseURI"
46
);
47
this.previousSibling = ctx.nativeMethods.getOwnPropertyDescriptor(
48
this.nodeProto,
49
"previousSibling"
50
);
51
this.ownerDocument = ctx.nativeMethods.getOwnPropertyDescriptor(
52
this.nodeProto,
53
"ownerDocument"
54
);
55
}
56
overrideTextContent() {
57
this.ctx.overrideDescriptor(this.nodeProto, "textContent", {
58
get: (target, that) => {
59
const event = new HookEvent({ value: target.call(that) }, target, that);
60
this.emit("getTextContent", event);
61
62
if (event.intercepted) return event.returnValue;
63
return event.data.value;
64
},
65
set: (target, that, [val]) => {
66
const event = new HookEvent({ value: val }, target, that);
67
this.emit("setTextContent", event);
68
69
if (event.intercepted) return event.returnValue;
70
target.call(that, event.data.value);
71
},
72
});
73
}
74
overrideAppend() {
75
this.ctx.override(this.nodeProto, "append", (target, that, [...nodes]) => {
76
const event = new HookEvent({ nodes }, target, that);
77
this.emit("append", event);
78
79
if (event.intercepted) return event.returnValue;
80
return event.target.call(event.that, event.data.nodes);
81
});
82
this.ctx.override(this.nodeProto, "appendChild", (target, that, args) => {
83
if (!args.length) return target.apply(that, args);
84
let [node] = args;
85
86
const event = new HookEvent({ node }, target, that);
87
this.emit("appendChild", event);
88
89
if (event.intercepted) return event.returnValue;
90
return event.target.call(event.that, event.data.node);
91
});
92
}
93
overrideBaseURI() {
94
this.ctx.overrideDescriptor(this.nodeProto, "baseURI", {
95
get: (target, that) => {
96
const event = new HookEvent({ value: target.call(that) }, target, that);
97
this.emit("baseURI", event);
98
99
if (event.intercepted) return event.returnValue;
100
return event.data.value;
101
},
102
});
103
}
104
overrideParent() {
105
this.ctx.overrideDescriptor(this.nodeProto, "parentNode", {
106
get: (target, that) => {
107
const event = new HookEvent({ node: target.call(that) }, target, that);
108
this.emit("parentNode", event);
109
110
if (event.intercepted) return event.returnValue;
111
return event.data.node;
112
},
113
});
114
this.ctx.overrideDescriptor(this.nodeProto, "parentElement", {
115
get: (target, that) => {
116
const event = new HookEvent(
117
{ element: target.call(that) },
118
target,
119
that
120
);
121
this.emit("parentElement", event);
122
123
if (event.intercepted) return event.returnValue;
124
return event.data.node;
125
},
126
});
127
}
128
overrideOwnerDocument() {
129
this.ctx.overrideDescriptor(this.nodeProto, "ownerDocument", {
130
get: (target, that) => {
131
const event = new HookEvent(
132
{ document: target.call(that) },
133
target,
134
that
135
);
136
this.emit("ownerDocument", event);
137
138
if (event.intercepted) return event.returnValue;
139
return event.data.document;
140
},
141
});
142
}
143
overrideCompareDocumentPosit1ion() {
144
this.ctx.override(
145
this.nodeProto,
146
"compareDocumentPosition",
147
(target, that, args) => {
148
if (!args.length) return target.apply(that, args);
149
let [node] = args;
150
const event = new HookEvent({ node }, target, that);
151
152
if (event.intercepted) return event.returnValue;
153
return event.target.call(event.that, event.data.node);
154
}
155
);
156
}
157
overrideChildMethods() {
158
this.ctx.override(this.nodeProto, "removeChild");
159
}
160
}
161
162
export default NodeApi;
163
164