1 module.exports = Tokenizer;
3 var decodeCodePoint = require("entities/lib/decode_codepoint.js");
4 var entityMap = require("entities/maps/entities.json");
5 var legacyMap = require("entities/maps/legacy.json");
6 var xmlMap = require("entities/maps/xml.json");
11 var BEFORE_TAG_NAME = i++; //after <
12 var IN_TAG_NAME = i++;
13 var IN_SELF_CLOSING_TAG = i++;
14 var BEFORE_CLOSING_TAG_NAME = i++;
15 var IN_CLOSING_TAG_NAME = i++;
16 var AFTER_CLOSING_TAG_NAME = i++;
19 var BEFORE_ATTRIBUTE_NAME = i++;
20 var IN_ATTRIBUTE_NAME = i++;
21 var AFTER_ATTRIBUTE_NAME = i++;
22 var BEFORE_ATTRIBUTE_VALUE = i++;
23 var IN_ATTRIBUTE_VALUE_DQ = i++; // "
24 var IN_ATTRIBUTE_VALUE_SQ = i++; // '
25 var IN_ATTRIBUTE_VALUE_NQ = i++;
28 var BEFORE_DECLARATION = i++; // !
29 var IN_DECLARATION = i++;
31 //processing instructions
32 var IN_PROCESSING_INSTRUCTION = i++; // ?
35 var BEFORE_COMMENT = i++;
37 var AFTER_COMMENT_1 = i++;
38 var AFTER_COMMENT_2 = i++;
41 var BEFORE_CDATA_1 = i++; // [
42 var BEFORE_CDATA_2 = i++; // C
43 var BEFORE_CDATA_3 = i++; // D
44 var BEFORE_CDATA_4 = i++; // A
45 var BEFORE_CDATA_5 = i++; // T
46 var BEFORE_CDATA_6 = i++; // A
47 var IN_CDATA = i++; // [
48 var AFTER_CDATA_1 = i++; // ]
49 var AFTER_CDATA_2 = i++; // ]
52 var BEFORE_SPECIAL = i++; //S
53 var BEFORE_SPECIAL_END = i++; //S
55 var BEFORE_SCRIPT_1 = i++; //C
56 var BEFORE_SCRIPT_2 = i++; //R
57 var BEFORE_SCRIPT_3 = i++; //I
58 var BEFORE_SCRIPT_4 = i++; //P
59 var BEFORE_SCRIPT_5 = i++; //T
60 var AFTER_SCRIPT_1 = i++; //C
61 var AFTER_SCRIPT_2 = i++; //R
62 var AFTER_SCRIPT_3 = i++; //I
63 var AFTER_SCRIPT_4 = i++; //P
64 var AFTER_SCRIPT_5 = i++; //T
66 var BEFORE_STYLE_1 = i++; //T
67 var BEFORE_STYLE_2 = i++; //Y
68 var BEFORE_STYLE_3 = i++; //L
69 var BEFORE_STYLE_4 = i++; //E
70 var AFTER_STYLE_1 = i++; //T
71 var AFTER_STYLE_2 = i++; //Y
72 var AFTER_STYLE_3 = i++; //L
73 var AFTER_STYLE_4 = i++; //E
75 var BEFORE_ENTITY = i++; //&
76 var BEFORE_NUMERIC_ENTITY = i++; //#
77 var IN_NAMED_ENTITY = i++;
78 var IN_NUMERIC_ENTITY = i++;
79 var IN_HEX_ENTITY = i++; //X
83 var SPECIAL_NONE = j++;
84 var SPECIAL_SCRIPT = j++;
85 var SPECIAL_STYLE = j++;
87 function whitespace(c) {
88 return c === " " || c === "\n" || c === "\t" || c === "\f" || c === "\r";
91 function ifElseState(upper, SUCCESS, FAILURE) {
92 var lower = upper.toLowerCase();
94 if (upper === lower) {
97 this._state = SUCCESS;
99 this._state = FAILURE;
105 if (c === lower || c === upper) {
106 this._state = SUCCESS;
108 this._state = FAILURE;
115 function consumeSpecialNameChar(upper, NEXT_STATE) {
116 var lower = upper.toLowerCase();
119 if (c === lower || c === upper) {
120 this._state = NEXT_STATE;
122 this._state = IN_TAG_NAME;
123 this._index--; //consume the token again
128 function Tokenizer(options, cbs) {
131 this._sectionStart = 0;
133 this._bufferOffset = 0; //chars removed from _buffer
134 this._baseState = TEXT;
135 this._special = SPECIAL_NONE;
137 this._running = true;
139 this._xmlMode = !!(options && options.xmlMode);
140 this._decodeEntities = !!(options && options.decodeEntities);
143 Tokenizer.prototype._stateText = function(c) {
145 if (this._index > this._sectionStart) {
146 this._cbs.ontext(this._getSection());
148 this._state = BEFORE_TAG_NAME;
149 this._sectionStart = this._index;
151 this._decodeEntities &&
152 this._special === SPECIAL_NONE &&
155 if (this._index > this._sectionStart) {
156 this._cbs.ontext(this._getSection());
158 this._baseState = TEXT;
159 this._state = BEFORE_ENTITY;
160 this._sectionStart = this._index;
164 Tokenizer.prototype._stateBeforeTagName = function(c) {
166 this._state = BEFORE_CLOSING_TAG_NAME;
167 } else if (c === "<") {
168 this._cbs.ontext(this._getSection());
169 this._sectionStart = this._index;
170 } else if (c === ">" || this._special !== SPECIAL_NONE || whitespace(c)) {
172 } else if (c === "!") {
173 this._state = BEFORE_DECLARATION;
174 this._sectionStart = this._index + 1;
175 } else if (c === "?") {
176 this._state = IN_PROCESSING_INSTRUCTION;
177 this._sectionStart = this._index + 1;
180 !this._xmlMode && (c === "s" || c === "S")
183 this._sectionStart = this._index;
187 Tokenizer.prototype._stateInTagName = function(c) {
188 if (c === "/" || c === ">" || whitespace(c)) {
189 this._emitToken("onopentagname");
190 this._state = BEFORE_ATTRIBUTE_NAME;
195 Tokenizer.prototype._stateBeforeCloseingTagName = function(c) {
197 else if (c === ">") {
199 } else if (this._special !== SPECIAL_NONE) {
200 if (c === "s" || c === "S") {
201 this._state = BEFORE_SPECIAL_END;
207 this._state = IN_CLOSING_TAG_NAME;
208 this._sectionStart = this._index;
212 Tokenizer.prototype._stateInCloseingTagName = function(c) {
213 if (c === ">" || whitespace(c)) {
214 this._emitToken("onclosetag");
215 this._state = AFTER_CLOSING_TAG_NAME;
220 Tokenizer.prototype._stateAfterCloseingTagName = function(c) {
221 //skip everything until ">"
224 this._sectionStart = this._index + 1;
228 Tokenizer.prototype._stateBeforeAttributeName = function(c) {
230 this._cbs.onopentagend();
232 this._sectionStart = this._index + 1;
233 } else if (c === "/") {
234 this._state = IN_SELF_CLOSING_TAG;
235 } else if (!whitespace(c)) {
236 this._state = IN_ATTRIBUTE_NAME;
237 this._sectionStart = this._index;
241 Tokenizer.prototype._stateInSelfClosingTag = function(c) {
243 this._cbs.onselfclosingtag();
245 this._sectionStart = this._index + 1;
246 } else if (!whitespace(c)) {
247 this._state = BEFORE_ATTRIBUTE_NAME;
252 Tokenizer.prototype._stateInAttributeName = function(c) {
253 if (c === "=" || c === "/" || c === ">" || whitespace(c)) {
254 this._cbs.onattribname(this._getSection());
255 this._sectionStart = -1;
256 this._state = AFTER_ATTRIBUTE_NAME;
261 Tokenizer.prototype._stateAfterAttributeName = function(c) {
263 this._state = BEFORE_ATTRIBUTE_VALUE;
264 } else if (c === "/" || c === ">") {
265 this._cbs.onattribend();
266 this._state = BEFORE_ATTRIBUTE_NAME;
268 } else if (!whitespace(c)) {
269 this._cbs.onattribend();
270 this._state = IN_ATTRIBUTE_NAME;
271 this._sectionStart = this._index;
275 Tokenizer.prototype._stateBeforeAttributeValue = function(c) {
277 this._state = IN_ATTRIBUTE_VALUE_DQ;
278 this._sectionStart = this._index + 1;
279 } else if (c === "'") {
280 this._state = IN_ATTRIBUTE_VALUE_SQ;
281 this._sectionStart = this._index + 1;
282 } else if (!whitespace(c)) {
283 this._state = IN_ATTRIBUTE_VALUE_NQ;
284 this._sectionStart = this._index;
285 this._index--; //reconsume token
289 Tokenizer.prototype._stateInAttributeValueDoubleQuotes = function(c) {
291 this._emitToken("onattribdata");
292 this._cbs.onattribend();
293 this._state = BEFORE_ATTRIBUTE_NAME;
294 } else if (this._decodeEntities && c === "&") {
295 this._emitToken("onattribdata");
296 this._baseState = this._state;
297 this._state = BEFORE_ENTITY;
298 this._sectionStart = this._index;
302 Tokenizer.prototype._stateInAttributeValueSingleQuotes = function(c) {
304 this._emitToken("onattribdata");
305 this._cbs.onattribend();
306 this._state = BEFORE_ATTRIBUTE_NAME;
307 } else if (this._decodeEntities && c === "&") {
308 this._emitToken("onattribdata");
309 this._baseState = this._state;
310 this._state = BEFORE_ENTITY;
311 this._sectionStart = this._index;
315 Tokenizer.prototype._stateInAttributeValueNoQuotes = function(c) {
316 if (whitespace(c) || c === ">") {
317 this._emitToken("onattribdata");
318 this._cbs.onattribend();
319 this._state = BEFORE_ATTRIBUTE_NAME;
321 } else if (this._decodeEntities && c === "&") {
322 this._emitToken("onattribdata");
323 this._baseState = this._state;
324 this._state = BEFORE_ENTITY;
325 this._sectionStart = this._index;
329 Tokenizer.prototype._stateBeforeDeclaration = function(c) {
338 Tokenizer.prototype._stateInDeclaration = function(c) {
340 this._cbs.ondeclaration(this._getSection());
342 this._sectionStart = this._index + 1;
346 Tokenizer.prototype._stateInProcessingInstruction = function(c) {
348 this._cbs.onprocessinginstruction(this._getSection());
350 this._sectionStart = this._index + 1;
354 Tokenizer.prototype._stateBeforeComment = function(c) {
356 this._state = IN_COMMENT;
357 this._sectionStart = this._index + 1;
359 this._state = IN_DECLARATION;
363 Tokenizer.prototype._stateInComment = function(c) {
364 if (c === "-") this._state = AFTER_COMMENT_1;
367 Tokenizer.prototype._stateAfterComment1 = function(c) {
369 this._state = AFTER_COMMENT_2;
371 this._state = IN_COMMENT;
375 Tokenizer.prototype._stateAfterComment2 = function(c) {
377 //remove 2 trailing chars
379 this._buffer.substring(this._sectionStart, this._index - 2)
382 this._sectionStart = this._index + 1;
383 } else if (c !== "-") {
384 this._state = IN_COMMENT;
386 // else: stay in AFTER_COMMENT_2 (`--->`)
389 Tokenizer.prototype._stateBeforeCdata1 = ifElseState(
394 Tokenizer.prototype._stateBeforeCdata2 = ifElseState(
399 Tokenizer.prototype._stateBeforeCdata3 = ifElseState(
404 Tokenizer.prototype._stateBeforeCdata4 = ifElseState(
409 Tokenizer.prototype._stateBeforeCdata5 = ifElseState(
415 Tokenizer.prototype._stateBeforeCdata6 = function(c) {
417 this._state = IN_CDATA;
418 this._sectionStart = this._index + 1;
420 this._state = IN_DECLARATION;
425 Tokenizer.prototype._stateInCdata = function(c) {
426 if (c === "]") this._state = AFTER_CDATA_1;
429 Tokenizer.prototype._stateAfterCdata1 = function(c) {
430 if (c === "]") this._state = AFTER_CDATA_2;
431 else this._state = IN_CDATA;
434 Tokenizer.prototype._stateAfterCdata2 = function(c) {
436 //remove 2 trailing chars
438 this._buffer.substring(this._sectionStart, this._index - 2)
441 this._sectionStart = this._index + 1;
442 } else if (c !== "]") {
443 this._state = IN_CDATA;
445 //else: stay in AFTER_CDATA_2 (`]]]>`)
448 Tokenizer.prototype._stateBeforeSpecial = function(c) {
449 if (c === "c" || c === "C") {
450 this._state = BEFORE_SCRIPT_1;
451 } else if (c === "t" || c === "T") {
452 this._state = BEFORE_STYLE_1;
454 this._state = IN_TAG_NAME;
455 this._index--; //consume the token again
459 Tokenizer.prototype._stateBeforeSpecialEnd = function(c) {
460 if (this._special === SPECIAL_SCRIPT && (c === "c" || c === "C")) {
461 this._state = AFTER_SCRIPT_1;
462 } else if (this._special === SPECIAL_STYLE && (c === "t" || c === "T")) {
463 this._state = AFTER_STYLE_1;
464 } else this._state = TEXT;
467 Tokenizer.prototype._stateBeforeScript1 = consumeSpecialNameChar(
471 Tokenizer.prototype._stateBeforeScript2 = consumeSpecialNameChar(
475 Tokenizer.prototype._stateBeforeScript3 = consumeSpecialNameChar(
479 Tokenizer.prototype._stateBeforeScript4 = consumeSpecialNameChar(
484 Tokenizer.prototype._stateBeforeScript5 = function(c) {
485 if (c === "/" || c === ">" || whitespace(c)) {
486 this._special = SPECIAL_SCRIPT;
488 this._state = IN_TAG_NAME;
489 this._index--; //consume the token again
492 Tokenizer.prototype._stateAfterScript1 = ifElseState("R", AFTER_SCRIPT_2, TEXT);
493 Tokenizer.prototype._stateAfterScript2 = ifElseState("I", AFTER_SCRIPT_3, TEXT);
494 Tokenizer.prototype._stateAfterScript3 = ifElseState("P", AFTER_SCRIPT_4, TEXT);
495 Tokenizer.prototype._stateAfterScript4 = ifElseState("T", AFTER_SCRIPT_5, TEXT);
497 Tokenizer.prototype._stateAfterScript5 = function(c) {
498 if (c === ">" || whitespace(c)) {
499 this._special = SPECIAL_NONE;
500 this._state = IN_CLOSING_TAG_NAME;
501 this._sectionStart = this._index - 6;
502 this._index--; //reconsume the token
503 } else this._state = TEXT;
506 Tokenizer.prototype._stateBeforeStyle1 = consumeSpecialNameChar(
510 Tokenizer.prototype._stateBeforeStyle2 = consumeSpecialNameChar(
514 Tokenizer.prototype._stateBeforeStyle3 = consumeSpecialNameChar(
519 Tokenizer.prototype._stateBeforeStyle4 = function(c) {
520 if (c === "/" || c === ">" || whitespace(c)) {
521 this._special = SPECIAL_STYLE;
523 this._state = IN_TAG_NAME;
524 this._index--; //consume the token again
527 Tokenizer.prototype._stateAfterStyle1 = ifElseState("Y", AFTER_STYLE_2, TEXT);
528 Tokenizer.prototype._stateAfterStyle2 = ifElseState("L", AFTER_STYLE_3, TEXT);
529 Tokenizer.prototype._stateAfterStyle3 = ifElseState("E", AFTER_STYLE_4, TEXT);
531 Tokenizer.prototype._stateAfterStyle4 = function(c) {
532 if (c === ">" || whitespace(c)) {
533 this._special = SPECIAL_NONE;
534 this._state = IN_CLOSING_TAG_NAME;
535 this._sectionStart = this._index - 5;
536 this._index--; //reconsume the token
537 } else this._state = TEXT;
540 Tokenizer.prototype._stateBeforeEntity = ifElseState(
542 BEFORE_NUMERIC_ENTITY,
545 Tokenizer.prototype._stateBeforeNumericEntity = ifElseState(
551 //for entities terminated with a semicolon
552 Tokenizer.prototype._parseNamedEntityStrict = function() {
554 if (this._sectionStart + 1 < this._index) {
555 var entity = this._buffer.substring(
556 this._sectionStart + 1,
559 map = this._xmlMode ? xmlMap : entityMap;
561 if (map.hasOwnProperty(entity)) {
562 this._emitPartial(map[entity]);
563 this._sectionStart = this._index + 1;
568 //parses legacy entities (without trailing semicolon)
569 Tokenizer.prototype._parseLegacyEntity = function() {
570 var start = this._sectionStart + 1,
571 limit = this._index - start;
573 if (limit > 6) limit = 6; //the max length of legacy entities is 6
576 //the min length of legacy entities is 2
577 var entity = this._buffer.substr(start, limit);
579 if (legacyMap.hasOwnProperty(entity)) {
580 this._emitPartial(legacyMap[entity]);
581 this._sectionStart += limit + 1;
589 Tokenizer.prototype._stateInNamedEntity = function(c) {
591 this._parseNamedEntityStrict();
592 if (this._sectionStart + 1 < this._index && !this._xmlMode) {
593 this._parseLegacyEntity();
595 this._state = this._baseState;
597 (c < "a" || c > "z") &&
598 (c < "A" || c > "Z") &&
602 else if (this._sectionStart + 1 === this._index);
603 else if (this._baseState !== TEXT) {
605 this._parseNamedEntityStrict();
608 this._parseLegacyEntity();
611 this._state = this._baseState;
616 Tokenizer.prototype._decodeNumericEntity = function(offset, base) {
617 var sectionStart = this._sectionStart + offset;
619 if (sectionStart !== this._index) {
621 var entity = this._buffer.substring(sectionStart, this._index);
622 var parsed = parseInt(entity, base);
624 this._emitPartial(decodeCodePoint(parsed));
625 this._sectionStart = this._index;
627 this._sectionStart--;
630 this._state = this._baseState;
633 Tokenizer.prototype._stateInNumericEntity = function(c) {
635 this._decodeNumericEntity(2, 10);
636 this._sectionStart++;
637 } else if (c < "0" || c > "9") {
638 if (!this._xmlMode) {
639 this._decodeNumericEntity(2, 10);
641 this._state = this._baseState;
647 Tokenizer.prototype._stateInHexEntity = function(c) {
649 this._decodeNumericEntity(3, 16);
650 this._sectionStart++;
652 (c < "a" || c > "f") &&
653 (c < "A" || c > "F") &&
656 if (!this._xmlMode) {
657 this._decodeNumericEntity(3, 16);
659 this._state = this._baseState;
665 Tokenizer.prototype._cleanup = function() {
666 if (this._sectionStart < 0) {
668 this._bufferOffset += this._index;
670 } else if (this._running) {
671 if (this._state === TEXT) {
672 if (this._sectionStart !== this._index) {
673 this._cbs.ontext(this._buffer.substr(this._sectionStart));
676 this._bufferOffset += this._index;
678 } else if (this._sectionStart === this._index) {
679 //the section just started
681 this._bufferOffset += this._index;
684 //remove everything unnecessary
685 this._buffer = this._buffer.substr(this._sectionStart);
686 this._index -= this._sectionStart;
687 this._bufferOffset += this._sectionStart;
690 this._sectionStart = 0;
694 //TODO make events conditional
695 Tokenizer.prototype.write = function(chunk) {
696 if (this._ended) this._cbs.onerror(Error(".write() after done!"));
698 this._buffer += chunk;
702 Tokenizer.prototype._parse = function() {
703 while (this._index < this._buffer.length && this._running) {
704 var c = this._buffer.charAt(this._index);
705 if (this._state === TEXT) {
707 } else if (this._state === BEFORE_TAG_NAME) {
708 this._stateBeforeTagName(c);
709 } else if (this._state === IN_TAG_NAME) {
710 this._stateInTagName(c);
711 } else if (this._state === BEFORE_CLOSING_TAG_NAME) {
712 this._stateBeforeCloseingTagName(c);
713 } else if (this._state === IN_CLOSING_TAG_NAME) {
714 this._stateInCloseingTagName(c);
715 } else if (this._state === AFTER_CLOSING_TAG_NAME) {
716 this._stateAfterCloseingTagName(c);
717 } else if (this._state === IN_SELF_CLOSING_TAG) {
718 this._stateInSelfClosingTag(c);
719 } else if (this._state === BEFORE_ATTRIBUTE_NAME) {
724 this._stateBeforeAttributeName(c);
725 } else if (this._state === IN_ATTRIBUTE_NAME) {
726 this._stateInAttributeName(c);
727 } else if (this._state === AFTER_ATTRIBUTE_NAME) {
728 this._stateAfterAttributeName(c);
729 } else if (this._state === BEFORE_ATTRIBUTE_VALUE) {
730 this._stateBeforeAttributeValue(c);
731 } else if (this._state === IN_ATTRIBUTE_VALUE_DQ) {
732 this._stateInAttributeValueDoubleQuotes(c);
733 } else if (this._state === IN_ATTRIBUTE_VALUE_SQ) {
734 this._stateInAttributeValueSingleQuotes(c);
735 } else if (this._state === IN_ATTRIBUTE_VALUE_NQ) {
736 this._stateInAttributeValueNoQuotes(c);
737 } else if (this._state === BEFORE_DECLARATION) {
742 this._stateBeforeDeclaration(c);
743 } else if (this._state === IN_DECLARATION) {
744 this._stateInDeclaration(c);
745 } else if (this._state === IN_PROCESSING_INSTRUCTION) {
748 * processing instructions
750 this._stateInProcessingInstruction(c);
751 } else if (this._state === BEFORE_COMMENT) {
756 this._stateBeforeComment(c);
757 } else if (this._state === IN_COMMENT) {
758 this._stateInComment(c);
759 } else if (this._state === AFTER_COMMENT_1) {
760 this._stateAfterComment1(c);
761 } else if (this._state === AFTER_COMMENT_2) {
762 this._stateAfterComment2(c);
763 } else if (this._state === BEFORE_CDATA_1) {
768 this._stateBeforeCdata1(c);
769 } else if (this._state === BEFORE_CDATA_2) {
770 this._stateBeforeCdata2(c);
771 } else if (this._state === BEFORE_CDATA_3) {
772 this._stateBeforeCdata3(c);
773 } else if (this._state === BEFORE_CDATA_4) {
774 this._stateBeforeCdata4(c);
775 } else if (this._state === BEFORE_CDATA_5) {
776 this._stateBeforeCdata5(c);
777 } else if (this._state === BEFORE_CDATA_6) {
778 this._stateBeforeCdata6(c);
779 } else if (this._state === IN_CDATA) {
780 this._stateInCdata(c);
781 } else if (this._state === AFTER_CDATA_1) {
782 this._stateAfterCdata1(c);
783 } else if (this._state === AFTER_CDATA_2) {
784 this._stateAfterCdata2(c);
785 } else if (this._state === BEFORE_SPECIAL) {
790 this._stateBeforeSpecial(c);
791 } else if (this._state === BEFORE_SPECIAL_END) {
792 this._stateBeforeSpecialEnd(c);
793 } else if (this._state === BEFORE_SCRIPT_1) {
798 this._stateBeforeScript1(c);
799 } else if (this._state === BEFORE_SCRIPT_2) {
800 this._stateBeforeScript2(c);
801 } else if (this._state === BEFORE_SCRIPT_3) {
802 this._stateBeforeScript3(c);
803 } else if (this._state === BEFORE_SCRIPT_4) {
804 this._stateBeforeScript4(c);
805 } else if (this._state === BEFORE_SCRIPT_5) {
806 this._stateBeforeScript5(c);
807 } else if (this._state === AFTER_SCRIPT_1) {
808 this._stateAfterScript1(c);
809 } else if (this._state === AFTER_SCRIPT_2) {
810 this._stateAfterScript2(c);
811 } else if (this._state === AFTER_SCRIPT_3) {
812 this._stateAfterScript3(c);
813 } else if (this._state === AFTER_SCRIPT_4) {
814 this._stateAfterScript4(c);
815 } else if (this._state === AFTER_SCRIPT_5) {
816 this._stateAfterScript5(c);
817 } else if (this._state === BEFORE_STYLE_1) {
822 this._stateBeforeStyle1(c);
823 } else if (this._state === BEFORE_STYLE_2) {
824 this._stateBeforeStyle2(c);
825 } else if (this._state === BEFORE_STYLE_3) {
826 this._stateBeforeStyle3(c);
827 } else if (this._state === BEFORE_STYLE_4) {
828 this._stateBeforeStyle4(c);
829 } else if (this._state === AFTER_STYLE_1) {
830 this._stateAfterStyle1(c);
831 } else if (this._state === AFTER_STYLE_2) {
832 this._stateAfterStyle2(c);
833 } else if (this._state === AFTER_STYLE_3) {
834 this._stateAfterStyle3(c);
835 } else if (this._state === AFTER_STYLE_4) {
836 this._stateAfterStyle4(c);
837 } else if (this._state === BEFORE_ENTITY) {
842 this._stateBeforeEntity(c);
843 } else if (this._state === BEFORE_NUMERIC_ENTITY) {
844 this._stateBeforeNumericEntity(c);
845 } else if (this._state === IN_NAMED_ENTITY) {
846 this._stateInNamedEntity(c);
847 } else if (this._state === IN_NUMERIC_ENTITY) {
848 this._stateInNumericEntity(c);
849 } else if (this._state === IN_HEX_ENTITY) {
850 this._stateInHexEntity(c);
852 this._cbs.onerror(Error("unknown _state"), this._state);
861 Tokenizer.prototype.pause = function() {
862 this._running = false;
864 Tokenizer.prototype.resume = function() {
865 this._running = true;
867 if (this._index < this._buffer.length) {
875 Tokenizer.prototype.end = function(chunk) {
876 if (this._ended) this._cbs.onerror(Error(".end() after done!"));
877 if (chunk) this.write(chunk);
881 if (this._running) this._finish();
884 Tokenizer.prototype._finish = function() {
885 //if there is remaining data, emit it in a reasonable way
886 if (this._sectionStart < this._index) {
887 this._handleTrailingData();
893 Tokenizer.prototype._handleTrailingData = function() {
894 var data = this._buffer.substr(this._sectionStart);
897 this._state === IN_CDATA ||
898 this._state === AFTER_CDATA_1 ||
899 this._state === AFTER_CDATA_2
901 this._cbs.oncdata(data);
903 this._state === IN_COMMENT ||
904 this._state === AFTER_COMMENT_1 ||
905 this._state === AFTER_COMMENT_2
907 this._cbs.oncomment(data);
908 } else if (this._state === IN_NAMED_ENTITY && !this._xmlMode) {
909 this._parseLegacyEntity();
910 if (this._sectionStart < this._index) {
911 this._state = this._baseState;
912 this._handleTrailingData();
914 } else if (this._state === IN_NUMERIC_ENTITY && !this._xmlMode) {
915 this._decodeNumericEntity(2, 10);
916 if (this._sectionStart < this._index) {
917 this._state = this._baseState;
918 this._handleTrailingData();
920 } else if (this._state === IN_HEX_ENTITY && !this._xmlMode) {
921 this._decodeNumericEntity(3, 16);
922 if (this._sectionStart < this._index) {
923 this._state = this._baseState;
924 this._handleTrailingData();
927 this._state !== IN_TAG_NAME &&
928 this._state !== BEFORE_ATTRIBUTE_NAME &&
929 this._state !== BEFORE_ATTRIBUTE_VALUE &&
930 this._state !== AFTER_ATTRIBUTE_NAME &&
931 this._state !== IN_ATTRIBUTE_NAME &&
932 this._state !== IN_ATTRIBUTE_VALUE_SQ &&
933 this._state !== IN_ATTRIBUTE_VALUE_DQ &&
934 this._state !== IN_ATTRIBUTE_VALUE_NQ &&
935 this._state !== IN_CLOSING_TAG_NAME
937 this._cbs.ontext(data);
939 //else, ignore remaining data
940 //TODO add a way to remove current tag
943 Tokenizer.prototype.reset = function() {
946 { xmlMode: this._xmlMode, decodeEntities: this._decodeEntities },
951 Tokenizer.prototype.getAbsoluteIndex = function() {
952 return this._bufferOffset + this._index;
955 Tokenizer.prototype._getSection = function() {
956 return this._buffer.substring(this._sectionStart, this._index);
959 Tokenizer.prototype._emitToken = function(name) {
960 this._cbs[name](this._getSection());
961 this._sectionStart = -1;
964 Tokenizer.prototype._emitPartial = function(value) {
965 if (this._baseState !== TEXT) {
966 this._cbs.onattribdata(value); //TODO implement the new event
968 this._cbs.ontext(value);