1 | var ElementType = require("./ElementType.js");
|
2 |
|
3 | function Parser(cbs, options){
|
4 | if(options) this._options = options;
|
5 | if(cbs) this._cbs = cbs;
|
6 |
|
7 | this._buffer = "";
|
8 | this._tagSep = "";
|
9 | this._stack = [];
|
10 | this._wroteSpecial = false;
|
11 | this._contentFlags = 0;
|
12 | this._done = false;
|
13 | this._running = true;
|
14 | }
|
15 |
|
16 |
|
17 | var _reAttrib = /\s([^\s\/]+?)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|(\S+))|(?=\s)|\/|$)/g,
|
18 | _reTail = /\s|\/|$/;
|
19 |
|
20 | Parser.prototype._options = {
|
21 | xmlMode: false,
|
22 | lowerCaseTags: false
|
23 | };
|
24 |
|
25 | Parser.prototype._cbs = {
|
26 | |
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | |
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | };
|
45 |
|
46 |
|
47 | Parser.prototype.parseComplete = function(data){
|
48 | this.reset();
|
49 | this.write(data);
|
50 | this.end();
|
51 | };
|
52 |
|
53 |
|
54 | Parser.prototype.parseChunk =
|
55 | Parser.prototype.write = function(data){
|
56 | if(this._done) this._handleError(Error("Attempted to parse chunk after parsing already done"));
|
57 | this._buffer += data;
|
58 | if(this._running) this._parseTags();
|
59 | };
|
60 |
|
61 |
|
62 | Parser.prototype.done =
|
63 | Parser.prototype.end = function(chunk){
|
64 | if(this._done) return;
|
65 |
|
66 | if(chunk) this.write(chunk);
|
67 | this._done = true;
|
68 |
|
69 | if(this._running) this._finishParsing();
|
70 | };
|
71 |
|
72 | Parser.prototype._finishParsing = function(){
|
73 |
|
74 | if(this._buffer) this._parseTags(true);
|
75 |
|
76 | if(this._cbs.onclosetag){
|
77 | while(this._stack.length) this._cbs.onclosetag(this._stack.pop());
|
78 | }
|
79 |
|
80 | if(this._cbs.onend) this._cbs.onend();
|
81 | };
|
82 |
|
83 | Parser.prototype.pause = function(){
|
84 | if(!this._done) this._running = false;
|
85 | };
|
86 |
|
87 | Parser.prototype.resume = function(){
|
88 | if(this._running) return;
|
89 | this._running = true;
|
90 | this._parseTags();
|
91 | if(this._done) this._finishParsing();
|
92 | };
|
93 |
|
94 |
|
95 | Parser.prototype.reset = function(){
|
96 | Parser.call(this);
|
97 | if(this._cbs.onreset) this._cbs.onreset();
|
98 | };
|
99 |
|
100 |
|
101 | Parser.prototype._parseTagName = function(data){
|
102 | var match = data.substr(0, data.search(_reTail));
|
103 | if(!this._options.lowerCaseTags) return match;
|
104 | return match.toLowerCase();
|
105 | };
|
106 |
|
107 |
|
108 | var SpecialTags = {};
|
109 |
|
110 | SpecialTags[ElementType.Style] = 1;
|
111 | SpecialTags[ElementType.Script] = 2;
|
112 | SpecialTags[ElementType.Comment] = 4;
|
113 | SpecialTags[ElementType.CDATA] = 8;
|
114 |
|
115 | var TagValues = {
|
116 | style: 1,
|
117 | script: 2
|
118 | };
|
119 |
|
120 |
|
121 | Parser.prototype._parseTags = function(force){
|
122 | var buffer = this._buffer, current = 0;
|
123 |
|
124 | var next, rawData, elementData, lastTagSep;
|
125 |
|
126 | var opening = buffer.indexOf("<"), closing = buffer.indexOf(">");
|
127 |
|
128 |
|
129 | if(force) opening = Infinity;
|
130 |
|
131 |
|
132 | while(opening !== closing && this._running){
|
133 | lastTagSep = this._tagSep;
|
134 |
|
135 | if((opening !== -1 && opening < closing) || closing === -1){
|
136 | next = opening;
|
137 | this._tagSep = "<";
|
138 | opening = buffer.indexOf("<", next + 1);
|
139 | }
|
140 | else{
|
141 | next = closing;
|
142 | this._tagSep = ">";
|
143 | closing = buffer.indexOf(">", next + 1);
|
144 | }
|
145 | rawData = buffer.substring(current, next);
|
146 |
|
147 |
|
148 | current = next + 1;
|
149 |
|
150 | if(this._contentFlags >= SpecialTags[ElementType.CDATA]){
|
151 |
|
152 | this._writeCDATA(rawData);
|
153 |
|
154 | }
|
155 | else if(this._contentFlags >= SpecialTags[ElementType.Comment]){
|
156 |
|
157 | this._writeComment(rawData);
|
158 | }
|
159 | else if(lastTagSep === "<"){
|
160 | elementData = rawData.trimLeft();
|
161 | if(elementData.charAt(0) === "/"){
|
162 |
|
163 | elementData = this._parseTagName(elementData.substr(1));
|
164 | if(this._contentFlags !== 0){
|
165 |
|
166 | if(this._contentFlags >= TagValues[elementData]){
|
167 |
|
168 | this._contentFlags -= TagValues[elementData];
|
169 | }
|
170 | else {
|
171 | this._writeSpecial(rawData, lastTagSep);
|
172 | continue;
|
173 | }
|
174 | }
|
175 | this._processCloseTag(elementData);
|
176 | }
|
177 | else if(elementData.charAt(0) === "!"){
|
178 | if(elementData.substr(1, 2) === "--"){
|
179 |
|
180 | this._contentFlags += SpecialTags[ElementType.Comment];
|
181 | this._writeComment(rawData.substr(3));
|
182 | }
|
183 | else if(elementData.substr(1, 7) === "[CDATA["){
|
184 | this._contentFlags += SpecialTags[ElementType.CDATA];
|
185 | if(this._cbs.oncdatastart) this._cbs.oncdatastart();
|
186 | this._writeCDATA(elementData.substr(8));
|
187 | }
|
188 | else if(this._contentFlags !== 0) this._writeSpecial(rawData, lastTagSep);
|
189 |
|
190 | else if(this._cbs.onprocessinginstruction){
|
191 | this._cbs.onprocessinginstruction(
|
192 | "!" + this._parseTagName(elementData.substr(1)),
|
193 | elementData
|
194 | );
|
195 | }
|
196 | }
|
197 | else if(this._contentFlags !== 0) this._writeSpecial(rawData, lastTagSep);
|
198 | else if(elementData.charAt(0) === "?"){
|
199 | if(this._cbs.onprocessinginstruction){
|
200 | this._cbs.onprocessinginstruction(
|
201 | "?" + this._parseTagName(elementData.substr(1)),
|
202 | elementData
|
203 | );
|
204 | }
|
205 | }
|
206 | else this._processOpenTag(this._parseTagName(elementData), elementData);
|
207 | }
|
208 | else{
|
209 | if(this._contentFlags !== 0){
|
210 | this._writeSpecial(rawData, ">");
|
211 | }
|
212 | else if(rawData !== "" && this._cbs.ontext){
|
213 | if(this._tagSep === ">") rawData += ">";
|
214 | this._cbs.ontext(rawData);
|
215 | }
|
216 | }
|
217 | }
|
218 |
|
219 | this._buffer = buffer.substring(current);
|
220 | };
|
221 |
|
222 | Parser.prototype._writeCDATA = function(data){
|
223 | if(this._tagSep === ">" && data.substr(-2) === "]]"){
|
224 |
|
225 | if(data.length !== 2 && this._cbs.ontext){
|
226 | this._cbs.ontext(data.slice(0,-2));
|
227 | }
|
228 | this._contentFlags -= SpecialTags[ElementType.CDATA];
|
229 | if(this._cbs.oncdataend) this._cbs.oncdataend();
|
230 | }
|
231 | else if(this._cbs.ontext) this._cbs.ontext(data + this._tagSep);
|
232 | };
|
233 |
|
234 | Parser.prototype._writeComment = function(rawData){
|
235 | if(this._tagSep === ">" && rawData.substr(-2) === "--"){
|
236 |
|
237 | this._contentFlags -= SpecialTags[ElementType.Comment];
|
238 | this._wroteSpecial = false;
|
239 | if(this._cbs.oncomment) this._cbs.oncomment(rawData.slice(0, -2));
|
240 | if(this._cbs.oncommentend) this._cbs.oncommentend();
|
241 | }
|
242 | else if(this._cbs.oncomment) this._cbs.oncomment(rawData + this._tagSep);
|
243 | };
|
244 |
|
245 | Parser.prototype._writeSpecial = function(rawData, lastTagSep){
|
246 |
|
247 | if(this._wroteSpecial){
|
248 | if(this._cbs.ontext) this._cbs.ontext(lastTagSep + rawData);
|
249 | }
|
250 | else{
|
251 | this._wroteSpecial = true;
|
252 | if(rawData !== "" && this._cbs.ontext) this._cbs.ontext(rawData);
|
253 | }
|
254 | };
|
255 |
|
256 | var emptyTags = {
|
257 | area: true,
|
258 | base: true,
|
259 | basefont: true,
|
260 | br: true,
|
261 | col: true,
|
262 | frame: true,
|
263 | hr: true,
|
264 | img: true,
|
265 | input: true,
|
266 | isindex: true,
|
267 | link: true,
|
268 | meta: true,
|
269 | param: true,
|
270 | embed: true
|
271 | };
|
272 |
|
273 | Parser.prototype._processCloseTag = function(name){
|
274 | if(this._stack && (!(name in emptyTags) || this._options.xmlMode)){
|
275 | var pos = this._stack.lastIndexOf(name);
|
276 | if(pos !== -1)
|
277 | if(this._cbs.onclosetag){
|
278 | pos = this._stack.length - pos;
|
279 | while(pos--) this._cbs.onclosetag(this._stack.pop());
|
280 | }
|
281 | else this._stack.splice(pos);
|
282 | }
|
283 |
|
284 | else if(name === "br" && !this._options.xmlMode)
|
285 | this._processOpenTag(name, "/");
|
286 | };
|
287 |
|
288 | Parser.prototype._parseAttributes = function(data){
|
289 | for(var match; match = _reAttrib.exec(data);){
|
290 | this._cbs.onattribute(match[1], match[2] || match[3] || match[4] || "");
|
291 | }
|
292 | };
|
293 |
|
294 |
|
295 | var parseAttributes = function(data){
|
296 | var attrs = {};
|
297 | for(var match; match = _reAttrib.exec(data);){
|
298 | attrs[match[1]] = match[2] || match[3] || match[4] || "";
|
299 | }
|
300 | return attrs;
|
301 | };
|
302 |
|
303 | Parser.prototype._processOpenTag = function(name, data){
|
304 | var type = ElementType.Tag;
|
305 | if(this._options.xmlMode){ }
|
306 | else if(name === "script") type = ElementType.Script;
|
307 | else if(name === "style") type = ElementType.Style;
|
308 |
|
309 | if(this._cbs.onopentagname) this._cbs.onopentagname(name);
|
310 | if(this._cbs.onopentag) this._cbs.onopentag(name, parseAttributes(data));
|
311 | if(this._cbs.onattribute) this._parseAttributes(data);
|
312 |
|
313 |
|
314 | if(data.substr(-1) === "/" || (name in emptyTags && !this._options.xmlMode)){
|
315 | if(this._cbs.onclosetag) this._cbs.onclosetag(name);
|
316 | } else {
|
317 | if(type !== ElementType.Tag){
|
318 | this._contentFlags += SpecialTags[type];
|
319 | this._wroteSpecial = false;
|
320 | }
|
321 | this._stack.push(name);
|
322 | }
|
323 | };
|
324 |
|
325 | Parser.prototype._handleError = function(error){
|
326 | if(this._cbs.onerror)
|
327 | this._cbs.onerror(error);
|
328 | else throw error;
|
329 | };
|
330 |
|
331 | module.exports = Parser; |
\ | No newline at end of file |