parse5 2.2.2 → 3.0.2
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/LICENSE +19 -19
- package/README.md +40 -40
- package/lib/common/doctype.js +156 -137
- package/lib/common/foreign_content.js +1 -1
- package/lib/common/html.js +272 -266
- package/lib/common/merge_options.js +13 -13
- package/lib/common/unicode.js +47 -47
- package/lib/index.d.ts +1077 -0
- package/lib/index.js +44 -108
- package/lib/location_info/parser_mixin.js +57 -51
- package/lib/location_info/tokenizer_mixin.js +2 -23
- package/lib/parser/formatting_element_list.js +167 -167
- package/lib/parser/index.js +2819 -2817
- package/lib/parser/open_element_stack.js +395 -395
- package/lib/parser/parser_stream.js +76 -0
- package/lib/parser/plain_text_conversion_stream.js +20 -0
- package/lib/sax/dev_null_stream.js +14 -14
- package/lib/sax/index.js +0 -117
- package/lib/serializer/index.js +162 -180
- package/lib/serializer/{stream.js → serializer_stream.js} +28 -49
- package/lib/tokenizer/index.js +63 -16
- package/lib/tokenizer/named_entity_data.js +5 -0
- package/lib/tree_adapters/default.js +209 -578
- package/lib/tree_adapters/htmlparser2.js +338 -327
- package/package.json +13 -14
- package/lib/parser/stream.js +0 -140
- package/lib/tokenizer/named_entity_trie.js +0 -6
|
@@ -1,578 +1,209 @@
|
|
|
1
|
-
'use strict';
|
|
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
|
-
exports.
|
|
150
|
-
|
|
151
|
-
};
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
exports.
|
|
167
|
-
return
|
|
168
|
-
};
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
};
|
|
210
|
-
|
|
211
|
-
/**
|
|
212
|
-
* Sets the document's quirks mode flag.
|
|
213
|
-
*
|
|
214
|
-
* @function setQuirksMode
|
|
215
|
-
* @memberof TreeAdapter
|
|
216
|
-
*
|
|
217
|
-
* @param {ASTNode<Document>} document - Document node.
|
|
218
|
-
*
|
|
219
|
-
* @see {@link https://github.com/inikulin/parse5/blob/tree-adapter-docs-rev/lib/tree_adapters/default.js#L221|default implementation.}
|
|
220
|
-
*/
|
|
221
|
-
exports.setQuirksMode = function (document) {
|
|
222
|
-
document.quirksMode = true;
|
|
223
|
-
};
|
|
224
|
-
|
|
225
|
-
/**
|
|
226
|
-
* Determines if the document's quirks mode flag is set.
|
|
227
|
-
*
|
|
228
|
-
* @function isQuirksMode
|
|
229
|
-
* @memberof TreeAdapter
|
|
230
|
-
*
|
|
231
|
-
* @param {ASTNode<Document>} document - Document node.
|
|
232
|
-
|
|
233
|
-
* @returns {Boolean}
|
|
234
|
-
*
|
|
235
|
-
* @see {@link https://github.com/inikulin/parse5/blob/tree-adapter-docs-rev/lib/tree_adapters/default.js#L237|default implementation.}
|
|
236
|
-
*/
|
|
237
|
-
exports.isQuirksMode = function (document) {
|
|
238
|
-
return document.quirksMode;
|
|
239
|
-
};
|
|
240
|
-
|
|
241
|
-
/**
|
|
242
|
-
* Removes a node from its parent.
|
|
243
|
-
*
|
|
244
|
-
* @function detachNode
|
|
245
|
-
* @memberof TreeAdapter
|
|
246
|
-
*
|
|
247
|
-
* @param {ASTNode} node - Node.
|
|
248
|
-
|
|
249
|
-
* @see {@link https://github.com/inikulin/parse5/blob/tree-adapter-docs-rev/lib/tree_adapters/default.js#L251|default implementation.}
|
|
250
|
-
*/
|
|
251
|
-
exports.detachNode = function (node) {
|
|
252
|
-
if (node.parentNode) {
|
|
253
|
-
var idx = node.parentNode.childNodes.indexOf(node);
|
|
254
|
-
|
|
255
|
-
node.parentNode.childNodes.splice(idx, 1);
|
|
256
|
-
node.parentNode = null;
|
|
257
|
-
}
|
|
258
|
-
};
|
|
259
|
-
|
|
260
|
-
/**
|
|
261
|
-
* Inserts text into a node. If the last child of the node is a text node, the provided text will be appended to the
|
|
262
|
-
* text node content. Otherwise, inserts a new text node with the given text.
|
|
263
|
-
*
|
|
264
|
-
*
|
|
265
|
-
* @function insertText
|
|
266
|
-
* @memberof TreeAdapter
|
|
267
|
-
*
|
|
268
|
-
* @param {ASTNode} parentNode - Node to insert text into.
|
|
269
|
-
* @param {String} text - Text to insert.
|
|
270
|
-
|
|
271
|
-
* @see {@link https://github.com/inikulin/parse5/blob/tree-adapter-docs-rev/lib/tree_adapters/default.js#L273|default implementation.}
|
|
272
|
-
*/
|
|
273
|
-
exports.insertText = function (parentNode, text) {
|
|
274
|
-
if (parentNode.childNodes.length) {
|
|
275
|
-
var prevNode = parentNode.childNodes[parentNode.childNodes.length - 1];
|
|
276
|
-
|
|
277
|
-
if (prevNode.nodeName === '#text') {
|
|
278
|
-
prevNode.value += text;
|
|
279
|
-
return;
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
appendChild(parentNode, createTextNode(text));
|
|
284
|
-
};
|
|
285
|
-
|
|
286
|
-
/**
|
|
287
|
-
* Inserts text into a sibling node that goes before the reference node. If this sibling node is the text node,
|
|
288
|
-
* the provided text will be appended to the text node content. Otherwise, inserts a new sibling text node with
|
|
289
|
-
* the given text before the reference node.
|
|
290
|
-
*
|
|
291
|
-
*
|
|
292
|
-
* @function insertTextBefore
|
|
293
|
-
* @memberof TreeAdapter
|
|
294
|
-
*
|
|
295
|
-
* @param {ASTNode} parentNode - Node to insert text into.
|
|
296
|
-
* @param {String} text - Text to insert.
|
|
297
|
-
* @param {ASTNode} referenceNode - Node to insert text before.
|
|
298
|
-
*
|
|
299
|
-
* @see {@link https://github.com/inikulin/parse5/blob/tree-adapter-docs-rev/lib/tree_adapters/default.js#L301|default implementation.}
|
|
300
|
-
*/
|
|
301
|
-
exports.insertTextBefore = function (parentNode, text, referenceNode) {
|
|
302
|
-
var prevNode = parentNode.childNodes[parentNode.childNodes.indexOf(referenceNode) - 1];
|
|
303
|
-
|
|
304
|
-
if (prevNode && prevNode.nodeName === '#text')
|
|
305
|
-
prevNode.value += text;
|
|
306
|
-
else
|
|
307
|
-
insertBefore(parentNode, createTextNode(text), referenceNode);
|
|
308
|
-
};
|
|
309
|
-
|
|
310
|
-
/**
|
|
311
|
-
* Copies attributes to the given node. Only attributes that are not yet present in the node are copied.
|
|
312
|
-
*
|
|
313
|
-
* @function adoptAttributes
|
|
314
|
-
* @memberof TreeAdapter
|
|
315
|
-
*
|
|
316
|
-
* @param {ASTNode} recipientNode - Node to copy attributes into.
|
|
317
|
-
* @param {Array} attrs - Attributes to copy.
|
|
318
|
-
|
|
319
|
-
* @see {@link https://github.com/inikulin/parse5/blob/tree-adapter-docs-rev/lib/tree_adapters/default.js#L321|default implementation.}
|
|
320
|
-
*/
|
|
321
|
-
exports.adoptAttributes = function (recipientNode, attrs) {
|
|
322
|
-
var recipientAttrsMap = [];
|
|
323
|
-
|
|
324
|
-
for (var i = 0; i < recipientNode.attrs.length; i++)
|
|
325
|
-
recipientAttrsMap.push(recipientNode.attrs[i].name);
|
|
326
|
-
|
|
327
|
-
for (var j = 0; j < attrs.length; j++) {
|
|
328
|
-
if (recipientAttrsMap.indexOf(attrs[j].name) === -1)
|
|
329
|
-
recipientNode.attrs.push(attrs[j]);
|
|
330
|
-
}
|
|
331
|
-
};
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
//Tree traversing
|
|
335
|
-
|
|
336
|
-
/**
|
|
337
|
-
* Returns the first child of the given node.
|
|
338
|
-
*
|
|
339
|
-
* @function getFirstChild
|
|
340
|
-
* @memberof TreeAdapter
|
|
341
|
-
*
|
|
342
|
-
* @param {ASTNode} node - Node.
|
|
343
|
-
*
|
|
344
|
-
* @returns {ASTNode} firstChild
|
|
345
|
-
*
|
|
346
|
-
* @see {@link https://github.com/inikulin/parse5/blob/tree-adapter-docs-rev/lib/tree_adapters/default.js#L348|default implementation.}
|
|
347
|
-
*/
|
|
348
|
-
exports.getFirstChild = function (node) {
|
|
349
|
-
return node.childNodes[0];
|
|
350
|
-
};
|
|
351
|
-
|
|
352
|
-
/**
|
|
353
|
-
* Returns the given node's children in an array.
|
|
354
|
-
*
|
|
355
|
-
* @function getChildNodes
|
|
356
|
-
* @memberof TreeAdapter
|
|
357
|
-
*
|
|
358
|
-
* @param {ASTNode} node - Node.
|
|
359
|
-
*
|
|
360
|
-
* @returns {Array} children
|
|
361
|
-
*
|
|
362
|
-
* @see {@link https://github.com/inikulin/parse5/blob/tree-adapter-docs-rev/lib/tree_adapters/default.js#L364|default implementation.}
|
|
363
|
-
*/
|
|
364
|
-
exports.getChildNodes = function (node) {
|
|
365
|
-
return node.childNodes;
|
|
366
|
-
};
|
|
367
|
-
|
|
368
|
-
/**
|
|
369
|
-
* Returns the given node's parent.
|
|
370
|
-
*
|
|
371
|
-
* @function getParentNode
|
|
372
|
-
* @memberof TreeAdapter
|
|
373
|
-
*
|
|
374
|
-
* @param {ASTNode} node - Node.
|
|
375
|
-
*
|
|
376
|
-
* @returns {ASTNode} parent
|
|
377
|
-
*
|
|
378
|
-
* @see {@link https://github.com/inikulin/parse5/blob/tree-adapter-docs-rev/lib/tree_adapters/default.js#L380|default implementation.}
|
|
379
|
-
*/
|
|
380
|
-
exports.getParentNode = function (node) {
|
|
381
|
-
return node.parentNode;
|
|
382
|
-
};
|
|
383
|
-
|
|
384
|
-
/**
|
|
385
|
-
* Returns the given node's attributes in an array, in the form of name-value pairs.
|
|
386
|
-
* Foreign attributes may contain `namespace` and `prefix` fields as well.
|
|
387
|
-
*
|
|
388
|
-
* @function getAttrList
|
|
389
|
-
* @memberof TreeAdapter
|
|
390
|
-
*
|
|
391
|
-
* @param {ASTNode} node - Node.
|
|
392
|
-
*
|
|
393
|
-
* @returns {Array} attributes
|
|
394
|
-
*
|
|
395
|
-
* @see {@link https://github.com/inikulin/parse5/blob/tree-adapter-docs-rev/lib/tree_adapters/default.js#L397|default implementation.}
|
|
396
|
-
*/
|
|
397
|
-
exports.getAttrList = function (node) {
|
|
398
|
-
return node.attrs;
|
|
399
|
-
};
|
|
400
|
-
|
|
401
|
-
//Node data
|
|
402
|
-
|
|
403
|
-
/**
|
|
404
|
-
* Returns the given element's tag name.
|
|
405
|
-
*
|
|
406
|
-
* @function getTagName
|
|
407
|
-
* @memberof TreeAdapter
|
|
408
|
-
*
|
|
409
|
-
* @param {ASTNode<Element>} element - Element.
|
|
410
|
-
*
|
|
411
|
-
* @returns {String} tagName
|
|
412
|
-
*
|
|
413
|
-
* @see {@link https://github.com/inikulin/parse5/blob/tree-adapter-docs-rev/lib/tree_adapters/default.js#L415|default implementation.}
|
|
414
|
-
*/
|
|
415
|
-
exports.getTagName = function (element) {
|
|
416
|
-
return element.tagName;
|
|
417
|
-
};
|
|
418
|
-
|
|
419
|
-
/**
|
|
420
|
-
* Returns the given element's namespace.
|
|
421
|
-
*
|
|
422
|
-
* @function getNamespaceURI
|
|
423
|
-
* @memberof TreeAdapter
|
|
424
|
-
*
|
|
425
|
-
* @param {ASTNode<Element>} element - Element.
|
|
426
|
-
*
|
|
427
|
-
* @returns {String} namespaceURI
|
|
428
|
-
*
|
|
429
|
-
* @see {@link https://github.com/inikulin/parse5/blob/tree-adapter-docs-rev/lib/tree_adapters/default.js#L431|default implementation.}
|
|
430
|
-
*/
|
|
431
|
-
exports.getNamespaceURI = function (element) {
|
|
432
|
-
return element.namespaceURI;
|
|
433
|
-
};
|
|
434
|
-
|
|
435
|
-
/**
|
|
436
|
-
* Returns the given text node's content.
|
|
437
|
-
*
|
|
438
|
-
* @function getTextNodeContent
|
|
439
|
-
* @memberof TreeAdapter
|
|
440
|
-
*
|
|
441
|
-
* @param {ASTNode<Text>} textNode - Text node.
|
|
442
|
-
*
|
|
443
|
-
* @returns {String} text
|
|
444
|
-
*
|
|
445
|
-
* @see {@link https://github.com/inikulin/parse5/blob/tree-adapter-docs-rev/lib/tree_adapters/default.js#L447|default implementation.}
|
|
446
|
-
*/
|
|
447
|
-
exports.getTextNodeContent = function (textNode) {
|
|
448
|
-
return textNode.value;
|
|
449
|
-
};
|
|
450
|
-
|
|
451
|
-
/**
|
|
452
|
-
* Returns the given comment node's content.
|
|
453
|
-
*
|
|
454
|
-
* @function getCommentNodeContent
|
|
455
|
-
* @memberof TreeAdapter
|
|
456
|
-
*
|
|
457
|
-
* @param {ASTNode<Comment>} commentNode - Comment node.
|
|
458
|
-
*
|
|
459
|
-
* @returns {String} commentText
|
|
460
|
-
*
|
|
461
|
-
* @see {@link https://github.com/inikulin/parse5/blob/tree-adapter-docs-rev/lib/tree_adapters/default.js#L463|default implementation.}
|
|
462
|
-
*/
|
|
463
|
-
exports.getCommentNodeContent = function (commentNode) {
|
|
464
|
-
return commentNode.data;
|
|
465
|
-
};
|
|
466
|
-
|
|
467
|
-
/**
|
|
468
|
-
* Returns the given document type node's name.
|
|
469
|
-
*
|
|
470
|
-
* @function getDocumentTypeNodeName
|
|
471
|
-
* @memberof TreeAdapter
|
|
472
|
-
*
|
|
473
|
-
* @param {ASTNode<DocumentType>} doctypeNode - Document type node.
|
|
474
|
-
*
|
|
475
|
-
* @returns {String} name
|
|
476
|
-
*
|
|
477
|
-
* @see {@link https://github.com/inikulin/parse5/blob/tree-adapter-docs-rev/lib/tree_adapters/default.js#L479|default implementation.}
|
|
478
|
-
*/
|
|
479
|
-
exports.getDocumentTypeNodeName = function (doctypeNode) {
|
|
480
|
-
return doctypeNode.name;
|
|
481
|
-
};
|
|
482
|
-
|
|
483
|
-
/**
|
|
484
|
-
* Returns the given document type node's public identifier.
|
|
485
|
-
*
|
|
486
|
-
* @function getDocumentTypeNodePublicId
|
|
487
|
-
* @memberof TreeAdapter
|
|
488
|
-
*
|
|
489
|
-
* @param {ASTNode<DocumentType>} doctypeNode - Document type node.
|
|
490
|
-
*
|
|
491
|
-
* @returns {String} publicId
|
|
492
|
-
*
|
|
493
|
-
* @see {@link https://github.com/inikulin/parse5/blob/tree-adapter-docs-rev/lib/tree_adapters/default.js#L495|default implementation.}
|
|
494
|
-
*/
|
|
495
|
-
exports.getDocumentTypeNodePublicId = function (doctypeNode) {
|
|
496
|
-
return doctypeNode.publicId;
|
|
497
|
-
};
|
|
498
|
-
|
|
499
|
-
/**
|
|
500
|
-
* Returns the given document type node's system identifier.
|
|
501
|
-
*
|
|
502
|
-
* @function getDocumentTypeNodeSystemId
|
|
503
|
-
* @memberof TreeAdapter
|
|
504
|
-
*
|
|
505
|
-
* @param {ASTNode<DocumentType>} doctypeNode - Document type node.
|
|
506
|
-
*
|
|
507
|
-
* @returns {String} systemId
|
|
508
|
-
*
|
|
509
|
-
* @see {@link https://github.com/inikulin/parse5/blob/tree-adapter-docs-rev/lib/tree_adapters/default.js#L511|default implementation.}
|
|
510
|
-
*/
|
|
511
|
-
exports.getDocumentTypeNodeSystemId = function (doctypeNode) {
|
|
512
|
-
return doctypeNode.systemId;
|
|
513
|
-
};
|
|
514
|
-
|
|
515
|
-
//Node types
|
|
516
|
-
/**
|
|
517
|
-
* Determines if the given node is a text node.
|
|
518
|
-
*
|
|
519
|
-
* @function isTextNode
|
|
520
|
-
* @memberof TreeAdapter
|
|
521
|
-
*
|
|
522
|
-
* @param {ASTNode} node - Node.
|
|
523
|
-
*
|
|
524
|
-
* @returns {Boolean}
|
|
525
|
-
*
|
|
526
|
-
* @see {@link https://github.com/inikulin/parse5/blob/tree-adapter-docs-rev/lib/tree_adapters/default.js#L526|default implementation.}
|
|
527
|
-
*/
|
|
528
|
-
exports.isTextNode = function (node) {
|
|
529
|
-
return node.nodeName === '#text';
|
|
530
|
-
};
|
|
531
|
-
|
|
532
|
-
/**
|
|
533
|
-
* Determines if the given node is a comment node.
|
|
534
|
-
*
|
|
535
|
-
* @function isCommentNode
|
|
536
|
-
* @memberof TreeAdapter
|
|
537
|
-
*
|
|
538
|
-
* @param {ASTNode} node - Node.
|
|
539
|
-
*
|
|
540
|
-
* @returns {Boolean}
|
|
541
|
-
*
|
|
542
|
-
* @see {@link https://github.com/inikulin/parse5/blob/tree-adapter-docs-rev/lib/tree_adapters/default.js#L544|default implementation.}
|
|
543
|
-
*/
|
|
544
|
-
exports.isCommentNode = function (node) {
|
|
545
|
-
return node.nodeName === '#comment';
|
|
546
|
-
};
|
|
547
|
-
|
|
548
|
-
/**
|
|
549
|
-
* Determines if the given node is a document type node.
|
|
550
|
-
*
|
|
551
|
-
* @function isDocumentTypeNode
|
|
552
|
-
* @memberof TreeAdapter
|
|
553
|
-
*
|
|
554
|
-
* @param {ASTNode} node - Node.
|
|
555
|
-
*
|
|
556
|
-
* @returns {Boolean}
|
|
557
|
-
*
|
|
558
|
-
* @see {@link https://github.com/inikulin/parse5/blob/tree-adapter-docs-rev/lib/tree_adapters/default.js#L560|default implementation.}
|
|
559
|
-
*/
|
|
560
|
-
exports.isDocumentTypeNode = function (node) {
|
|
561
|
-
return node.nodeName === '#documentType';
|
|
562
|
-
};
|
|
563
|
-
|
|
564
|
-
/**
|
|
565
|
-
* Determines if the given node is an element.
|
|
566
|
-
*
|
|
567
|
-
* @function isElementNode
|
|
568
|
-
* @memberof TreeAdapter
|
|
569
|
-
*
|
|
570
|
-
* @param {ASTNode} node - Node.
|
|
571
|
-
*
|
|
572
|
-
* @returns {Boolean}
|
|
573
|
-
*
|
|
574
|
-
* @see {@link https://github.com/inikulin/parse5/blob/tree-adapter-docs-rev/lib/tree_adapters/default.js#L576|default implementation.}
|
|
575
|
-
*/
|
|
576
|
-
exports.isElementNode = function (node) {
|
|
577
|
-
return !!node.tagName;
|
|
578
|
-
};
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var DOCUMENT_MODE = require('../common/html').DOCUMENT_MODE;
|
|
4
|
+
|
|
5
|
+
//Node construction
|
|
6
|
+
exports.createDocument = function () {
|
|
7
|
+
return {
|
|
8
|
+
nodeName: '#document',
|
|
9
|
+
mode: DOCUMENT_MODE.NO_QUIRKS,
|
|
10
|
+
childNodes: []
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
exports.createDocumentFragment = function () {
|
|
15
|
+
return {
|
|
16
|
+
nodeName: '#document-fragment',
|
|
17
|
+
childNodes: []
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
exports.createElement = function (tagName, namespaceURI, attrs) {
|
|
22
|
+
return {
|
|
23
|
+
nodeName: tagName,
|
|
24
|
+
tagName: tagName,
|
|
25
|
+
attrs: attrs,
|
|
26
|
+
namespaceURI: namespaceURI,
|
|
27
|
+
childNodes: [],
|
|
28
|
+
parentNode: null
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
exports.createCommentNode = function (data) {
|
|
33
|
+
return {
|
|
34
|
+
nodeName: '#comment',
|
|
35
|
+
data: data,
|
|
36
|
+
parentNode: null
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
var createTextNode = function (value) {
|
|
41
|
+
return {
|
|
42
|
+
nodeName: '#text',
|
|
43
|
+
value: value,
|
|
44
|
+
parentNode: null
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
//Tree mutation
|
|
50
|
+
var appendChild = exports.appendChild = function (parentNode, newNode) {
|
|
51
|
+
parentNode.childNodes.push(newNode);
|
|
52
|
+
newNode.parentNode = parentNode;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
var insertBefore = exports.insertBefore = function (parentNode, newNode, referenceNode) {
|
|
56
|
+
var insertionIdx = parentNode.childNodes.indexOf(referenceNode);
|
|
57
|
+
|
|
58
|
+
parentNode.childNodes.splice(insertionIdx, 0, newNode);
|
|
59
|
+
newNode.parentNode = parentNode;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
exports.setTemplateContent = function (templateElement, contentElement) {
|
|
63
|
+
templateElement.content = contentElement;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
exports.getTemplateContent = function (templateElement) {
|
|
67
|
+
return templateElement.content;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
exports.setDocumentType = function (document, name, publicId, systemId) {
|
|
71
|
+
var doctypeNode = null;
|
|
72
|
+
|
|
73
|
+
for (var i = 0; i < document.childNodes.length; i++) {
|
|
74
|
+
if (document.childNodes[i].nodeName === '#documentType') {
|
|
75
|
+
doctypeNode = document.childNodes[i];
|
|
76
|
+
break;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (doctypeNode) {
|
|
81
|
+
doctypeNode.name = name;
|
|
82
|
+
doctypeNode.publicId = publicId;
|
|
83
|
+
doctypeNode.systemId = systemId;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
else {
|
|
87
|
+
appendChild(document, {
|
|
88
|
+
nodeName: '#documentType',
|
|
89
|
+
name: name,
|
|
90
|
+
publicId: publicId,
|
|
91
|
+
systemId: systemId
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
exports.setDocumentMode = function (document, mode) {
|
|
97
|
+
document.mode = mode;
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
exports.getDocumentMode = function (document) {
|
|
101
|
+
return document.mode;
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
exports.detachNode = function (node) {
|
|
105
|
+
if (node.parentNode) {
|
|
106
|
+
var idx = node.parentNode.childNodes.indexOf(node);
|
|
107
|
+
|
|
108
|
+
node.parentNode.childNodes.splice(idx, 1);
|
|
109
|
+
node.parentNode = null;
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
exports.insertText = function (parentNode, text) {
|
|
114
|
+
if (parentNode.childNodes.length) {
|
|
115
|
+
var prevNode = parentNode.childNodes[parentNode.childNodes.length - 1];
|
|
116
|
+
|
|
117
|
+
if (prevNode.nodeName === '#text') {
|
|
118
|
+
prevNode.value += text;
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
appendChild(parentNode, createTextNode(text));
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
exports.insertTextBefore = function (parentNode, text, referenceNode) {
|
|
127
|
+
var prevNode = parentNode.childNodes[parentNode.childNodes.indexOf(referenceNode) - 1];
|
|
128
|
+
|
|
129
|
+
if (prevNode && prevNode.nodeName === '#text')
|
|
130
|
+
prevNode.value += text;
|
|
131
|
+
else
|
|
132
|
+
insertBefore(parentNode, createTextNode(text), referenceNode);
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
exports.adoptAttributes = function (recipient, attrs) {
|
|
136
|
+
var recipientAttrsMap = [];
|
|
137
|
+
|
|
138
|
+
for (var i = 0; i < recipient.attrs.length; i++)
|
|
139
|
+
recipientAttrsMap.push(recipient.attrs[i].name);
|
|
140
|
+
|
|
141
|
+
for (var j = 0; j < attrs.length; j++) {
|
|
142
|
+
if (recipientAttrsMap.indexOf(attrs[j].name) === -1)
|
|
143
|
+
recipient.attrs.push(attrs[j]);
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
//Tree traversing
|
|
149
|
+
exports.getFirstChild = function (node) {
|
|
150
|
+
return node.childNodes[0];
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
exports.getChildNodes = function (node) {
|
|
154
|
+
return node.childNodes;
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
exports.getParentNode = function (node) {
|
|
158
|
+
return node.parentNode;
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
exports.getAttrList = function (element) {
|
|
162
|
+
return element.attrs;
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
//Node data
|
|
166
|
+
exports.getTagName = function (element) {
|
|
167
|
+
return element.tagName;
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
exports.getNamespaceURI = function (element) {
|
|
171
|
+
return element.namespaceURI;
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
exports.getTextNodeContent = function (textNode) {
|
|
175
|
+
return textNode.value;
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
exports.getCommentNodeContent = function (commentNode) {
|
|
179
|
+
return commentNode.data;
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
exports.getDocumentTypeNodeName = function (doctypeNode) {
|
|
183
|
+
return doctypeNode.name;
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
exports.getDocumentTypeNodePublicId = function (doctypeNode) {
|
|
187
|
+
return doctypeNode.publicId;
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
exports.getDocumentTypeNodeSystemId = function (doctypeNode) {
|
|
191
|
+
return doctypeNode.systemId;
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
//Node types
|
|
195
|
+
exports.isTextNode = function (node) {
|
|
196
|
+
return node.nodeName === '#text';
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
exports.isCommentNode = function (node) {
|
|
200
|
+
return node.nodeName === '#comment';
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
exports.isDocumentTypeNode = function (node) {
|
|
204
|
+
return node.nodeName === '#documentType';
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
exports.isElementNode = function (node) {
|
|
208
|
+
return !!node.tagName;
|
|
209
|
+
};
|