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,122 +1,122 @@
1
- 'use strict';
2
-
3
- var Tokenizer = require('../tokenization/tokenizer'),
4
- ForeignContent = require('../common/foreign_content'),
5
- UNICODE = require('../common/unicode'),
6
- HTML = require('../common/html');
7
-
8
- //Aliases
9
- var $ = HTML.TAG_NAMES,
10
- NS = HTML.NAMESPACES;
11
-
12
-
13
- //Tokenizer proxy
14
- //NOTE: this proxy simulates adjustment of the Tokenizer which performed by standard parser during tree construction.
15
- var TokenizerProxy = module.exports = function (html, options) {
16
- this.tokenizer = new Tokenizer(html, options);
17
-
18
- this.namespaceStack = [];
19
- this.namespaceStackTop = -1;
20
- this.currentNamespace = null;
21
- this.inForeignContent = false;
22
- };
23
-
24
- //API
25
- TokenizerProxy.prototype.getNextToken = function () {
26
- var token = this.tokenizer.getNextToken();
27
-
28
- if (token.type === Tokenizer.START_TAG_TOKEN)
29
- this._handleStartTagToken(token);
30
-
31
- else if (token.type === Tokenizer.END_TAG_TOKEN)
32
- this._handleEndTagToken(token);
33
-
34
- else if (token.type === Tokenizer.NULL_CHARACTER_TOKEN && this.inForeignContent) {
35
- token.type = Tokenizer.CHARACTER_TOKEN;
36
- token.chars = UNICODE.REPLACEMENT_CHARACTER;
37
- }
38
-
39
- return token;
40
- };
41
-
42
- //Namespace stack mutations
43
- TokenizerProxy.prototype._enterNamespace = function (namespace) {
44
- this.namespaceStackTop++;
45
- this.namespaceStack.push(namespace);
46
-
47
- this.inForeignContent = namespace !== NS.HTML;
48
- this.currentNamespace = namespace;
49
- this.tokenizer.allowCDATA = this.inForeignContent;
50
- };
51
-
52
- TokenizerProxy.prototype._leaveCurrentNamespace = function () {
53
- this.namespaceStackTop--;
54
- this.namespaceStack.pop();
55
-
56
- this.currentNamespace = this.namespaceStack[this.namespaceStackTop];
57
- this.inForeignContent = this.currentNamespace !== NS.HTML;
58
- this.tokenizer.allowCDATA = this.inForeignContent;
59
- };
60
-
61
- //Token handlers
62
- TokenizerProxy.prototype._ensureTokenizerMode = function (tn) {
63
- if (tn === $.TEXTAREA || tn === $.TITLE)
64
- this.tokenizer.state = Tokenizer.MODE.RCDATA;
65
-
66
- else if (tn === $.PLAINTEXT)
67
- this.tokenizer.state = Tokenizer.MODE.PLAINTEXT;
68
-
69
- else if (tn === $.SCRIPT)
70
- this.tokenizer.state = Tokenizer.MODE.SCRIPT_DATA;
71
-
72
- else if (tn === $.STYLE || tn === $.IFRAME || tn === $.XMP ||
73
- tn === $.NOEMBED || tn === $.NOFRAMES || tn === $.NOSCRIPT) {
74
- this.tokenizer.state = Tokenizer.MODE.RAWTEXT;
75
- }
76
- };
77
-
78
- TokenizerProxy.prototype._handleStartTagToken = function (token) {
79
- var tn = token.tagName;
80
-
81
- if (tn === $.SVG)
82
- this._enterNamespace(NS.SVG);
83
-
84
- else if (tn === $.MATH)
85
- this._enterNamespace(NS.MATHML);
86
-
87
- else {
88
- if (this.inForeignContent) {
89
- if (ForeignContent.causesExit(token))
90
- this._leaveCurrentNamespace();
91
-
92
- else if (ForeignContent.isMathMLTextIntegrationPoint(tn, this.currentNamespace) ||
93
- ForeignContent.isHtmlIntegrationPoint(tn, this.currentNamespace, token.attrs)) {
94
- this._enterNamespace(NS.HTML);
95
- }
96
- }
97
-
98
- else
99
- this._ensureTokenizerMode(tn);
100
- }
101
- };
102
-
103
- TokenizerProxy.prototype._handleEndTagToken = function (token) {
104
- var tn = token.tagName;
105
-
106
- if (!this.inForeignContent) {
107
- var previousNs = this.namespaceStack[this.namespaceStackTop - 1];
108
-
109
- //NOTE: check for exit from integration point
110
- if (ForeignContent.isMathMLTextIntegrationPoint(tn, previousNs) ||
111
- ForeignContent.isHtmlIntegrationPoint(tn, previousNs, token.attrs)) {
112
- this._leaveCurrentNamespace();
113
- }
114
-
115
- else if (tn === $.SCRIPT)
116
- this.tokenizer.state = Tokenizer.MODE.DATA;
117
- }
118
-
119
- else if ((tn === $.SVG && this.currentNamespace === NS.SVG) ||
120
- (tn === $.MATH && this.currentNamespace === NS.MATHML))
121
- this._leaveCurrentNamespace();
122
- };
1
+ 'use strict';
2
+
3
+ var Tokenizer = require('../tokenization/tokenizer'),
4
+ ForeignContent = require('../common/foreign_content'),
5
+ UNICODE = require('../common/unicode'),
6
+ HTML = require('../common/html');
7
+
8
+ //Aliases
9
+ var $ = HTML.TAG_NAMES,
10
+ NS = HTML.NAMESPACES;
11
+
12
+
13
+ //Tokenizer proxy
14
+ //NOTE: this proxy simulates adjustment of the Tokenizer which performed by standard parser during tree construction.
15
+ var TokenizerProxy = module.exports = function (html, options) {
16
+ this.tokenizer = new Tokenizer(html, options);
17
+
18
+ this.namespaceStack = [];
19
+ this.namespaceStackTop = -1;
20
+ this.currentNamespace = null;
21
+ this.inForeignContent = false;
22
+ };
23
+
24
+ //API
25
+ TokenizerProxy.prototype.getNextToken = function () {
26
+ var token = this.tokenizer.getNextToken();
27
+
28
+ if (token.type === Tokenizer.START_TAG_TOKEN)
29
+ this._handleStartTagToken(token);
30
+
31
+ else if (token.type === Tokenizer.END_TAG_TOKEN)
32
+ this._handleEndTagToken(token);
33
+
34
+ else if (token.type === Tokenizer.NULL_CHARACTER_TOKEN && this.inForeignContent) {
35
+ token.type = Tokenizer.CHARACTER_TOKEN;
36
+ token.chars = UNICODE.REPLACEMENT_CHARACTER;
37
+ }
38
+
39
+ return token;
40
+ };
41
+
42
+ //Namespace stack mutations
43
+ TokenizerProxy.prototype._enterNamespace = function (namespace) {
44
+ this.namespaceStackTop++;
45
+ this.namespaceStack.push(namespace);
46
+
47
+ this.inForeignContent = namespace !== NS.HTML;
48
+ this.currentNamespace = namespace;
49
+ this.tokenizer.allowCDATA = this.inForeignContent;
50
+ };
51
+
52
+ TokenizerProxy.prototype._leaveCurrentNamespace = function () {
53
+ this.namespaceStackTop--;
54
+ this.namespaceStack.pop();
55
+
56
+ this.currentNamespace = this.namespaceStack[this.namespaceStackTop];
57
+ this.inForeignContent = this.currentNamespace !== NS.HTML;
58
+ this.tokenizer.allowCDATA = this.inForeignContent;
59
+ };
60
+
61
+ //Token handlers
62
+ TokenizerProxy.prototype._ensureTokenizerMode = function (tn) {
63
+ if (tn === $.TEXTAREA || tn === $.TITLE)
64
+ this.tokenizer.state = Tokenizer.MODE.RCDATA;
65
+
66
+ else if (tn === $.PLAINTEXT)
67
+ this.tokenizer.state = Tokenizer.MODE.PLAINTEXT;
68
+
69
+ else if (tn === $.SCRIPT)
70
+ this.tokenizer.state = Tokenizer.MODE.SCRIPT_DATA;
71
+
72
+ else if (tn === $.STYLE || tn === $.IFRAME || tn === $.XMP ||
73
+ tn === $.NOEMBED || tn === $.NOFRAMES || tn === $.NOSCRIPT) {
74
+ this.tokenizer.state = Tokenizer.MODE.RAWTEXT;
75
+ }
76
+ };
77
+
78
+ TokenizerProxy.prototype._handleStartTagToken = function (token) {
79
+ var tn = token.tagName;
80
+
81
+ if (tn === $.SVG)
82
+ this._enterNamespace(NS.SVG);
83
+
84
+ else if (tn === $.MATH)
85
+ this._enterNamespace(NS.MATHML);
86
+
87
+ else {
88
+ if (this.inForeignContent) {
89
+ if (ForeignContent.causesExit(token))
90
+ this._leaveCurrentNamespace();
91
+
92
+ else if (ForeignContent.isMathMLTextIntegrationPoint(tn, this.currentNamespace) ||
93
+ ForeignContent.isHtmlIntegrationPoint(tn, this.currentNamespace, token.attrs)) {
94
+ this._enterNamespace(NS.HTML);
95
+ }
96
+ }
97
+
98
+ else
99
+ this._ensureTokenizerMode(tn);
100
+ }
101
+ };
102
+
103
+ TokenizerProxy.prototype._handleEndTagToken = function (token) {
104
+ var tn = token.tagName;
105
+
106
+ if (!this.inForeignContent) {
107
+ var previousNs = this.namespaceStack[this.namespaceStackTop - 1];
108
+
109
+ //NOTE: check for exit from integration point
110
+ if (ForeignContent.isMathMLTextIntegrationPoint(tn, previousNs) ||
111
+ ForeignContent.isHtmlIntegrationPoint(tn, previousNs, token.attrs)) {
112
+ this._leaveCurrentNamespace();
113
+ }
114
+
115
+ else if (tn === $.SCRIPT)
116
+ this.tokenizer.state = Tokenizer.MODE.DATA;
117
+ }
118
+
119
+ else if ((tn === $.SVG && this.currentNamespace === NS.SVG) ||
120
+ (tn === $.MATH && this.currentNamespace === NS.MATHML))
121
+ this._leaveCurrentNamespace();
122
+ };
@@ -1,80 +1,80 @@
1
- 'use strict';
2
-
3
- exports.assign = function (tokenizer) {
4
- //NOTE: obtain Tokenizer proto this way to avoid module circular references
5
- var tokenizerProto = Object.getPrototypeOf(tokenizer);
6
-
7
- tokenizer.tokenStartLoc = -1;
8
-
9
- //NOTE: add location info builder method
10
- tokenizer._attachLocationInfo = function (token) {
11
- token.location = {
12
- start: this.tokenStartLoc,
13
- end: -1
14
- };
15
- };
16
-
17
- //NOTE: patch token creation methods and attach location objects
18
- tokenizer._createStartTagToken = function (tagNameFirstCh) {
19
- tokenizerProto._createStartTagToken.call(this, tagNameFirstCh);
20
- this._attachLocationInfo(this.currentToken);
21
- };
22
-
23
- tokenizer._createEndTagToken = function (tagNameFirstCh) {
24
- tokenizerProto._createEndTagToken.call(this, tagNameFirstCh);
25
- this._attachLocationInfo(this.currentToken);
26
- };
27
-
28
- tokenizer._createCommentToken = function () {
29
- tokenizerProto._createCommentToken.call(this);
30
- this._attachLocationInfo(this.currentToken);
31
- };
32
-
33
- tokenizer._createDoctypeToken = function (doctypeNameFirstCh) {
34
- tokenizerProto._createDoctypeToken.call(this, doctypeNameFirstCh);
35
- this._attachLocationInfo(this.currentToken);
36
- };
37
-
38
- tokenizer._createCharacterToken = function (type, ch) {
39
- tokenizerProto._createCharacterToken.call(this, type, ch);
40
- this._attachLocationInfo(this.currentCharacterToken);
41
- };
42
-
43
- //NOTE: patch token emission methods to determine end location
44
- tokenizer._emitCurrentToken = function () {
45
- //NOTE: if we have pending character tokenmake it's end location equal to the
46
- //current token's start location.
47
- if (this.currentCharacterToken)
48
- this.currentCharacterToken.location.end = this.currentToken.location.start;
49
-
50
- this.currentToken.location.end = this.preprocessor.pos + 1;
51
- tokenizerProto._emitCurrentToken.call(this);
52
- };
53
-
54
- tokenizer._emitCurrentCharacterToken = function () {
55
- //NOTE: if we have character token and it's location wasn't set in the _emitCurrentToken(),
56
- //then set it's location at the current preprocessor position
57
- if (this.currentCharacterToken && this.currentCharacterToken.location.end === -1) {
58
- //NOTE: we don't need to increment preprocessor position here, since character token
59
- //emission here is always forced by the start of the next token character token.
60
- //So, we already have advanced position here.
61
- this.currentCharacterToken.location.end = this.preprocessor.pos;
62
- }
63
-
64
- tokenizerProto._emitCurrentCharacterToken.call(this);
65
- };
66
-
67
- //NOTE: patch initial states for each mode to obtain token start position
68
- Object.keys(tokenizerProto.MODE)
69
-
70
- .map(function (modeName) {
71
- return tokenizerProto.MODE[modeName];
72
- })
73
-
74
- .forEach(function (state) {
75
- tokenizer[state] = function (cp) {
76
- this.tokenStartLoc = this.preprocessor.pos;
77
- tokenizerProto[state].call(this, cp);
78
- };
79
- });
80
- };
1
+ 'use strict';
2
+
3
+ exports.assign = function (tokenizer) {
4
+ //NOTE: obtain Tokenizer proto this way to avoid module circular references
5
+ var tokenizerProto = Object.getPrototypeOf(tokenizer);
6
+
7
+ tokenizer.tokenStartLoc = -1;
8
+
9
+ //NOTE: add location info builder method
10
+ tokenizer._attachLocationInfo = function (token) {
11
+ token.location = {
12
+ start: this.tokenStartLoc,
13
+ end: -1
14
+ };
15
+ };
16
+
17
+ //NOTE: patch token creation methods and attach location objects
18
+ tokenizer._createStartTagToken = function (tagNameFirstCh) {
19
+ tokenizerProto._createStartTagToken.call(this, tagNameFirstCh);
20
+ this._attachLocationInfo(this.currentToken);
21
+ };
22
+
23
+ tokenizer._createEndTagToken = function (tagNameFirstCh) {
24
+ tokenizerProto._createEndTagToken.call(this, tagNameFirstCh);
25
+ this._attachLocationInfo(this.currentToken);
26
+ };
27
+
28
+ tokenizer._createCommentToken = function () {
29
+ tokenizerProto._createCommentToken.call(this);
30
+ this._attachLocationInfo(this.currentToken);
31
+ };
32
+
33
+ tokenizer._createDoctypeToken = function (doctypeNameFirstCh) {
34
+ tokenizerProto._createDoctypeToken.call(this, doctypeNameFirstCh);
35
+ this._attachLocationInfo(this.currentToken);
36
+ };
37
+
38
+ tokenizer._createCharacterToken = function (type, ch) {
39
+ tokenizerProto._createCharacterToken.call(this, type, ch);
40
+ this._attachLocationInfo(this.currentCharacterToken);
41
+ };
42
+
43
+ //NOTE: patch token emission methods to determine end location
44
+ tokenizer._emitCurrentToken = function () {
45
+ //NOTE: if we have pending character token make it's end location equal to the
46
+ //current token's start location.
47
+ if (this.currentCharacterToken)
48
+ this.currentCharacterToken.location.end = this.currentToken.location.start;
49
+
50
+ this.currentToken.location.end = this.preprocessor.pos + 1;
51
+ tokenizerProto._emitCurrentToken.call(this);
52
+ };
53
+
54
+ tokenizer._emitCurrentCharacterToken = function () {
55
+ //NOTE: if we have character token and it's location wasn't set in the _emitCurrentToken(),
56
+ //then set it's location at the current preprocessor position
57
+ if (this.currentCharacterToken && this.currentCharacterToken.location.end === -1) {
58
+ //NOTE: we don't need to increment preprocessor position, since character token
59
+ //emission is always forced by the start of the next character token here.
60
+ //So, we already have advanced position.
61
+ this.currentCharacterToken.location.end = this.preprocessor.pos;
62
+ }
63
+
64
+ tokenizerProto._emitCurrentCharacterToken.call(this);
65
+ };
66
+
67
+ //NOTE: patch initial states for each mode to obtain token start position
68
+ Object.keys(tokenizerProto.MODE)
69
+
70
+ .map(function (modeName) {
71
+ return tokenizerProto.MODE[modeName];
72
+ })
73
+
74
+ .forEach(function (state) {
75
+ tokenizer[state] = function (cp) {
76
+ this.tokenStartLoc = this.preprocessor.pos;
77
+ tokenizerProto[state].call(this, cp);
78
+ };
79
+ });
80
+ };