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.
@@ -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 = '!DOCTYPE';
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
- //NOTE: patch open elements stack, so we can assign end location for the elements
12
- function patchOpenElementsStack(stack, parser) {
13
- function setEndLocation(element) {
14
- if (element.__location)
15
- element.__location.end = parser.currentToken.location.end;
16
- }
17
-
18
- stack.pop = function () {
19
- setEndLocation(this.current);
20
- OpenElementStack.prototype.pop.call(this);
21
- };
22
-
23
- stack.popAllUpToHtmlElement = function () {
24
- for (var i = this.stackTop; i > 0; i--)
25
- setEndLocation(this.items[i]);
26
-
27
- OpenElementStack.prototype.popAllUpToHtmlElement.call(this);
28
- };
29
-
30
- stack.remove = function (element) {
31
- setEndLocation(element);
32
- OpenElementStack.prototype.remove.call(this, element);
33
- };
34
- }
35
-
36
- exports.assign = function (parser) {
37
- //NOTE: obtain Parser proto this way to avoid module circular references
38
- var parserProto = Object.getPrototypeOf(parser);
39
-
40
- //NOTE: patch _reset method
41
- parser._reset = function (html, document, fragmentContext) {
42
- parserProto._reset.call(this, html, document, fragmentContext);
43
-
44
- this.attachableElementLocation = null;
45
- this.lastFosterParentingLocation = null;
46
- this.currentToken = null;
47
-
48
- patchOpenElementsStack(this.openElements, parser);
49
- };
50
-
51
- parser._processTokenInForeignContent = function (token) {
52
- this.currentToken = token;
53
- parserProto._processTokenInForeignContent.call(this, token);
54
- };
55
-
56
- parser._processToken = function (token) {
57
- this.currentToken = token;
58
- parserProto._processToken.call(this, token);
59
-
60
- //NOTE: <body> and <html> are never popped from the stack, so we need to updated
61
- //their end location explicitly.
62
- if (token.type === Tokenizer.END_TAG_TOKEN &&
63
- (token.tagName === $.HTML ||
64
- (token.tagName === $.BODY && this.openElements.hasInScope($.BODY)))) {
65
- for (var i = this.openElements.stackTop; i >= 0; i--) {
66
- var element = this.openElements.items[i];
67
-
68
- if (this.treeAdapter.getTagName(element) === token.tagName) {
69
- element.__location.end = token.location.end;
70
- break;
71
- }
72
- }
73
- }
74
- };
75
-
76
- //Doctype
77
- parser._setDocumentType = function (token) {
78
- parserProto._setDocumentType.call(this, token);
79
-
80
- var documentChildren = this.treeAdapter.getChildNodes(this.document),
81
- cnLength = documentChildren.length;
82
-
83
- for (var i = 0; i < cnLength; i++) {
84
- var node = documentChildren[i];
85
-
86
- if (this.treeAdapter.isDocumentTypeNode(node)) {
87
- node.__location = token.location;
88
- break;
89
- }
90
- }
91
- };
92
-
93
- //Elements
94
- parser._attachElementToTree = function (element) {
95
- //NOTE: _attachElementToTree is called from _appendElement, _insertElement and _insertTemplate methods.
96
- //So we will use token location stored in this methods for the element.
97
- element.__location = this.attachableElementLocation || null;
98
- this.attachableElementLocation = null;
99
- parserProto._attachElementToTree.call(this, element);
100
- };
101
-
102
- parser._appendElement = function (token, namespaceURI) {
103
- this.attachableElementLocation = token.location;
104
- parserProto._appendElement.call(this, token, namespaceURI);
105
- };
106
-
107
- parser._insertElement = function (token, namespaceURI) {
108
- this.attachableElementLocation = token.location;
109
- parserProto._insertElement.call(this, token, namespaceURI);
110
- };
111
-
112
- parser._insertTemplate = function (token) {
113
- this.attachableElementLocation = token.location;
114
- parserProto._insertTemplate.call(this, token);
115
-
116
- var tmplContent = this.treeAdapter.getChildNodes(this.openElements.current)[0];
117
-
118
- tmplContent.__location = null;
119
- };
120
-
121
- parser._insertFakeRootElement = function () {
122
- parserProto._insertFakeRootElement.call(this);
123
- this.openElements.current.__location = null;
124
- };
125
-
126
- //Comments
127
- parser._appendCommentNode = function (token, parent) {
128
- parserProto._appendCommentNode.call(this, token, parent);
129
-
130
- var children = this.treeAdapter.getChildNodes(parent),
131
- commentNode = children[children.length - 1];
132
-
133
- commentNode.__location = token.location;
134
- };
135
-
136
- //Text
137
- parser._findFosterParentingLocation = function () {
138
- //NOTE: store last foster parenting location, so we will be able to find inserted text
139
- //in case of foster parenting
140
- this.lastFosterParentingLocation = parserProto._findFosterParentingLocation.call(this);
141
- return this.lastFosterParentingLocation;
142
- };
143
-
144
- parser._insertCharacters = function (token) {
145
- parserProto._insertCharacters.call(this, token);
146
-
147
- var hasFosterParent = this._shouldFosterParentOnInsertion(),
148
- parentingLocation = this.lastFosterParentingLocation,
149
- parent = (hasFosterParent && parentingLocation.parent) ||
150
- this.openElements.currentTmplContent ||
151
- this.openElements.current,
152
- siblings = this.treeAdapter.getChildNodes(parent),
153
- textNodeIdx = hasFosterParent && parentingLocation.beforeElement ?
154
- siblings.indexOf(parentingLocation.beforeElement) - 1 :
155
- siblings.length - 1,
156
- textNode = siblings[textNodeIdx];
157
-
158
- //NOTE: if we have location assigned by another token, then just update end position
159
- if (textNode.__location)
160
- textNode.__location.end = token.location.end;
161
-
162
- else
163
- textNode.__location = token.location;
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
+