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.
@@ -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
- //Preprocessor
22
- //NOTE: HTML input preprocessing
23
- //(see: http://www.whatwg.org/specs/web-apps/current-work/multipage/parsing.html#preprocessing-the-input-stream)
24
- var Preprocessor = module.exports = function () {
25
- this.html = null;
26
-
27
- this.pos = -1;
28
- this.lastGapPos = -1;
29
-
30
- this.gapStack = [];
31
-
32
- this.skipNextNewLine = false;
33
-
34
- this.lastChunkWritten = false;
35
- this.endOfChunkHit = false;
36
- };
37
-
38
-
39
- Preprocessor.prototype._addGap = function () {
40
- this.gapStack.push(this.lastGapPos);
41
- this.lastGapPos = this.pos;
42
- };
43
-
44
- Preprocessor.prototype._processHighRangeCodePoint = function (cp) {
45
- //NOTE: try to peek a surrogate pair
46
- if (this.pos !== this.lastCharPos) {
47
- var nextCp = this.html.charCodeAt(this.pos + 1);
48
-
49
- if (isSurrogatePair(cp, nextCp)) {
50
- //NOTE: we have a surrogate pair. Peek pair character and recalculate code point.
51
- this.pos++;
52
- cp = getSurrogatePairCodePoint(cp, nextCp);
53
-
54
- //NOTE: add gap that should be avoided during retreat
55
- this._addGap();
56
- }
57
- }
58
-
59
- // NOTE: we've hit the end of chunk, stop processing at this point
60
- else if (!this.lastChunkWritten) {
61
- this.endOfChunkHit = true;
62
- return $.EOF;
63
- }
64
-
65
- return cp;
66
- };
67
-
68
- Preprocessor.prototype.write = function (chunk, isLastChunk) {
69
- if (this.html)
70
- this.html += chunk;
71
-
72
- else
73
- this.html = chunk;
74
-
75
- this.lastCharPos = this.html.length - 1;
76
- this.endOfChunkHit = false;
77
- this.lastChunkWritten = isLastChunk;
78
- };
79
-
80
- Preprocessor.prototype.insertHtmlAtCurrentPos = function (chunk) {
81
- this.html = this.html.substring(0, this.pos + 1) +
82
- chunk +
83
- this.html.substring(this.pos + 1, this.html.length);
84
-
85
- this.lastCharPos = this.html.length - 1;
86
- this.endOfChunkHit = false;
87
- };
88
-
89
-
90
- Preprocessor.prototype.advance = function () {
91
- this.pos++;
92
-
93
- if (this.pos > this.lastCharPos) {
94
- if (!this.lastChunkWritten)
95
- this.endOfChunkHit = true;
96
-
97
- return $.EOF;
98
- }
99
-
100
- var cp = this.html.charCodeAt(this.pos);
101
-
102
- //NOTE: any U+000A LINE FEED (LF) characters that immediately follow a U+000D CARRIAGE RETURN (CR) character
103
- //must be ignored.
104
- if (this.skipNextNewLine && cp === $.LINE_FEED) {
105
- this.skipNextNewLine = false;
106
- this._addGap();
107
- return this.advance();
108
- }
109
-
110
- //NOTE: all U+000D CARRIAGE RETURN (CR) characters must be converted to U+000A LINE FEED (LF) characters
111
- if (cp === $.CARRIAGE_RETURN) {
112
- this.skipNextNewLine = true;
113
- return $.LINE_FEED;
114
- }
115
-
116
- this.skipNextNewLine = false;
117
-
118
- //OPTIMIZATION: first perform check if the code point in the allowed range that covers most common
119
- //HTML input (e.g. ASCII codes) to avoid performance-cost operations for high-range code points.
120
- return cp >= 0xD800 ? this._processHighRangeCodePoint(cp) : cp;
121
- };
122
-
123
- Preprocessor.prototype.retreat = function () {
124
- if (this.pos === this.lastGapPos) {
125
- this.lastGapPos = this.gapStack.pop();
126
- this.pos--;
127
- }
128
-
129
- this.pos--;
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
+