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.
Files changed (43) hide show
  1. package/README.md +14 -16
  2. package/lib/common/doctype.js +125 -108
  3. package/lib/common/error-codes.js +65 -0
  4. package/lib/common/foreign-content.js +265 -0
  5. package/lib/common/html.js +102 -102
  6. package/lib/common/unicode.js +81 -19
  7. package/lib/extensions/error-reporting/mixin-base.js +43 -0
  8. package/lib/extensions/error-reporting/parser-mixin.js +52 -0
  9. package/lib/extensions/error-reporting/preprocessor-mixin.js +24 -0
  10. package/lib/extensions/error-reporting/tokenizer-mixin.js +17 -0
  11. package/lib/extensions/location-info/open-element-stack-mixin.js +35 -0
  12. package/lib/extensions/location-info/parser-mixin.js +222 -0
  13. package/lib/extensions/location-info/tokenizer-mixin.js +146 -0
  14. package/lib/extensions/position-tracking/preprocessor-mixin.js +64 -0
  15. package/lib/index.js +6 -21
  16. package/lib/parser/formatting-element-list.js +181 -0
  17. package/lib/parser/index.js +1469 -1332
  18. package/lib/parser/open-element-stack.js +482 -0
  19. package/lib/serializer/index.js +136 -122
  20. package/lib/tokenizer/index.js +1856 -1804
  21. package/lib/tokenizer/{named_entity_data.js → named-entity-data.js} +1 -1
  22. package/lib/tokenizer/preprocessor.js +115 -103
  23. package/lib/tree-adapters/default.js +217 -0
  24. package/lib/utils/{merge_options.js → merge-options.js} +2 -2
  25. package/lib/utils/mixin.js +33 -12
  26. package/package.json +32 -61
  27. package/LICENSE +0 -19
  28. package/lib/common/foreign_content.js +0 -260
  29. package/lib/extensions/location_info/open_element_stack_mixin.js +0 -34
  30. package/lib/extensions/location_info/parser_mixin.js +0 -213
  31. package/lib/extensions/location_info/tokenizer_mixin.js +0 -117
  32. package/lib/extensions/position_tracking/preprocessor_mixin.js +0 -72
  33. package/lib/index.d.ts +0 -1077
  34. package/lib/parser/formatting_element_list.js +0 -167
  35. package/lib/parser/open_element_stack.js +0 -395
  36. package/lib/parser/parser_stream.js +0 -76
  37. package/lib/parser/plain_text_conversion_stream.js +0 -20
  38. package/lib/sax/dev_null_stream.js +0 -14
  39. package/lib/sax/index.js +0 -118
  40. package/lib/sax/parser_feedback_simulator.js +0 -153
  41. package/lib/serializer/serializer_stream.js +0 -28
  42. package/lib/tree_adapters/default.js +0 -209
  43. package/lib/tree_adapters/htmlparser2.js +0 -338
@@ -1,162 +1,176 @@
1
1
  'use strict';
2
2
 
3
- var defaultTreeAdapter = require('../tree_adapters/default'),
4
- mergeOptions = require('../utils/merge_options'),
5
- doctype = require('../common/doctype'),
6
- HTML = require('../common/html');
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
- var $ = HTML.TAG_NAMES,
10
- NS = HTML.NAMESPACES;
9
+ const $ = HTML.TAG_NAMES;
10
+ const NS = HTML.NAMESPACES;
11
11
 
12
12
  //Default serializer options
