@mavogel/cdk-vscode-server 0.0.123 → 0.0.125
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.jsii +3 -3
- package/lib/idle-monitor/idle-monitor.js +1 -1
- package/lib/vscode-server.js +1 -1
- package/mavogelcdkvscodeserver/go.mod +1 -1
- package/node_modules/node-html-parser/CHANGELOG.md +79 -0
- package/node_modules/node-html-parser/README.md +39 -39
- package/node_modules/node-html-parser/dist/index.cjs +5079 -0
- package/node_modules/node-html-parser/dist/index.d.ts +10 -10
- package/node_modules/node-html-parser/dist/index.mjs +5068 -0
- package/node_modules/node-html-parser/dist/index.umd.js +5083 -0
- package/node_modules/node-html-parser/dist/matcher.d.ts +22 -6
- package/node_modules/node-html-parser/dist/nodes/comment.d.ts +1 -1
- package/node_modules/node-html-parser/dist/nodes/html.d.ts +2 -2
- package/node_modules/node-html-parser/dist/nodes/node.d.ts +2 -2
- package/node_modules/node-html-parser/dist/nodes/text.d.ts +1 -1
- package/node_modules/node-html-parser/dist/valid.d.ts +1 -1
- package/node_modules/node-html-parser/node_modules/entities/LICENSE +11 -0
- package/node_modules/node-html-parser/node_modules/entities/dist/decode-codepoint.d.ts +28 -0
- package/node_modules/node-html-parser/node_modules/entities/dist/decode-codepoint.d.ts.map +1 -0
- package/node_modules/node-html-parser/node_modules/entities/dist/decode-codepoint.js +61 -0
- package/node_modules/node-html-parser/node_modules/entities/dist/decode-codepoint.js.map +1 -0
- package/node_modules/node-html-parser/node_modules/entities/dist/decode.d.ts +227 -0
- package/node_modules/node-html-parser/node_modules/entities/dist/decode.d.ts.map +1 -0
- package/node_modules/node-html-parser/node_modules/entities/dist/decode.js +1132 -0
- package/node_modules/node-html-parser/node_modules/entities/dist/decode.js.map +1 -0
- package/node_modules/node-html-parser/node_modules/entities/dist/encode.d.ts +26 -0
- package/node_modules/node-html-parser/node_modules/entities/dist/encode.d.ts.map +1 -0
- package/node_modules/node-html-parser/node_modules/entities/dist/encode.js +295 -0
- package/node_modules/node-html-parser/node_modules/entities/dist/encode.js.map +1 -0
- package/node_modules/node-html-parser/node_modules/entities/dist/escape.d.ts +60 -0
- package/node_modules/node-html-parser/node_modules/entities/dist/escape.d.ts.map +1 -0
- package/node_modules/node-html-parser/node_modules/entities/dist/escape.js +172 -0
- package/node_modules/node-html-parser/node_modules/entities/dist/escape.js.map +1 -0
- package/node_modules/node-html-parser/node_modules/entities/dist/generated/decode-data-html.d.ts +3 -0
- package/node_modules/node-html-parser/node_modules/entities/dist/generated/decode-data-html.d.ts.map +1 -0
- package/node_modules/node-html-parser/node_modules/entities/dist/generated/decode-data-html.js +5 -0
- package/node_modules/node-html-parser/node_modules/entities/dist/generated/decode-data-html.js.map +1 -0
- package/node_modules/node-html-parser/node_modules/entities/dist/generated/decode-data-xml.d.ts +3 -0
- package/node_modules/node-html-parser/node_modules/entities/dist/generated/decode-data-xml.d.ts.map +1 -0
- package/node_modules/node-html-parser/node_modules/entities/dist/generated/decode-data-xml.js +7 -0
- package/node_modules/node-html-parser/node_modules/entities/dist/generated/decode-data-xml.js.map +1 -0
- package/node_modules/node-html-parser/node_modules/entities/dist/generated/encode-html.d.ts +3 -0
- package/node_modules/node-html-parser/node_modules/entities/dist/generated/encode-html.d.ts.map +1 -0
- package/node_modules/node-html-parser/node_modules/entities/dist/generated/encode-html.js +3 -0
- package/node_modules/node-html-parser/node_modules/entities/dist/generated/encode-html.js.map +1 -0
- package/node_modules/node-html-parser/node_modules/entities/dist/index.d.ts +89 -0
- package/node_modules/node-html-parser/node_modules/entities/dist/index.d.ts.map +1 -0
- package/node_modules/node-html-parser/node_modules/entities/dist/index.js +87 -0
- package/node_modules/node-html-parser/node_modules/entities/dist/index.js.map +1 -0
- package/node_modules/node-html-parser/node_modules/entities/dist/internal/bin-trie-flags.d.ts +48 -0
- package/node_modules/node-html-parser/node_modules/entities/dist/internal/bin-trie-flags.d.ts.map +1 -0
- package/node_modules/node-html-parser/node_modules/entities/dist/internal/bin-trie-flags.js +49 -0
- package/node_modules/node-html-parser/node_modules/entities/dist/internal/bin-trie-flags.js.map +1 -0
- package/node_modules/node-html-parser/node_modules/entities/dist/internal/decode-shared.d.ts +38 -0
- package/node_modules/node-html-parser/node_modules/entities/dist/internal/decode-shared.d.ts.map +1 -0
- package/node_modules/node-html-parser/node_modules/entities/dist/internal/decode-shared.js +195 -0
- package/node_modules/node-html-parser/node_modules/entities/dist/internal/decode-shared.js.map +1 -0
- package/node_modules/node-html-parser/node_modules/entities/package.json +83 -0
- package/node_modules/node-html-parser/node_modules/entities/readme.md +131 -0
- package/node_modules/node-html-parser/node_modules/entities/src/decode-codepoint.ts +69 -0
- package/node_modules/node-html-parser/node_modules/entities/src/decode.ts +1368 -0
- package/node_modules/node-html-parser/node_modules/entities/src/encode.ts +337 -0
- package/node_modules/node-html-parser/node_modules/entities/src/escape.ts +187 -0
- package/node_modules/node-html-parser/node_modules/entities/src/generated/decode-data-html.ts +12 -0
- package/node_modules/node-html-parser/node_modules/entities/src/generated/decode-data-xml.ts +7 -0
- package/node_modules/node-html-parser/node_modules/entities/src/generated/encode-html.ts +3 -0
- package/node_modules/node-html-parser/node_modules/entities/src/index.ts +160 -0
- package/node_modules/node-html-parser/node_modules/entities/src/internal/bin-trie-flags.ts +47 -0
- package/node_modules/node-html-parser/node_modules/entities/src/internal/decode-shared.ts +209 -0
- package/node_modules/node-html-parser/package.json +35 -30
- package/package.json +3 -3
- package/renovate.json5 +1 -0
- package/scripts/sync-agent-assets.mjs +5 -1
- package/node_modules/he/LICENSE-MIT.txt +0 -20
- package/node_modules/he/README.md +0 -379
- package/node_modules/he/bin/he +0 -148
- package/node_modules/he/he.js +0 -345
- package/node_modules/he/man/he.1 +0 -78
- package/node_modules/he/package.json +0 -58
- package/node_modules/node-html-parser/dist/back.js +0 -6
- package/node_modules/node-html-parser/dist/index.js +0 -31
- package/node_modules/node-html-parser/dist/main.js +0 -1804
- package/node_modules/node-html-parser/dist/matcher.js +0 -106
- package/node_modules/node-html-parser/dist/nodes/comment.js +0 -33
- package/node_modules/node-html-parser/dist/nodes/html.js +0 -1186
- package/node_modules/node-html-parser/dist/nodes/node.js +0 -41
- package/node_modules/node-html-parser/dist/nodes/text.js +0 -105
- package/node_modules/node-html-parser/dist/nodes/type.js +0 -9
- package/node_modules/node-html-parser/dist/parse.js +0 -5
- package/node_modules/node-html-parser/dist/valid.js +0 -12
- package/node_modules/node-html-parser/dist/void-tag.js +0 -27
|
@@ -1,1186 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.parse = exports.base_parse = void 0;
|
|
7
|
-
const css_select_1 = require("css-select");
|
|
8
|
-
const he_1 = __importDefault(require("he"));
|
|
9
|
-
const back_1 = __importDefault(require("../back"));
|
|
10
|
-
const matcher_1 = __importDefault(require("../matcher"));
|
|
11
|
-
const void_tag_1 = __importDefault(require("../void-tag"));
|
|
12
|
-
const comment_1 = __importDefault(require("./comment"));
|
|
13
|
-
const node_1 = __importDefault(require("./node"));
|
|
14
|
-
const text_1 = __importDefault(require("./text"));
|
|
15
|
-
const type_1 = __importDefault(require("./type"));
|
|
16
|
-
function decode(val) {
|
|
17
|
-
// clone string
|
|
18
|
-
return JSON.parse(JSON.stringify(he_1.default.decode(val)));
|
|
19
|
-
}
|
|
20
|
-
// https://developer.mozilla.org/en-US/docs/Web/HTML/Block-level_elements
|
|
21
|
-
const Htags = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'header', 'hgroup'];
|
|
22
|
-
const Dtags = ['details', 'dialog', 'dd', 'div', 'dt'];
|
|
23
|
-
const Ftags = ['fieldset', 'figcaption', 'figure', 'footer', 'form'];
|
|
24
|
-
const tableTags = ['table', 'td', 'tr'];
|
|
25
|
-
const htmlTags = ['address', 'article', 'aside', 'blockquote', 'br', 'hr', 'li', 'main', 'nav', 'ol', 'p', 'pre', 'section', 'ul'];
|
|
26
|
-
const kBlockElements = new Set();
|
|
27
|
-
function addToKBlockElement(...args) {
|
|
28
|
-
const addToSet = (array) => {
|
|
29
|
-
for (let index = 0; index < array.length; index++) {
|
|
30
|
-
const element = array[index];
|
|
31
|
-
kBlockElements.add(element);
|
|
32
|
-
kBlockElements.add(element.toUpperCase());
|
|
33
|
-
}
|
|
34
|
-
};
|
|
35
|
-
for (const arg of args)
|
|
36
|
-
addToSet(arg);
|
|
37
|
-
}
|
|
38
|
-
addToKBlockElement(Htags, Dtags, Ftags, tableTags, htmlTags);
|
|
39
|
-
class DOMTokenList {
|
|
40
|
-
_validate(c) {
|
|
41
|
-
if (/\s/.test(c)) {
|
|
42
|
-
throw new Error(`DOMException in DOMTokenList.add: The token '${c}' contains HTML space characters, which are not valid in tokens.`);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
constructor(valuesInit = [], afterUpdate = () => null) {
|
|
46
|
-
this._set = new Set(valuesInit);
|
|
47
|
-
this._afterUpdate = afterUpdate;
|
|
48
|
-
}
|
|
49
|
-
add(c) {
|
|
50
|
-
this._validate(c);
|
|
51
|
-
this._set.add(c);
|
|
52
|
-
this._afterUpdate(this); // eslint-disable-line @typescript-eslint/no-unsafe-call
|
|
53
|
-
}
|
|
54
|
-
replace(c1, c2) {
|
|
55
|
-
this._validate(c2);
|
|
56
|
-
this._set.delete(c1);
|
|
57
|
-
this._set.add(c2);
|
|
58
|
-
this._afterUpdate(this); // eslint-disable-line @typescript-eslint/no-unsafe-call
|
|
59
|
-
}
|
|
60
|
-
remove(c) {
|
|
61
|
-
this._set.delete(c) && this._afterUpdate(this); // eslint-disable-line @typescript-eslint/no-unsafe-call
|
|
62
|
-
}
|
|
63
|
-
toggle(c) {
|
|
64
|
-
this._validate(c);
|
|
65
|
-
if (this._set.has(c))
|
|
66
|
-
this._set.delete(c);
|
|
67
|
-
else
|
|
68
|
-
this._set.add(c);
|
|
69
|
-
this._afterUpdate(this); // eslint-disable-line @typescript-eslint/no-unsafe-call
|
|
70
|
-
}
|
|
71
|
-
contains(c) {
|
|
72
|
-
return this._set.has(c);
|
|
73
|
-
}
|
|
74
|
-
get length() {
|
|
75
|
-
return this._set.size;
|
|
76
|
-
}
|
|
77
|
-
values() {
|
|
78
|
-
return this._set.values();
|
|
79
|
-
}
|
|
80
|
-
get value() {
|
|
81
|
-
return Array.from(this._set.values());
|
|
82
|
-
}
|
|
83
|
-
toString() {
|
|
84
|
-
return Array.from(this._set.values()).join(' ');
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
/**
|
|
88
|
-
* HTMLElement, which contains a set of children.
|
|
89
|
-
*
|
|
90
|
-
* Note: this is a minimalist implementation, no complete tree
|
|
91
|
-
* structure provided (no parentNode, nextSibling,
|
|
92
|
-
* previousSibling etc).
|
|
93
|
-
* @class HTMLElement
|
|
94
|
-
* @extends {Node}
|
|
95
|
-
*/
|
|
96
|
-
class HTMLElement extends node_1.default {
|
|
97
|
-
/**
|
|
98
|
-
* Quote attribute values
|
|
99
|
-
* @param attr attribute value
|
|
100
|
-
* @returns {string} quoted value
|
|
101
|
-
*/
|
|
102
|
-
quoteAttribute(attr) {
|
|
103
|
-
if (attr == null) {
|
|
104
|
-
return 'null';
|
|
105
|
-
}
|
|
106
|
-
return JSON.stringify(attr.replace(/"/g, '"'))
|
|
107
|
-
.replace(/\\t/g, '\t')
|
|
108
|
-
.replace(/\\n/g, '\n')
|
|
109
|
-
.replace(/\\r/g, '\r')
|
|
110
|
-
.replace(/\\/g, '');
|
|
111
|
-
}
|
|
112
|
-
/**
|
|
113
|
-
* Creates an instance of HTMLElement.
|
|
114
|
-
* @param keyAttrs id and class attribute
|
|
115
|
-
* @param [rawAttrs] attributes in string
|
|
116
|
-
*
|
|
117
|
-
* @memberof HTMLElement
|
|
118
|
-
*/
|
|
119
|
-
constructor(tagName, keyAttrs, rawAttrs = '', parentNode = null, range, voidTag = new void_tag_1.default(), _parseOptions = {}) {
|
|
120
|
-
super(parentNode, range);
|
|
121
|
-
this.rawAttrs = rawAttrs;
|
|
122
|
-
this.voidTag = voidTag;
|
|
123
|
-
/**
|
|
124
|
-
* Node Type declaration.
|
|
125
|
-
*/
|
|
126
|
-
this.nodeType = type_1.default.ELEMENT_NODE;
|
|
127
|
-
this.rawTagName = tagName;
|
|
128
|
-
this.rawAttrs = rawAttrs || '';
|
|
129
|
-
this._id = keyAttrs.id || '';
|
|
130
|
-
this.childNodes = [];
|
|
131
|
-
this._parseOptions = _parseOptions;
|
|
132
|
-
this.classList = new DOMTokenList(keyAttrs.class ? keyAttrs.class.split(/\s+/) : [], (classList) => this.setAttribute('class', classList.toString()) // eslint-disable-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
|
|
133
|
-
);
|
|
134
|
-
if (keyAttrs.id) {
|
|
135
|
-
if (!rawAttrs) {
|
|
136
|
-
this.rawAttrs = `id="${keyAttrs.id}"`;
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
if (keyAttrs.class) {
|
|
140
|
-
if (!rawAttrs) {
|
|
141
|
-
const cls = `class="${this.classList.toString()}"`;
|
|
142
|
-
if (this.rawAttrs) {
|
|
143
|
-
this.rawAttrs += ` ${cls}`;
|
|
144
|
-
}
|
|
145
|
-
else {
|
|
146
|
-
this.rawAttrs = cls;
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
/**
|
|
152
|
-
* Remove Child element from childNodes array
|
|
153
|
-
* @param {HTMLElement} node node to remove
|
|
154
|
-
*/
|
|
155
|
-
removeChild(node) {
|
|
156
|
-
this.childNodes = this.childNodes.filter((child) => {
|
|
157
|
-
return child !== node;
|
|
158
|
-
});
|
|
159
|
-
return this;
|
|
160
|
-
}
|
|
161
|
-
/**
|
|
162
|
-
* Exchanges given child with new child
|
|
163
|
-
* @param {HTMLElement} oldNode node to exchange
|
|
164
|
-
* @param {HTMLElement} newNode new node
|
|
165
|
-
*/
|
|
166
|
-
exchangeChild(oldNode, newNode) {
|
|
167
|
-
const children = this.childNodes;
|
|
168
|
-
this.childNodes = children.map((child) => {
|
|
169
|
-
if (child === oldNode) {
|
|
170
|
-
return newNode;
|
|
171
|
-
}
|
|
172
|
-
return child;
|
|
173
|
-
});
|
|
174
|
-
return this;
|
|
175
|
-
}
|
|
176
|
-
get tagName() {
|
|
177
|
-
return this.rawTagName ? this.rawTagName.toUpperCase() : this.rawTagName;
|
|
178
|
-
}
|
|
179
|
-
set tagName(newname) {
|
|
180
|
-
this.rawTagName = newname.toLowerCase();
|
|
181
|
-
}
|
|
182
|
-
get localName() {
|
|
183
|
-
return this.rawTagName.toLowerCase();
|
|
184
|
-
}
|
|
185
|
-
get isVoidElement() {
|
|
186
|
-
return this.voidTag.isVoidElement(this.localName);
|
|
187
|
-
}
|
|
188
|
-
get id() {
|
|
189
|
-
return this._id;
|
|
190
|
-
}
|
|
191
|
-
set id(newid) {
|
|
192
|
-
this.setAttribute('id', newid);
|
|
193
|
-
}
|
|
194
|
-
/**
|
|
195
|
-
* Get escpaed (as-it) text value of current node and its children.
|
|
196
|
-
* @return {string} text content
|
|
197
|
-
*/
|
|
198
|
-
get rawText() {
|
|
199
|
-
// https://github.com/taoqf/node-html-parser/issues/249
|
|
200
|
-
if (/^br$/i.test(this.rawTagName)) {
|
|
201
|
-
return '\n';
|
|
202
|
-
}
|
|
203
|
-
return this.childNodes.reduce((pre, cur) => {
|
|
204
|
-
return (pre += cur.rawText);
|
|
205
|
-
}, '');
|
|
206
|
-
}
|
|
207
|
-
get textContent() {
|
|
208
|
-
return decode(this.rawText);
|
|
209
|
-
}
|
|
210
|
-
set textContent(val) {
|
|
211
|
-
const content = [new text_1.default(val, this)];
|
|
212
|
-
this.childNodes = content;
|
|
213
|
-
}
|
|
214
|
-
/**
|
|
215
|
-
* Get unescaped text value of current node and its children.
|
|
216
|
-
* @return {string} text content
|
|
217
|
-
*/
|
|
218
|
-
get text() {
|
|
219
|
-
return decode(this.rawText);
|
|
220
|
-
}
|
|
221
|
-
/**
|
|
222
|
-
* Get structured Text (with '\n' etc.)
|
|
223
|
-
* @return {string} structured text
|
|
224
|
-
*/
|
|
225
|
-
get structuredText() {
|
|
226
|
-
let currentBlock = [];
|
|
227
|
-
const blocks = [currentBlock];
|
|
228
|
-
function dfs(node) {
|
|
229
|
-
if (node.nodeType === type_1.default.ELEMENT_NODE) {
|
|
230
|
-
if (kBlockElements.has(node.rawTagName)) {
|
|
231
|
-
if (currentBlock.length > 0) {
|
|
232
|
-
blocks.push((currentBlock = []));
|
|
233
|
-
}
|
|
234
|
-
node.childNodes.forEach(dfs);
|
|
235
|
-
if (currentBlock.length > 0) {
|
|
236
|
-
blocks.push((currentBlock = []));
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
else {
|
|
240
|
-
node.childNodes.forEach(dfs);
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
else if (node.nodeType === type_1.default.TEXT_NODE) {
|
|
244
|
-
if (node.isWhitespace) {
|
|
245
|
-
// Whitespace node, postponed output
|
|
246
|
-
currentBlock.prependWhitespace = true;
|
|
247
|
-
}
|
|
248
|
-
else {
|
|
249
|
-
let text = node.trimmedText;
|
|
250
|
-
if (currentBlock.prependWhitespace) {
|
|
251
|
-
text = ` ${text}`;
|
|
252
|
-
currentBlock.prependWhitespace = false;
|
|
253
|
-
}
|
|
254
|
-
currentBlock.push(text);
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
dfs(this);
|
|
259
|
-
return blocks
|
|
260
|
-
.map((block) => {
|
|
261
|
-
return block.join('').replace(/\s{2,}/g, ' '); // Normalize each line's whitespace
|
|
262
|
-
})
|
|
263
|
-
.join('\n')
|
|
264
|
-
.replace(/\s+$/, ''); // trimRight;
|
|
265
|
-
}
|
|
266
|
-
toString() {
|
|
267
|
-
const tag = this.rawTagName;
|
|
268
|
-
if (tag) {
|
|
269
|
-
const attrs = this.rawAttrs ? ` ${this.rawAttrs}` : '';
|
|
270
|
-
return this.voidTag.formatNode(tag, attrs, this.innerHTML);
|
|
271
|
-
}
|
|
272
|
-
return this.innerHTML;
|
|
273
|
-
}
|
|
274
|
-
get innerHTML() {
|
|
275
|
-
return this.childNodes
|
|
276
|
-
.map((child) => {
|
|
277
|
-
return child.toString();
|
|
278
|
-
})
|
|
279
|
-
.join('');
|
|
280
|
-
}
|
|
281
|
-
set innerHTML(content) {
|
|
282
|
-
const r = parse(content, this._parseOptions);
|
|
283
|
-
const nodes = r.childNodes.length ? r.childNodes : [new text_1.default(content, this)];
|
|
284
|
-
resetParent(nodes, this);
|
|
285
|
-
resetParent(this.childNodes, null);
|
|
286
|
-
this.childNodes = nodes;
|
|
287
|
-
}
|
|
288
|
-
set_content(content, options = {}) {
|
|
289
|
-
if (content instanceof node_1.default) {
|
|
290
|
-
content = [content];
|
|
291
|
-
}
|
|
292
|
-
else if (typeof content == 'string') {
|
|
293
|
-
options = Object.assign(Object.assign({}, this._parseOptions), options);
|
|
294
|
-
const r = parse(content, options);
|
|
295
|
-
content = r.childNodes.length ? r.childNodes : [new text_1.default(r.innerHTML, this)];
|
|
296
|
-
}
|
|
297
|
-
resetParent(this.childNodes, null);
|
|
298
|
-
resetParent(content, this);
|
|
299
|
-
this.childNodes = content;
|
|
300
|
-
return this;
|
|
301
|
-
}
|
|
302
|
-
replaceWith(...nodes) {
|
|
303
|
-
const parent = this.parentNode;
|
|
304
|
-
const content = nodes
|
|
305
|
-
.map((node) => {
|
|
306
|
-
if (node instanceof node_1.default) {
|
|
307
|
-
return [node];
|
|
308
|
-
}
|
|
309
|
-
else if (typeof node == 'string') {
|
|
310
|
-
const r = parse(node, this._parseOptions);
|
|
311
|
-
return r.childNodes.length ? r.childNodes : [new text_1.default(node, this)];
|
|
312
|
-
}
|
|
313
|
-
return [];
|
|
314
|
-
})
|
|
315
|
-
.flat();
|
|
316
|
-
const idx = parent.childNodes.findIndex((child) => {
|
|
317
|
-
return child === this;
|
|
318
|
-
});
|
|
319
|
-
resetParent([this], null);
|
|
320
|
-
parent.childNodes = [...parent.childNodes.slice(0, idx), ...resetParent(content, parent), ...parent.childNodes.slice(idx + 1)];
|
|
321
|
-
return this;
|
|
322
|
-
}
|
|
323
|
-
get outerHTML() {
|
|
324
|
-
return this.toString();
|
|
325
|
-
}
|
|
326
|
-
/**
|
|
327
|
-
* Trim element from right (in block) after seeing pattern in a TextNode.
|
|
328
|
-
* @param {RegExp} pattern pattern to find
|
|
329
|
-
* @return {HTMLElement} reference to current node
|
|
330
|
-
*/
|
|
331
|
-
trimRight(pattern) {
|
|
332
|
-
for (let i = 0; i < this.childNodes.length; i++) {
|
|
333
|
-
const childNode = this.childNodes[i];
|
|
334
|
-
if (childNode.nodeType === type_1.default.ELEMENT_NODE) {
|
|
335
|
-
childNode.trimRight(pattern);
|
|
336
|
-
}
|
|
337
|
-
else {
|
|
338
|
-
const index = childNode.rawText.search(pattern);
|
|
339
|
-
if (index > -1) {
|
|
340
|
-
childNode.rawText = childNode.rawText.substr(0, index);
|
|
341
|
-
// trim all following nodes.
|
|
342
|
-
this.childNodes.length = i + 1;
|
|
343
|
-
}
|
|
344
|
-
}
|
|
345
|
-
}
|
|
346
|
-
return this;
|
|
347
|
-
}
|
|
348
|
-
/**
|
|
349
|
-
* Get DOM structure
|
|
350
|
-
* @return {string} structure
|
|
351
|
-
*/
|
|
352
|
-
get structure() {
|
|
353
|
-
const res = [];
|
|
354
|
-
let indention = 0;
|
|
355
|
-
function write(str) {
|
|
356
|
-
res.push(' '.repeat(indention) + str);
|
|
357
|
-
}
|
|
358
|
-
function dfs(node) {
|
|
359
|
-
const idStr = node._id ? `#${node._id}` : '';
|
|
360
|
-
const classStr = node.classList.length ? `.${node.classList.value.join('.')}` : ''; // eslint-disable-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/restrict-template-expressions, @typescript-eslint/no-unsafe-call
|
|
361
|
-
write(`${node.rawTagName}${idStr}${classStr}`);
|
|
362
|
-
indention++;
|
|
363
|
-
node.childNodes.forEach((childNode) => {
|
|
364
|
-
if (childNode.nodeType === type_1.default.ELEMENT_NODE) {
|
|
365
|
-
dfs(childNode);
|
|
366
|
-
}
|
|
367
|
-
else if (childNode.nodeType === type_1.default.TEXT_NODE) {
|
|
368
|
-
if (!childNode.isWhitespace) {
|
|
369
|
-
write('#text');
|
|
370
|
-
}
|
|
371
|
-
}
|
|
372
|
-
});
|
|
373
|
-
indention--;
|
|
374
|
-
}
|
|
375
|
-
dfs(this);
|
|
376
|
-
return res.join('\n');
|
|
377
|
-
}
|
|
378
|
-
/**
|
|
379
|
-
* Remove whitespaces in this sub tree.
|
|
380
|
-
* @return {HTMLElement} pointer to this
|
|
381
|
-
*/
|
|
382
|
-
removeWhitespace() {
|
|
383
|
-
let o = 0;
|
|
384
|
-
this.childNodes.forEach((node) => {
|
|
385
|
-
if (node.nodeType === type_1.default.TEXT_NODE) {
|
|
386
|
-
if (node.isWhitespace) {
|
|
387
|
-
return;
|
|
388
|
-
}
|
|
389
|
-
node.rawText = node.trimmedRawText;
|
|
390
|
-
}
|
|
391
|
-
else if (node.nodeType === type_1.default.ELEMENT_NODE) {
|
|
392
|
-
node.removeWhitespace();
|
|
393
|
-
}
|
|
394
|
-
this.childNodes[o++] = node;
|
|
395
|
-
});
|
|
396
|
-
this.childNodes.length = o;
|
|
397
|
-
// remove whitespace between attributes
|
|
398
|
-
const attrs = Object.keys(this.rawAttributes)
|
|
399
|
-
.map((key) => {
|
|
400
|
-
const val = this.rawAttributes[key];
|
|
401
|
-
return `${key}=${JSON.stringify(val)}`;
|
|
402
|
-
})
|
|
403
|
-
.join(' ');
|
|
404
|
-
this.rawAttrs = attrs;
|
|
405
|
-
delete this._rawAttrs;
|
|
406
|
-
return this;
|
|
407
|
-
}
|
|
408
|
-
/**
|
|
409
|
-
* Query CSS selector to find matching nodes.
|
|
410
|
-
* @param {string} selector Simplified CSS selector
|
|
411
|
-
* @return {HTMLElement[]} matching elements
|
|
412
|
-
*/
|
|
413
|
-
querySelectorAll(selector) {
|
|
414
|
-
return (0, css_select_1.selectAll)(selector, this, {
|
|
415
|
-
xmlMode: true,
|
|
416
|
-
adapter: matcher_1.default,
|
|
417
|
-
});
|
|
418
|
-
}
|
|
419
|
-
/**
|
|
420
|
-
* Query CSS Selector to find matching node.
|
|
421
|
-
* @param {string} selector Simplified CSS selector
|
|
422
|
-
* @return {(HTMLElement|null)} matching node
|
|
423
|
-
*/
|
|
424
|
-
querySelector(selector) {
|
|
425
|
-
return (0, css_select_1.selectOne)(selector, this, {
|
|
426
|
-
xmlMode: true,
|
|
427
|
-
adapter: matcher_1.default,
|
|
428
|
-
});
|
|
429
|
-
}
|
|
430
|
-
/**
|
|
431
|
-
* Tests whether the node matches a given CSS selector.
|
|
432
|
-
* @param {string} selector Simplified CSS selector
|
|
433
|
-
* @return {boolean}
|
|
434
|
-
*/
|
|
435
|
-
matches(selector) {
|
|
436
|
-
return (0, css_select_1.is)(this, selector, {
|
|
437
|
-
xmlMode: true,
|
|
438
|
-
adapter: matcher_1.default,
|
|
439
|
-
});
|
|
440
|
-
}
|
|
441
|
-
/**
|
|
442
|
-
* find elements by their tagName
|
|
443
|
-
* @param {string} tagName the tagName of the elements to select
|
|
444
|
-
*/
|
|
445
|
-
getElementsByTagName(tagName) {
|
|
446
|
-
const upperCasedTagName = tagName.toUpperCase();
|
|
447
|
-
const re = [];
|
|
448
|
-
const stack = [];
|
|
449
|
-
let currentNodeReference = this;
|
|
450
|
-
let index = 0;
|
|
451
|
-
// index turns to undefined once the stack is empty and the first condition occurs
|
|
452
|
-
// which happens once all relevant children are searched through
|
|
453
|
-
while (index !== undefined) {
|
|
454
|
-
let child;
|
|
455
|
-
// make it work with sparse arrays
|
|
456
|
-
do {
|
|
457
|
-
child = currentNodeReference.childNodes[index++];
|
|
458
|
-
} while (index < currentNodeReference.childNodes.length && child === undefined);
|
|
459
|
-
// if the child does not exist we move on with the last provided index (which belongs to the parentNode)
|
|
460
|
-
if (child === undefined) {
|
|
461
|
-
currentNodeReference = currentNodeReference.parentNode;
|
|
462
|
-
index = stack.pop();
|
|
463
|
-
continue;
|
|
464
|
-
}
|
|
465
|
-
if (child.nodeType === type_1.default.ELEMENT_NODE) {
|
|
466
|
-
// https://developer.mozilla.org/en-US/docs/Web/API/Element/getElementsByTagName#syntax
|
|
467
|
-
if (tagName === '*' || child.tagName === upperCasedTagName)
|
|
468
|
-
re.push(child);
|
|
469
|
-
// if children are existing push the current status to the stack and keep searching for elements in the level below
|
|
470
|
-
if (child.childNodes.length > 0) {
|
|
471
|
-
stack.push(index);
|
|
472
|
-
currentNodeReference = child;
|
|
473
|
-
index = 0;
|
|
474
|
-
}
|
|
475
|
-
}
|
|
476
|
-
}
|
|
477
|
-
return re;
|
|
478
|
-
}
|
|
479
|
-
/**
|
|
480
|
-
* find element by it's id
|
|
481
|
-
* @param {string} id the id of the element to select
|
|
482
|
-
* @returns {HTMLElement | null} the element with the given id or null if not found
|
|
483
|
-
*/
|
|
484
|
-
getElementById(id) {
|
|
485
|
-
const stack = [];
|
|
486
|
-
let currentNodeReference = this;
|
|
487
|
-
let index = 0;
|
|
488
|
-
// index turns to undefined once the stack is empty and the first condition occurs
|
|
489
|
-
// which happens once all relevant children are searched through
|
|
490
|
-
while (index !== undefined) {
|
|
491
|
-
let child;
|
|
492
|
-
// make it work with sparse arrays
|
|
493
|
-
do {
|
|
494
|
-
child = currentNodeReference.childNodes[index++];
|
|
495
|
-
} while (index < currentNodeReference.childNodes.length && child === undefined);
|
|
496
|
-
// if the child does not exist we move on with the last provided index (which belongs to the parentNode)
|
|
497
|
-
if (child === undefined) {
|
|
498
|
-
currentNodeReference = currentNodeReference.parentNode;
|
|
499
|
-
index = stack.pop();
|
|
500
|
-
continue;
|
|
501
|
-
}
|
|
502
|
-
if (child.nodeType === type_1.default.ELEMENT_NODE) {
|
|
503
|
-
if (child._id === id) {
|
|
504
|
-
return child;
|
|
505
|
-
}
|
|
506
|
-
// if children are existing push the current status to the stack and keep searching for elements in the level below
|
|
507
|
-
if (child.childNodes.length > 0) {
|
|
508
|
-
stack.push(index);
|
|
509
|
-
currentNodeReference = child;
|
|
510
|
-
index = 0;
|
|
511
|
-
}
|
|
512
|
-
}
|
|
513
|
-
}
|
|
514
|
-
return null;
|
|
515
|
-
}
|
|
516
|
-
/**
|
|
517
|
-
* traverses the Element and its parents (heading toward the document root) until it finds a node that matches the provided selector string. Will return itself or the matching ancestor. If no such element exists, it returns null.
|
|
518
|
-
* @param selector a DOMString containing a selector list
|
|
519
|
-
* @returns {HTMLElement | null} the element with the given id or null if not found
|
|
520
|
-
*/
|
|
521
|
-
closest(selector) {
|
|
522
|
-
const mapChild = new Map();
|
|
523
|
-
let el = this;
|
|
524
|
-
let old = null;
|
|
525
|
-
function findOne(test, elems) {
|
|
526
|
-
let elem = null;
|
|
527
|
-
for (let i = 0, l = elems.length; i < l && !elem; i++) {
|
|
528
|
-
const el = elems[i];
|
|
529
|
-
if (test(el)) {
|
|
530
|
-
elem = el;
|
|
531
|
-
}
|
|
532
|
-
else {
|
|
533
|
-
const child = mapChild.get(el);
|
|
534
|
-
if (child) {
|
|
535
|
-
elem = findOne(test, [child]);
|
|
536
|
-
}
|
|
537
|
-
}
|
|
538
|
-
}
|
|
539
|
-
return elem;
|
|
540
|
-
}
|
|
541
|
-
while (el) {
|
|
542
|
-
mapChild.set(el, old);
|
|
543
|
-
old = el;
|
|
544
|
-
el = el.parentNode;
|
|
545
|
-
}
|
|
546
|
-
el = this;
|
|
547
|
-
while (el) {
|
|
548
|
-
const e = (0, css_select_1.selectOne)(selector, el, {
|
|
549
|
-
xmlMode: true,
|
|
550
|
-
adapter: Object.assign(Object.assign({}, matcher_1.default), { getChildren(node) {
|
|
551
|
-
const child = mapChild.get(node);
|
|
552
|
-
return child && [child];
|
|
553
|
-
},
|
|
554
|
-
getSiblings(node) {
|
|
555
|
-
return [node];
|
|
556
|
-
},
|
|
557
|
-
findOne,
|
|
558
|
-
findAll() {
|
|
559
|
-
return [];
|
|
560
|
-
} }),
|
|
561
|
-
});
|
|
562
|
-
if (e) {
|
|
563
|
-
return e;
|
|
564
|
-
}
|
|
565
|
-
el = el.parentNode;
|
|
566
|
-
}
|
|
567
|
-
return null;
|
|
568
|
-
}
|
|
569
|
-
/**
|
|
570
|
-
* Append a child node to childNodes
|
|
571
|
-
* @param {Node} node node to append
|
|
572
|
-
* @return {Node} node appended
|
|
573
|
-
*/
|
|
574
|
-
appendChild(node) {
|
|
575
|
-
this.append(node);
|
|
576
|
-
return node;
|
|
577
|
-
}
|
|
578
|
-
/**
|
|
579
|
-
* Get attributes
|
|
580
|
-
* @access private
|
|
581
|
-
* @return {Object} parsed and unescaped attributes
|
|
582
|
-
*/
|
|
583
|
-
get attrs() {
|
|
584
|
-
if (this._attrs) {
|
|
585
|
-
return this._attrs;
|
|
586
|
-
}
|
|
587
|
-
this._attrs = {};
|
|
588
|
-
const attrs = this.rawAttributes;
|
|
589
|
-
for (const key in attrs) {
|
|
590
|
-
const val = attrs[key] || '';
|
|
591
|
-
this._attrs[key.toLowerCase()] = decode(val);
|
|
592
|
-
}
|
|
593
|
-
return this._attrs;
|
|
594
|
-
}
|
|
595
|
-
get attributes() {
|
|
596
|
-
const ret_attrs = {};
|
|
597
|
-
const attrs = this.rawAttributes;
|
|
598
|
-
for (const key in attrs) {
|
|
599
|
-
const val = attrs[key] || '';
|
|
600
|
-
ret_attrs[key] = decode(val);
|
|
601
|
-
}
|
|
602
|
-
return ret_attrs;
|
|
603
|
-
}
|
|
604
|
-
/**
|
|
605
|
-
* Get escaped (as-is) attributes
|
|
606
|
-
* @return {Object} parsed attributes
|
|
607
|
-
*/
|
|
608
|
-
get rawAttributes() {
|
|
609
|
-
if (this._rawAttrs) {
|
|
610
|
-
return this._rawAttrs;
|
|
611
|
-
}
|
|
612
|
-
const attrs = {};
|
|
613
|
-
if (this.rawAttrs) {
|
|
614
|
-
const re = /([a-zA-Z()[\]#@$.?:][a-zA-Z0-9-._:()[\]#]*)(?:\s*=\s*((?:'[^']*')|(?:"[^"]*")|\S+))?/g;
|
|
615
|
-
let match;
|
|
616
|
-
while ((match = re.exec(this.rawAttrs))) {
|
|
617
|
-
const key = match[1];
|
|
618
|
-
let val = match[2] || null;
|
|
619
|
-
if (val && (val[0] === `'` || val[0] === `"`))
|
|
620
|
-
val = val.slice(1, val.length - 1);
|
|
621
|
-
attrs[key] = attrs[key] || val;
|
|
622
|
-
}
|
|
623
|
-
}
|
|
624
|
-
this._rawAttrs = attrs;
|
|
625
|
-
return attrs;
|
|
626
|
-
}
|
|
627
|
-
removeAttribute(key) {
|
|
628
|
-
const attrs = this.rawAttributes;
|
|
629
|
-
delete attrs[key];
|
|
630
|
-
// Update this.attribute
|
|
631
|
-
if (this._attrs) {
|
|
632
|
-
delete this._attrs[key];
|
|
633
|
-
}
|
|
634
|
-
// Update rawString
|
|
635
|
-
this.rawAttrs = Object.keys(attrs)
|
|
636
|
-
.map((name) => {
|
|
637
|
-
const val = this.quoteAttribute(attrs[name]);
|
|
638
|
-
if (val === 'null' || val === '""')
|
|
639
|
-
return name;
|
|
640
|
-
return `${name}=${val}`;
|
|
641
|
-
})
|
|
642
|
-
.join(' ');
|
|
643
|
-
// Update this._id
|
|
644
|
-
if (key === 'id') {
|
|
645
|
-
this._id = '';
|
|
646
|
-
}
|
|
647
|
-
return this;
|
|
648
|
-
}
|
|
649
|
-
hasAttribute(key) {
|
|
650
|
-
return key.toLowerCase() in this.attrs;
|
|
651
|
-
}
|
|
652
|
-
/**
|
|
653
|
-
* Get an attribute
|
|
654
|
-
* @return {string | undefined} value of the attribute; or undefined if not exist
|
|
655
|
-
*/
|
|
656
|
-
getAttribute(key) {
|
|
657
|
-
return this.attrs[key.toLowerCase()];
|
|
658
|
-
}
|
|
659
|
-
/**
|
|
660
|
-
* Set an attribute value to the HTMLElement
|
|
661
|
-
* @param {string} key The attribute name
|
|
662
|
-
* @param {string} value The value to set, or null / undefined to remove an attribute
|
|
663
|
-
*/
|
|
664
|
-
setAttribute(key, value) {
|
|
665
|
-
if (arguments.length < 2) {
|
|
666
|
-
throw new Error("Failed to execute 'setAttribute' on 'Element'");
|
|
667
|
-
}
|
|
668
|
-
const k2 = key.toLowerCase();
|
|
669
|
-
const attrs = this.rawAttributes;
|
|
670
|
-
for (const k in attrs) {
|
|
671
|
-
if (k.toLowerCase() === k2) {
|
|
672
|
-
key = k;
|
|
673
|
-
break;
|
|
674
|
-
}
|
|
675
|
-
}
|
|
676
|
-
attrs[key] = String(value);
|
|
677
|
-
// update this.attrs
|
|
678
|
-
if (this._attrs) {
|
|
679
|
-
this._attrs[k2] = decode(attrs[key]);
|
|
680
|
-
}
|
|
681
|
-
// Update rawString
|
|
682
|
-
this.rawAttrs = Object.keys(attrs)
|
|
683
|
-
.map((name) => {
|
|
684
|
-
const val = this.quoteAttribute(attrs[name]);
|
|
685
|
-
if (val === 'null' || val === '""')
|
|
686
|
-
return name;
|
|
687
|
-
return `${name}=${val}`;
|
|
688
|
-
})
|
|
689
|
-
.join(' ');
|
|
690
|
-
// Update this._id
|
|
691
|
-
if (key === 'id') {
|
|
692
|
-
this._id = value;
|
|
693
|
-
}
|
|
694
|
-
return this;
|
|
695
|
-
}
|
|
696
|
-
/**
|
|
697
|
-
* Replace all the attributes of the HTMLElement by the provided attributes
|
|
698
|
-
* @param {Attributes} attributes the new attribute set
|
|
699
|
-
*/
|
|
700
|
-
setAttributes(attributes) {
|
|
701
|
-
// Invalidate current this.attributes
|
|
702
|
-
if (this._attrs) {
|
|
703
|
-
delete this._attrs;
|
|
704
|
-
}
|
|
705
|
-
// Invalidate current this.rawAttributes
|
|
706
|
-
if (this._rawAttrs) {
|
|
707
|
-
delete this._rawAttrs;
|
|
708
|
-
}
|
|
709
|
-
// Update rawString
|
|
710
|
-
this.rawAttrs = Object.keys(attributes)
|
|
711
|
-
.map((name) => {
|
|
712
|
-
const val = attributes[name];
|
|
713
|
-
if (val === 'null' || val === '""')
|
|
714
|
-
return name;
|
|
715
|
-
return `${name}=${this.quoteAttribute(String(val))}`;
|
|
716
|
-
})
|
|
717
|
-
.join(' ');
|
|
718
|
-
// Update this._id
|
|
719
|
-
if ('id' in attributes) {
|
|
720
|
-
this._id = attributes['id'];
|
|
721
|
-
}
|
|
722
|
-
return this;
|
|
723
|
-
}
|
|
724
|
-
insertAdjacentHTML(where, html) {
|
|
725
|
-
if (arguments.length < 2) {
|
|
726
|
-
throw new Error('2 arguments required');
|
|
727
|
-
}
|
|
728
|
-
const p = parse(html, this._parseOptions);
|
|
729
|
-
if (where === 'afterend') {
|
|
730
|
-
this.after(...p.childNodes);
|
|
731
|
-
}
|
|
732
|
-
else if (where === 'afterbegin') {
|
|
733
|
-
this.prepend(...p.childNodes);
|
|
734
|
-
}
|
|
735
|
-
else if (where === 'beforeend') {
|
|
736
|
-
this.append(...p.childNodes);
|
|
737
|
-
}
|
|
738
|
-
else if (where === 'beforebegin') {
|
|
739
|
-
this.before(...p.childNodes);
|
|
740
|
-
}
|
|
741
|
-
else {
|
|
742
|
-
throw new Error(`The value provided ('${where}') is not one of 'beforebegin', 'afterbegin', 'beforeend', or 'afterend'`);
|
|
743
|
-
}
|
|
744
|
-
return this;
|
|
745
|
-
}
|
|
746
|
-
/** Prepend nodes or strings to this node's children. */
|
|
747
|
-
prepend(...insertable) {
|
|
748
|
-
const nodes = resolveInsertable(insertable);
|
|
749
|
-
resetParent(nodes, this);
|
|
750
|
-
this.childNodes.unshift(...nodes);
|
|
751
|
-
}
|
|
752
|
-
/** Append nodes or strings to this node's children. */
|
|
753
|
-
append(...insertable) {
|
|
754
|
-
const nodes = resolveInsertable(insertable);
|
|
755
|
-
resetParent(nodes, this);
|
|
756
|
-
this.childNodes.push(...nodes);
|
|
757
|
-
}
|
|
758
|
-
/** Insert nodes or strings before this node. */
|
|
759
|
-
before(...insertable) {
|
|
760
|
-
const nodes = resolveInsertable(insertable);
|
|
761
|
-
const siblings = this.parentNode.childNodes;
|
|
762
|
-
resetParent(nodes, this.parentNode);
|
|
763
|
-
siblings.splice(siblings.indexOf(this), 0, ...nodes);
|
|
764
|
-
}
|
|
765
|
-
/** Insert nodes or strings after this node. */
|
|
766
|
-
after(...insertable) {
|
|
767
|
-
const nodes = resolveInsertable(insertable);
|
|
768
|
-
const siblings = this.parentNode.childNodes;
|
|
769
|
-
resetParent(nodes, this.parentNode);
|
|
770
|
-
siblings.splice(siblings.indexOf(this) + 1, 0, ...nodes);
|
|
771
|
-
}
|
|
772
|
-
get nextSibling() {
|
|
773
|
-
if (this.parentNode) {
|
|
774
|
-
const children = this.parentNode.childNodes;
|
|
775
|
-
let i = 0;
|
|
776
|
-
while (i < children.length) {
|
|
777
|
-
const child = children[i++];
|
|
778
|
-
if (this === child)
|
|
779
|
-
return children[i] || null;
|
|
780
|
-
}
|
|
781
|
-
return null;
|
|
782
|
-
}
|
|
783
|
-
}
|
|
784
|
-
get nextElementSibling() {
|
|
785
|
-
if (this.parentNode) {
|
|
786
|
-
const children = this.parentNode.childNodes;
|
|
787
|
-
let i = 0;
|
|
788
|
-
let find = false;
|
|
789
|
-
while (i < children.length) {
|
|
790
|
-
const child = children[i++];
|
|
791
|
-
if (find) {
|
|
792
|
-
if (child instanceof HTMLElement) {
|
|
793
|
-
return child || null;
|
|
794
|
-
}
|
|
795
|
-
}
|
|
796
|
-
else if (this === child) {
|
|
797
|
-
find = true;
|
|
798
|
-
}
|
|
799
|
-
}
|
|
800
|
-
return null;
|
|
801
|
-
}
|
|
802
|
-
}
|
|
803
|
-
get previousSibling() {
|
|
804
|
-
if (this.parentNode) {
|
|
805
|
-
const children = this.parentNode.childNodes;
|
|
806
|
-
let i = children.length;
|
|
807
|
-
while (i > 0) {
|
|
808
|
-
const child = children[--i];
|
|
809
|
-
if (this === child)
|
|
810
|
-
return children[i - 1] || null;
|
|
811
|
-
}
|
|
812
|
-
return null;
|
|
813
|
-
}
|
|
814
|
-
}
|
|
815
|
-
get previousElementSibling() {
|
|
816
|
-
if (this.parentNode) {
|
|
817
|
-
const children = this.parentNode.childNodes;
|
|
818
|
-
let i = children.length;
|
|
819
|
-
let find = false;
|
|
820
|
-
while (i > 0) {
|
|
821
|
-
const child = children[--i];
|
|
822
|
-
if (find) {
|
|
823
|
-
if (child instanceof HTMLElement) {
|
|
824
|
-
return child || null;
|
|
825
|
-
}
|
|
826
|
-
}
|
|
827
|
-
else if (this === child) {
|
|
828
|
-
find = true;
|
|
829
|
-
}
|
|
830
|
-
}
|
|
831
|
-
return null;
|
|
832
|
-
}
|
|
833
|
-
}
|
|
834
|
-
/** Get all childNodes of type {@link HTMLElement}. */
|
|
835
|
-
get children() {
|
|
836
|
-
const children = [];
|
|
837
|
-
for (const childNode of this.childNodes) {
|
|
838
|
-
if (childNode instanceof HTMLElement) {
|
|
839
|
-
children.push(childNode);
|
|
840
|
-
}
|
|
841
|
-
}
|
|
842
|
-
return children;
|
|
843
|
-
}
|
|
844
|
-
/**
|
|
845
|
-
* Get the first child node.
|
|
846
|
-
* @return The first child or undefined if none exists.
|
|
847
|
-
*/
|
|
848
|
-
get firstChild() {
|
|
849
|
-
return this.childNodes[0];
|
|
850
|
-
}
|
|
851
|
-
/**
|
|
852
|
-
* Get the first child node of type {@link HTMLElement}.
|
|
853
|
-
* @return The first child element or undefined if none exists.
|
|
854
|
-
*/
|
|
855
|
-
get firstElementChild() {
|
|
856
|
-
return this.children[0];
|
|
857
|
-
}
|
|
858
|
-
/**
|
|
859
|
-
* Get the last child node.
|
|
860
|
-
* @return The last child or undefined if none exists.
|
|
861
|
-
*/
|
|
862
|
-
get lastChild() {
|
|
863
|
-
return (0, back_1.default)(this.childNodes);
|
|
864
|
-
}
|
|
865
|
-
/**
|
|
866
|
-
* Get the last child node of type {@link HTMLElement}.
|
|
867
|
-
* @return The last child element or undefined if none exists.
|
|
868
|
-
*/
|
|
869
|
-
get lastElementChild() {
|
|
870
|
-
return this.children[this.children.length - 1];
|
|
871
|
-
}
|
|
872
|
-
get childElementCount() {
|
|
873
|
-
return this.children.length;
|
|
874
|
-
}
|
|
875
|
-
get classNames() {
|
|
876
|
-
return this.classList.toString();
|
|
877
|
-
}
|
|
878
|
-
/** Clone this Node */
|
|
879
|
-
clone() {
|
|
880
|
-
return parse(this.toString(), this._parseOptions).firstChild;
|
|
881
|
-
}
|
|
882
|
-
}
|
|
883
|
-
exports.default = HTMLElement;
|
|
884
|
-
// #xB7 | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x37D] | [#x37F-#x1FFF] | [#x200C-#x200D] | [#x203F-#x2040] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]
|
|
885
|
-
// https://html.spec.whatwg.org/multipage/custom-elements.html#valid-custom-element-name
|
|
886
|
-
const kMarkupPattern = /<!--[\s\S]*?-->|<(\/?)([a-zA-Z][-.:0-9_a-zA-Z@\xB7\xC0-\xD6\xD8-\xF6\u00F8-\u03A1\u03A3-\u03D9\u03DB-\u03EF\u03F7-\u03FF\u0400-\u04FF\u0500-\u052F\u1D00-\u1D2B\u1D6B-\u1D77\u1D79-\u1D9A\u1E00-\u1E9B\u1F00-\u1F15\u1F18-\u1F1D\u1F20-\u1F45\u1F48-\u1F4D\u1F50-\u1F57\u1F59\u1F5B\u1F5D\u1F5F-\u1F7D\u1F80-\u1FB4\u1FB6-\u1FBC\u1FBE\u1FC2-\u1FC4\u1FC6-\u1FCC\u1FD0-\u1FD3\u1FD6-\u1FDB\u1FE0-\u1FEC\u1FF2-\u1FF4\u1FF6-\u1FFC\u2126\u212A-\u212B\u2132\u214E\u2160-\u2188\u2C60-\u2C7F\uA722-\uA787\uA78B-\uA78E\uA790-\uA7AD\uA7B0-\uA7B7\uA7F7-\uA7FF\uAB30-\uAB5A\uAB5C-\uAB5F\uAB64-\uAB65\uFB00-\uFB06\uFB13-\uFB17\uFF21-\uFF3A\uFF41-\uFF5A\x37F-\u1FFF\u200C-\u200D\u203F-\u2040\u2070-\u218F\u2C00-\u2FEF\u3001-\uD7FF\uF900-\uFDCF\uFDF0-\uFFFD]*)((?:\s+[^>]*?(?:(?:'[^']*')|(?:"[^"]*"))?)*)\s*(\/?)>/gu;
|
|
887
|
-
// const kMarkupPattern = /<!--[\s\S]*?-->|<(\/?)([a-zA-Z][-.:0-9_a-zA-Z]*)((?:\s+[^>]*?(?:(?:'[^']*')|(?:"[^"]*"))?)*)\s*(\/?)>/g;
|
|
888
|
-
const kAttributePattern = /(?:^|\s)(id|class)\s*=\s*((?:'[^']*')|(?:"[^"]*")|\S+)/gi;
|
|
889
|
-
const kElementsClosedByOpening = {
|
|
890
|
-
li: { li: true, LI: true },
|
|
891
|
-
LI: { li: true, LI: true },
|
|
892
|
-
p: { p: true, div: true, P: true, DIV: true },
|
|
893
|
-
P: { p: true, div: true, P: true, DIV: true },
|
|
894
|
-
b: { div: true, DIV: true },
|
|
895
|
-
B: { div: true, DIV: true },
|
|
896
|
-
td: { td: true, th: true, TD: true, TH: true },
|
|
897
|
-
TD: { td: true, th: true, TD: true, TH: true },
|
|
898
|
-
th: { td: true, th: true, TD: true, TH: true },
|
|
899
|
-
TH: { td: true, th: true, TD: true, TH: true },
|
|
900
|
-
h1: { h1: true, H1: true },
|
|
901
|
-
H1: { h1: true, H1: true },
|
|
902
|
-
h2: { h2: true, H2: true },
|
|
903
|
-
H2: { h2: true, H2: true },
|
|
904
|
-
h3: { h3: true, H3: true },
|
|
905
|
-
H3: { h3: true, H3: true },
|
|
906
|
-
h4: { h4: true, H4: true },
|
|
907
|
-
H4: { h4: true, H4: true },
|
|
908
|
-
h5: { h5: true, H5: true },
|
|
909
|
-
H5: { h5: true, H5: true },
|
|
910
|
-
h6: { h6: true, H6: true },
|
|
911
|
-
H6: { h6: true, H6: true },
|
|
912
|
-
};
|
|
913
|
-
const kElementsClosedByClosing = {
|
|
914
|
-
li: { ul: true, ol: true, UL: true, OL: true },
|
|
915
|
-
LI: { ul: true, ol: true, UL: true, OL: true },
|
|
916
|
-
a: { div: true, DIV: true },
|
|
917
|
-
A: { div: true, DIV: true },
|
|
918
|
-
b: { div: true, DIV: true },
|
|
919
|
-
B: { div: true, DIV: true },
|
|
920
|
-
i: { div: true, DIV: true },
|
|
921
|
-
I: { div: true, DIV: true },
|
|
922
|
-
p: { div: true, DIV: true },
|
|
923
|
-
P: { div: true, DIV: true },
|
|
924
|
-
td: { tr: true, table: true, TR: true, TABLE: true },
|
|
925
|
-
TD: { tr: true, table: true, TR: true, TABLE: true },
|
|
926
|
-
th: { tr: true, table: true, TR: true, TABLE: true },
|
|
927
|
-
TH: { tr: true, table: true, TR: true, TABLE: true },
|
|
928
|
-
};
|
|
929
|
-
const kElementsClosedByClosingExcept = {
|
|
930
|
-
p: { a: true, audio: true, del: true, ins: true, map: true, noscript: true, video: true },
|
|
931
|
-
};
|
|
932
|
-
const frameflag = 'documentfragmentcontainer';
|
|
933
|
-
/**
|
|
934
|
-
* Parses HTML and returns a root element
|
|
935
|
-
* Parse a chuck of HTML source.
|
|
936
|
-
* @param {string} data html
|
|
937
|
-
* @return {HTMLElement} root element
|
|
938
|
-
*/
|
|
939
|
-
function base_parse(data, options = {}) {
|
|
940
|
-
var _a, _b;
|
|
941
|
-
const voidTag = new void_tag_1.default((_a = options === null || options === void 0 ? void 0 : options.voidTag) === null || _a === void 0 ? void 0 : _a.closingSlash, (_b = options === null || options === void 0 ? void 0 : options.voidTag) === null || _b === void 0 ? void 0 : _b.tags);
|
|
942
|
-
const elements = options.blockTextElements || {
|
|
943
|
-
script: true,
|
|
944
|
-
noscript: true,
|
|
945
|
-
style: true,
|
|
946
|
-
pre: true,
|
|
947
|
-
};
|
|
948
|
-
const element_names = Object.keys(elements);
|
|
949
|
-
const kBlockTextElements = element_names.map((it) => new RegExp(`^${it}$`, 'i'));
|
|
950
|
-
const kIgnoreElements = element_names.filter((it) => Boolean(elements[it])).map((it) => new RegExp(`^${it}$`, 'i'));
|
|
951
|
-
function element_should_be_ignore(tag) {
|
|
952
|
-
return kIgnoreElements.some((it) => it.test(tag));
|
|
953
|
-
}
|
|
954
|
-
function is_block_text_element(tag) {
|
|
955
|
-
return kBlockTextElements.some((it) => it.test(tag));
|
|
956
|
-
}
|
|
957
|
-
const createRange = (startPos, endPos) => [startPos - frameFlagOffset, endPos - frameFlagOffset];
|
|
958
|
-
const root = new HTMLElement(null, {}, '', null, [0, data.length], voidTag, options);
|
|
959
|
-
let currentParent = root;
|
|
960
|
-
const stack = [root];
|
|
961
|
-
let lastTextPos = -1;
|
|
962
|
-
let noNestedTagIndex = undefined;
|
|
963
|
-
let match;
|
|
964
|
-
// https://github.com/taoqf/node-html-parser/issues/38
|
|
965
|
-
data = `<${frameflag}>${data}</${frameflag}>`;
|
|
966
|
-
const { lowerCaseTagName, fixNestedATags } = options;
|
|
967
|
-
const dataEndPos = data.length - (frameflag.length + 2);
|
|
968
|
-
const frameFlagOffset = frameflag.length + 2;
|
|
969
|
-
while ((match = kMarkupPattern.exec(data))) {
|
|
970
|
-
// Note: Object destructuring here consistently tests as higher performance than array destructuring
|
|
971
|
-
// eslint-disable-next-line prefer-const
|
|
972
|
-
let { 0: matchText, 1: leadingSlash, 2: tagName, 3: attributes, 4: closingSlash } = match;
|
|
973
|
-
const matchLength = matchText.length;
|
|
974
|
-
const tagStartPos = kMarkupPattern.lastIndex - matchLength;
|
|
975
|
-
const tagEndPos = kMarkupPattern.lastIndex;
|
|
976
|
-
// Add TextNode if content
|
|
977
|
-
if (lastTextPos > -1) {
|
|
978
|
-
if (lastTextPos + matchLength < tagEndPos) {
|
|
979
|
-
const text = data.substring(lastTextPos, tagStartPos);
|
|
980
|
-
currentParent.appendChild(new text_1.default(text, currentParent, createRange(lastTextPos, tagStartPos)));
|
|
981
|
-
}
|
|
982
|
-
}
|
|
983
|
-
lastTextPos = kMarkupPattern.lastIndex;
|
|
984
|
-
// https://github.com/taoqf/node-html-parser/issues/38
|
|
985
|
-
// Skip frameflag node
|
|
986
|
-
if (tagName === frameflag)
|
|
987
|
-
continue;
|
|
988
|
-
// Handle comments
|
|
989
|
-
if (matchText[1] === '!') {
|
|
990
|
-
if (options.comment) {
|
|
991
|
-
// Only keep what is in between <!-- and -->
|
|
992
|
-
const text = data.substring(tagStartPos + 4, tagEndPos - 3);
|
|
993
|
-
currentParent.appendChild(new comment_1.default(text, currentParent, createRange(tagStartPos, tagEndPos)));
|
|
994
|
-
}
|
|
995
|
-
continue;
|
|
996
|
-
}
|
|
997
|
-
/* -- Handle tag matching -- */
|
|
998
|
-
// Fix tag casing if necessary
|
|
999
|
-
if (lowerCaseTagName)
|
|
1000
|
-
tagName = tagName.toLowerCase();
|
|
1001
|
-
// Handle opening tags (ie. <this> not </that>)
|
|
1002
|
-
if (!leadingSlash) {
|
|
1003
|
-
/* Populate attributes */
|
|
1004
|
-
const attrs = {};
|
|
1005
|
-
for (let attMatch; (attMatch = kAttributePattern.exec(attributes));) {
|
|
1006
|
-
const { 1: key, 2: val } = attMatch;
|
|
1007
|
-
const isQuoted = val[0] === `'` || val[0] === `"`;
|
|
1008
|
-
attrs[key.toLowerCase()] = isQuoted ? val.slice(1, val.length - 1) : val;
|
|
1009
|
-
}
|
|
1010
|
-
const parentTagName = currentParent.rawTagName;
|
|
1011
|
-
if (!closingSlash && !options.preserveTagNesting && kElementsClosedByOpening[parentTagName]) {
|
|
1012
|
-
if (kElementsClosedByOpening[parentTagName][tagName]) {
|
|
1013
|
-
stack.pop();
|
|
1014
|
-
currentParent = (0, back_1.default)(stack);
|
|
1015
|
-
}
|
|
1016
|
-
}
|
|
1017
|
-
// Prevent nested A tags by terminating the last A and starting a new one : see issue #144
|
|
1018
|
-
if (fixNestedATags && (tagName === 'a' || tagName === 'A')) {
|
|
1019
|
-
if (noNestedTagIndex !== undefined) {
|
|
1020
|
-
stack.splice(noNestedTagIndex);
|
|
1021
|
-
currentParent = (0, back_1.default)(stack);
|
|
1022
|
-
}
|
|
1023
|
-
noNestedTagIndex = stack.length;
|
|
1024
|
-
}
|
|
1025
|
-
const tagEndPos = kMarkupPattern.lastIndex;
|
|
1026
|
-
const tagStartPos = tagEndPos - matchLength;
|
|
1027
|
-
currentParent = currentParent.appendChild(
|
|
1028
|
-
// Initialize range (end position updated later for closed tags)
|
|
1029
|
-
new HTMLElement(tagName, attrs, attributes.slice(1), null, createRange(tagStartPos, tagEndPos), voidTag, options));
|
|
1030
|
-
stack.push(currentParent);
|
|
1031
|
-
if (is_block_text_element(tagName)) {
|
|
1032
|
-
// Find closing tag
|
|
1033
|
-
const closeMarkup = `</${tagName}>`;
|
|
1034
|
-
const closeIndex = lowerCaseTagName
|
|
1035
|
-
? data.toLocaleLowerCase().indexOf(closeMarkup, kMarkupPattern.lastIndex)
|
|
1036
|
-
: data.indexOf(closeMarkup, kMarkupPattern.lastIndex);
|
|
1037
|
-
const textEndPos = closeIndex === -1 ? dataEndPos : closeIndex;
|
|
1038
|
-
if (element_should_be_ignore(tagName)) {
|
|
1039
|
-
const text = data.substring(tagEndPos, textEndPos);
|
|
1040
|
-
if (text.length > 0 && /\S/.test(text)) {
|
|
1041
|
-
currentParent.appendChild(new text_1.default(text, currentParent, createRange(tagEndPos, textEndPos)));
|
|
1042
|
-
}
|
|
1043
|
-
}
|
|
1044
|
-
if (closeIndex === -1) {
|
|
1045
|
-
lastTextPos = kMarkupPattern.lastIndex = data.length + 1;
|
|
1046
|
-
}
|
|
1047
|
-
else {
|
|
1048
|
-
lastTextPos = kMarkupPattern.lastIndex = closeIndex + closeMarkup.length;
|
|
1049
|
-
// Cause to be treated as self-closing, because no close found
|
|
1050
|
-
leadingSlash = '/';
|
|
1051
|
-
}
|
|
1052
|
-
}
|
|
1053
|
-
}
|
|
1054
|
-
// Handle closing tags or self-closed elements (ie </tag> or <br>)
|
|
1055
|
-
if (leadingSlash || closingSlash || voidTag.isVoidElement(tagName)) {
|
|
1056
|
-
while (true) {
|
|
1057
|
-
if (noNestedTagIndex != null && (tagName === 'a' || tagName === 'A'))
|
|
1058
|
-
noNestedTagIndex = undefined;
|
|
1059
|
-
if (currentParent.rawTagName === tagName) {
|
|
1060
|
-
// Update range end for closed tag
|
|
1061
|
-
currentParent.range[1] = createRange(-1, Math.max(lastTextPos, tagEndPos))[1];
|
|
1062
|
-
stack.pop();
|
|
1063
|
-
currentParent = (0, back_1.default)(stack);
|
|
1064
|
-
break;
|
|
1065
|
-
}
|
|
1066
|
-
else {
|
|
1067
|
-
const parentTagName = currentParent.tagName;
|
|
1068
|
-
// Trying to close current tag, and move on
|
|
1069
|
-
if (kElementsClosedByClosing[parentTagName]) {
|
|
1070
|
-
if (kElementsClosedByClosing[parentTagName][tagName]) {
|
|
1071
|
-
stack.pop();
|
|
1072
|
-
currentParent = (0, back_1.default)(stack);
|
|
1073
|
-
continue;
|
|
1074
|
-
}
|
|
1075
|
-
}
|
|
1076
|
-
const openTag = currentParent.rawTagName ?
|
|
1077
|
-
currentParent.rawTagName.toLowerCase() :
|
|
1078
|
-
'';
|
|
1079
|
-
if (kElementsClosedByClosingExcept[openTag]) {
|
|
1080
|
-
const closingTag = tagName.toLowerCase();
|
|
1081
|
-
if (stack.length > 1) {
|
|
1082
|
-
const possibleContainer = stack[stack.length - 2];
|
|
1083
|
-
if (possibleContainer &&
|
|
1084
|
-
possibleContainer.rawTagName &&
|
|
1085
|
-
possibleContainer.rawTagName.toLowerCase() === closingTag &&
|
|
1086
|
-
!kElementsClosedByClosingExcept[openTag][closingTag]) {
|
|
1087
|
-
// Update range end for closed tag
|
|
1088
|
-
currentParent.range[1] = createRange(-1, Math.max(lastTextPos, tagEndPos))[1];
|
|
1089
|
-
stack.pop();
|
|
1090
|
-
currentParent = (0, back_1.default)(stack);
|
|
1091
|
-
continue;
|
|
1092
|
-
}
|
|
1093
|
-
}
|
|
1094
|
-
}
|
|
1095
|
-
if (options.closeAllByClosing === true) {
|
|
1096
|
-
// If tag was opened, close all nested tags
|
|
1097
|
-
let i;
|
|
1098
|
-
for (i = stack.length - 2; i >= 0; i--) {
|
|
1099
|
-
if (stack[i].rawTagName === tagName)
|
|
1100
|
-
break;
|
|
1101
|
-
}
|
|
1102
|
-
if (i >= 0) {
|
|
1103
|
-
while (stack.length > i) {
|
|
1104
|
-
// Update range end for closed tag
|
|
1105
|
-
currentParent.range[1] = createRange(-1, Math.max(lastTextPos, tagEndPos))[1];
|
|
1106
|
-
stack.pop();
|
|
1107
|
-
currentParent = (0, back_1.default)(stack);
|
|
1108
|
-
}
|
|
1109
|
-
continue;
|
|
1110
|
-
}
|
|
1111
|
-
}
|
|
1112
|
-
// Use aggressive strategy to handle unmatching markups.
|
|
1113
|
-
break;
|
|
1114
|
-
}
|
|
1115
|
-
}
|
|
1116
|
-
}
|
|
1117
|
-
}
|
|
1118
|
-
return stack;
|
|
1119
|
-
}
|
|
1120
|
-
exports.base_parse = base_parse;
|
|
1121
|
-
/**
|
|
1122
|
-
* Parses HTML and returns a root element
|
|
1123
|
-
* Parse a chuck of HTML source.
|
|
1124
|
-
*/
|
|
1125
|
-
function parse(data, options = {}) {
|
|
1126
|
-
const stack = base_parse(data, options);
|
|
1127
|
-
const [root] = stack;
|
|
1128
|
-
while (stack.length > 1) {
|
|
1129
|
-
// Handle each error elements.
|
|
1130
|
-
const last = stack.pop();
|
|
1131
|
-
const oneBefore = (0, back_1.default)(stack);
|
|
1132
|
-
if (last.parentNode && last.parentNode.parentNode) {
|
|
1133
|
-
if (last.parentNode === oneBefore && last.tagName === oneBefore.tagName) {
|
|
1134
|
-
// Pair error case <h3> <h3> handle : Fixes to <h3> </h3>
|
|
1135
|
-
// this is wrong, becouse this will put the H3 outside the current right position which should be inside the current Html Element, see issue 152 for more info
|
|
1136
|
-
if (options.parseNoneClosedTags !== true) {
|
|
1137
|
-
oneBefore.removeChild(last);
|
|
1138
|
-
last.childNodes.forEach((child) => {
|
|
1139
|
-
oneBefore.parentNode.appendChild(child);
|
|
1140
|
-
});
|
|
1141
|
-
stack.pop();
|
|
1142
|
-
}
|
|
1143
|
-
}
|
|
1144
|
-
else {
|
|
1145
|
-
// Single error <div> <h3> </div> handle: Just removes <h3>
|
|
1146
|
-
// Why remove? this is already a HtmlElement and the missing <H3> is already added in this case. see issue 152 for more info
|
|
1147
|
-
// eslint-disable-next-line no-lonely-if
|
|
1148
|
-
if (options.parseNoneClosedTags !== true) {
|
|
1149
|
-
oneBefore.removeChild(last);
|
|
1150
|
-
last.childNodes.forEach((child) => {
|
|
1151
|
-
oneBefore.appendChild(child);
|
|
1152
|
-
});
|
|
1153
|
-
}
|
|
1154
|
-
}
|
|
1155
|
-
}
|
|
1156
|
-
else {
|
|
1157
|
-
// If it's final element just skip.
|
|
1158
|
-
}
|
|
1159
|
-
}
|
|
1160
|
-
// response.childNodes.forEach((node) => {
|
|
1161
|
-
// if (node instanceof HTMLElement) {
|
|
1162
|
-
// node.parentNode = null;
|
|
1163
|
-
// }
|
|
1164
|
-
// });
|
|
1165
|
-
return root;
|
|
1166
|
-
}
|
|
1167
|
-
exports.parse = parse;
|
|
1168
|
-
/**
|
|
1169
|
-
* Resolves a list of {@link NodeInsertable} to a list of nodes,
|
|
1170
|
-
* and removes nodes from any potential parent.
|
|
1171
|
-
*/
|
|
1172
|
-
function resolveInsertable(insertable) {
|
|
1173
|
-
return insertable.map((val) => {
|
|
1174
|
-
if (typeof val === 'string') {
|
|
1175
|
-
return new text_1.default(val);
|
|
1176
|
-
}
|
|
1177
|
-
val.remove();
|
|
1178
|
-
return val;
|
|
1179
|
-
});
|
|
1180
|
-
}
|
|
1181
|
-
function resetParent(nodes, parent) {
|
|
1182
|
-
return nodes.map((node) => {
|
|
1183
|
-
node.parentNode = parent;
|
|
1184
|
-
return node;
|
|
1185
|
-
});
|
|
1186
|
-
}
|