parse5 1.4.0 → 1.5.1
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/CHANGELOG.md +69 -57
- package/LICENSE +1 -1
- package/README.md +247 -246
- package/docs/build/index.md +963 -0
- package/lib/{tree_construction → common}/doctype.js +25 -0
- package/lib/serialization/serializer.js +178 -194
- package/lib/simple_api/simple_api_parser.js +107 -107
- package/lib/simple_api/tokenizer_proxy.js +122 -122
- package/lib/tokenization/location_info_mixin.js +80 -80
- package/lib/tokenization/named_entity_trie.js +1 -54
- package/lib/tokenization/preprocessor.js +115 -115
- package/lib/tree_adapters/htmlparser2.js +4 -12
- package/lib/tree_construction/location_info_mixin.js +197 -166
- package/lib/tree_construction/parser.js +2827 -2827
- package/package.json +47 -46
|
@@ -1,115 +1,115 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var UNICODE = require('../common/unicode');
|
|
4
|
-
|
|
5
|
-
//Aliases
|
|
6
|
-
var $ = UNICODE.CODE_POINTS;
|
|
7
|
-
|
|
8
|
-
//Utils
|
|
9
|
-
|
|
10
|
-
//OPTIMIZATION: these utility functions should not be moved out of this module. V8 Crankshaft will not inline
|
|
11
|
-
//this functions if they will be situated in another module due to context switch.
|
|
12
|
-
//Always perform inlining check before modifying this functions ('node --trace-inlining').
|
|
13
|
-
function isReservedCodePoint(cp) {
|
|
14
|
-
return cp >= 0xD800 && cp <= 0xDFFF || cp > 0x10FFFF;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function isSurrogatePair(cp1, cp2) {
|
|
18
|
-
return cp1 >= 0xD800 && cp1 <= 0xDBFF && cp2 >= 0xDC00 && cp2 <= 0xDFFF;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
function getSurrogatePairCodePoint(cp1, cp2) {
|
|
22
|
-
return (cp1 - 0xD800) * 0x400 + 0x2400 + cp2;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
//Preprocessor
|
|
26
|
-
//NOTE: HTML input preprocessing
|
|
27
|
-
//(see: http://www.whatwg.org/specs/web-apps/current-work/multipage/parsing.html#preprocessing-the-input-stream)
|
|
28
|
-
var Preprocessor = module.exports = function (html) {
|
|
29
|
-
this.write(html);
|
|
30
|
-
|
|
31
|
-
//NOTE: one leading U+FEFF BYTE ORDER MARK character must be ignored if any are present in the input stream.
|
|
32
|
-
this.pos = this.html.charCodeAt(0) === $.BOM ? 0 : -1;
|
|
33
|
-
|
|
34
|
-
this.gapStack = [];
|
|
35
|
-
this.lastGapPos = -1;
|
|
36
|
-
this.skipNextNewLine = false;
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
Preprocessor.prototype.write = function (html) {
|
|
40
|
-
if (this.html) {
|
|
41
|
-
this.html = this.html.substring(0, this.pos + 1) +
|
|
42
|
-
html +
|
|
43
|
-
this.html.substring(this.pos + 1, this.html.length);
|
|
44
|
-
|
|
45
|
-
}
|
|
46
|
-
else
|
|
47
|
-
this.html = html;
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
this.lastCharPos = this.html.length - 1;
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
Preprocessor.prototype.advanceAndPeekCodePoint = function () {
|
|
54
|
-
this.pos++;
|
|
55
|
-
|
|
56
|
-
if (this.pos > this.lastCharPos)
|
|
57
|
-
return $.EOF;
|
|
58
|
-
|
|
59
|
-
var cp = this.html.charCodeAt(this.pos);
|
|
60
|
-
|
|
61
|
-
//NOTE: any U+000A LINE FEED (LF) characters that immediately follow a U+000D CARRIAGE RETURN (CR) character
|
|
62
|
-
//must be ignored.
|
|
63
|
-
if (this.skipNextNewLine && cp === $.LINE_FEED) {
|
|
64
|
-
this.skipNextNewLine = false;
|
|
65
|
-
this._addGap();
|
|
66
|
-
return this.advanceAndPeekCodePoint();
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
//NOTE: all U+000D CARRIAGE RETURN (CR) characters must be converted to U+000A LINE FEED (LF) characters
|
|
70
|
-
if (cp === $.CARRIAGE_RETURN) {
|
|
71
|
-
this.skipNextNewLine = true;
|
|
72
|
-
return $.LINE_FEED;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
this.skipNextNewLine = false;
|
|
76
|
-
|
|
77
|
-
//OPTIMIZATION: first perform check if the code point in the allowed range that covers most common
|
|
78
|
-
//HTML input (e.g. ASCII codes) to avoid performance-cost operations for high-range code points.
|
|
79
|
-
return cp >= 0xD800 ? this._processHighRangeCodePoint(cp) : cp;
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
Preprocessor.prototype._processHighRangeCodePoint = function (cp) {
|
|
83
|
-
//NOTE: try to peek a surrogate pair
|
|
84
|
-
if (this.pos !== this.lastCharPos) {
|
|
85
|
-
var nextCp = this.html.charCodeAt(this.pos + 1);
|
|
86
|
-
|
|
87
|
-
if (isSurrogatePair(cp, nextCp)) {
|
|
88
|
-
//NOTE: we have a surrogate pair. Peek pair character and recalculate code point.
|
|
89
|
-
this.pos++;
|
|
90
|
-
cp = getSurrogatePairCodePoint(cp, nextCp);
|
|
91
|
-
|
|
92
|
-
//NOTE: add gap that should be avoided during retreat
|
|
93
|
-
this._addGap();
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
if (isReservedCodePoint(cp))
|
|
98
|
-
cp = $.REPLACEMENT_CHARACTER;
|
|
99
|
-
|
|
100
|
-
return cp;
|
|
101
|
-
};
|
|
102
|
-
|
|
103
|
-
Preprocessor.prototype._addGap = function () {
|
|
104
|
-
this.gapStack.push(this.lastGapPos);
|
|
105
|
-
this.lastGapPos = this.pos;
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
Preprocessor.prototype.retreat = function () {
|
|
109
|
-
if (this.pos === this.lastGapPos) {
|
|
110
|
-
this.lastGapPos = this.gapStack.pop();
|
|
111
|
-
this.pos--;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
this.pos--;
|
|
115
|
-
};
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var UNICODE = require('../common/unicode');
|
|
4
|
+
|
|
5
|
+
//Aliases
|
|
6
|
+
var $ = UNICODE.CODE_POINTS;
|
|
7
|
+
|
|
8
|
+
//Utils
|
|
9
|
+
|
|
10
|
+
//OPTIMIZATION: these utility functions should not be moved out of this module. V8 Crankshaft will not inline
|
|
11
|
+
//this functions if they will be situated in another module due to context switch.
|
|
12
|
+
//Always perform inlining check before modifying this functions ('node --trace-inlining').
|
|
13
|
+
function isReservedCodePoint(cp) {
|
|
14
|
+
return cp >= 0xD800 && cp <= 0xDFFF || cp > 0x10FFFF;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function isSurrogatePair(cp1, cp2) {
|
|
18
|
+
return cp1 >= 0xD800 && cp1 <= 0xDBFF && cp2 >= 0xDC00 && cp2 <= 0xDFFF;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function getSurrogatePairCodePoint(cp1, cp2) {
|
|
22
|
+
return (cp1 - 0xD800) * 0x400 + 0x2400 + cp2;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
//Preprocessor
|
|
26
|
+
//NOTE: HTML input preprocessing
|
|
27
|
+
//(see: http://www.whatwg.org/specs/web-apps/current-work/multipage/parsing.html#preprocessing-the-input-stream)
|
|
28
|
+
var Preprocessor = module.exports = function (html) {
|
|
29
|
+
this.write(html);
|
|
30
|
+
|
|
31
|
+
//NOTE: one leading U+FEFF BYTE ORDER MARK character must be ignored if any are present in the input stream.
|
|
32
|
+
this.pos = this.html.charCodeAt(0) === $.BOM ? 0 : -1;
|
|
33
|
+
|
|
34
|
+
this.gapStack = [];
|
|
35
|
+
this.lastGapPos = -1;
|
|
36
|
+
this.skipNextNewLine = false;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
Preprocessor.prototype.write = function (html) {
|
|
40
|
+
if (this.html) {
|
|
41
|
+
this.html = this.html.substring(0, this.pos + 1) +
|
|
42
|
+
html +
|
|
43
|
+
this.html.substring(this.pos + 1, this.html.length);
|
|
44
|
+
|
|
45
|
+
}
|
|
46
|
+
else
|
|
47
|
+
this.html = html;
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
this.lastCharPos = this.html.length - 1;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
Preprocessor.prototype.advanceAndPeekCodePoint = function () {
|
|
54
|
+
this.pos++;
|
|
55
|
+
|
|
56
|
+
if (this.pos > this.lastCharPos)
|
|
57
|
+
return $.EOF;
|
|
58
|
+
|
|
59
|
+
var cp = this.html.charCodeAt(this.pos);
|
|
60
|
+
|
|
61
|
+
//NOTE: any U+000A LINE FEED (LF) characters that immediately follow a U+000D CARRIAGE RETURN (CR) character
|
|
62
|
+
//must be ignored.
|
|
63
|
+
if (this.skipNextNewLine && cp === $.LINE_FEED) {
|
|
64
|
+
this.skipNextNewLine = false;
|
|
65
|
+
this._addGap();
|
|
66
|
+
return this.advanceAndPeekCodePoint();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
//NOTE: all U+000D CARRIAGE RETURN (CR) characters must be converted to U+000A LINE FEED (LF) characters
|
|
70
|
+
if (cp === $.CARRIAGE_RETURN) {
|
|
71
|
+
this.skipNextNewLine = true;
|
|
72
|
+
return $.LINE_FEED;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
this.skipNextNewLine = false;
|
|
76
|
+
|
|
77
|
+
//OPTIMIZATION: first perform check if the code point in the allowed range that covers most common
|
|
78
|
+
//HTML input (e.g. ASCII codes) to avoid performance-cost operations for high-range code points.
|
|
79
|
+
return cp >= 0xD800 ? this._processHighRangeCodePoint(cp) : cp;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
Preprocessor.prototype._processHighRangeCodePoint = function (cp) {
|
|
83
|
+
//NOTE: try to peek a surrogate pair
|
|
84
|
+
if (this.pos !== this.lastCharPos) {
|
|
85
|
+
var nextCp = this.html.charCodeAt(this.pos + 1);
|
|
86
|
+
|
|
87
|
+
if (isSurrogatePair(cp, nextCp)) {
|
|
88
|
+
//NOTE: we have a surrogate pair. Peek pair character and recalculate code point.
|
|
89
|
+
this.pos++;
|
|
90
|
+
cp = getSurrogatePairCodePoint(cp, nextCp);
|
|
91
|
+
|
|
92
|
+
//NOTE: add gap that should be avoided during retreat
|
|
93
|
+
this._addGap();
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (isReservedCodePoint(cp))
|
|
98
|
+
cp = $.REPLACEMENT_CHARACTER;
|
|
99
|
+
|
|
100
|
+
return cp;
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
Preprocessor.prototype._addGap = function () {
|
|
104
|
+
this.gapStack.push(this.lastGapPos);
|
|
105
|
+
this.lastGapPos = this.pos;
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
Preprocessor.prototype.retreat = function () {
|
|
109
|
+
if (this.pos === this.lastGapPos) {
|
|
110
|
+
this.lastGapPos = this.gapStack.pop();
|
|
111
|
+
this.pos--;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
this.pos--;
|
|
115
|
+
};
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
var Doctype = require('../common/doctype');
|
|
4
|
+
|
|
3
5
|
//Conversion tables for DOM Level1 structure emulation
|
|
4
6
|
var nodeTypes = {
|
|
5
7
|
element: 1,
|
|
@@ -119,18 +121,8 @@ var createTextNode = function (value) {
|
|
|
119
121
|
|
|
120
122
|
//Tree mutation
|
|
121
123
|
exports.setDocumentType = function (document, name, publicId, systemId) {
|
|
122
|
-
var data =
|
|
123
|
-
|
|
124
|
-
if (name)
|
|
125
|
-
data += ' ' + name;
|
|
126
|
-
|
|
127
|
-
if (publicId)
|
|
128
|
-
data += ' PUBLIC "' + publicId + '"';
|
|
129
|
-
|
|
130
|
-
if (systemId)
|
|
131
|
-
data += ' "' + systemId + '"';
|
|
132
|
-
|
|
133
|
-
var doctypeNode = null;
|
|
124
|
+
var data = Doctype.serializeContent(name, publicId, systemId),
|
|
125
|
+
doctypeNode = null;
|
|
134
126
|
|
|
135
127
|
for (var i = 0; i < document.children.length; i++) {
|
|
136
128
|
if (document.children[i].type === 'directive' && document.children[i].name === '!doctype') {
|
|
@@ -1,166 +1,197 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var OpenElementStack = require('./open_element_stack'),
|
|
4
|
-
Tokenizer = require('../tokenization/tokenizer'),
|
|
5
|
-
HTML = require('../common/html');
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
//Aliases
|
|
9
|
-
var $ = HTML.TAG_NAMES;
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
function
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
};
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
parserProto.
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
parserProto.
|
|
146
|
-
|
|
147
|
-
var
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
};
|
|
166
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var OpenElementStack = require('./open_element_stack'),
|
|
4
|
+
Tokenizer = require('../tokenization/tokenizer'),
|
|
5
|
+
HTML = require('../common/html');
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
//Aliases
|
|
9
|
+
var $ = HTML.TAG_NAMES;
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
function setEndLocation(element, closingToken, treeAdapter) {
|
|
13
|
+
var loc = element.__location;
|
|
14
|
+
|
|
15
|
+
if (!loc)
|
|
16
|
+
return;
|
|
17
|
+
|
|
18
|
+
if (!loc.startTag) {
|
|
19
|
+
loc.startTag = {
|
|
20
|
+
start: loc.start,
|
|
21
|
+
end: loc.end
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (closingToken.location) {
|
|
26
|
+
var tn = treeAdapter.getTagName(element),
|
|
27
|
+
// NOTE: For cases like <p> <p> </p> - First 'p' closes without a closing tag and
|
|
28
|
+
// for cases like <td> <p> </td> - 'p' closes without a closing tag
|
|
29
|
+
isClosingEndTag = closingToken.type === Tokenizer.END_TAG_TOKEN &&
|
|
30
|
+
tn === closingToken.tagName;
|
|
31
|
+
|
|
32
|
+
if (isClosingEndTag) {
|
|
33
|
+
loc.endTag = {
|
|
34
|
+
start: closingToken.location.start,
|
|
35
|
+
end: closingToken.location.end
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
loc.end = closingToken.location.end;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
//NOTE: patch open elements stack, so we can assign end location for the elements
|
|
44
|
+
function patchOpenElementsStack(stack, parser) {
|
|
45
|
+
var treeAdapter = parser.treeAdapter;
|
|
46
|
+
|
|
47
|
+
stack.pop = function () {
|
|
48
|
+
setEndLocation(this.current, parser.currentToken, treeAdapter);
|
|
49
|
+
OpenElementStack.prototype.pop.call(this);
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
stack.popAllUpToHtmlElement = function () {
|
|
53
|
+
for (var i = this.stackTop; i > 0; i--)
|
|
54
|
+
setEndLocation(this.items[i], parser.currentToken, treeAdapter);
|
|
55
|
+
|
|
56
|
+
OpenElementStack.prototype.popAllUpToHtmlElement.call(this);
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
stack.remove = function (element) {
|
|
60
|
+
setEndLocation(element, parser.currentToken, treeAdapter);
|
|
61
|
+
OpenElementStack.prototype.remove.call(this, element);
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
exports.assign = function (parser) {
|
|
66
|
+
//NOTE: obtain Parser proto this way to avoid module circular references
|
|
67
|
+
var parserProto = Object.getPrototypeOf(parser),
|
|
68
|
+
treeAdapter = parser.treeAdapter;
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
//NOTE: patch _reset method
|
|
72
|
+
parser._reset = function (html, document, fragmentContext) {
|
|
73
|
+
parserProto._reset.call(this, html, document, fragmentContext);
|
|
74
|
+
|
|
75
|
+
this.attachableElementLocation = null;
|
|
76
|
+
this.lastFosterParentingLocation = null;
|
|
77
|
+
this.currentToken = null;
|
|
78
|
+
|
|
79
|
+
patchOpenElementsStack(this.openElements, parser);
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
parser._processTokenInForeignContent = function (token) {
|
|
83
|
+
this.currentToken = token;
|
|
84
|
+
parserProto._processTokenInForeignContent.call(this, token);
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
parser._processToken = function (token) {
|
|
88
|
+
this.currentToken = token;
|
|
89
|
+
parserProto._processToken.call(this, token);
|
|
90
|
+
|
|
91
|
+
//NOTE: <body> and <html> are never popped from the stack, so we need to updated
|
|
92
|
+
//their end location explicitly.
|
|
93
|
+
if (token.type === Tokenizer.END_TAG_TOKEN &&
|
|
94
|
+
(token.tagName === $.HTML ||
|
|
95
|
+
(token.tagName === $.BODY && this.openElements.hasInScope($.BODY)))) {
|
|
96
|
+
for (var i = this.openElements.stackTop; i >= 0; i--) {
|
|
97
|
+
var element = this.openElements.items[i];
|
|
98
|
+
|
|
99
|
+
if (this.treeAdapter.getTagName(element) === token.tagName) {
|
|
100
|
+
setEndLocation(element, token, treeAdapter);
|
|
101
|
+
break;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
//Doctype
|
|
108
|
+
parser._setDocumentType = function (token) {
|
|
109
|
+
parserProto._setDocumentType.call(this, token);
|
|
110
|
+
|
|
111
|
+
var documentChildren = this.treeAdapter.getChildNodes(this.document),
|
|
112
|
+
cnLength = documentChildren.length;
|
|
113
|
+
|
|
114
|
+
for (var i = 0; i < cnLength; i++) {
|
|
115
|
+
var node = documentChildren[i];
|
|
116
|
+
|
|
117
|
+
if (this.treeAdapter.isDocumentTypeNode(node)) {
|
|
118
|
+
node.__location = token.location;
|
|
119
|
+
break;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
//Elements
|
|
125
|
+
parser._attachElementToTree = function (element) {
|
|
126
|
+
//NOTE: _attachElementToTree is called from _appendElement, _insertElement and _insertTemplate methods.
|
|
127
|
+
//So we will use token location stored in this methods for the element.
|
|
128
|
+
element.__location = this.attachableElementLocation || null;
|
|
129
|
+
this.attachableElementLocation = null;
|
|
130
|
+
parserProto._attachElementToTree.call(this, element);
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
parser._appendElement = function (token, namespaceURI) {
|
|
134
|
+
this.attachableElementLocation = token.location;
|
|
135
|
+
parserProto._appendElement.call(this, token, namespaceURI);
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
parser._insertElement = function (token, namespaceURI) {
|
|
139
|
+
this.attachableElementLocation = token.location;
|
|
140
|
+
parserProto._insertElement.call(this, token, namespaceURI);
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
parser._insertTemplate = function (token) {
|
|
144
|
+
this.attachableElementLocation = token.location;
|
|
145
|
+
parserProto._insertTemplate.call(this, token);
|
|
146
|
+
|
|
147
|
+
var tmplContent = this.treeAdapter.getChildNodes(this.openElements.current)[0];
|
|
148
|
+
|
|
149
|
+
tmplContent.__location = null;
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
parser._insertFakeRootElement = function () {
|
|
153
|
+
parserProto._insertFakeRootElement.call(this);
|
|
154
|
+
this.openElements.current.__location = null;
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
//Comments
|
|
158
|
+
parser._appendCommentNode = function (token, parent) {
|
|
159
|
+
parserProto._appendCommentNode.call(this, token, parent);
|
|
160
|
+
|
|
161
|
+
var children = this.treeAdapter.getChildNodes(parent),
|
|
162
|
+
commentNode = children[children.length - 1];
|
|
163
|
+
|
|
164
|
+
commentNode.__location = token.location;
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
//Text
|
|
168
|
+
parser._findFosterParentingLocation = function () {
|
|
169
|
+
//NOTE: store last foster parenting location, so we will be able to find inserted text
|
|
170
|
+
//in case of foster parenting
|
|
171
|
+
this.lastFosterParentingLocation = parserProto._findFosterParentingLocation.call(this);
|
|
172
|
+
return this.lastFosterParentingLocation;
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
parser._insertCharacters = function (token) {
|
|
176
|
+
parserProto._insertCharacters.call(this, token);
|
|
177
|
+
|
|
178
|
+
var hasFosterParent = this._shouldFosterParentOnInsertion(),
|
|
179
|
+
parentingLocation = this.lastFosterParentingLocation,
|
|
180
|
+
parent = (hasFosterParent && parentingLocation.parent) ||
|
|
181
|
+
this.openElements.currentTmplContent ||
|
|
182
|
+
this.openElements.current,
|
|
183
|
+
siblings = this.treeAdapter.getChildNodes(parent),
|
|
184
|
+
textNodeIdx = hasFosterParent && parentingLocation.beforeElement ?
|
|
185
|
+
siblings.indexOf(parentingLocation.beforeElement) - 1 :
|
|
186
|
+
siblings.length - 1,
|
|
187
|
+
textNode = siblings[textNodeIdx];
|
|
188
|
+
|
|
189
|
+
//NOTE: if we have location assigned by another token, then just update end position
|
|
190
|
+
if (textNode.__location)
|
|
191
|
+
textNode.__location.end = token.location.end;
|
|
192
|
+
|
|
193
|
+
else
|
|
194
|
+
textNode.__location = token.location;
|
|
195
|
+
};
|
|
196
|
+
};
|
|
197
|
+
|