13
- var DEFAULT_OPTIONS = {
13
+ const DEFAULT_OPTIONS = {
14
14
  treeAdapter: defaultTreeAdapter
15
15
  };
16
16
 
17
17
  //Escaping regexes
18
- var AMP_REGEX = /&/g,
19
- NBSP_REGEX = /\u00a0/g,
20
- DOUBLE_QUOTE_REGEX = /"/g,
21
- LT_REGEX = /</g,
22
- GT_REGEX = />/g;
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
- var Serializer = module.exports = function (node, options) {
26
- this.options = mergeOptions(DEFAULT_OPTIONS, options);
27
- this.treeAdapter = this.options.treeAdapter;
25
+ class Serializer {
26
+ constructor(node, options) {
27
+ this.options = mergeOptions(DEFAULT_OPTIONS, options);
28
+ this.treeAdapter = this.options.treeAdapter;
28
29
 
29
- this.html = '';
30
- this.startNode = node;
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, '&amp;')
37
- .replace(NBSP_REGEX, '&nbsp;');
38
-
39
- if (attrMode)
40
- str = str.replace(DOUBLE_QUOTE_REGEX, '&quot;');
41
-
42
- else {
43
- str = str
44
- .replace(LT_REGEX, '&lt;')
45
- .replace(GT_REGEX, '&gt;');
30
+ this.html = '';
31
+ this.startNode = node;
46
32
  }
47
33
 
48
- return str;
49
- };
50
-
34
+ //API
35
+ serialize() {
36
+ this._serializeChildNodes(this.startNode);
51
37
 
52
- //API
53
- Serializer.prototype.serialize = function () {
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
- else if (this.treeAdapter.isDocumentTypeNode(currentNode))
78
- this._serializeDocumentTypeNode(currentNode);
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
- this.html += '<' + tn;
88
- this._serializeAttributes(node);
89
- this.html += '>';
90
-
91
- if (tn !== $.AREA && tn !== $.BASE && tn !== $.BASEFONT && tn !== $.BGSOUND && tn !== $.BR && tn !== $.BR &&
92
- tn !== $.COL && tn !== $.EMBED && tn !== $.FRAME && tn !== $.HR && tn !== $.IMG && tn !== $.INPUT &&
93
- tn !== $.KEYGEN && tn !== $.LINK && tn !== $.MENUITEM && tn !== $.META && tn !== $.PARAM && tn !== $.SOURCE &&
94
- tn !== $.TRACK && tn !== $.WBR) {
95
-
96
- var childNodesHolder = tn === $.TEMPLATE && ns === NS.HTML ?
97
- this.treeAdapter.getTemplateContent(node) :
98
- node;
99
-
100
- this._serializeChildNodes(childNodesHolder);
101
- this.html += '</' + tn + '>';
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
- Serializer.prototype._serializeAttributes = function (node) {
106
- var attrs = this.treeAdapter.getAttrList(node);
98
+ _serializeAttributes(node) {
99
+ const attrs = this.treeAdapter.getAttrList(node);
107
100
 
108
- for (var i = 0, attrsLength = attrs.length; i < attrsLength; i++) {
109
- var attr = attrs[i],
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
- this.html += ' ';
105
+ this.html += ' ';
113
106
 
114
- if (!attr.namespace)
115
- this.html += attr.name;
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
- else if (attr.namespace === NS.XML)
118
- this.html += 'xml:' + attr.name;
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
- else if (attr.namespace === NS.XMLNS) {
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
- else if (attr.namespace === NS.XLINK)
128
- this.html += 'xlink:' + attr.name;
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
- else
131
- this.html += attr.namespace + ':' + attr.name;
132
+ if (parent && this.treeAdapter.isElementNode(parent)) {
133
+ parentTn = this.treeAdapter.getTagName(parent);
134
+ }
132
135
 
133
- this.html += '="' + value + '"';
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
- Serializer.prototype._serializeTextNode = function (node) {
138
- var content = this.treeAdapter.getTextNodeContent(node),
139
- parent = this.treeAdapter.getParentNode(node),
140
- parentTn = void 0;
152
+ _serializeCommentNode(node) {
153
+ this.html += '<!--' + this.treeAdapter.getCommentNodeContent(node) + '-->';
154
+ }
141
155
 
142
- if (parent && this.treeAdapter.isElementNode(parent))
143
- parentTn = this.treeAdapter.getTagName(parent);
156
+ _serializeDocumentTypeNode(node) {
157
+ const name = this.treeAdapter.getDocumentTypeNodeName(node);
144
158
 
145
- if (parentTn === $.STYLE || parentTn === $.SCRIPT || parentTn === $.XMP || parentTn === $.IFRAME ||
146
- parentTn === $.NOEMBED || parentTn === $.NOFRAMES || parentTn === $.PLAINTEXT || parentTn === $.NOSCRIPT)
159
+ this.html += '<' + doctype.serializeContent(name, null, null) + '>';
160
+ }
161
+ }
147
162
 
148
- this.html += content;
163
+ // NOTE: used in tests and by rewriting stream
164
+ Serializer.escapeString = function(str, attrMode) {
165
+ str = str.replace(AMP_REGEX, '&amp;').replace(NBSP_REGEX, '&nbsp;');
149
166
 
150
- else
151
- this.html += Serializer.escapeString(content, false);
152
- };
167
+ if (attrMode) {
168
+ str = str.replace(DOUBLE_QUOTE_REGEX, '&quot;');
169
+ } else {
170
+ str = str.replace(LT_REGEX, '&lt;').replace(GT_REGEX, '&gt;');
171
+ }
153
172
 
154
- Serializer.prototype._serializeCommentNode = function (node) {
155
- this.html += '<!--' + this.treeAdapter.getCommentNodeContent(node) + '-->';
173
+ return str;
156
174
  };
157
175
 
158
- Serializer.prototype._serializeDocumentTypeNode = function (node) {
159
- var name = this.treeAdapter.getDocumentTypeNodeName(node);
160
-
161
- this.html += '<' + doctype.serializeContent(name, null, null) + '>';
162
- };
176
+ module.exports = Serializer;