parse5 3.0.3 → 5.1.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.
Files changed (43) hide show
  1. package/README.md +14 -16
  2. package/lib/common/doctype.js +125 -108
  3. package/lib/common/error-codes.js +65 -0
  4. package/lib/common/foreign-content.js +265 -0
  5. package/lib/common/html.js +102 -102
  6. package/lib/common/unicode.js +81 -19
  7. package/lib/extensions/error-reporting/mixin-base.js +43 -0
  8. package/lib/extensions/error-reporting/parser-mixin.js +52 -0
  9. package/lib/extensions/error-reporting/preprocessor-mixin.js +24 -0
  10. package/lib/extensions/error-reporting/tokenizer-mixin.js +17 -0
  11. package/lib/extensions/location-info/open-element-stack-mixin.js +35 -0
  12. package/lib/extensions/location-info/parser-mixin.js +222 -0
  13. package/lib/extensions/location-info/tokenizer-mixin.js +146 -0
  14. package/lib/extensions/position-tracking/preprocessor-mixin.js +64 -0
  15. package/lib/index.js +6 -21
  16. package/lib/parser/formatting-element-list.js +181 -0
  17. package/lib/parser/index.js +1469 -1332
  18. package/lib/parser/open-element-stack.js +482 -0
  19. package/lib/serializer/index.js +136 -122
  20. package/lib/tokenizer/index.js +1856 -1804
  21. package/lib/tokenizer/{named_entity_data.js → named-entity-data.js} +1 -1
  22. package/lib/tokenizer/preprocessor.js +115 -103
  23. package/lib/tree-adapters/default.js +217 -0
  24. package/lib/utils/{merge_options.js → merge-options.js} +2 -2
  25. package/lib/utils/mixin.js +33 -12
  26. package/package.json +32 -61
  27. package/LICENSE +0 -19
  28. package/lib/common/foreign_content.js +0 -260
  29. package/lib/extensions/location_info/open_element_stack_mixin.js +0 -34
  30. package/lib/extensions/location_info/parser_mixin.js +0 -213
  31. package/lib/extensions/location_info/tokenizer_mixin.js +0 -117
  32. package/lib/extensions/position_tracking/preprocessor_mixin.js +0 -72
  33. package/lib/index.d.ts +0 -1077
  34. package/lib/parser/formatting_element_list.js +0 -167
  35. package/lib/parser/open_element_stack.js +0 -395
  36. package/lib/parser/parser_stream.js +0 -76
  37. package/lib/parser/plain_text_conversion_stream.js +0 -20
  38. package/lib/sax/dev_null_stream.js +0 -14
  39. package/lib/sax/index.js +0 -118
  40. package/lib/sax/parser_feedback_simulator.js +0 -153
  41. package/lib/serializer/serializer_stream.js +0 -28
  42. package/lib/tree_adapters/default.js +0 -209
  43. package/lib/tree_adapters/htmlparser2.js +0 -338
@@ -1,18 +1,39 @@
1
1
  'use strict';
2
2
 
