parse5 3.0.3 → 5.1.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 +14 -16
- package/lib/common/doctype.js +125 -108
- package/lib/common/error-codes.js +65 -0
- package/lib/common/foreign-content.js +265 -0
- package/lib/common/html.js +102 -102
- package/lib/common/unicode.js +81 -19
- package/lib/extensions/error-reporting/mixin-base.js +43 -0
- package/lib/extensions/error-reporting/parser-mixin.js +52 -0
- package/lib/extensions/error-reporting/preprocessor-mixin.js +24 -0
- package/lib/extensions/error-reporting/tokenizer-mixin.js +17 -0
- package/lib/extensions/location-info/open-element-stack-mixin.js +35 -0
- package/lib/extensions/location-info/parser-mixin.js +222 -0
- package/lib/extensions/location-info/tokenizer-mixin.js +146 -0
- package/lib/extensions/position-tracking/preprocessor-mixin.js +64 -0
- package/lib/index.js +6 -21
- package/lib/parser/formatting-element-list.js +181 -0
- package/lib/parser/index.js +1469 -1332
- package/lib/parser/open-element-stack.js +482 -0
- package/lib/serializer/index.js +136 -122
- package/lib/tokenizer/index.js +1856 -1804
- package/lib/tokenizer/{named_entity_data.js → named-entity-data.js} +1 -1
- package/lib/tokenizer/preprocessor.js +115 -103
- package/lib/tree-adapters/default.js +217 -0
- package/lib/utils/{merge_options.js → merge-options.js} +2 -2
- package/lib/utils/mixin.js +33 -12
- package/package.json +32 -61
- package/LICENSE +0 -19
- package/lib/common/foreign_content.js +0 -260
- package/lib/extensions/location_info/open_element_stack_mixin.js +0 -34
- package/lib/extensions/location_info/parser_mixin.js +0 -213
- package/lib/extensions/location_info/tokenizer_mixin.js +0 -117
- package/lib/extensions/position_tracking/preprocessor_mixin.js +0 -72
- package/lib/index.d.ts +0 -1077
- package/lib/parser/formatting_element_list.js +0 -167
- package/lib/parser/open_element_stack.js +0 -395
- package/lib/parser/parser_stream.js +0 -76
- package/lib/parser/plain_text_conversion_stream.js +0 -20
- package/lib/sax/dev_null_stream.js +0 -14
- package/lib/sax/index.js +0 -118
- package/lib/sax/parser_feedback_simulator.js +0 -153
- package/lib/serializer/serializer_stream.js +0 -28
- package/lib/tree_adapters/default.js +0 -209
- package/lib/tree_adapters/htmlparser2.js +0 -338
package/lib/serializer/index.js
CHANGED
|
@@ -1,162 +1,176 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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
7
|
|
|
8
8
|
//Aliases
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
const $ = HTML.TAG_NAMES;
|
|
10
|
+
const NS = HTML.NAMESPACES;
|
|
11
11
|
|
|
12
12
|
//Default serializer options
|
|
13
|
-
|
|
13
|
+
const DEFAULT_OPTIONS = {
|
|
14
14
|
treeAdapter: defaultTreeAdapter
|
|
15
15
|
};
|
|
16
16
|
|
|
17
17
|
//Escaping regexes
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
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
23
|
|
|
24
24
|
//Serializer
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
25
|
+
class Serializer {
|
|
26
|
+
constructor(node, options) {
|
|
27
|
+
this.options = mergeOptions(DEFAULT_OPTIONS, options);
|
|
28
|
+
this.treeAdapter = this.options.treeAdapter;
|
|
28
29
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
// NOTE: exported as static method for the testing purposes
|
|
34
|
-
Serializer.escapeString = function (str, attrMode) {
|
|
35
|
-
str = str
|
|
36
|
-
.replace(AMP_REGEX, '&')
|
|
37
|
-
.replace(NBSP_REGEX, ' ');
|
|
38
|
-
|
|
39
|
-
if (attrMode)
|
|
40
|
-
str = str.replace(DOUBLE_QUOTE_REGEX, '"');
|
|
41
|
-
|
|
42
|
-
else {
|
|
43
|
-
str = str
|
|
44
|
-
.replace(LT_REGEX, '<')
|
|
45
|
-
.replace(GT_REGEX, '>');
|
|
30
|
+
this.html = '';
|
|
31
|
+
this.startNode = node;
|
|
46
32
|
}
|
|
47
33
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
34
|
+
//API
|
|
35
|
+
serialize() {
|
|
36
|
+
this._serializeChildNodes(this.startNode);
|
|
51
37
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
this._serializeChildNodes(this.startNode);
|
|
55
|
-
|
|
56
|
-
return this.html;
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
//Internals
|
|
61
|
-
Serializer.prototype._serializeChildNodes = function (parentNode) {
|
|
62
|
-
var childNodes = this.treeAdapter.getChildNodes(parentNode);
|
|
63
|
-
|
|
64
|
-
if (childNodes) {
|
|
65
|
-
for (var i = 0, cnLength = childNodes.length; i < cnLength; i++) {
|
|
66
|
-
var currentNode = childNodes[i];
|
|
67
|
-
|
|
68
|
-
if (this.treeAdapter.isElementNode(currentNode))
|
|
69
|
-
this._serializeElement(currentNode);
|
|
70
|
-
|
|
71
|
-
else if (this.treeAdapter.isTextNode(currentNode))
|
|
72
|
-
this._serializeTextNode(currentNode);
|
|
73
|
-
|
|
74
|
-
else if (this.treeAdapter.isCommentNode(currentNode))
|
|
75
|
-
this._serializeCommentNode(currentNode);
|
|
38
|
+
return this.html;
|
|
39
|
+
}
|
|
76
40
|
|
|
77
|
-
|
|
78
|
-
|
|
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
|
+
}
|
|
79
59
|
}
|
|
80
60
|
}
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
Serializer.prototype._serializeElement = function (node) {
|
|
84
|
-
var tn = this.treeAdapter.getTagName(node),
|
|
85
|
-
ns = this.treeAdapter.getNamespaceURI(node);
|
|
86
61
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
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
|
+
}
|
|
102
96
|
}
|
|
103
|
-
};
|
|
104
97
|
|
|
105
|
-
|
|
106
|
-
|
|
98
|
+
_serializeAttributes(node) {
|
|
99
|
+
const attrs = this.treeAdapter.getAttrList(node);
|
|
107
100
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
value = Serializer.escapeString(attr.value, true);
|
|
101
|
+
for (let i = 0, attrsLength = attrs.length; i < attrsLength; i++) {
|
|
102
|
+
const attr = attrs[i];
|
|
103
|
+
const value = Serializer.escapeString(attr.value, true);
|
|
111
104
|
|
|
112
|
-
|
|
105
|
+
this.html += ' ';
|
|
113
106
|
|
|
114
|
-
|
|
115
|
-
|
|
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
|
+
}
|
|
116
115
|
|
|
117
|
-
|
|
118
|
-
|
|
116
|
+
this.html += attr.name;
|
|
117
|
+
} else if (attr.namespace === NS.XLINK) {
|
|
118
|
+
this.html += 'xlink:' + attr.name;
|
|
119
|
+
} else {
|
|
120
|
+
this.html += attr.namespace + ':' + attr.name;
|
|
121
|
+
}
|
|
119
122
|
|
|
120
|
-
|
|
121
|
-
if (attr.name !== 'xmlns')
|
|
122
|
-
this.html += 'xmlns:';
|
|
123
|
-
|
|
124
|
-
this.html += attr.name;
|
|
123
|
+
this.html += '="' + value + '"';
|
|
125
124
|
}
|
|
125
|
+
}
|
|
126
126
|
|
|
127
|
-
|
|
128
|
-
|
|
127
|
+
_serializeTextNode(node) {
|
|
128
|
+
const content = this.treeAdapter.getTextNodeContent(node);
|
|
129
|
+
const parent = this.treeAdapter.getParentNode(node);
|
|
130
|
+
let parentTn = void 0;
|
|
129
131
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
+
if (parent && this.treeAdapter.isElementNode(parent)) {
|
|
133
|
+
parentTn = this.treeAdapter.getTagName(parent);
|
|
134
|
+
}
|
|
132
135
|
|
|
133
|
-
|
|
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
|
+
}
|
|
134
150
|
}
|
|
135
|
-
};
|
|
136
151
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
parentTn = void 0;
|
|
152
|
+
_serializeCommentNode(node) {
|
|
153
|
+
this.html += '<!--' + this.treeAdapter.getCommentNodeContent(node) + '-->';
|
|
154
|
+
}
|
|
141
155
|
|
|
142
|
-
|
|
143
|
-
|
|
156
|
+
_serializeDocumentTypeNode(node) {
|
|
157
|
+
const name = this.treeAdapter.getDocumentTypeNodeName(node);
|
|
144
158
|
|
|
145
|
-
|
|
146
|
-
|
|
159
|
+
this.html += '<' + doctype.serializeContent(name, null, null) + '>';
|
|
160
|
+
}
|
|
161
|
+
}
|
|
147
162
|
|
|
148
|
-
|
|
163
|
+
// NOTE: used in tests and by rewriting stream
|
|
164
|
+
Serializer.escapeString = function(str, attrMode) {
|
|
165
|
+
str = str.replace(AMP_REGEX, '&').replace(NBSP_REGEX, ' ');
|
|
149
166
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
}
|
|
167
|
+
if (attrMode) {
|
|
168
|
+
str = str.replace(DOUBLE_QUOTE_REGEX, '"');
|
|
169
|
+
} else {
|
|
170
|
+
str = str.replace(LT_REGEX, '<').replace(GT_REGEX, '>');
|
|
171
|
+
}
|
|
153
172
|
|
|
154
|
-
|
|
155
|
-
this.html += '<!--' + this.treeAdapter.getCommentNodeContent(node) + '-->';
|
|
173
|
+
return str;
|
|
156
174
|
};
|
|
157
175
|
|
|
158
|
-
|
|
159
|
-
var name = this.treeAdapter.getDocumentTypeNodeName(node);
|
|
160
|
-
|
|
161
|
-
this.html += '<' + doctype.serializeContent(name, null, null) + '>';
|
|
162
|
-
};
|
|
176
|
+
module.exports = Serializer;
|