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,167 +1,167 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
//Const
|
|
4
|
-
var NOAH_ARK_CAPACITY = 3;
|
|
5
|
-
|
|
6
|
-
//List of formatting elements
|
|
7
|
-
var FormattingElementList = module.exports = function (treeAdapter) {
|
|
8
|
-
this.length = 0;
|
|
9
|
-
this.entries = [];
|
|
10
|
-
this.treeAdapter = treeAdapter;
|
|
11
|
-
this.bookmark = null;
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
//Entry types
|
|
15
|
-
FormattingElementList.MARKER_ENTRY = 'MARKER_ENTRY';
|
|
16
|
-
FormattingElementList.ELEMENT_ENTRY = 'ELEMENT_ENTRY';
|
|
17
|
-
|
|
18
|
-
//Noah Ark's condition
|
|
19
|
-
//OPTIMIZATION: at first we try to find possible candidates for exclusion using
|
|
20
|
-
//lightweight heuristics without thorough attributes check.
|
|
21
|
-
FormattingElementList.prototype._getNoahArkConditionCandidates = function (newElement) {
|
|
22
|
-
var candidates = [];
|
|
23
|
-
|
|
24
|
-
if (this.length >= NOAH_ARK_CAPACITY) {
|
|
25
|
-
var neAttrsLength = this.treeAdapter.getAttrList(newElement).length,
|
|
26
|
-
neTagName = this.treeAdapter.getTagName(newElement),
|
|
27
|
-
neNamespaceURI = this.treeAdapter.getNamespaceURI(newElement);
|
|
28
|
-
|
|
29
|
-
for (var i = this.length - 1; i >= 0; i--) {
|
|
30
|
-
var entry = this.entries[i];
|
|
31
|
-
|
|
32
|
-
if (entry.type === FormattingElementList.MARKER_ENTRY)
|
|
33
|
-
break;
|
|
34
|
-
|
|
35
|
-
var element = entry.element,
|
|
36
|
-
elementAttrs = this.treeAdapter.getAttrList(element),
|
|
37
|
-
isCandidate = this.treeAdapter.getTagName(element) === neTagName &&
|
|
38
|
-
this.treeAdapter.getNamespaceURI(element) === neNamespaceURI &&
|
|
39
|
-
elementAttrs.length === neAttrsLength;
|
|
40
|
-
|
|
41
|
-
if (isCandidate)
|
|
42
|
-
candidates.push({idx: i, attrs: elementAttrs});
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
return candidates.length < NOAH_ARK_CAPACITY ? [] : candidates;
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
FormattingElementList.prototype._ensureNoahArkCondition = function (newElement) {
|
|
50
|
-
var candidates = this._getNoahArkConditionCandidates(newElement),
|
|
51
|
-
cLength = candidates.length;
|
|
52
|
-
|
|
53
|
-
if (cLength) {
|
|
54
|
-
var neAttrs = this.treeAdapter.getAttrList(newElement),
|
|
55
|
-
neAttrsLength = neAttrs.length,
|
|
56
|
-
neAttrsMap =
|
|
57
|
-
|
|
58
|
-
//NOTE: build attrs map for the new element so we can perform fast lookups
|
|
59
|
-
for (var i = 0; i < neAttrsLength; i++) {
|
|
60
|
-
var neAttr = neAttrs[i];
|
|
61
|
-
|
|
62
|
-
neAttrsMap[neAttr.name] = neAttr.value;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
for (i = 0; i < neAttrsLength; i++) {
|
|
66
|
-
for (var j = 0; j < cLength; j++) {
|
|
67
|
-
var cAttr = candidates[j].attrs[i];
|
|
68
|
-
|
|
69
|
-
if (neAttrsMap[cAttr.name] !== cAttr.value) {
|
|
70
|
-
candidates.splice(j, 1);
|
|
71
|
-
cLength--;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
if (candidates.length < NOAH_ARK_CAPACITY)
|
|
75
|
-
return;
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
//NOTE: remove bottommost candidates until Noah's Ark condition will not be met
|
|
80
|
-
for (i = cLength - 1; i >= NOAH_ARK_CAPACITY - 1; i--) {
|
|
81
|
-
this.entries.splice(candidates[i].idx, 1);
|
|
82
|
-
this.length--;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
//Mutations
|
|
88
|
-
FormattingElementList.prototype.insertMarker = function () {
|
|
89
|
-
this.entries.push({type: FormattingElementList.MARKER_ENTRY});
|
|
90
|
-
this.length++;
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
FormattingElementList.prototype.pushElement = function (element, token) {
|
|
94
|
-
this._ensureNoahArkCondition(element);
|
|
95
|
-
|
|
96
|
-
this.entries.push({
|
|
97
|
-
type: FormattingElementList.ELEMENT_ENTRY,
|
|
98
|
-
element: element,
|
|
99
|
-
token: token
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
this.length++;
|
|
103
|
-
};
|
|
104
|
-
|
|
105
|
-
FormattingElementList.prototype.insertElementAfterBookmark = function (element, token) {
|
|
106
|
-
var bookmarkIdx = this.length - 1;
|
|
107
|
-
|
|
108
|
-
for (; bookmarkIdx >= 0; bookmarkIdx--) {
|
|
109
|
-
if (this.entries[bookmarkIdx] === this.bookmark)
|
|
110
|
-
break;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
this.entries.splice(bookmarkIdx + 1, 0, {
|
|
114
|
-
type: FormattingElementList.ELEMENT_ENTRY,
|
|
115
|
-
element: element,
|
|
116
|
-
token: token
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
this.length++;
|
|
120
|
-
};
|
|
121
|
-
|
|
122
|
-
FormattingElementList.prototype.removeEntry = function (entry) {
|
|
123
|
-
for (var i = this.length - 1; i >= 0; i--) {
|
|
124
|
-
if (this.entries[i] === entry) {
|
|
125
|
-
this.entries.splice(i, 1);
|
|
126
|
-
this.length--;
|
|
127
|
-
break;
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
};
|
|
131
|
-
|
|
132
|
-
FormattingElementList.prototype.clearToLastMarker = function () {
|
|
133
|
-
while (this.length) {
|
|
134
|
-
var entry = this.entries.pop();
|
|
135
|
-
|
|
136
|
-
this.length--;
|
|
137
|
-
|
|
138
|
-
if (entry.type === FormattingElementList.MARKER_ENTRY)
|
|
139
|
-
break;
|
|
140
|
-
}
|
|
141
|
-
};
|
|
142
|
-
|
|
143
|
-
//Search
|
|
144
|
-
FormattingElementList.prototype.getElementEntryInScopeWithTagName = function (tagName) {
|
|
145
|
-
for (var i = this.length - 1; i >= 0; i--) {
|
|
146
|
-
var entry = this.entries[i];
|
|
147
|
-
|
|
148
|
-
if (entry.type === FormattingElementList.MARKER_ENTRY)
|
|
149
|
-
return null;
|
|
150
|
-
|
|
151
|
-
if (this.treeAdapter.getTagName(entry.element) === tagName)
|
|
152
|
-
return entry;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
return null;
|
|
156
|
-
};
|
|
157
|
-
|
|
158
|
-
FormattingElementList.prototype.getElementEntry = function (element) {
|
|
159
|
-
for (var i = this.length - 1; i >= 0; i--) {
|
|
160
|
-
var entry = this.entries[i];
|
|
161
|
-
|
|
162
|
-
if (entry.type === FormattingElementList.ELEMENT_ENTRY && entry.element === element)
|
|
163
|
-
return entry;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
return null;
|
|
167
|
-
};
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
//Const
|
|
4
|
+
var NOAH_ARK_CAPACITY = 3;
|
|
5
|
+
|
|
6
|
+
//List of formatting elements
|
|
7
|
+
var FormattingElementList = module.exports = function (treeAdapter) {
|
|
8
|
+
this.length = 0;
|
|
9
|
+
this.entries = [];
|
|
10
|
+
this.treeAdapter = treeAdapter;
|
|
11
|
+
this.bookmark = null;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
//Entry types
|
|
15
|
+
FormattingElementList.MARKER_ENTRY = 'MARKER_ENTRY';
|
|
16
|
+
FormattingElementList.ELEMENT_ENTRY = 'ELEMENT_ENTRY';
|
|
17
|
+
|
|
18
|
+
//Noah Ark's condition
|
|
19
|
+
//OPTIMIZATION: at first we try to find possible candidates for exclusion using
|
|
20
|
+
//lightweight heuristics without thorough attributes check.
|
|
21
|
+
FormattingElementList.prototype._getNoahArkConditionCandidates = function (newElement) {
|
|
22
|
+
var candidates = [];
|
|
23
|
+
|
|
24
|
+
if (this.length >= NOAH_ARK_CAPACITY) {
|
|
25
|
+
var neAttrsLength = this.treeAdapter.getAttrList(newElement).length,
|
|
26
|
+
neTagName = this.treeAdapter.getTagName(newElement),
|
|
27
|
+
neNamespaceURI = this.treeAdapter.getNamespaceURI(newElement);
|
|
28
|
+
|
|
29
|
+
for (var i = this.length - 1; i >= 0; i--) {
|
|
30
|
+
var entry = this.entries[i];
|
|
31
|
+
|
|
32
|
+
if (entry.type === FormattingElementList.MARKER_ENTRY)
|
|
33
|
+
break;
|
|
34
|
+
|
|
35
|
+
var element = entry.element,
|
|
36
|
+
elementAttrs = this.treeAdapter.getAttrList(element),
|
|
37
|
+
isCandidate = this.treeAdapter.getTagName(element) === neTagName &&
|
|
38
|
+
this.treeAdapter.getNamespaceURI(element) === neNamespaceURI &&
|
|
39
|
+
elementAttrs.length === neAttrsLength;
|
|
40
|
+
|
|
41
|
+
if (isCandidate)
|
|
42
|
+
candidates.push({idx: i, attrs: elementAttrs});
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return candidates.length < NOAH_ARK_CAPACITY ? [] : candidates;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
FormattingElementList.prototype._ensureNoahArkCondition = function (newElement) {
|
|
50
|
+
var candidates = this._getNoahArkConditionCandidates(newElement),
|
|
51
|
+
cLength = candidates.length;
|
|
52
|
+
|
|
53
|
+
if (cLength) {
|
|
54
|
+
var neAttrs = this.treeAdapter.getAttrList(newElement),
|
|
55
|
+
neAttrsLength = neAttrs.length,
|
|
56
|
+
neAttrsMap = Object.create(null);
|
|
57
|
+
|
|
58
|
+
//NOTE: build attrs map for the new element so we can perform fast lookups
|
|
59
|
+
for (var i = 0; i < neAttrsLength; i++) {
|
|
60
|
+
var neAttr = neAttrs[i];
|
|
61
|
+
|
|
62
|
+
neAttrsMap[neAttr.name] = neAttr.value;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
for (i = 0; i < neAttrsLength; i++) {
|
|
66
|
+
for (var j = 0; j < cLength; j++) {
|
|
67
|
+
var cAttr = candidates[j].attrs[i];
|
|
68
|
+
|
|
69
|
+
if (neAttrsMap[cAttr.name] !== cAttr.value) {
|
|
70
|
+
candidates.splice(j, 1);
|
|
71
|
+
cLength--;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (candidates.length < NOAH_ARK_CAPACITY)
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
//NOTE: remove bottommost candidates until Noah's Ark condition will not be met
|
|
80
|
+
for (i = cLength - 1; i >= NOAH_ARK_CAPACITY - 1; i--) {
|
|
81
|
+
this.entries.splice(candidates[i].idx, 1);
|
|
82
|
+
this.length--;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
//Mutations
|
|
88
|
+
FormattingElementList.prototype.insertMarker = function () {
|
|
89
|
+
this.entries.push({type: FormattingElementList.MARKER_ENTRY});
|
|
90
|
+
this.length++;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
FormattingElementList.prototype.pushElement = function (element, token) {
|
|
94
|
+
this._ensureNoahArkCondition(element);
|
|
95
|
+
|
|
96
|
+
this.entries.push({
|
|
97
|
+
type: FormattingElementList.ELEMENT_ENTRY,
|
|
98
|
+
element: element,
|
|
99
|
+
token: token
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
this.length++;
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
FormattingElementList.prototype.insertElementAfterBookmark = function (element, token) {
|
|
106
|
+
var bookmarkIdx = this.length - 1;
|
|
107
|
+
|
|
108
|
+
for (; bookmarkIdx >= 0; bookmarkIdx--) {
|
|
109
|
+
if (this.entries[bookmarkIdx] === this.bookmark)
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
this.entries.splice(bookmarkIdx + 1, 0, {
|
|
114
|
+
type: FormattingElementList.ELEMENT_ENTRY,
|
|
115
|
+
element: element,
|
|
116
|
+
token: token
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
this.length++;
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
FormattingElementList.prototype.removeEntry = function (entry) {
|
|
123
|
+
for (var i = this.length - 1; i >= 0; i--) {
|
|
124
|
+
if (this.entries[i] === entry) {
|
|
125
|
+
this.entries.splice(i, 1);
|
|
126
|
+
this.length--;
|
|
127
|
+
break;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
FormattingElementList.prototype.clearToLastMarker = function () {
|
|
133
|
+
while (this.length) {
|
|
134
|
+
var entry = this.entries.pop();
|
|
135
|
+
|
|
136
|
+
this.length--;
|
|
137
|
+
|
|
138
|
+
if (entry.type === FormattingElementList.MARKER_ENTRY)
|
|
139
|
+
break;
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
//Search
|
|
144
|
+
FormattingElementList.prototype.getElementEntryInScopeWithTagName = function (tagName) {
|
|
145
|
+
for (var i = this.length - 1; i >= 0; i--) {
|
|
146
|
+
var entry = this.entries[i];
|
|
147
|
+
|
|
148
|
+
if (entry.type === FormattingElementList.MARKER_ENTRY)
|
|
149
|
+
return null;
|
|
150
|
+
|
|
151
|
+
if (this.treeAdapter.getTagName(entry.element) === tagName)
|
|
152
|
+
return entry;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return null;
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
FormattingElementList.prototype.getElementEntry = function (element) {
|
|
159
|
+
for (var i = this.length - 1; i >= 0; i--) {
|
|
160
|
+
var entry = this.entries[i];
|
|
161
|
+
|
|
162
|
+
if (entry.type === FormattingElementList.ELEMENT_ENTRY && entry.element === element)
|
|
163
|
+
return entry;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
return null;
|
|
167
|
+
};
|