forked from WebReflection/linkedom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcached.js
153 lines (131 loc) · 3.53 KB
/
cached.js
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
'use strict';
const {DOCUMENT_FRAGMENT_NODE} = require('./shared/constants.js');
const {defineProperties, getOwnPropertyDescriptors} = require('./shared/object.js');
const {Attr} = require('./interface/attr.js');
const {CharacterData} = require('./interface/character-data.js');
const {Element} = require('./interface/element.js');
const {ParentNode} = require('./mixin/parent-node.js');
const {NonElementParentNode} = require('./mixin/non-element-parent-node.js');
const {HTMLDocument} = require('./html/document.js');
const {
childNodesWM,
childrenWM,
querySelectorWM,
querySelectorAllWM,
get,
reset
} = require('./shared/cache.js');
// Attr
const {value: {
get: getAttributeValue,
set: setAttributeValue
}} = getOwnPropertyDescriptors(Attr.prototype);
defineProperties(Attr.prototype, {
value: {
get: getAttributeValue,
set(value) {
reset(this.ownerElement);
setAttributeValue.call(this, value);
}
}
});
// CharacterData
// TODO: is txtContent really necessary to patch here?
const {remove: removeCharacterData} = CharacterData.prototype;
defineProperties(CharacterData.prototype, {
remove: {value() {
reset(this.parentNode);
removeCharacterData.call(this);
}}
});
// Element
const elementProtoDescriptors = {};
for (const name of [
'remove',
'setAttribute',
'setAttributeNS',
'setAttributeNode',
'setAttributeNodeNS',
'removeAttribute',
'removeAttributeNS',
'removeAttributeNode'
]) {
const method = Element.prototype[name];
elementProtoDescriptors[name] = {
value() {
reset(this.parentNode);
return method.apply(this, arguments);
}
};
}
defineProperties(Element.prototype, elementProtoDescriptors);
// ParentNode
const {
childNodes: {get: getChildNodes},
children: {get: getChildren}
} = getOwnPropertyDescriptors(ParentNode.prototype);
defineProperties(ParentNode.prototype, {
childNodes: {get() {
return get(childNodesWM, this, getChildNodes);
}},
children: {get() {
return get(childrenWM, this, getChildren);
}}
});
const {
insertBefore,
querySelector,
querySelectorAll
} = ParentNode.prototype;
const query = (wm, method, self, selectors) => {
if (!wm.has(self))
wm.set(self, new Map);
const map = wm.get(self);
if (map.has(selectors))
return map.get(selectors);
const result = method.call(self, selectors);
map.set(selectors, result);
return result;
};
defineProperties(ParentNode.prototype, {
insertBefore: {value(node, before) {
reset(this);
if (node.nodeType === DOCUMENT_FRAGMENT_NODE)
reset(node);
return insertBefore.call(this, node, before);
}},
getElementsByClassName: {value(className) {
return this.querySelectorAll('.' + className);
}},
getElementsByTagName: {value(tagName) {
return this.querySelectorAll(tagName);
}},
querySelector: {value(selectors) {
return query(querySelectorWM, querySelector, this, selectors);
}},
querySelectorAll: {value(selectors) {
return query(querySelectorAllWM, querySelectorAll, this, selectors);
}}
});
// NonElementParentNode
defineProperties(NonElementParentNode.prototype, {
getElementById: {value(id) {
return this.querySelector('#' + id);
}}
});
// HTMLDocument
const {title: {
get: getTitle,
set: setTitle
}} = getOwnPropertyDescriptors(HTMLDocument.prototype);
defineProperties(HTMLDocument.prototype, {
title: {
get: getTitle,
set(value) {
reset(this.head);
setTitle.call(this, value);
}
}
});
(m => Object.keys(m).map(k => k !== 'default' && (exports[k] = m[k])))
(require('./index.js'));