parse5 3.0.1 → 4.0.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/LICENSE +19 -19
- package/lib/common/unicode.js +47 -47
- package/lib/extensions/location_info/open_element_stack_mixin.js +34 -0
- package/lib/extensions/location_info/parser_mixin.js +213 -0
- package/lib/extensions/location_info/tokenizer_mixin.js +117 -0
- package/lib/extensions/position_tracking/preprocessor_mixin.js +72 -0
- package/lib/index.d.ts +1085 -1077
- package/lib/index.js +26 -4
- package/lib/parser/index.js +10 -6
- package/lib/parser/parser_stream.js +2 -2
- package/lib/sax/dev_null_stream.js +14 -14
- package/lib/sax/index.js +6 -1
- package/lib/serializer/index.js +1 -1
- package/lib/tokenizer/index.js +2 -6
- package/lib/tokenizer/preprocessor.js +0 -8
- package/lib/{common → utils}/merge_options.js +0 -0
- package/lib/utils/mixin.js +18 -0
- package/package.json +60 -63
- package/lib/location_info/parser_mixin.js +0 -210
- package/lib/location_info/tokenizer_mixin.js +0 -148
package/LICENSE
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
Copyright (c) 2013-2016 Ivan Nikulin (ifaaan@gmail.com, https://github.com/inikulin)
|
|
2
|
-
|
|
3
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
-
in the Software without restriction, including without limitation the rights
|
|
6
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
-
furnished to do so, subject to the following conditions:
|
|
9
|
-
|
|
10
|
-
The above copyright notice and this permission notice shall be included in
|
|
11
|
-
all copies or substantial portions of the Software.
|
|
12
|
-
|
|
13
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
19
|
-
THE SOFTWARE.
|
|
1
|
+
Copyright (c) 2013-2016 Ivan Nikulin (ifaaan@gmail.com, https://github.com/inikulin)
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
|
11
|
+
all copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
19
|
+
THE SOFTWARE.
|
package/lib/common/unicode.js
CHANGED
|
@@ -1,47 +1,47 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
exports.REPLACEMENT_CHARACTER = '\uFFFD';
|
|
4
|
-
|
|
5
|
-
exports.CODE_POINTS = {
|
|
6
|
-
EOF: -1,
|
|
7
|
-
NULL: 0x00,
|
|
8
|
-
TABULATION: 0x09,
|
|
9
|
-
CARRIAGE_RETURN: 0x0D,
|
|
10
|
-
LINE_FEED: 0x0A,
|
|
11
|
-
FORM_FEED: 0x0C,
|
|
12
|
-
SPACE: 0x20,
|
|
13
|
-
EXCLAMATION_MARK: 0x21,
|
|
14
|
-
QUOTATION_MARK: 0x22,
|
|
15
|
-
NUMBER_SIGN: 0x23,
|
|
16
|
-
AMPERSAND: 0x26,
|
|
17
|
-
APOSTROPHE: 0x27,
|
|
18
|
-
HYPHEN_MINUS: 0x2D,
|
|
19
|
-
SOLIDUS: 0x2F,
|
|
20
|
-
DIGIT_0: 0x30,
|
|
21
|
-
DIGIT_9: 0x39,
|
|
22
|
-
SEMICOLON: 0x3B,
|
|
23
|
-
LESS_THAN_SIGN: 0x3C,
|
|
24
|
-
EQUALS_SIGN: 0x3D,
|
|
25
|
-
GREATER_THAN_SIGN: 0x3E,
|
|
26
|
-
QUESTION_MARK: 0x3F,
|
|
27
|
-
LATIN_CAPITAL_A: 0x41,
|
|
28
|
-
LATIN_CAPITAL_F: 0x46,
|
|
29
|
-
LATIN_CAPITAL_X: 0x58,
|
|
30
|
-
LATIN_CAPITAL_Z: 0x5A,
|
|
31
|
-
GRAVE_ACCENT: 0x60,
|
|
32
|
-
LATIN_SMALL_A: 0x61,
|
|
33
|
-
LATIN_SMALL_F: 0x66,
|
|
34
|
-
LATIN_SMALL_X: 0x78,
|
|
35
|
-
LATIN_SMALL_Z: 0x7A,
|
|
36
|
-
REPLACEMENT_CHARACTER: 0xFFFD
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
exports.CODE_POINT_SEQUENCES = {
|
|
40
|
-
DASH_DASH_STRING: [0x2D, 0x2D], //--
|
|
41
|
-
DOCTYPE_STRING: [0x44, 0x4F, 0x43, 0x54, 0x59, 0x50, 0x45], //DOCTYPE
|
|
42
|
-
CDATA_START_STRING: [0x5B, 0x43, 0x44, 0x41, 0x54, 0x41, 0x5B], //[CDATA[
|
|
43
|
-
CDATA_END_STRING: [0x5D, 0x5D, 0x3E], //]]>
|
|
44
|
-
SCRIPT_STRING: [0x73, 0x63, 0x72, 0x69, 0x70, 0x74], //script
|
|
45
|
-
PUBLIC_STRING: [0x50, 0x55, 0x42, 0x4C, 0x49, 0x43], //PUBLIC
|
|
46
|
-
SYSTEM_STRING: [0x53, 0x59, 0x53, 0x54, 0x45, 0x4D] //SYSTEM
|
|
47
|
-
};
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
exports.REPLACEMENT_CHARACTER = '\uFFFD';
|
|
4
|
+
|
|
5
|
+
exports.CODE_POINTS = {
|
|
6
|
+
EOF: -1,
|
|
7
|
+
NULL: 0x00,
|
|
8
|
+
TABULATION: 0x09,
|
|
9
|
+
CARRIAGE_RETURN: 0x0D,
|
|
10
|
+
LINE_FEED: 0x0A,
|
|
11
|
+
FORM_FEED: 0x0C,
|
|
12
|
+
SPACE: 0x20,
|
|
13
|
+
EXCLAMATION_MARK: 0x21,
|
|
14
|
+
QUOTATION_MARK: 0x22,
|
|
15
|
+
NUMBER_SIGN: 0x23,
|
|
16
|
+
AMPERSAND: 0x26,
|
|
17
|
+
APOSTROPHE: 0x27,
|
|
18
|
+
HYPHEN_MINUS: 0x2D,
|
|
19
|
+
SOLIDUS: 0x2F,
|
|
20
|
+
DIGIT_0: 0x30,
|
|
21
|
+
DIGIT_9: 0x39,
|
|
22
|
+
SEMICOLON: 0x3B,
|
|
23
|
+
LESS_THAN_SIGN: 0x3C,
|
|
24
|
+
EQUALS_SIGN: 0x3D,
|
|
25
|
+
GREATER_THAN_SIGN: 0x3E,
|
|
26
|
+
QUESTION_MARK: 0x3F,
|
|
27
|
+
LATIN_CAPITAL_A: 0x41,
|
|
28
|
+
LATIN_CAPITAL_F: 0x46,
|
|
29
|
+
LATIN_CAPITAL_X: 0x58,
|
|
30
|
+
LATIN_CAPITAL_Z: 0x5A,
|
|
31
|
+
GRAVE_ACCENT: 0x60,
|
|
32
|
+
LATIN_SMALL_A: 0x61,
|
|
33
|
+
LATIN_SMALL_F: 0x66,
|
|
34
|
+
LATIN_SMALL_X: 0x78,
|
|
35
|
+
LATIN_SMALL_Z: 0x7A,
|
|
36
|
+
REPLACEMENT_CHARACTER: 0xFFFD
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
exports.CODE_POINT_SEQUENCES = {
|
|
40
|
+
DASH_DASH_STRING: [0x2D, 0x2D], //--
|
|
41
|
+
DOCTYPE_STRING: [0x44, 0x4F, 0x43, 0x54, 0x59, 0x50, 0x45], //DOCTYPE
|
|
42
|
+
CDATA_START_STRING: [0x5B, 0x43, 0x44, 0x41, 0x54, 0x41, 0x5B], //[CDATA[
|
|
43
|
+
CDATA_END_STRING: [0x5D, 0x5D, 0x3E], //]]>
|
|
44
|
+
SCRIPT_STRING: [0x73, 0x63, 0x72, 0x69, 0x70, 0x74], //script
|
|
45
|
+
PUBLIC_STRING: [0x50, 0x55, 0x42, 0x4C, 0x49, 0x43], //PUBLIC
|
|
46
|
+
SYSTEM_STRING: [0x53, 0x59, 0x53, 0x54, 0x45, 0x4D] //SYSTEM
|
|
47
|
+
};
|
|
@@ -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
|
+
};
|