parse5 2.1.5 → 2.2.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.
- package/LICENSE +19 -19
- package/README.md +40 -40
- package/lib/common/doctype.js +1 -1
- package/lib/common/foreign_content.js +260 -250
- package/lib/common/html.js +0 -3
- package/lib/index.js +108 -108
- package/lib/location_info/parser_mixin.js +4 -1
- package/lib/location_info/tokenizer_mixin.js +169 -168
- package/lib/parser/index.js +2817 -2864
- package/lib/parser/open_element_stack.js +395 -382
- package/lib/sax/dev_null_stream.js +14 -14
- package/lib/sax/index.js +3 -5
- package/lib/sax/parser_feedback_simulator.js +153 -116
- package/lib/serializer/index.js +180 -180
- package/lib/serializer/stream.js +49 -49
- package/lib/tokenizer/index.js +69 -361
- package/lib/tokenizer/preprocessor.js +155 -131
- package/lib/tree_adapters/htmlparser2.js +327 -327
- package/package.json +7 -6
|
@@ -1,131 +1,155 @@
|
|
|
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 isSurrogatePair(cp1, cp2) {
|
|
14
|
-
return cp1 >= 0xD800 && cp1 <= 0xDBFF && cp2 >= 0xDC00 && cp2 <= 0xDFFF;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function getSurrogatePairCodePoint(cp1, cp2) {
|
|
18
|
-
return (cp1 - 0xD800) * 0x400 + 0x2400 + cp2;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
//
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
this.
|
|
31
|
-
|
|
32
|
-
this.
|
|
33
|
-
|
|
34
|
-
this.
|
|
35
|
-
this.
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
this.
|
|
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
|
-
Preprocessor.prototype.
|
|
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
|
-
if (this.
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
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 isSurrogatePair(cp1, cp2) {
|
|
14
|
+
return cp1 >= 0xD800 && cp1 <= 0xDBFF && cp2 >= 0xDC00 && cp2 <= 0xDFFF;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function getSurrogatePairCodePoint(cp1, cp2) {
|
|
18
|
+
return (cp1 - 0xD800) * 0x400 + 0x2400 + cp2;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
//Const
|
|
23
|
+
var DEFAULT_BUFFER_WATERLINE = 1 << 16;
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
//Preprocessor
|
|
27
|
+
//NOTE: HTML input preprocessing
|
|
28
|
+
//(see: http://www.whatwg.org/specs/web-apps/current-work/multipage/parsing.html#preprocessing-the-input-stream)
|
|
29
|
+
var Preprocessor = module.exports = function () {
|
|
30
|
+
this.html = null;
|
|
31
|
+
|
|
32
|
+
this.pos = -1;
|
|
33
|
+
this.lastGapPos = -1;
|
|
34
|
+
this.lastCharPos = -1;
|
|
35
|
+
this.droppedBufferSize = 0;
|
|
36
|
+
|
|
37
|
+
this.gapStack = [];
|
|
38
|
+
|
|
39
|
+
this.skipNextNewLine = false;
|
|
40
|
+
|
|
41
|
+
this.lastChunkWritten = false;
|
|
42
|
+
this.endOfChunkHit = false;
|
|
43
|
+
this.bufferWaterline = DEFAULT_BUFFER_WATERLINE;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
Object.defineProperty(Preprocessor.prototype, 'sourcePos', {
|
|
47
|
+
get: function () {
|
|
48
|
+
return this.droppedBufferSize + this.pos;
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
Preprocessor.prototype.dropParsedChunk = function () {
|
|
53
|
+
if (this.pos > this.bufferWaterline) {
|
|
54
|
+
this.lastCharPos -= this.pos;
|
|
55
|
+
this.droppedBufferSize += this.pos;
|
|
56
|
+
this.html = this.html.substring(this.pos);
|
|
57
|
+
this.pos = 0;
|
|
58
|
+
this.lastGapPos = -1;
|
|
59
|
+
this.gapStack = [];
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
Preprocessor.prototype._addGap = function () {
|
|
64
|
+
this.gapStack.push(this.lastGapPos);
|
|
65
|
+
this.lastGapPos = this.pos;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
Preprocessor.prototype._processHighRangeCodePoint = function (cp) {
|
|
69
|
+
//NOTE: try to peek a surrogate pair
|
|
70
|
+
if (this.pos !== this.lastCharPos) {
|
|
71
|
+
var nextCp = this.html.charCodeAt(this.pos + 1);
|
|
72
|
+
|
|
73
|
+
if (isSurrogatePair(cp, nextCp)) {
|
|
74
|
+
//NOTE: we have a surrogate pair. Peek pair character and recalculate code point.
|
|
75
|
+
this.pos++;
|
|
76
|
+
cp = getSurrogatePairCodePoint(cp, nextCp);
|
|
77
|
+
|
|
78
|
+
//NOTE: add gap that should be avoided during retreat
|
|
79
|
+
this._addGap();
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// NOTE: we've hit the end of chunk, stop processing at this point
|
|
84
|
+
else if (!this.lastChunkWritten) {
|
|
85
|
+
this.endOfChunkHit = true;
|
|
86
|
+
return $.EOF;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return cp;
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
Preprocessor.prototype.write = function (chunk, isLastChunk) {
|
|
93
|
+
if (this.html)
|
|
94
|
+
this.html += chunk;
|
|
95
|
+
|
|
96
|
+
else
|
|
97
|
+
this.html = chunk;
|
|
98
|
+
|
|
99
|
+
this.lastCharPos = this.html.length - 1;
|
|
100
|
+
this.endOfChunkHit = false;
|
|
101
|
+
this.lastChunkWritten = isLastChunk;
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
Preprocessor.prototype.insertHtmlAtCurrentPos = function (chunk) {
|
|
105
|
+
this.html = this.html.substring(0, this.pos + 1) +
|
|
106
|
+
chunk +
|
|
107
|
+
this.html.substring(this.pos + 1, this.html.length);
|
|
108
|
+
|
|
109
|
+
this.lastCharPos = this.html.length - 1;
|
|
110
|
+
this.endOfChunkHit = false;
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
Preprocessor.prototype.advance = function () {
|
|
115
|
+
this.pos++;
|
|
116
|
+
|
|
117
|
+
if (this.pos > this.lastCharPos) {
|
|
118
|
+
if (!this.lastChunkWritten)
|
|
119
|
+
this.endOfChunkHit = true;
|
|
120
|
+
|
|
121
|
+
return $.EOF;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
var cp = this.html.charCodeAt(this.pos);
|
|
125
|
+
|
|
126
|
+
//NOTE: any U+000A LINE FEED (LF) characters that immediately follow a U+000D CARRIAGE RETURN (CR) character
|
|
127
|
+
//must be ignored.
|
|
128
|
+
if (this.skipNextNewLine && cp === $.LINE_FEED) {
|
|
129
|
+
this.skipNextNewLine = false;
|
|
130
|
+
this._addGap();
|
|
131
|
+
return this.advance();
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
//NOTE: all U+000D CARRIAGE RETURN (CR) characters must be converted to U+000A LINE FEED (LF) characters
|
|
135
|
+
if (cp === $.CARRIAGE_RETURN) {
|
|
136
|
+
this.skipNextNewLine = true;
|
|
137
|
+
return $.LINE_FEED;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
this.skipNextNewLine = false;
|
|
141
|
+
|
|
142
|
+
//OPTIMIZATION: first perform check if the code point in the allowed range that covers most common
|
|
143
|
+
//HTML input (e.g. ASCII codes) to avoid performance-cost operations for high-range code points.
|
|
144
|
+
return cp >= 0xD800 ? this._processHighRangeCodePoint(cp) : cp;
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
Preprocessor.prototype.retreat = function () {
|
|
148
|
+
if (this.pos === this.lastGapPos) {
|
|
149
|
+
this.lastGapPos = this.gapStack.pop();
|
|
150
|
+
this.pos--;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
this.pos--;
|
|
154
|
+
};
|
|
155
|
+
|