Repository navigation
Expand file tree
/
Copy pathnode.js
More file actions
162 lines (148 loc) · 4.43 KB
/
Copy pathnode.js
File metadata and controls
162 lines (148 loc) · 4.43 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import EventEmitter from "events";
import HookEvent from "../hook.js";
/**
* @typedef {import('../index').default} UVClient
*/
class NodeApi extends EventEmitter {
/**
*
* @param {UVClient} ctx
*/
constructor(ctx) {
super();
this.ctx = ctx;
this.window = ctx.window;
this.Node = ctx.window.Node || {};
this.nodeProto = this.Node.prototype || {};
this.compareDocumentPosition = this.nodeProto.compareDocumentPosition;
this.contains = this.nodeProto.contains;
this.insertBefore = this.nodeProto.insertBefore;
this.replaceChild = this.nodeProto.replaceChild;
this.append = this.nodeProto.append;
this.appendChild = this.nodeProto.appendChild;
this.removeChild = this.nodeProto.removeChild;
this.textContent = ctx.nativeMethods.getOwnPropertyDescriptor(
this.nodeProto,
"textContent"
);
this.parentNode = ctx.nativeMethods.getOwnPropertyDescriptor(
this.nodeProto,
"parentNode"
);
this.parentElement = ctx.nativeMethods.getOwnPropertyDescriptor(
this.nodeProto,
"parentElement"
);
this.childNodes = ctx.nativeMethods.getOwnPropertyDescriptor(
this.nodeProto,
"childNodes"
);
this.baseURI = ctx.nativeMethods.getOwnPropertyDescriptor(
this.nodeProto,
"baseURI"
);
this.previousSibling = ctx.nativeMethods.getOwnPropertyDescriptor(
this.nodeProto,
"previousSibling"
);
this.ownerDocument = ctx.nativeMethods.getOwnPropertyDescriptor(
this.nodeProto,
"ownerDocument"
);
}
overrideTextContent() {
this.ctx.overrideDescriptor(this.nodeProto, "textContent", {
get: (target, that) => {
const event = new HookEvent({ value: target.call(that) }, target, that);
this.emit("getTextContent", event);
if (event.intercepted) return event.returnValue;
return event.data.value;
},
set: (target, that, [val]) => {
const event = new HookEvent({ value: val }, target, that);
this.emit("setTextContent", event);
if (event.intercepted) return event.returnValue;
target.call(that, event.data.value);
},
});
}
overrideAppend() {
this.ctx.override(this.nodeProto, "append", (target, that, [...nodes]) => {
const event = new HookEvent({ nodes }, target, that);
this.emit("append", event);
if (event.intercepted) return event.returnValue;
return event.target.call(event.that, event.data.nodes);
});
this.ctx.override(this.nodeProto, "appendChild", (target, that, args) => {
if (!args.length) return target.apply(that, args);
let [node] = args;
const event = new HookEvent({ node }, target, that);
this.emit("appendChild", event);
if (event.intercepted) return event.returnValue;
return event.target.call(event.that, event.data.node);
});
}
overrideBaseURI() {
this.ctx.overrideDescriptor(this.nodeProto, "baseURI", {
get: (target, that) => {
const event = new HookEvent({ value: target.call(that) }, target, that);
this.emit("baseURI", event);
if (event.intercepted) return event.returnValue;
return event.data.value;
},
});
}
overrideParent() {
this.ctx.overrideDescriptor(this.nodeProto, "parentNode", {
get: (target, that) => {
const event = new HookEvent({ node: target.call(that) }, target, that);
this.emit("parentNode", event);
if (event.intercepted) return event.returnValue;
return event.data.node;
},
});
this.ctx.overrideDescriptor(this.nodeProto, "parentElement", {
get: (target, that) => {
const event = new HookEvent(
{ element: target.call(that) },
target,
that
);
this.emit("parentElement", event);
if (event.intercepted) return event.returnValue;
return event.data.node;
},
});
}
overrideOwnerDocument() {
this.ctx.overrideDescriptor(this.nodeProto, "ownerDocument", {
get: (target, that) => {
const event = new HookEvent(
{ document: target.call(that) },
target,
that
);
this.emit("ownerDocument", event);
if (event.intercepted) return event.returnValue;
return event.data.document;
},
});
}
overrideCompareDocumentPosit1ion() {
this.ctx.override(
this.nodeProto,
"compareDocumentPosition",
(target, that, args) => {
if (!args.length) return target.apply(that, args);
let [node] = args;
const event = new HookEvent({ node }, target, that);
if (event.intercepted) return event.returnValue;
return event.target.call(event.that, event.data.node);
}
);
}
overrideChildMethods() {
this.ctx.override(this.nodeProto, "removeChild");
}
}
export default NodeApi;