parse5 2.2.3 → 3.0.3

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 (33) hide show
  1. package/LICENSE +19 -19
  2. package/README.md +40 -40
  3. package/lib/common/doctype.js +156 -137
  4. package/lib/common/foreign_content.js +1 -1
  5. package/lib/common/html.js +272 -266
  6. package/lib/common/unicode.js +47 -47
  7. package/lib/extensions/location_info/open_element_stack_mixin.js +34 -0
  8. package/lib/extensions/location_info/parser_mixin.js +213 -0
  9. package/lib/extensions/location_info/tokenizer_mixin.js +117 -0
  10. package/lib/extensions/position_tracking/preprocessor_mixin.js +72 -0
  11. package/lib/index.d.ts +1077 -0
  12. package/lib/index.js +44 -108
  13. package/lib/parser/formatting_element_list.js +167 -167
  14. package/lib/parser/index.js +2819 -2817
  15. package/lib/parser/open_element_stack.js +395 -395
  16. package/lib/parser/parser_stream.js +76 -0
  17. package/lib/parser/plain_text_conversion_stream.js +20 -0
  18. package/lib/sax/dev_null_stream.js +14 -14
  19. package/lib/sax/index.js +7 -119
  20. package/lib/serializer/index.js +162 -180
  21. package/lib/serializer/{stream.js → serializer_stream.js} +28 -49
  22. package/lib/tokenizer/index.js +65 -22
  23. package/lib/tokenizer/named_entity_data.js +5 -0
  24. package/lib/tokenizer/preprocessor.js +0 -8
  25. package/lib/tree_adapters/default.js +209 -578
  26. package/lib/tree_adapters/htmlparser2.js +338 -327
  27. package/lib/{common → utils}/merge_options.js +13 -13
  28. package/lib/utils/mixin.js +18 -0
  29. package/package.json +13 -14
  30. package/lib/location_info/parser_mixin.js +0 -217
  31. package/lib/location_info/tokenizer_mixin.js +0 -169
  32. package/lib/parser/stream.js +0 -140
  33. package/lib/tokenizer/named_entity_trie.js +0 -6
