parse5 6.0.1 → 7.3.0
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/README.md +2 -2
- package/dist/cjs/common/doctype.d.ts +4 -0
- package/dist/cjs/common/doctype.js +118 -0
- package/dist/cjs/common/error-codes.d.ts +67 -0
- package/dist/cjs/common/error-codes.js +66 -0
- package/dist/cjs/common/foreign-content.d.ts +9 -0
- package/dist/cjs/common/foreign-content.js +237 -0
- package/dist/cjs/common/html.d.ts +289 -0
- package/dist/cjs/common/html.js +528 -0
- package/dist/cjs/common/token.d.ts +84 -0
- package/dist/cjs/common/token.js +24 -0
- package/dist/cjs/common/unicode.d.ts +42 -0
- package/dist/cjs/common/unicode.js +70 -0
- package/dist/cjs/index.d.ts +71 -0
- package/dist/cjs/index.js +54 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/parser/formatting-element-list.d.ts +36 -0
- package/dist/cjs/parser/formatting-element-list.js +114 -0
- package/dist/cjs/parser/index.d.ts +221 -0
- package/dist/cjs/parser/index.js +3240 -0
- package/dist/cjs/parser/open-element-stack.d.ts +53 -0
- package/dist/cjs/parser/open-element-stack.js +328 -0
- package/dist/cjs/serializer/index.d.ts +60 -0
- package/dist/cjs/serializer/index.js +172 -0
- package/dist/cjs/tokenizer/index.d.ts +247 -0
- package/dist/cjs/tokenizer/index.js +2714 -0
- package/dist/cjs/tokenizer/preprocessor.d.ts +36 -0
- package/dist/cjs/tokenizer/preprocessor.js +200 -0
- package/dist/cjs/tree-adapters/default.d.ts +84 -0
- package/dist/cjs/tree-adapters/default.js +176 -0
- package/dist/cjs/tree-adapters/interface.d.ts +255 -0
- package/dist/cjs/tree-adapters/interface.js +2 -0
- package/dist/common/doctype.d.ts +4 -0
- package/{lib → dist}/common/doctype.js +24 -72
- package/dist/common/error-codes.d.ts +67 -0
- package/dist/common/error-codes.js +63 -0
- package/dist/common/foreign-content.d.ts +9 -0
- package/dist/common/foreign-content.js +228 -0
- package/dist/common/html.d.ts +289 -0
- package/dist/common/html.js +523 -0
- package/dist/common/token.d.ts +84 -0
- package/dist/common/token.js +20 -0
- package/dist/common/unicode.d.ts +42 -0
- package/dist/common/unicode.js +62 -0
- package/dist/index.d.ts +71 -0
- package/dist/index.js +42 -0
- package/dist/parser/formatting-element-list.d.ts +36 -0
- package/dist/parser/formatting-element-list.js +110 -0
- package/dist/parser/index.d.ts +221 -0
- package/dist/parser/index.js +3245 -0
- package/dist/parser/open-element-stack.d.ts +53 -0
- package/dist/parser/open-element-stack.js +324 -0
- package/dist/serializer/index.d.ts +60 -0
- package/dist/serializer/index.js +168 -0
- package/dist/tokenizer/index.d.ts +247 -0
- package/dist/tokenizer/index.js +2710 -0
- package/dist/tokenizer/preprocessor.d.ts +36 -0
- package/dist/tokenizer/preprocessor.js +196 -0
- package/dist/tree-adapters/default.d.ts +84 -0
- package/dist/tree-adapters/default.js +173 -0
- package/dist/tree-adapters/interface.d.ts +255 -0
- package/dist/tree-adapters/interface.js +1 -0
- package/package.json +21 -6
- package/lib/common/error-codes.js +0 -65
- package/lib/common/foreign-content.js +0 -265
- package/lib/common/html.js +0 -272
- package/lib/common/unicode.js +0 -109
- package/lib/extensions/error-reporting/mixin-base.js +0 -43
- package/lib/extensions/error-reporting/parser-mixin.js +0 -52
- package/lib/extensions/error-reporting/preprocessor-mixin.js +0 -24
- package/lib/extensions/error-reporting/tokenizer-mixin.js +0 -17
- package/lib/extensions/location-info/open-element-stack-mixin.js +0 -35
- package/lib/extensions/location-info/parser-mixin.js +0 -223
- package/lib/extensions/location-info/tokenizer-mixin.js +0 -146
- package/lib/extensions/position-tracking/preprocessor-mixin.js +0 -64
- package/lib/index.js +0 -29
- package/lib/parser/formatting-element-list.js +0 -181
- package/lib/parser/index.js +0 -2956
- package/lib/parser/open-element-stack.js +0 -482
- package/lib/serializer/index.js +0 -176
- package/lib/tokenizer/index.js +0 -2196
- package/lib/tokenizer/named-entity-data.js +0 -5
- package/lib/tokenizer/preprocessor.js +0 -159
- package/lib/tree-adapters/default.js +0 -221
- package/lib/utils/merge-options.js +0 -13
- package/lib/utils/mixin.js +0 -39
|
@@ -1,482 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const HTML = require('../common/html');
|
|
4
|
-
|
|
5
|
-
//Aliases
|
|
6
|
-
const $ = HTML.TAG_NAMES;
|
|
7
|
-
const NS = HTML.NAMESPACES;
|
|
8
|
-
|
|
9
|
-
//Element utils
|
|
10
|
-
|
|
11
|
-
//OPTIMIZATION: Integer comparisons are low-cost, so we can use very fast tag name length filters here.
|
|
12
|
-
//It's faster than using dictionary.
|
|
13
|
-
function isImpliedEndTagRequired(tn) {
|
|
14
|
-
switch (tn.length) {
|
|
15
|
-
case 1:
|
|
16
|
-
return tn === $.P;
|
|
17
|
-
|
|
18
|
-
case 2:
|
|
19
|
-
return tn === $.RB || tn === $.RP || tn === $.RT || tn === $.DD || tn === $.DT || tn === $.LI;
|
|
20
|
-
|
|
21
|
-
case 3:
|
|
22
|
-
return tn === $.RTC;
|
|
23
|
-
|
|
24
|
-
case 6:
|
|
25
|
-
return tn === $.OPTION;
|
|
26
|
-
|
|
27
|
-
case 8:
|
|
28
|
-
return tn === $.OPTGROUP;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
return false;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
function isImpliedEndTagRequiredThoroughly(tn) {
|
|
35
|
-
switch (tn.length) {
|
|
36
|
-
case 1:
|
|
37
|
-
return tn === $.P;
|
|
38
|
-
|
|
39
|
-
case 2:
|
|
40
|
-
return (
|
|
41
|
-
tn === $.RB ||
|
|
42
|
-
tn === $.RP ||
|
|
43
|
-
tn === $.RT ||
|
|
44
|
-
tn === $.DD ||
|
|
45
|
-
tn === $.DT ||
|
|
46
|
-
tn === $.LI ||
|
|
47
|
-
tn === $.TD ||
|
|
48
|
-
tn === $.TH ||
|
|
49
|
-
tn === $.TR
|
|
50
|
-
);
|
|
51
|
-
|
|
52
|
-
case 3:
|
|
53
|
-
return tn === $.RTC;
|
|
54
|
-
|
|
55
|
-
case 5:
|
|
56
|
-
return tn === $.TBODY || tn === $.TFOOT || tn === $.THEAD;
|
|
57
|
-
|
|
58
|
-
case 6:
|
|
59
|
-
return tn === $.OPTION;
|
|
60
|
-
|
|
61
|
-
case 7:
|
|
62
|
-
return tn === $.CAPTION;
|
|
63
|
-
|
|
64
|
-
case 8:
|
|
65
|
-
return tn === $.OPTGROUP || tn === $.COLGROUP;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
return false;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
function isScopingElement(tn, ns) {
|
|
72
|
-
switch (tn.length) {
|
|
73
|
-
case 2:
|
|
74
|
-
if (tn === $.TD || tn === $.TH) {
|
|
75
|
-
return ns === NS.HTML;
|
|
76
|
-
} else if (tn === $.MI || tn === $.MO || tn === $.MN || tn === $.MS) {
|
|
77
|
-
return ns === NS.MATHML;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
break;
|
|
81
|
-
|
|
82
|
-
case 4:
|
|
83
|
-
if (tn === $.HTML) {
|
|
84
|
-
return ns === NS.HTML;
|
|
85
|
-
} else if (tn === $.DESC) {
|
|
86
|
-
return ns === NS.SVG;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
break;
|
|
90
|
-
|
|
91
|
-
case 5:
|
|
92
|
-
if (tn === $.TABLE) {
|
|
93
|
-
return ns === NS.HTML;
|
|
94
|
-
} else if (tn === $.MTEXT) {
|
|
95
|
-
return ns === NS.MATHML;
|
|
96
|
-
} else if (tn === $.TITLE) {
|
|
97
|
-
return ns === NS.SVG;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
break;
|
|
101
|
-
|
|
102
|
-
case 6:
|
|
103
|
-
return (tn === $.APPLET || tn === $.OBJECT) && ns === NS.HTML;
|
|
104
|
-
|
|
105
|
-
case 7:
|
|
106
|
-
return (tn === $.CAPTION || tn === $.MARQUEE) && ns === NS.HTML;
|
|
107
|
-
|
|
108
|
-
case 8:
|
|
109
|
-
return tn === $.TEMPLATE && ns === NS.HTML;
|
|
110
|
-
|
|
111
|
-
case 13:
|
|
112
|
-
return tn === $.FOREIGN_OBJECT && ns === NS.SVG;
|
|
113
|
-
|
|
114
|
-
case 14:
|
|
115
|
-
return tn === $.ANNOTATION_XML && ns === NS.MATHML;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
return false;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
//Stack of open elements
|
|
122
|
-
class OpenElementStack {
|
|
123
|
-
constructor(document, treeAdapter) {
|
|
124
|
-
this.stackTop = -1;
|
|
125
|
-
this.items = [];
|
|
126
|
-
this.current = document;
|
|
127
|
-
this.currentTagName = null;
|
|
128
|
-
this.currentTmplContent = null;
|
|
129
|
-
this.tmplCount = 0;
|
|
130
|
-
this.treeAdapter = treeAdapter;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
//Index of element
|
|
134
|
-
_indexOf(element) {
|
|
135
|
-
let idx = -1;
|
|
136
|
-
|
|
137
|
-
for (let i = this.stackTop; i >= 0; i--) {
|
|
138
|
-
if (this.items[i] === element) {
|
|
139
|
-
idx = i;
|
|
140
|
-
break;
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
return idx;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
//Update current element
|
|
147
|
-
_isInTemplate() {
|
|
148
|
-
return this.currentTagName === $.TEMPLATE && this.treeAdapter.getNamespaceURI(this.current) === NS.HTML;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
_updateCurrentElement() {
|
|
152
|
-
this.current = this.items[this.stackTop];
|
|
153
|
-
this.currentTagName = this.current && this.treeAdapter.getTagName(this.current);
|
|
154
|
-
|
|
155
|
-
this.currentTmplContent = this._isInTemplate() ? this.treeAdapter.getTemplateContent(this.current) : null;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
//Mutations
|
|
159
|
-
push(element) {
|
|
160
|
-
this.items[++this.stackTop] = element;
|
|
161
|
-
this._updateCurrentElement();
|
|
162
|
-
|
|
163
|
-
if (this._isInTemplate()) {
|
|
164
|
-
this.tmplCount++;
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
pop() {
|
|
169
|
-
this.stackTop--;
|
|
170
|
-
|
|
171
|
-
if (this.tmplCount > 0 && this._isInTemplate()) {
|
|
172
|
-
this.tmplCount--;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
this._updateCurrentElement();
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
replace(oldElement, newElement) {
|
|
179
|
-
const idx = this._indexOf(oldElement);
|
|
180
|
-
|
|
181
|
-
this.items[idx] = newElement;
|
|
182
|
-
|
|
183
|
-
if (idx === this.stackTop) {
|
|
184
|
-
this._updateCurrentElement();
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
insertAfter(referenceElement, newElement) {
|
|
189
|
-
const insertionIdx = this._indexOf(referenceElement) + 1;
|
|
190
|
-
|
|
191
|
-
this.items.splice(insertionIdx, 0, newElement);
|
|
192
|
-
|
|
193
|
-
if (insertionIdx === ++this.stackTop) {
|
|
194
|
-
this._updateCurrentElement();
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
popUntilTagNamePopped(tagName) {
|
|
199
|
-
while (this.stackTop > -1) {
|
|
200
|
-
const tn = this.currentTagName;
|
|
201
|
-
const ns = this.treeAdapter.getNamespaceURI(this.current);
|
|
202
|
-
|
|
203
|
-
this.pop();
|
|
204
|
-
|
|
205
|
-
if (tn === tagName && ns === NS.HTML) {
|
|
206
|
-
break;
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
popUntilElementPopped(element) {
|
|
212
|
-
while (this.stackTop > -1) {
|
|
213
|
-
const poppedElement = this.current;
|
|
214
|
-
|
|
215
|
-
this.pop();
|
|
216
|
-
|
|
217
|
-
if (poppedElement === element) {
|
|
218
|
-
break;
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
popUntilNumberedHeaderPopped() {
|
|
224
|
-
while (this.stackTop > -1) {
|
|
225
|
-
const tn = this.currentTagName;
|
|
226
|
-
const ns = this.treeAdapter.getNamespaceURI(this.current);
|
|
227
|
-
|
|
228
|
-
this.pop();
|
|
229
|
-
|
|
230
|
-
if (
|
|
231
|
-
tn === $.H1 ||
|
|
232
|
-
tn === $.H2 ||
|
|
233
|
-
tn === $.H3 ||
|
|
234
|
-
tn === $.H4 ||
|
|
235
|
-
tn === $.H5 ||
|
|
236
|
-
(tn === $.H6 && ns === NS.HTML)
|
|
237
|
-
) {
|
|
238
|
-
break;
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
popUntilTableCellPopped() {
|
|
244
|
-
while (this.stackTop > -1) {
|
|
245
|
-
const tn = this.currentTagName;
|
|
246
|
-
const ns = this.treeAdapter.getNamespaceURI(this.current);
|
|
247
|
-
|
|
248
|
-
this.pop();
|
|
249
|
-
|
|
250
|
-
if (tn === $.TD || (tn === $.TH && ns === NS.HTML)) {
|
|
251
|
-
break;
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
popAllUpToHtmlElement() {
|
|
257
|
-
//NOTE: here we assume that root <html> element is always first in the open element stack, so
|
|
258
|
-
//we perform this fast stack clean up.
|
|
259
|
-
this.stackTop = 0;
|
|
260
|
-
this._updateCurrentElement();
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
clearBackToTableContext() {
|
|
264
|
-
while (
|
|
265
|
-
(this.currentTagName !== $.TABLE && this.currentTagName !== $.TEMPLATE && this.currentTagName !== $.HTML) ||
|
|
266
|
-
this.treeAdapter.getNamespaceURI(this.current) !== NS.HTML
|
|
267
|
-
) {
|
|
268
|
-
this.pop();
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
clearBackToTableBodyContext() {
|
|
273
|
-
while (
|
|
274
|
-
(this.currentTagName !== $.TBODY &&
|
|
275
|
-
this.currentTagName !== $.TFOOT &&
|
|
276
|
-
this.currentTagName !== $.THEAD &&
|
|
277
|
-
this.currentTagName !== $.TEMPLATE &&
|
|
278
|
-
this.currentTagName !== $.HTML) ||
|
|
279
|
-
this.treeAdapter.getNamespaceURI(this.current) !== NS.HTML
|
|
280
|
-
) {
|
|
281
|
-
this.pop();
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
clearBackToTableRowContext() {
|
|
286
|
-
while (
|
|
287
|
-
(this.currentTagName !== $.TR && this.currentTagName !== $.TEMPLATE && this.currentTagName !== $.HTML) ||
|
|
288
|
-
this.treeAdapter.getNamespaceURI(this.current) !== NS.HTML
|
|
289
|
-
) {
|
|
290
|
-
this.pop();
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
remove(element) {
|
|
295
|
-
for (let i = this.stackTop; i >= 0; i--) {
|
|
296
|
-
if (this.items[i] === element) {
|
|
297
|
-
this.items.splice(i, 1);
|
|
298
|
-
this.stackTop--;
|
|
299
|
-
this._updateCurrentElement();
|
|
300
|
-
break;
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
//Search
|
|
306
|
-
tryPeekProperlyNestedBodyElement() {
|
|
307
|
-
//Properly nested <body> element (should be second element in stack).
|
|
308
|
-
const element = this.items[1];
|
|
309
|
-
|
|
310
|
-
return element && this.treeAdapter.getTagName(element) === $.BODY ? element : null;
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
contains(element) {
|
|
314
|
-
return this._indexOf(element) > -1;
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
getCommonAncestor(element) {
|
|
318
|
-
let elementIdx = this._indexOf(element);
|
|
319
|
-
|
|
320
|
-
return --elementIdx >= 0 ? this.items[elementIdx] : null;
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
isRootHtmlElementCurrent() {
|
|
324
|
-
return this.stackTop === 0 && this.currentTagName === $.HTML;
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
//Element in scope
|
|
328
|
-
hasInScope(tagName) {
|
|
329
|
-
for (let i = this.stackTop; i >= 0; i--) {
|
|
330
|
-
const tn = this.treeAdapter.getTagName(this.items[i]);
|
|
331
|
-
const ns = this.treeAdapter.getNamespaceURI(this.items[i]);
|
|
332
|
-
|
|
333
|
-
if (tn === tagName && ns === NS.HTML) {
|
|
334
|
-
return true;
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
if (isScopingElement(tn, ns)) {
|
|
338
|
-
return false;
|
|
339
|
-
}
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
return true;
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
hasNumberedHeaderInScope() {
|
|
346
|
-
for (let i = this.stackTop; i >= 0; i--) {
|
|
347
|
-
const tn = this.treeAdapter.getTagName(this.items[i]);
|
|
348
|
-
const ns = this.treeAdapter.getNamespaceURI(this.items[i]);
|
|
349
|
-
|
|
350
|
-
if (
|
|
351
|
-
(tn === $.H1 || tn === $.H2 || tn === $.H3 || tn === $.H4 || tn === $.H5 || tn === $.H6) &&
|
|
352
|
-
ns === NS.HTML
|
|
353
|
-
) {
|
|
354
|
-
return true;
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
if (isScopingElement(tn, ns)) {
|
|
358
|
-
return false;
|
|
359
|
-
}
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
return true;
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
hasInListItemScope(tagName) {
|
|
366
|
-
for (let i = this.stackTop; i >= 0; i--) {
|
|
367
|
-
const tn = this.treeAdapter.getTagName(this.items[i]);
|
|
368
|
-
const ns = this.treeAdapter.getNamespaceURI(this.items[i]);
|
|
369
|
-
|
|
370
|
-
if (tn === tagName && ns === NS.HTML) {
|
|
371
|
-
return true;
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
if (((tn === $.UL || tn === $.OL) && ns === NS.HTML) || isScopingElement(tn, ns)) {
|
|
375
|
-
return false;
|
|
376
|
-
}
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
return true;
|
|
380
|
-
}
|
|
381
|
-
|
|
382
|
-
hasInButtonScope(tagName) {
|
|
383
|
-
for (let i = this.stackTop; i >= 0; i--) {
|
|
384
|
-
const tn = this.treeAdapter.getTagName(this.items[i]);
|
|
385
|
-
const ns = this.treeAdapter.getNamespaceURI(this.items[i]);
|
|
386
|
-
|
|
387
|
-
if (tn === tagName && ns === NS.HTML) {
|
|
388
|
-
return true;
|
|
389
|
-
}
|
|
390
|
-
|
|
391
|
-
if ((tn === $.BUTTON && ns === NS.HTML) || isScopingElement(tn, ns)) {
|
|
392
|
-
return false;
|
|
393
|
-
}
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
return true;
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
hasInTableScope(tagName) {
|
|
400
|
-
for (let i = this.stackTop; i >= 0; i--) {
|
|
401
|
-
const tn = this.treeAdapter.getTagName(this.items[i]);
|
|
402
|
-
const ns = this.treeAdapter.getNamespaceURI(this.items[i]);
|
|
403
|
-
|
|
404
|
-
if (ns !== NS.HTML) {
|
|
405
|
-
continue;
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
if (tn === tagName) {
|
|
409
|
-
return true;
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
if (tn === $.TABLE || tn === $.TEMPLATE || tn === $.HTML) {
|
|
413
|
-
return false;
|
|
414
|
-
}
|
|
415
|
-
}
|
|
416
|
-
|
|
417
|
-
return true;
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
hasTableBodyContextInTableScope() {
|
|
421
|
-
for (let i = this.stackTop; i >= 0; i--) {
|
|
422
|
-
const tn = this.treeAdapter.getTagName(this.items[i]);
|
|
423
|
-
const ns = this.treeAdapter.getNamespaceURI(this.items[i]);
|
|
424
|
-
|
|
425
|
-
if (ns !== NS.HTML) {
|
|
426
|
-
continue;
|
|
427
|
-
}
|
|
428
|
-
|
|
429
|
-
if (tn === $.TBODY || tn === $.THEAD || tn === $.TFOOT) {
|
|
430
|
-
return true;
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
if (tn === $.TABLE || tn === $.HTML) {
|
|
434
|
-
return false;
|
|
435
|
-
}
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
return true;
|
|
439
|
-
}
|
|
440
|
-
|
|
441
|
-
hasInSelectScope(tagName) {
|
|
442
|
-
for (let i = this.stackTop; i >= 0; i--) {
|
|
443
|
-
const tn = this.treeAdapter.getTagName(this.items[i]);
|
|
444
|
-
const ns = this.treeAdapter.getNamespaceURI(this.items[i]);
|
|
445
|
-
|
|
446
|
-
if (ns !== NS.HTML) {
|
|
447
|
-
continue;
|
|
448
|
-
}
|
|
449
|
-
|
|
450
|
-
if (tn === tagName) {
|
|
451
|
-
return true;
|
|
452
|
-
}
|
|
453
|
-
|
|
454
|
-
if (tn !== $.OPTION && tn !== $.OPTGROUP) {
|
|
455
|
-
return false;
|
|
456
|
-
}
|
|
457
|
-
}
|
|
458
|
-
|
|
459
|
-
return true;
|
|
460
|
-
}
|
|
461
|
-
|
|
462
|
-
//Implied end tags
|
|
463
|
-
generateImpliedEndTags() {
|
|
464
|
-
while (isImpliedEndTagRequired(this.currentTagName)) {
|
|
465
|
-
this.pop();
|
|
466
|
-
}
|
|
467
|
-
}
|
|
468
|
-
|
|
469
|
-
generateImpliedEndTagsThoroughly() {
|
|
470
|
-
while (isImpliedEndTagRequiredThoroughly(this.currentTagName)) {
|
|
471
|
-
this.pop();
|
|
472
|
-
}
|
|
473
|
-
}
|
|
474
|
-
|
|
475
|
-
generateImpliedEndTagsWithExclusion(exclusionTagName) {
|
|
476
|
-
while (isImpliedEndTagRequired(this.currentTagName) && this.currentTagName !== exclusionTagName) {
|
|
477
|
-
this.pop();
|
|
478
|
-
}
|
|
479
|
-
}
|
|
480
|
-
}
|
|
481
|
-
|
|
482
|
-
module.exports = OpenElementStack;
|
package/lib/serializer/index.js
DELETED
|
@@ -1,176 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const defaultTreeAdapter = require('../tree-adapters/default');
|
|
4
|
-
const mergeOptions = require('../utils/merge-options');
|
|
5
|
-
const doctype = require('../common/doctype');
|
|
6
|
-
const HTML = require('../common/html');
|
|
7
|
-
|
|
8
|
-
//Aliases
|
|
9
|
-
const $ = HTML.TAG_NAMES;
|
|
10
|
-
const NS = HTML.NAMESPACES;
|
|
11
|
-
|
|
12
|
-
//Default serializer options
|
|
13
|
-
const DEFAULT_OPTIONS = {
|
|
14
|
-
treeAdapter: defaultTreeAdapter
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
//Escaping regexes
|
|
18
|
-
const AMP_REGEX = /&/g;
|
|
19
|
-
const NBSP_REGEX = /\u00a0/g;
|
|
20
|
-
const DOUBLE_QUOTE_REGEX = /"/g;
|
|
21
|
-
const LT_REGEX = /</g;
|
|
22
|
-
const GT_REGEX = />/g;
|
|
23
|
-
|
|
24
|
-
//Serializer
|
|
25
|
-
class Serializer {
|
|
26
|
-
constructor(node, options) {
|
|
27
|
-
this.options = mergeOptions(DEFAULT_OPTIONS, options);
|
|
28
|
-
this.treeAdapter = this.options.treeAdapter;
|
|
29
|
-
|
|
30
|
-
this.html = '';
|
|
31
|
-
this.startNode = node;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
//API
|
|
35
|
-
serialize() {
|
|
36
|
-
this._serializeChildNodes(this.startNode);
|
|
37
|
-
|
|
38
|
-
return this.html;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
//Internals
|
|
42
|
-
_serializeChildNodes(parentNode) {
|
|
43
|
-
const childNodes = this.treeAdapter.getChildNodes(parentNode);
|
|
44
|
-
|
|
45
|
-
if (childNodes) {
|
|
46
|
-
for (let i = 0, cnLength = childNodes.length; i < cnLength; i++) {
|
|
47
|
-
const currentNode = childNodes[i];
|
|
48
|
-
|
|
49
|
-
if (this.treeAdapter.isElementNode(currentNode)) {
|
|
50
|
-
this._serializeElement(currentNode);
|
|
51
|
-
} else if (this.treeAdapter.isTextNode(currentNode)) {
|
|
52
|
-
this._serializeTextNode(currentNode);
|
|
53
|
-
} else if (this.treeAdapter.isCommentNode(currentNode)) {
|
|
54
|
-
this._serializeCommentNode(currentNode);
|
|
55
|
-
} else if (this.treeAdapter.isDocumentTypeNode(currentNode)) {
|
|
56
|
-
this._serializeDocumentTypeNode(currentNode);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
_serializeElement(node) {
|
|
63
|
-
const tn = this.treeAdapter.getTagName(node);
|
|
64
|
-
const ns = this.treeAdapter.getNamespaceURI(node);
|
|
65
|
-
|
|
66
|
-
this.html += '<' + tn;
|
|
67
|
-
this._serializeAttributes(node);
|
|
68
|
-
this.html += '>';
|
|
69
|
-
|
|
70
|
-
if (
|
|
71
|
-
tn !== $.AREA &&
|
|
72
|
-
tn !== $.BASE &&
|
|
73
|
-
tn !== $.BASEFONT &&
|
|
74
|
-
tn !== $.BGSOUND &&
|
|
75
|
-
tn !== $.BR &&
|
|
76
|
-
tn !== $.COL &&
|
|
77
|
-
tn !== $.EMBED &&
|
|
78
|
-
tn !== $.FRAME &&
|
|
79
|
-
tn !== $.HR &&
|
|
80
|
-
tn !== $.IMG &&
|
|
81
|
-
tn !== $.INPUT &&
|
|
82
|
-
tn !== $.KEYGEN &&
|
|
83
|
-
tn !== $.LINK &&
|
|
84
|
-
tn !== $.META &&
|
|
85
|
-
tn !== $.PARAM &&
|
|
86
|
-
tn !== $.SOURCE &&
|
|
87
|
-
tn !== $.TRACK &&
|
|
88
|
-
tn !== $.WBR
|
|
89
|
-
) {
|
|
90
|
-
const childNodesHolder =
|
|
91
|
-
tn === $.TEMPLATE && ns === NS.HTML ? this.treeAdapter.getTemplateContent(node) : node;
|
|
92
|
-
|
|
93
|
-
this._serializeChildNodes(childNodesHolder);
|
|
94
|
-
this.html += '</' + tn + '>';
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
_serializeAttributes(node) {
|
|
99
|
-
const attrs = this.treeAdapter.getAttrList(node);
|
|
100
|
-
|
|
101
|
-
for (let i = 0, attrsLength = attrs.length; i < attrsLength; i++) {
|
|
102
|
-
const attr = attrs[i];
|
|
103
|
-
const value = Serializer.escapeString(attr.value, true);
|
|
104
|
-
|
|
105
|
-
this.html += ' ';
|
|
106
|
-
|
|
107
|
-
if (!attr.namespace) {
|
|
108
|
-
this.html += attr.name;
|
|
109
|
-
} else if (attr.namespace === NS.XML) {
|
|
110
|
-
this.html += 'xml:' + attr.name;
|
|
111
|
-
} else if (attr.namespace === NS.XMLNS) {
|
|
112
|
-
if (attr.name !== 'xmlns') {
|
|
113
|
-
this.html += 'xmlns:';
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
this.html += attr.name;
|
|
117
|
-
} else if (attr.namespace === NS.XLINK) {
|
|
118
|
-
this.html += 'xlink:' + attr.name;
|
|
119
|
-
} else {
|
|
120
|
-
this.html += attr.prefix + ':' + attr.name;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
this.html += '="' + value + '"';
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
_serializeTextNode(node) {
|
|
128
|
-
const content = this.treeAdapter.getTextNodeContent(node);
|
|
129
|
-
const parent = this.treeAdapter.getParentNode(node);
|
|
130
|
-
let parentTn = void 0;
|
|
131
|
-
|
|
132
|
-
if (parent && this.treeAdapter.isElementNode(parent)) {
|
|
133
|
-
parentTn = this.treeAdapter.getTagName(parent);
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
if (
|
|
137
|
-
parentTn === $.STYLE ||
|
|
138
|
-
parentTn === $.SCRIPT ||
|
|
139
|
-
parentTn === $.XMP ||
|
|
140
|
-
parentTn === $.IFRAME ||
|
|
141
|
-
parentTn === $.NOEMBED ||
|
|
142
|
-
parentTn === $.NOFRAMES ||
|
|
143
|
-
parentTn === $.PLAINTEXT ||
|
|
144
|
-
parentTn === $.NOSCRIPT
|
|
145
|
-
) {
|
|
146
|
-
this.html += content;
|
|
147
|
-
} else {
|
|
148
|
-
this.html += Serializer.escapeString(content, false);
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
_serializeCommentNode(node) {
|
|
153
|
-
this.html += '<!--' + this.treeAdapter.getCommentNodeContent(node) + '-->';
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
_serializeDocumentTypeNode(node) {
|
|
157
|
-
const name = this.treeAdapter.getDocumentTypeNodeName(node);
|
|
158
|
-
|
|
159
|
-
this.html += '<' + doctype.serializeContent(name, null, null) + '>';
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
// NOTE: used in tests and by rewriting stream
|
|
164
|
-
Serializer.escapeString = function(str, attrMode) {
|
|
165
|
-
str = str.replace(AMP_REGEX, '&').replace(NBSP_REGEX, ' ');
|
|
166
|
-
|
|
167
|
-
if (attrMode) {
|
|
168
|
-
str = str.replace(DOUBLE_QUOTE_REGEX, '"');
|
|
169
|
-
} else {
|
|
170
|
-
str = str.replace(LT_REGEX, '<').replace(GT_REGEX, '>');
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
return str;
|
|
174
|
-
};
|
|
175
|
-
|
|
176
|
-
module.exports = Serializer;
|