-
-
Notifications
You must be signed in to change notification settings - Fork 232
/
index.js
162 lines (134 loc) · 4.84 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
'use strict';
const { Transform } = require('stream');
const Tokenizer = require('parse5/lib/tokenizer');
const LocationInfoTokenizerMixin = require('parse5/lib/extensions/location-info/tokenizer-mixin');
const Mixin = require('parse5/lib/utils/mixin');
const mergeOptions = require('parse5/lib/utils/merge-options');
const DevNullStream = require('./dev-null-stream');
const ParserFeedbackSimulator = require('./parser-feedback-simulator');
const DEFAULT_OPTIONS = {
sourceCodeLocationInfo: false
};
class SAXParser extends Transform {
constructor(options) {
super();
this.options = mergeOptions(DEFAULT_OPTIONS, options);
this.tokenizer = new Tokenizer(options);
this.locInfoMixin = null;
if (this.options.sourceCodeLocationInfo) {
this.locInfoMixin = Mixin.install(this.tokenizer, LocationInfoTokenizerMixin);
}
this.parserFeedbackSimulator = new ParserFeedbackSimulator(this.tokenizer);
this.pendingText = null;
this.lastChunkWritten = false;
this.stopped = false;
// NOTE: always pipe stream to the /dev/null stream to avoid
// `highWaterMark` hit even if we don't have consumers.
// (see: https://github.com/inikulin/parse5/issues/97#issuecomment-171940774)
this.pipe(new DevNullStream());
}
//TransformStream implementation
_transform(chunk, encoding, callback) {
callback(null, this._transformChunk(chunk));
}
end(chunk, encoding, callback) {
this.lastChunkWritten = true;
super.end(chunk, encoding, callback);
}
stop() {
this.stopped = true;
}
//Internals
_transformChunk(chunk) {
if (!this.stopped) {
this.tokenizer.write(chunk.toString('utf8'), this.lastChunkWritten);
this._runParsingLoop();
}
return chunk;
}
_runParsingLoop() {
let token = null;
do {
token = this.parserFeedbackSimulator.getNextToken();
if (token.type === Tokenizer.HIBERNATION_TOKEN) {
break;
}
if (
token.type === Tokenizer.CHARACTER_TOKEN ||
token.type === Tokenizer.WHITESPACE_CHARACTER_TOKEN ||
token.type === Tokenizer.NULL_CHARACTER_TOKEN
) {
if (this.pendingText === null) {
token.type = Tokenizer.CHARACTER_TOKEN;
this.pendingText = token;
} else {
this.pendingText.chars += token.chars;
if (this.options.sourceCodeLocationInfo) {
const { endLine, endCol, endOffset } = token.location;
Object.assign(this.pendingText.location, {
endLine,
endCol,
endOffset
});
}
}
} else {
this._emitPendingText();
this._handleToken(token);
}
} while (!this.stopped && token.type !== Tokenizer.EOF_TOKEN);
}
_handleToken(token) {
if (token.type === Tokenizer.EOF_TOKEN) {
return true;
}
const { eventName, reshapeToken } = TOKEN_EMISSION_HELPERS[token.type];
if (this.listenerCount(eventName) === 0) {
return false;
}
this._emitToken(eventName, reshapeToken(token));
return true;
}
_emitToken(eventName, token) {
this.emit(eventName, token);
}
_emitPendingText() {
if (this.pendingText !== null) {
this._handleToken(this.pendingText);
this.pendingText = null;
}
}
}
const TOKEN_EMISSION_HELPERS = {
[Tokenizer.START_TAG_TOKEN]: {
eventName: 'startTag',
reshapeToken: origToken => ({
tagName: origToken.tagName,
attrs: origToken.attrs,
selfClosing: origToken.selfClosing,
sourceCodeLocation: origToken.location
})
},
[Tokenizer.END_TAG_TOKEN]: {
eventName: 'endTag',
reshapeToken: origToken => ({ tagName: origToken.tagName, sourceCodeLocation: origToken.location })
},
[Tokenizer.COMMENT_TOKEN]: {
eventName: 'comment',
reshapeToken: origToken => ({ text: origToken.data, sourceCodeLocation: origToken.location })
},
[Tokenizer.DOCTYPE_TOKEN]: {
eventName: 'doctype',
reshapeToken: origToken => ({
name: origToken.name,
publicId: origToken.publicId,
systemId: origToken.systemId,
sourceCodeLocation: origToken.location
})
},
[Tokenizer.CHARACTER_TOKEN]: {
eventName: 'text',
reshapeToken: origToken => ({ text: origToken.chars, sourceCodeLocation: origToken.location })
}
};
module.exports = SAXParser;