3
- var Mixin = module.exports = function (host) {
4
- var originalMethods = {},
5
- overriddenMethods = this._getOverriddenMethods(this, originalMethods);
6
-
7
- Object.keys(overriddenMethods).forEach(function (key) {
8
- if (typeof overriddenMethods[key] === 'function') {
9
- originalMethods[key] = host[key];
10
- host[key] = overriddenMethods[key];
3
+ class Mixin {
4
+ constructor(host) {
5
+ const originalMethods = {};
6
+ const overriddenMethods = this._getOverriddenMethods(this, originalMethods);
7
+
8
+ for (const key of Object.keys(overriddenMethods)) {
9
+ if (typeof overriddenMethods[key] === 'function') {
10
+ originalMethods[key] = host[key];
11
+ host[key] = overriddenMethods[key];
12
+ }
11
13
  }
12
- });
13
- };
14
+ }
15
+
16
+ _getOverriddenMethods() {
17
+ throw new Error('Not implemented');
18
+ }
19
+ }
20
+
21
+ Mixin.install = function(host, Ctor, opts) {
22
+ if (!host.__mixins) {
23
+ host.__mixins = [];
24
+ }
25
+
26
+ for (let i = 0; i < host.__mixins.length; i++) {
27
+ if (host.__mixins[i].constructor === Ctor) {
28
+ return host.__mixins[i];
29
+ }
30
+ }
31
+
32
+ const mixin = new Ctor(host, opts);
33
+
34
+ host.__mixins.push(mixin);
14
35
 
15
- Mixin.prototype._getOverriddenMethods = function () {
16
- throw new Error('Not implemented');
36
+ return mixin;
17
37
  };
18
38
 
39
+ module.exports = Mixin;
package/package.json CHANGED
@@ -1,63 +1,34 @@
1
1
  {
2
- "name": "parse5",
3
- "description": "HTML parsing/serialization toolset for Node.js. WHATWG HTML Living Standard (aka HTML5)-compliant.",
4
- "version": "3.0.3",
5
- "author": "Ivan Nikulin <ifaaan@gmail.com> (https://github.com/inikulin)",
6
- "contributors": "https://github.com/inikulin/parse5/graphs/contributors",
7
- "homepage": "https://github.com/inikulin/parse5",
8
- "devDependencies": {
9
- "del": "^2.0.2",
10
- "gulp": "^3.9.0",
11
- "gulp-benchmark": "^1.1.1",
12
- "gulp-download": "0.0.1",
13
- "gulp-eslint": "^3.0.1",
14
- "gulp-install": "^0.6.0",
15
- "gulp-mocha": "^2.1.3",
16
- "gulp-rename": "^1.2.2",
17
- "gulp-typedoc": "^2.0.0",
18
- "gulp-typescript": "^3.1.2",
19
- "publish-please": "^2.2.0",
20
- "through2": "^2.0.0",
21
- "typedoc": "^0.5.1",
22
- "typescript": "^2.0.6"
23
- },
24
- "keywords": [
25
- "html",
26
- "parser",
27
- "html5",
28
- "WHATWG",
29
- "specification",
30
- "fast",
31
- "html parser",
32
- "html5 parser",
33
- "htmlparser",
34
- "parse5",
35
- "serializer",
36
- "html serializer",
37
- "htmlserializer",
38
- "sax",
39
- "simple api",
40
- "parse",
41
- "tokenize",
42
- "serialize",
43
- "tokenizer"
44
- ],
45
- "license": "MIT",
46
- "main": "./lib/index.js",
47
- "types": "./lib/index.d.ts",
48
- "repository": {
49
- "type": "git",
50
- "url": "git://github.com/inikulin/parse5.git"
51
- },
52
- "scripts": {
53
- "test": "gulp test",
54
- "publish-please": "publish-please",
55
- "prepublish": "publish-please guard"
56
- },
57
- "files": [
58
- "lib"
59
- ],
60
- "dependencies": {
61
- "@types/node": "*"
62
- }
2
+ "name": "parse5",
3
+ "description": "HTML parser and serializer.",
4
+ "version": "5.1.0",
5
+ "author": "Ivan Nikulin <ifaaan@gmail.com> (https://github.com/inikulin)",
6
+ "contributors": "https://github.com/inikulin/parse5/graphs/contributors",
7
+ "homepage": "https://github.com/inikulin/parse5",
8
+ "keywords": [
9
+ "html",
10
+ "parser",
11
+ "html5",
12
+ "WHATWG",
13
+ "specification",
14
+ "fast",
15
+ "html parser",
16
+ "html5 parser",
17
+ "htmlparser",
18
+ "parse5",
19
+ "serializer",
20
+ "html serializer",
21
+ "htmlserializer",
22
+ "parse",
23
+ "serialize"
24
+ ],
25
+ "license": "MIT",
26
+ "main": "./lib/index.js",
27
+ "repository": {
28
+ "type": "git",
29
+ "url": "git://github.com/inikulin/parse5.git"
30
+ },
31
+ "files": [
32
+ "lib"
33
+ ]
63
34
  }
package/LICENSE DELETED
@@ -1,19 +0,0 @@
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,260 +0,0 @@
1
- 'use strict';
2
-
3
- var Tokenizer = require('../tokenizer'),
4
- HTML = require('./html');
5
-
6
- //Aliases
7
- var $ = HTML.TAG_NAMES,
8
- NS = HTML.NAMESPACES,
9
- ATTRS = HTML.ATTRS;
10
-
11
-
12
- //MIME types
13
- var MIME_TYPES = {
14
- TEXT_HTML: 'text/html',
15
- APPLICATION_XML: 'application/xhtml+xml'
16
- };
17
-
18
- //Attributes
19
- var DEFINITION_URL_ATTR = 'definitionurl',
20
- ADJUSTED_DEFINITION_URL_ATTR = 'definitionURL',
21
- SVG_ATTRS_ADJUSTMENT_MAP = {
22
- 'attributename': 'attributeName',
23
- 'attributetype': 'attributeType',
24
- 'basefrequency': 'baseFrequency',
25
- 'baseprofile': 'baseProfile',
26
- 'calcmode': 'calcMode',
27
- 'clippathunits': 'clipPathUnits',
28
- 'diffuseconstant': 'diffuseConstant',
29
- 'edgemode': 'edgeMode',
30
- 'filterunits': 'filterUnits',
31
- 'glyphref': 'glyphRef',
32
- 'gradienttransform': 'gradientTransform',
33
- 'gradientunits': 'gradientUnits',
34
- 'kernelmatrix': 'kernelMatrix',
35
- 'kernelunitlength': 'kernelUnitLength',
36
- 'keypoints': 'keyPoints',
37
- 'keysplines': 'keySplines',
38
- 'keytimes': 'keyTimes',
39
- 'lengthadjust': 'lengthAdjust',
40
- 'limitingconeangle': 'limitingConeAngle',
41
- 'markerheight': 'markerHeight',
42
- 'markerunits': 'markerUnits',
43
- 'markerwidth': 'markerWidth',
44
- 'maskcontentunits': 'maskContentUnits',
45
- 'maskunits': 'maskUnits',
46
- 'numoctaves': 'numOctaves',
47
- 'pathlength': 'pathLength',
48
- 'patterncontentunits': 'patternContentUnits',
49
- 'patterntransform': 'patternTransform',
50
- 'patternunits': 'patternUnits',
51
- 'pointsatx': 'pointsAtX',
52
- 'pointsaty': 'pointsAtY',
53
- 'pointsatz': 'pointsAtZ',
54
- 'preservealpha': 'preserveAlpha',
55
- 'preserveaspectratio': 'preserveAspectRatio',
56
- 'primitiveunits': 'primitiveUnits',
57
- 'refx': 'refX',
58
- 'refy': 'refY',
59
- 'repeatcount': 'repeatCount',
60
- 'repeatdur': 'repeatDur',
61
- 'requiredextensions': 'requiredExtensions',
62
- 'requiredfeatures': 'requiredFeatures',
63
- 'specularconstant': 'specularConstant',
64
- 'specularexponent': 'specularExponent',
65
- 'spreadmethod': 'spreadMethod',
66
- 'startoffset': 'startOffset',
67
- 'stddeviation': 'stdDeviation',
68
- 'stitchtiles': 'stitchTiles',
69
- 'surfacescale': 'surfaceScale',
70
- 'systemlanguage': 'systemLanguage',
71
- 'tablevalues': 'tableValues',
72
- 'targetx': 'targetX',
73
- 'targety': 'targetY',
74
- 'textlength': 'textLength',
75
- 'viewbox': 'viewBox',
76
- 'viewtarget': 'viewTarget',
77
- 'xchannelselector': 'xChannelSelector',
78
- 'ychannelselector': 'yChannelSelector',
79
- 'zoomandpan': 'zoomAndPan'
80
- },
81
- XML_ATTRS_ADJUSTMENT_MAP = {
82
- 'xlink:actuate': {prefix: 'xlink', name: 'actuate', namespace: NS.XLINK},
83
- 'xlink:arcrole': {prefix: 'xlink', name: 'arcrole', namespace: NS.XLINK},
84
- 'xlink:href': {prefix: 'xlink', name: 'href', namespace: NS.XLINK},
85
- 'xlink:role': {prefix: 'xlink', name: 'role', namespace: NS.XLINK},
86
- 'xlink:show': {prefix: 'xlink', name: 'show', namespace: NS.XLINK},
87
- 'xlink:title': {prefix: 'xlink', name: 'title', namespace: NS.XLINK},
88
- 'xlink:type': {prefix: 'xlink', name: 'type', namespace: NS.XLINK},
89
- 'xml:base': {prefix: 'xml', name: 'base', namespace: NS.XML},
90
- 'xml:lang': {prefix: 'xml', name: 'lang', namespace: NS.XML},
91
- 'xml:space': {prefix: 'xml', name: 'space', namespace: NS.XML},
92
- 'xmlns': {prefix: '', name: 'xmlns', namespace: NS.XMLNS},
93
- 'xmlns:xlink': {prefix: 'xmlns', name: 'xlink', namespace: NS.XMLNS}
94
-
95
- };
96
-
97
- //SVG tag names adjustment map
98
- var SVG_TAG_NAMES_ADJUSTMENT_MAP = exports.SVG_TAG_NAMES_ADJUSTMENT_MAP = {
99
- 'altglyph': 'altGlyph',
100
- 'altglyphdef': 'altGlyphDef',
101
- 'altglyphitem': 'altGlyphItem',
102
- 'animatecolor': 'animateColor',
103
- 'animatemotion': 'animateMotion',
104
- 'animatetransform': 'animateTransform',
105
- 'clippath': 'clipPath',
106
- 'feblend': 'feBlend',
107
- 'fecolormatrix': 'feColorMatrix',
108
- 'fecomponenttransfer': 'feComponentTransfer',
109
- 'fecomposite': 'feComposite',
110
- 'feconvolvematrix': 'feConvolveMatrix',
111
- 'fediffuselighting': 'feDiffuseLighting',
112
- 'fedisplacementmap': 'feDisplacementMap',
113
- 'fedistantlight': 'feDistantLight',
114
- 'feflood': 'feFlood',
115
- 'fefunca': 'feFuncA',
116
- 'fefuncb': 'feFuncB',
117
- 'fefuncg': 'feFuncG',
118
- 'fefuncr': 'feFuncR',
119
- 'fegaussianblur': 'feGaussianBlur',
120
- 'feimage': 'feImage',
121
- 'femerge': 'feMerge',
122
- 'femergenode': 'feMergeNode',
123
- 'femorphology': 'feMorphology',
124
- 'feoffset': 'feOffset',
125
- 'fepointlight': 'fePointLight',
126
- 'fespecularlighting': 'feSpecularLighting',
127
- 'fespotlight': 'feSpotLight',
128
- 'fetile': 'feTile',
129
- 'feturbulence': 'feTurbulence',
130
- 'foreignobject': 'foreignObject',
131
- 'glyphref': 'glyphRef',
132
- 'lineargradient': 'linearGradient',
133
- 'radialgradient': 'radialGradient',
134
- 'textpath': 'textPath'
135
- };
136
-
137
- //Tags that causes exit from foreign content
138
- var EXITS_FOREIGN_CONTENT = Object.create(null);
139
-
140
- EXITS_FOREIGN_CONTENT[$.B] = true;
141
- EXITS_FOREIGN_CONTENT[$.BIG] = true;
142
- EXITS_FOREIGN_CONTENT[$.BLOCKQUOTE] = true;
143
- EXITS_FOREIGN_CONTENT[$.BODY] = true;
144
- EXITS_FOREIGN_CONTENT[$.BR] = true;
145
- EXITS_FOREIGN_CONTENT[$.CENTER] = true;
146
- EXITS_FOREIGN_CONTENT[$.CODE] = true;
147
- EXITS_FOREIGN_CONTENT[$.DD] = true;
148
- EXITS_FOREIGN_CONTENT[$.DIV] = true;
149
- EXITS_FOREIGN_CONTENT[$.DL] = true;
150
- EXITS_FOREIGN_CONTENT[$.DT] = true;
151
- EXITS_FOREIGN_CONTENT[$.EM] = true;
152
- EXITS_FOREIGN_CONTENT[$.EMBED] = true;
153
- EXITS_FOREIGN_CONTENT[$.H1] = true;
154
- EXITS_FOREIGN_CONTENT[$.H2] = true;
155
- EXITS_FOREIGN_CONTENT[$.H3] = true;
156
- EXITS_FOREIGN_CONTENT[$.H4] = true;
157
- EXITS_FOREIGN_CONTENT[$.H5] = true;
158
- EXITS_FOREIGN_CONTENT[$.H6] = true;
159
- EXITS_FOREIGN_CONTENT[$.HEAD] = true;
160
- EXITS_FOREIGN_CONTENT[$.HR] = true;
161
- EXITS_FOREIGN_CONTENT[$.I] = true;
162
- EXITS_FOREIGN_CONTENT[$.IMG] = true;
163
- EXITS_FOREIGN_CONTENT[$.LI] = true;
164
- EXITS_FOREIGN_CONTENT[$.LISTING] = true;
165
- EXITS_FOREIGN_CONTENT[$.MENU] = true;
166
- EXITS_FOREIGN_CONTENT[$.META] = true;
167
- EXITS_FOREIGN_CONTENT[$.NOBR] = true;
168
- EXITS_FOREIGN_CONTENT[$.OL] = true;
169
- EXITS_FOREIGN_CONTENT[$.P] = true;
170
- EXITS_FOREIGN_CONTENT[$.PRE] = true;
171
- EXITS_FOREIGN_CONTENT[$.RUBY] = true;
172
- EXITS_FOREIGN_CONTENT[$.S] = true;
173
- EXITS_FOREIGN_CONTENT[$.SMALL] = true;
174
- EXITS_FOREIGN_CONTENT[$.SPAN] = true;
175
- EXITS_FOREIGN_CONTENT[$.STRONG] = true;
176
- EXITS_FOREIGN_CONTENT[$.STRIKE] = true;
177
- EXITS_FOREIGN_CONTENT[$.SUB] = true;
178
- EXITS_FOREIGN_CONTENT[$.SUP] = true;
179
- EXITS_FOREIGN_CONTENT[$.TABLE] = true;
180
- EXITS_FOREIGN_CONTENT[$.TT] = true;
181
- EXITS_FOREIGN_CONTENT[$.U] = true;
182
- EXITS_FOREIGN_CONTENT[$.UL] = true;
183
- EXITS_FOREIGN_CONTENT[$.VAR] = true;
184
-
185
- //Check exit from foreign content
186
- exports.causesExit = function (startTagToken) {
187
- var tn = startTagToken.tagName;
188
- var isFontWithAttrs = tn === $.FONT && (Tokenizer.getTokenAttr(startTagToken, ATTRS.COLOR) !== null ||
189
- Tokenizer.getTokenAttr(startTagToken, ATTRS.SIZE) !== null ||
190
- Tokenizer.getTokenAttr(startTagToken, ATTRS.FACE) !== null);
191
-
192
- return isFontWithAttrs ? true : EXITS_FOREIGN_CONTENT[tn];
193
- };
194
-
195
- //Token adjustments
196
- exports.adjustTokenMathMLAttrs = function (token) {
197
- for (var i = 0; i < token.attrs.length; i++) {
198
- if (token.attrs[i].name === DEFINITION_URL_ATTR) {
199
- token.attrs[i].name = ADJUSTED_DEFINITION_URL_ATTR;
200
- break;
201
- }
202
- }
203
- };
204
-
205
- exports.adjustTokenSVGAttrs = function (token) {
206
- for (var i = 0; i < token.attrs.length; i++) {
207
- var adjustedAttrName = SVG_ATTRS_ADJUSTMENT_MAP[token.attrs[i].name];
208
-
209
- if (adjustedAttrName)
210
- token.attrs[i].name = adjustedAttrName;
211
- }
212
- };
213
-
214
- exports.adjustTokenXMLAttrs = function (token) {
215
- for (var i = 0; i < token.attrs.length; i++) {
216
- var adjustedAttrEntry = XML_ATTRS_ADJUSTMENT_MAP[token.attrs[i].name];
217
-
218
- if (adjustedAttrEntry) {
219
- token.attrs[i].prefix = adjustedAttrEntry.prefix;
220
- token.attrs[i].name = adjustedAttrEntry.name;
221
- token.attrs[i].namespace = adjustedAttrEntry.namespace;
222
- }
223
- }
224
- };
225
-
226
- exports.adjustTokenSVGTagName = function (token) {
227
- var adjustedTagName = SVG_TAG_NAMES_ADJUSTMENT_MAP[token.tagName];
228
-
229
- if (adjustedTagName)
230
- token.tagName = adjustedTagName;
231
- };
232
-
233
- //Integration points
234
- function isMathMLTextIntegrationPoint(tn, ns) {
235
- return ns === NS.MATHML && (tn === $.MI || tn === $.MO || tn === $.MN || tn === $.MS || tn === $.MTEXT);
236
- }
237
-
238
- function isHtmlIntegrationPoint(tn, ns, attrs) {
239
- if (ns === NS.MATHML && tn === $.ANNOTATION_XML) {
240
- for (var i = 0; i < attrs.length; i++) {
241
- if (attrs[i].name === ATTRS.ENCODING) {
242
- var value = attrs[i].value.toLowerCase();
243
-
244
- return value === MIME_TYPES.TEXT_HTML || value === MIME_TYPES.APPLICATION_XML;
245
- }
246
- }
247
- }
248
-
249
- return ns === NS.SVG && (tn === $.FOREIGN_OBJECT || tn === $.DESC || tn === $.TITLE);
250
- }
251
-
252
- exports.isIntegrationPoint = function (tn, ns, attrs, foreignNS) {
253
- if ((!foreignNS || foreignNS === NS.HTML) && isHtmlIntegrationPoint(tn, ns, attrs))
254
- return true;
255
-
256
- if ((!foreignNS || foreignNS === NS.MATHML) && isMathMLTextIntegrationPoint(tn, ns))
257
- return true;
258
-
259
- return false;
260
- };
@@ -1,34 +0,0 @@
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
-
@@ -1,213 +0,0 @@
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
-