@@ -0,0 +1,34 @@
1
+ 'use strict';
2
+
3
+ var Mixin = require('../../utils/mixin'),
4
+ inherits = require('util').inherits;
5
+
6
+ var LocationInfoOpenElementStackMixin = module.exports = function (stack, options) {
7
+ Mixin.call(this, stack);
8
+
9
+ this.onItemPop = options.onItemPop;
10
+ };
11
+
12
+ inherits(LocationInfoOpenElementStackMixin, Mixin);
13
+
14
+ LocationInfoOpenElementStackMixin.prototype._getOverriddenMethods = function (mxn, orig) {
15
+ return {
16
+ pop: function () {
17
+ mxn.onItemPop(this.current);
18
+ orig.pop.call(this);
19
+ },
20
+
21
+ popAllUpToHtmlElement: function () {
22
+ for (var i = this.stackTop; i > 0; i--)
23
+ mxn.onItemPop(this.items[i]);
24
+
25
+ orig.popAllUpToHtmlElement.call(this);
26
+ },
27
+
28
+ remove: function (element) {
29
+ mxn.onItemPop(this.current);
30
+ orig.remove.call(this, element);
31
+ }
32
+ };
33
+ };
34
+
@@ -0,0 +1,213 @@
1
+ 'use strict';
2
+
3
+ var Mixin = require('../../utils/mixin'),
4
+ Tokenizer = require('../../tokenizer'),
5
+ LocationInfoTokenizerMixin = require('./tokenizer_mixin'),
6
+ PositionTrackingPreprocessorMixin = require('../position_tracking/preprocessor_mixin'),
7
+ LocationInfoOpenElementStackMixin = require('./open_element_stack_mixin'),
8
+ HTML = require('../../common/html'),
9
+ inherits = require('util').inherits;
10
+
11
+
12
+ //Aliases
13
+ var $ = HTML.TAG_NAMES;
14
+
15
+ var LocationInfoParserMixin = module.exports = function (parser) {
16
+ Mixin.call(this, parser);
17
+
18
+ this.parser = parser;
19
+ this.posTracker = null;
20
+ this.lastStartTagToken = null;
21
+ this.lastFosterParentingLocation = null;
22
+ this.currentToken = null;
23
+ };
24
+
25
+ inherits(LocationInfoParserMixin, Mixin);
26
+
27
+
28
+ LocationInfoParserMixin.prototype._setStartLocation = function (element) {
29
+ if (this.lastStartTagToken) {
30
+ element.__location = Object.create(this.lastStartTagToken.location);
31
+ element.__location.startTag = this.lastStartTagToken.location;
32
+ }
33
+ else
34
+ element.__location = null;
35
+ };
36
+
37
+ LocationInfoParserMixin.prototype._setEndLocation = function (element, closingToken) {
38
+ var loc = element.__location;
39
+
40
+ if (loc) {
41
+ if (closingToken.location) {
42
+ var ctLoc = closingToken.location,
43
+ tn = this.parser.treeAdapter.getTagName(element);
44
+
45
+ // NOTE: For cases like <p> <p> </p> - First 'p' closes without a closing
46
+ // tag and for cases like <td> <p> </td> - 'p' closes without a closing tag.
47
+ var isClosingEndTag = closingToken.type === Tokenizer.END_TAG_TOKEN && tn === closingToken.tagName;
48
+
49
+ if (isClosingEndTag) {
50
+ loc.endTag = Object.create(ctLoc);
51
+ loc.endOffset = ctLoc.endOffset;
52
+ }
53
+
54
+ else
55
+ loc.endOffset = ctLoc.startOffset;
56
+ }
57
+
58
+ else if (closingToken.type === Tokenizer.EOF_TOKEN)
59
+ loc.endOffset = this.posTracker.offset;
60
+ }
61
+ };
62
+
63
+ LocationInfoParserMixin.prototype._getOverriddenMethods = function (mxn, orig) {
64
+ return {
65
+ _bootstrap: function (document, fragmentContext) {
66
+ orig._bootstrap.call(this, document, fragmentContext);
67
+
68
+ mxn.lastStartTagToken = null;
69
+ mxn.lastFosterParentingLocation = null;
70
+ mxn.currentToken = null;
71
+ mxn.posTracker = new PositionTrackingPreprocessorMixin(this.tokenizer.preprocessor);
72
+
73
+ new LocationInfoTokenizerMixin(this.tokenizer);
74
+
75
+ new LocationInfoOpenElementStackMixin(this.openElements, {
76
+ onItemPop: function (element) {
77
+ mxn._setEndLocation(element, mxn.currentToken);
78
+ }
79
+ });
80
+ },
81
+
82
+ _runParsingLoop: function (scriptHandler) {
83
+ orig._runParsingLoop.call(this, scriptHandler);
84
+
85
+ // NOTE: generate location info for elements
86
+ // that remains on open element stack
87
+ for (var i = this.openElements.stackTop; i >= 0; i--)
88
+ mxn._setEndLocation(this.openElements.items[i], mxn.currentToken);
89
+ },
90
+
91
+
92
+ //Token processing
93
+ _processTokenInForeignContent: function (token) {
94
+ mxn.currentToken = token;
95
+ orig._processTokenInForeignContent.call(this, token);
96
+ },
97
+
98
+ _processToken: function (token) {
99
+ mxn.currentToken = token;
100
+ orig._processToken.call(this, token);
101
+
102
+ //NOTE: <body> and <html> are never popped from the stack, so we need to updated
103
+ //their end location explicitly.
104
+ var requireExplicitUpdate = token.type === Tokenizer.END_TAG_TOKEN &&
105
+ (token.tagName === $.HTML ||
106
+ token.tagName === $.BODY && this.openElements.hasInScope($.BODY));
107
+
108
+ if (requireExplicitUpdate) {
109
+ for (var i = this.openElements.stackTop; i >= 0; i--) {
110
+ var element = this.openElements.items[i];
111
+
112
+ if (this.treeAdapter.getTagName(element) === token.tagName) {
113
+ mxn._setEndLocation(element, token);
114
+ break;
115
+ }
116
+ }
117
+ }
118
+ },
119
+
120
+
121
+ //Doctype
122
+ _setDocumentType: function (token) {
123
+ orig._setDocumentType.call(this, token);
124
+
125
+ var documentChildren = this.treeAdapter.getChildNodes(this.document),
126
+ cnLength = documentChildren.length;
127
+
128
+ for (var i = 0; i < cnLength; i++) {
129
+ var node = documentChildren[i];
130
+
131
+ if (this.treeAdapter.isDocumentTypeNode(node)) {
132
+ node.__location = token.location;
133
+ break;
134
+ }
135
+ }
136
+ },
137
+
138
+
139
+ //Elements
140
+ _attachElementToTree: function (element) {
141
+ //NOTE: _attachElementToTree is called from _appendElement, _insertElement and _insertTemplate methods.
142
+ //So we will use token location stored in this methods for the element.
143
+ mxn._setStartLocation(element);
144
+ mxn.lastStartTagToken = null;
145
+ orig._attachElementToTree.call(this, element);
146
+ },
147
+
148
+ _appendElement: function (token, namespaceURI) {
149
+ mxn.lastStartTagToken = token;
150
+ orig._appendElement.call(this, token, namespaceURI);
151
+ },
152
+
153
+ _insertElement: function (token, namespaceURI) {
154
+ mxn.lastStartTagToken = token;
155
+ orig._insertElement.call(this, token, namespaceURI);
156
+ },
157
+
158
+ _insertTemplate: function (token) {
159
+ mxn.lastStartTagToken = token;
160
+ orig._insertTemplate.call(this, token);
161
+
162
+ var tmplContent = this.treeAdapter.getTemplateContent(this.openElements.current);
163
+
164
+ tmplContent.__location = null;
165
+ },
166
+
167
+ _insertFakeRootElement: function () {
168
+ orig._insertFakeRootElement.call(this);
169
+ this.openElements.current.__location = null;
170
+ },
171
+
172
+ //Comments
173
+ _appendCommentNode: function (token, parent) {
174
+ orig._appendCommentNode.call(this, token, parent);
175
+
176
+ var children = this.treeAdapter.getChildNodes(parent),
177
+ commentNode = children[children.length - 1];
178
+
179
+ commentNode.__location = token.location;
180
+ },
181
+
182
+ //Text
183
+ _findFosterParentingLocation: function () {
184
+ //NOTE: store last foster parenting location, so we will be able to find inserted text
185
+ //in case of foster parenting
186
+ mxn.lastFosterParentingLocation = orig._findFosterParentingLocation.call(this);
187
+
188
+ return mxn.lastFosterParentingLocation;
189
+ },
190
+
191
+ _insertCharacters: function (token) {
192
+ orig._insertCharacters.call(this, token);
193
+
194
+ var hasFosterParent = this._shouldFosterParentOnInsertion(),
195
+ parent = hasFosterParent && mxn.lastFosterParentingLocation.parent ||
196
+ this.openElements.currentTmplContent ||
197
+ this.openElements.current,
198
+ siblings = this.treeAdapter.getChildNodes(parent),
199
+ textNodeIdx = hasFosterParent && mxn.lastFosterParentingLocation.beforeElement ?
200
+ siblings.indexOf(mxn.lastFosterParentingLocation.beforeElement) - 1 :
201
+ siblings.length - 1,
202
+ textNode = siblings[textNodeIdx];
203
+
204
+ //NOTE: if we have location assigned by another token, then just update end position
205
+ if (textNode.__location)
206
+ textNode.__location.endOffset = token.location.endOffset;
207
+
208
+ else
209
+ textNode.__location = token.location;
210
+ }
211
+ };
212
+ };
213
+
@@ -0,0 +1,117 @@
1
+ 'use strict';
2
+
3
+ var Mixin = require('../../utils/mixin'),
4
+ Tokenizer = require('../../tokenizer'),
5
+ PositionTrackingPreprocessorMixin = require('../position_tracking/preprocessor_mixin'),
6
+ inherits = require('util').inherits;
7
+
8
+ var LocationInfoTokenizerMixin = module.exports = function (tokenizer) {
9
+ Mixin.call(this, tokenizer);
10
+
11
+ this.tokenizer = tokenizer;
12
+ this.posTracker = new PositionTrackingPreprocessorMixin(tokenizer.preprocessor);
13
+ this.currentAttrLocation = null;
14
+ this.currentTokenLocation = null;
15
+ };
16
+
17
+ inherits(LocationInfoTokenizerMixin, Mixin);
18
+
19
+ LocationInfoTokenizerMixin.prototype._getCurrentLocation = function () {
20
+ return {
21
+ line: this.posTracker.line,
22
+ col: this.posTracker.col,
23
+ startOffset: this.posTracker.offset,
24
+ endOffset: -1
25
+ };
26
+ };
27
+
28
+ LocationInfoTokenizerMixin.prototype._attachCurrentAttrLocationInfo = function () {
29
+ this.currentAttrLocation.endOffset = this.posTracker.offset;
30
+
31
+ var currentToken = this.tokenizer.currentToken,
32
+ currentAttr = this.tokenizer.currentAttr;
33
+
34
+ if (!currentToken.location.attrs)
35
+ currentToken.location.attrs = Object.create(null);
36
+
37
+ currentToken.location.attrs[currentAttr.name] = this.currentAttrLocation;
38
+ };
39
+
40
+ LocationInfoTokenizerMixin.prototype._getOverriddenMethods = function (mxn, orig) {
41
+ var methods = {
42
+ _createStartTagToken: function () {
43
+ orig._createStartTagToken.call(this);
44
+ this.currentToken.location = mxn.currentTokenLocation;
45
+ },
46
+
47
+ _createEndTagToken: function () {
48
+ orig._createEndTagToken.call(this);
49
+ this.currentToken.location = mxn.currentTokenLocation;
50
+ },
51
+
52
+ _createCommentToken: function () {
53
+ orig._createCommentToken.call(this);
54
+ this.currentToken.location = mxn.currentTokenLocation;
55
+ },
56
+
57
+ _createDoctypeToken: function (initialName) {
58
+ orig._createDoctypeToken.call(this, initialName);
59
+ this.currentToken.location = mxn.currentTokenLocation;
60
+ },
61
+
62
+ _createCharacterToken: function (type, ch) {
63
+ orig._createCharacterToken.call(this, type, ch);
64
+ this.currentCharacterToken.location = mxn.currentTokenLocation;
65
+ },
66
+
67
+ _createAttr: function (attrNameFirstCh) {
68
+ orig._createAttr.call(this, attrNameFirstCh);
69
+ mxn.currentAttrLocation = mxn._getCurrentLocation();
70
+ },
71
+
72
+ _leaveAttrName: function (toState) {
73
+ orig._leaveAttrName.call(this, toState);
74
+ mxn._attachCurrentAttrLocationInfo();
75
+ },
76
+
77
+ _leaveAttrValue: function (toState) {
78
+ orig._leaveAttrValue.call(this, toState);
79
+ mxn._attachCurrentAttrLocationInfo();
80
+ },
81
+
82
+ _emitCurrentToken: function () {
83
+ //NOTE: if we have pending character token make it's end location equal to the
84
+ //current token's start location.
85
+ if (this.currentCharacterToken)
86
+ this.currentCharacterToken.location.endOffset = this.currentToken.location.startOffset;
87
+
88
+ this.currentToken.location.endOffset = mxn.posTracker.offset + 1;
89
+ orig._emitCurrentToken.call(this);
90
+ },
91
+
92
+ _emitCurrentCharacterToken: function () {
93
+ //NOTE: if we have character token and it's location wasn't set in the _emitCurrentToken(),
94
+ //then set it's location at the current preprocessor position.
95
+ //We don't need to increment preprocessor position, since character token
96
+ //emission is always forced by the start of the next character token here.
97
+ //So, we already have advanced position.
98
+ if (this.currentCharacterToken && this.currentCharacterToken.location.endOffset === -1)
99
+ this.currentCharacterToken.location.endOffset = mxn.posTracker.offset;
100
+
101
+ orig._emitCurrentCharacterToken.call(this);
102
+ }
103
+ };
104
+
105
+ //NOTE: patch initial states for each mode to obtain token start position
106
+ Object.keys(Tokenizer.MODE).forEach(function (modeName) {
107
+ var state = Tokenizer.MODE[modeName];
108
+
109
+ methods[state] = function (cp) {
110
+ mxn.currentTokenLocation = mxn._getCurrentLocation();
111
+ orig[state].call(this, cp);
112
+ };
113
+ });
114
+
115
+ return methods;
116
+ };
117
+
@@ -0,0 +1,72 @@
1
+ 'use strict';
2
+
3
+ var Mixin = require('../../utils/mixin'),
4
+ inherits = require('util').inherits,
5
+ UNICODE = require('../../common/unicode');
6
+
7
+ //Aliases
8
+ var $ = UNICODE.CODE_POINTS;
9
+
10
+ var PositionTrackingPreprocessorMixin = module.exports = function (preprocessor) {
11
+ // NOTE: avoid installing tracker twice
12
+ if (!preprocessor.__locTracker) {
13
+ preprocessor.__locTracker = this;
14
+
15
+ Mixin.call(this, preprocessor);
16
+
17
+ this.preprocessor = preprocessor;
18
+ this.isEol = false;
19
+ this.lineStartPos = 0;
20
+ this.droppedBufferSize = 0;
21
+
22
+ this.col = -1;
23
+ this.line = 1;
24
+ }
25
+
26
+ return preprocessor.__locTracker;
27
+ };
28
+
29
+ inherits(PositionTrackingPreprocessorMixin, Mixin);
30
+
31
+ Object.defineProperty(PositionTrackingPreprocessorMixin.prototype, 'offset', {
32
+ get: function () {
33
+ return this.droppedBufferSize + this.preprocessor.pos;
34
+ }
35
+ });
36
+
37
+ PositionTrackingPreprocessorMixin.prototype._getOverriddenMethods = function (mxn, orig) {
38
+ return {
39
+ advance: function () {
40
+ var cp = orig.advance.call(this);
41
+
42
+ //NOTE: LF should be in the last column of the line
43
+ if (mxn.isEol) {
44
+ mxn.isEol = false;
45
+ mxn.line++;
46
+ mxn.lineStartPos = mxn.offset;
47
+ }
48
+
49
+ if (cp === $.LINE_FEED)
50
+ mxn.isEol = true;
51
+
52
+ mxn.col = mxn.offset - mxn.lineStartPos + 1;
53
+
54
+ return cp;
55
+ },
56
+
57
+ retreat: function () {
58
+ orig.retreat.call(this);
59
+ mxn.isEol = false;
60
+
61
+ mxn.col = mxn.offset - mxn.lineStartPos + 1;
62
+ },
63
+
64
+ dropParsedChunk: function () {
65
+ var prevPos = this.pos;
66
+
67
+ orig.dropParsedChunk.call(this);
68
+
69
+ mxn.droppedBufferSize += prevPos - this.pos;
70
+ }
71
+ };
72
+ };