parse5-sax-parser 5.0.0 → 6.0.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.
- package/LICENSE +19 -0
- package/lib/index.js +64 -55
- package/package.json +13 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Copyright (c) 2013-2019 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.
|
package/lib/index.js
CHANGED
|
@@ -14,7 +14,7 @@ const DEFAULT_OPTIONS = {
|
|
|
14
14
|
|
|
15
15
|
class SAXParser extends Transform {
|
|
16
16
|
constructor(options) {
|
|
17
|
-
super();
|
|
17
|
+
super({ encoding: 'utf8', decodeStrings: false });
|
|
18
18
|
|
|
19
19
|
this.options = mergeOptions(DEFAULT_OPTIONS, options);
|
|
20
20
|
|
|
@@ -28,7 +28,6 @@ class SAXParser extends Transform {
|
|
|
28
28
|
this.parserFeedbackSimulator = new ParserFeedbackSimulator(this.tokenizer);
|
|
29
29
|
|
|
30
30
|
this.pendingText = null;
|
|
31
|
-
this.currentTokenLocation = void 0;
|
|
32
31
|
|
|
33
32
|
this.lastChunkWritten = false;
|
|
34
33
|
this.stopped = false;
|
|
@@ -41,19 +40,16 @@ class SAXParser extends Transform {
|
|
|
41
40
|
|
|
42
41
|
//TransformStream implementation
|
|
43
42
|
_transform(chunk, encoding, callback) {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
callback();
|
|
48
|
-
}
|
|
43
|
+
if (typeof chunk !== 'string') {
|
|
44
|
+
throw new TypeError('Parser can work only with string streams.');
|
|
45
|
+
}
|
|
49
46
|
|
|
50
|
-
|
|
51
|
-
callback();
|
|
47
|
+
callback(null, this._transformChunk(chunk));
|
|
52
48
|
}
|
|
53
49
|
|
|
54
|
-
|
|
50
|
+
_final(callback) {
|
|
55
51
|
this.lastChunkWritten = true;
|
|
56
|
-
|
|
52
|
+
callback(null, this._transformChunk(''));
|
|
57
53
|
}
|
|
58
54
|
|
|
59
55
|
stop() {
|
|
@@ -61,11 +57,12 @@ class SAXParser extends Transform {
|
|
|
61
57
|
}
|
|
62
58
|
|
|
63
59
|
//Internals
|
|
64
|
-
|
|
60
|
+
_transformChunk(chunk) {
|
|
65
61
|
if (!this.stopped) {
|
|
66
|
-
this.tokenizer.write(chunk
|
|
62
|
+
this.tokenizer.write(chunk, this.lastChunkWritten);
|
|
67
63
|
this._runParsingLoop();
|
|
68
64
|
}
|
|
65
|
+
return chunk;
|
|
69
66
|
}
|
|
70
67
|
|
|
71
68
|
_runParsingLoop() {
|
|
@@ -83,15 +80,21 @@ class SAXParser extends Transform {
|
|
|
83
80
|
token.type === Tokenizer.WHITESPACE_CHARACTER_TOKEN ||
|
|
84
81
|
token.type === Tokenizer.NULL_CHARACTER_TOKEN
|
|
85
82
|
) {
|
|
86
|
-
if (this.
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
83
|
+
if (this.pendingText === null) {
|
|
84
|
+
token.type = Tokenizer.CHARACTER_TOKEN;
|
|
85
|
+
this.pendingText = token;
|
|
86
|
+
} else {
|
|
87
|
+
this.pendingText.chars += token.chars;
|
|
88
|
+
|
|
89
|
+
if (this.options.sourceCodeLocationInfo) {
|
|
90
|
+
const { endLine, endCol, endOffset } = token.location;
|
|
91
|
+
Object.assign(this.pendingText.location, {
|
|
92
|
+
endLine,
|
|
93
|
+
endCol,
|
|
94
|
+
endOffset
|
|
95
|
+
});
|
|
91
96
|
}
|
|
92
97
|
}
|
|
93
|
-
|
|
94
|
-
this.pendingText = (this.pendingText || '') + token.chars;
|
|
95
98
|
} else {
|
|
96
99
|
this._emitPendingText();
|
|
97
100
|
this._handleToken(token);
|
|
@@ -100,58 +103,64 @@ class SAXParser extends Transform {
|
|
|
100
103
|
}
|
|
101
104
|
|
|
102
105
|
_handleToken(token) {
|
|
103
|
-
if (
|
|
104
|
-
|
|
106
|
+
if (token.type === Tokenizer.EOF_TOKEN) {
|
|
107
|
+
return true;
|
|
105
108
|
}
|
|
106
109
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
} else if (token.type === Tokenizer.COMMENT_TOKEN) {
|
|
112
|
-
this.emit('comment', this._reshapeCommentToken(token));
|
|
113
|
-
} else if (token.type === Tokenizer.DOCTYPE_TOKEN) {
|
|
114
|
-
this.emit('doctype', this._reshapeDoctypeToken(token));
|
|
110
|
+
const { eventName, reshapeToken } = TOKEN_EMISSION_HELPERS[token.type];
|
|
111
|
+
|
|
112
|
+
if (this.listenerCount(eventName) === 0) {
|
|
113
|
+
return false;
|
|
115
114
|
}
|
|
115
|
+
|
|
116
|
+
this._emitToken(eventName, reshapeToken(token));
|
|
117
|
+
|
|
118
|
+
return true;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
_emitToken(eventName, token) {
|
|
122
|
+
this.emit(eventName, token);
|
|
116
123
|
}
|
|
117
124
|
|
|
118
125
|
_emitPendingText() {
|
|
119
126
|
if (this.pendingText !== null) {
|
|
120
|
-
this.
|
|
127
|
+
this._handleToken(this.pendingText);
|
|
121
128
|
this.pendingText = null;
|
|
122
129
|
}
|
|
123
130
|
}
|
|
131
|
+
}
|
|
124
132
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
_reshapeStartTagToken(origToken) {
|
|
131
|
-
return {
|
|
133
|
+
const TOKEN_EMISSION_HELPERS = {
|
|
134
|
+
[Tokenizer.START_TAG_TOKEN]: {
|
|
135
|
+
eventName: 'startTag',
|
|
136
|
+
reshapeToken: origToken => ({
|
|
132
137
|
tagName: origToken.tagName,
|
|
133
138
|
attrs: origToken.attrs,
|
|
134
139
|
selfClosing: origToken.selfClosing,
|
|
135
|
-
sourceCodeLocation:
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
140
|
+
sourceCodeLocation: origToken.location
|
|
141
|
+
})
|
|
142
|
+
},
|
|
143
|
+
[Tokenizer.END_TAG_TOKEN]: {
|
|
144
|
+
eventName: 'endTag',
|
|
145
|
+
reshapeToken: origToken => ({ tagName: origToken.tagName, sourceCodeLocation: origToken.location })
|
|
146
|
+
},
|
|
147
|
+
[Tokenizer.COMMENT_TOKEN]: {
|
|
148
|
+
eventName: 'comment',
|
|
149
|
+
reshapeToken: origToken => ({ text: origToken.data, sourceCodeLocation: origToken.location })
|
|
150
|
+
},
|
|
151
|
+
[Tokenizer.DOCTYPE_TOKEN]: {
|
|
152
|
+
eventName: 'doctype',
|
|
153
|
+
reshapeToken: origToken => ({
|
|
149
154
|
name: origToken.name,
|
|
150
155
|
publicId: origToken.publicId,
|
|
151
156
|
systemId: origToken.systemId,
|
|
152
|
-
sourceCodeLocation:
|
|
153
|
-
}
|
|
157
|
+
sourceCodeLocation: origToken.location
|
|
158
|
+
})
|
|
159
|
+
},
|
|
160
|
+
[Tokenizer.CHARACTER_TOKEN]: {
|
|
161
|
+
eventName: 'text',
|
|
162
|
+
reshapeToken: origToken => ({ text: origToken.chars, sourceCodeLocation: origToken.location })
|
|
154
163
|
}
|
|
155
|
-
}
|
|
164
|
+
};
|
|
156
165
|
|
|
157
166
|
module.exports = SAXParser;
|
package/package.json
CHANGED
|
@@ -1,19 +1,28 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "parse5-sax-parser",
|
|
3
3
|
"description": "Streaming SAX-style HTML parser.",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "6.0.1",
|
|
5
5
|
"author": "Ivan Nikulin <ifaaan@gmail.com> (https://github.com/inikulin)",
|
|
6
6
|
"contributors": "https://github.com/inikulin/parse5/graphs/contributors",
|
|
7
7
|
"homepage": "https://github.com/inikulin/parse5",
|
|
8
|
-
"keywords": [
|
|
8
|
+
"keywords": [
|
|
9
|
+
"parse5",
|
|
10
|
+
"parser",
|
|
11
|
+
"stream",
|
|
12
|
+
"streaming",
|
|
13
|
+
"SAX"
|
|
14
|
+
],
|
|
9
15
|
"license": "MIT",
|
|
10
16
|
"main": "./lib/index.js",
|
|
11
17
|
"dependencies": {
|
|
12
|
-
"parse5": "^
|
|
18
|
+
"parse5": "^6.0.1"
|
|
13
19
|
},
|
|
14
20
|
"repository": {
|
|
15
21
|
"type": "git",
|
|
16
22
|
"url": "git://github.com/inikulin/parse5.git"
|
|
17
23
|
},
|
|
18
|
-
"files": [
|
|
24
|
+
"files": [
|
|
25
|
+
"lib"
|
|
26
|
+
],
|
|
27
|
+
"gitHead": "37227a3429584903cbd1799dade995266fc2dbe6"
|
|
19
28
|
}
|