react / wstein / node_modules / jest-cli / node_modules / jsdom / node_modules / htmlparser2 / lib / Tokenizer.js
80684 viewsmodule.exports = Tokenizer;12var decodeCodePoint = require("entities/lib/decode_codepoint.js"),3entityMap = require("entities/maps/entities.json"),4legacyMap = require("entities/maps/legacy.json"),5xmlMap = require("entities/maps/xml.json"),67i = 0,89TEXT = i++,10BEFORE_TAG_NAME = i++, //after <11IN_TAG_NAME = i++,12IN_SELF_CLOSING_TAG = i++,13BEFORE_CLOSING_TAG_NAME = i++,14IN_CLOSING_TAG_NAME = i++,15AFTER_CLOSING_TAG_NAME = i++,1617//attributes18BEFORE_ATTRIBUTE_NAME = i++,19IN_ATTRIBUTE_NAME = i++,20AFTER_ATTRIBUTE_NAME = i++,21BEFORE_ATTRIBUTE_VALUE = i++,22IN_ATTRIBUTE_VALUE_DQ = i++, // "23IN_ATTRIBUTE_VALUE_SQ = i++, // '24IN_ATTRIBUTE_VALUE_NQ = i++,2526//declarations27BEFORE_DECLARATION = i++, // !28IN_DECLARATION = i++,2930//processing instructions31IN_PROCESSING_INSTRUCTION = i++, // ?3233//comments34BEFORE_COMMENT = i++,35IN_COMMENT = i++,36AFTER_COMMENT_1 = i++,37AFTER_COMMENT_2 = i++,3839//cdata40BEFORE_CDATA_1 = i++, // [41BEFORE_CDATA_2 = i++, // C42BEFORE_CDATA_3 = i++, // D43BEFORE_CDATA_4 = i++, // A44BEFORE_CDATA_5 = i++, // T45BEFORE_CDATA_6 = i++, // A46IN_CDATA = i++, // [47AFTER_CDATA_1 = i++, // ]48AFTER_CDATA_2 = i++, // ]4950//special tags51BEFORE_SPECIAL = i++, //S52BEFORE_SPECIAL_END = i++, //S5354BEFORE_SCRIPT_1 = i++, //C55BEFORE_SCRIPT_2 = i++, //R56BEFORE_SCRIPT_3 = i++, //I57BEFORE_SCRIPT_4 = i++, //P58BEFORE_SCRIPT_5 = i++, //T59AFTER_SCRIPT_1 = i++, //C60AFTER_SCRIPT_2 = i++, //R61AFTER_SCRIPT_3 = i++, //I62AFTER_SCRIPT_4 = i++, //P63AFTER_SCRIPT_5 = i++, //T6465BEFORE_STYLE_1 = i++, //T66BEFORE_STYLE_2 = i++, //Y67BEFORE_STYLE_3 = i++, //L68BEFORE_STYLE_4 = i++, //E69AFTER_STYLE_1 = i++, //T70AFTER_STYLE_2 = i++, //Y71AFTER_STYLE_3 = i++, //L72AFTER_STYLE_4 = i++, //E7374BEFORE_ENTITY = i++, //&75BEFORE_NUMERIC_ENTITY = i++, //#76IN_NAMED_ENTITY = i++,77IN_NUMERIC_ENTITY = i++,78IN_HEX_ENTITY = i++, //X7980j = 0,8182SPECIAL_NONE = j++,83SPECIAL_SCRIPT = j++,84SPECIAL_STYLE = j++;8586function whitespace(c){87return c === " " || c === "\n" || c === "\t" || c === "\f" || c === "\r";88}8990function characterState(char, SUCCESS){91return function(c){92if(c === char) this._state = SUCCESS;93};94}9596function ifElseState(upper, SUCCESS, FAILURE){97var lower = upper.toLowerCase();9899if(upper === lower){100return function(c){101if(c === lower){102this._state = SUCCESS;103} else {104this._state = FAILURE;105this._index--;106}107};108} else {109return function(c){110if(c === lower || c === upper){111this._state = SUCCESS;112} else {113this._state = FAILURE;114this._index--;115}116};117}118}119120function consumeSpecialNameChar(upper, NEXT_STATE){121var lower = upper.toLowerCase();122123return function(c){124if(c === lower || c === upper){125this._state = NEXT_STATE;126} else {127this._state = IN_TAG_NAME;128this._index--; //consume the token again129}130};131}132133function Tokenizer(options, cbs){134this._state = TEXT;135this._buffer = "";136this._sectionStart = 0;137this._index = 0;138this._bufferOffset = 0; //chars removed from _buffer139this._baseState = TEXT;140this._special = SPECIAL_NONE;141this._cbs = cbs;142this._running = true;143this._ended = false;144this._xmlMode = !!(options && options.xmlMode);145this._decodeEntities = !!(options && options.decodeEntities);146}147148Tokenizer.prototype._stateText = function(c){149if(c === "<"){150if(this._index > this._sectionStart){151this._cbs.ontext(this._getSection());152}153this._state = BEFORE_TAG_NAME;154this._sectionStart = this._index;155} else if(this._decodeEntities && this._special === SPECIAL_NONE && c === "&"){156if(this._index > this._sectionStart){157this._cbs.ontext(this._getSection());158}159this._baseState = TEXT;160this._state = BEFORE_ENTITY;161this._sectionStart = this._index;162}163};164165Tokenizer.prototype._stateBeforeTagName = function(c){166if(c === "/"){167this._state = BEFORE_CLOSING_TAG_NAME;168} else if(c === ">" || this._special !== SPECIAL_NONE || whitespace(c)) {169this._state = TEXT;170} else if(c === "!"){171this._state = BEFORE_DECLARATION;172this._sectionStart = this._index + 1;173} else if(c === "?"){174this._state = IN_PROCESSING_INSTRUCTION;175this._sectionStart = this._index + 1;176} else if(c === "<"){177this._cbs.ontext(this._getSection());178this._sectionStart = this._index;179} else {180this._state = (!this._xmlMode && (c === "s" || c === "S")) ?181BEFORE_SPECIAL : IN_TAG_NAME;182this._sectionStart = this._index;183}184};185186Tokenizer.prototype._stateInTagName = function(c){187if(c === "/" || c === ">" || whitespace(c)){188this._emitToken("onopentagname");189this._state = BEFORE_ATTRIBUTE_NAME;190this._index--;191}192};193194Tokenizer.prototype._stateBeforeCloseingTagName = function(c){195if(whitespace(c));196else if(c === ">"){197this._state = TEXT;198} else if(this._special !== SPECIAL_NONE){199if(c === "s" || c === "S"){200this._state = BEFORE_SPECIAL_END;201} else {202this._state = TEXT;203this._index--;204}205} else {206this._state = IN_CLOSING_TAG_NAME;207this._sectionStart = this._index;208}209};210211Tokenizer.prototype._stateInCloseingTagName = function(c){212if(c === ">" || whitespace(c)){213this._emitToken("onclosetag");214this._state = AFTER_CLOSING_TAG_NAME;215this._index--;216}217};218219Tokenizer.prototype._stateAfterCloseingTagName = function(c){220//skip everything until ">"221if(c === ">"){222this._state = TEXT;223this._sectionStart = this._index + 1;224}225};226227Tokenizer.prototype._stateBeforeAttributeName = function(c){228if(c === ">"){229this._cbs.onopentagend();230this._state = TEXT;231this._sectionStart = this._index + 1;232} else if(c === "/"){233this._state = IN_SELF_CLOSING_TAG;234} else if(!whitespace(c)){235this._state = IN_ATTRIBUTE_NAME;236this._sectionStart = this._index;237}238};239240Tokenizer.prototype._stateInSelfClosingTag = function(c){241if(c === ">"){242this._cbs.onselfclosingtag();243this._state = TEXT;244this._sectionStart = this._index + 1;245} else if(!whitespace(c)){246this._state = BEFORE_ATTRIBUTE_NAME;247this._index--;248}249};250251Tokenizer.prototype._stateInAttributeName = function(c){252if(c === "=" || c === "/" || c === ">" || whitespace(c)){253this._cbs.onattribname(this._getSection());254this._sectionStart = -1;255this._state = AFTER_ATTRIBUTE_NAME;256this._index--;257}258};259260Tokenizer.prototype._stateAfterAttributeName = function(c){261if(c === "="){262this._state = BEFORE_ATTRIBUTE_VALUE;263} else if(c === "/" || c === ">"){264this._cbs.onattribend();265this._state = BEFORE_ATTRIBUTE_NAME;266this._index--;267} else if(!whitespace(c)){268this._cbs.onattribend();269this._state = IN_ATTRIBUTE_NAME;270this._sectionStart = this._index;271}272};273274Tokenizer.prototype._stateBeforeAttributeValue = function(c){275if(c === "\""){276this._state = IN_ATTRIBUTE_VALUE_DQ;277this._sectionStart = this._index + 1;278} else if(c === "'"){279this._state = IN_ATTRIBUTE_VALUE_SQ;280this._sectionStart = this._index + 1;281} else if(!whitespace(c)){282this._state = IN_ATTRIBUTE_VALUE_NQ;283this._sectionStart = this._index;284this._index--; //reconsume token285}286};287288Tokenizer.prototype._stateInAttributeValueDoubleQuotes = function(c){289if(c === "\""){290this._emitToken("onattribdata");291this._cbs.onattribend();292this._state = BEFORE_ATTRIBUTE_NAME;293} else if(this._decodeEntities && c === "&"){294this._emitToken("onattribdata");295this._baseState = this._state;296this._state = BEFORE_ENTITY;297this._sectionStart = this._index;298}299};300301Tokenizer.prototype._stateInAttributeValueSingleQuotes = function(c){302if(c === "'"){303this._emitToken("onattribdata");304this._cbs.onattribend();305this._state = BEFORE_ATTRIBUTE_NAME;306} else if(this._decodeEntities && c === "&"){307this._emitToken("onattribdata");308this._baseState = this._state;309this._state = BEFORE_ENTITY;310this._sectionStart = this._index;311}312};313314Tokenizer.prototype._stateInAttributeValueNoQuotes = function(c){315if(whitespace(c) || c === ">"){316this._emitToken("onattribdata");317this._cbs.onattribend();318this._state = BEFORE_ATTRIBUTE_NAME;319this._index--;320} else if(this._decodeEntities && c === "&"){321this._emitToken("onattribdata");322this._baseState = this._state;323this._state = BEFORE_ENTITY;324this._sectionStart = this._index;325}326};327328Tokenizer.prototype._stateBeforeDeclaration = function(c){329this._state = c === "[" ? BEFORE_CDATA_1 :330c === "-" ? BEFORE_COMMENT :331IN_DECLARATION;332};333334Tokenizer.prototype._stateInDeclaration = function(c){335if(c === ">"){336this._cbs.ondeclaration(this._getSection());337this._state = TEXT;338this._sectionStart = this._index + 1;339}340};341342Tokenizer.prototype._stateInProcessingInstruction = function(c){343if(c === ">"){344this._cbs.onprocessinginstruction(this._getSection());345this._state = TEXT;346this._sectionStart = this._index + 1;347}348};349350Tokenizer.prototype._stateBeforeComment = function(c){351if(c === "-"){352this._state = IN_COMMENT;353this._sectionStart = this._index + 1;354} else {355this._state = IN_DECLARATION;356}357};358359Tokenizer.prototype._stateInComment = function(c){360if(c === "-") this._state = AFTER_COMMENT_1;361};362363Tokenizer.prototype._stateAfterComment1 = function(c){364if(c === "-"){365this._state = AFTER_COMMENT_2;366} else {367this._state = IN_COMMENT;368}369};370371Tokenizer.prototype._stateAfterComment2 = function(c){372if(c === ">"){373//remove 2 trailing chars374this._cbs.oncomment(this._buffer.substring(this._sectionStart, this._index - 2));375this._state = TEXT;376this._sectionStart = this._index + 1;377} else if(c !== "-"){378this._state = IN_COMMENT;379}380// else: stay in AFTER_COMMENT_2 (`--->`)381};382383Tokenizer.prototype._stateBeforeCdata1 = ifElseState("C", BEFORE_CDATA_2, IN_DECLARATION);384Tokenizer.prototype._stateBeforeCdata2 = ifElseState("D", BEFORE_CDATA_3, IN_DECLARATION);385Tokenizer.prototype._stateBeforeCdata3 = ifElseState("A", BEFORE_CDATA_4, IN_DECLARATION);386Tokenizer.prototype._stateBeforeCdata4 = ifElseState("T", BEFORE_CDATA_5, IN_DECLARATION);387Tokenizer.prototype._stateBeforeCdata5 = ifElseState("A", BEFORE_CDATA_6, IN_DECLARATION);388389Tokenizer.prototype._stateBeforeCdata6 = function(c){390if(c === "["){391this._state = IN_CDATA;392this._sectionStart = this._index + 1;393} else {394this._state = IN_DECLARATION;395this._index--;396}397};398399Tokenizer.prototype._stateInCdata = function(c){400if(c === "]") this._state = AFTER_CDATA_1;401};402403Tokenizer.prototype._stateAfterCdata1 = characterState("]", AFTER_CDATA_2);404405Tokenizer.prototype._stateAfterCdata2 = function(c){406if(c === ">"){407//remove 2 trailing chars408this._cbs.oncdata(this._buffer.substring(this._sectionStart, this._index - 2));409this._state = TEXT;410this._sectionStart = this._index + 1;411} else if(c !== "]") {412this._state = IN_CDATA;413}414//else: stay in AFTER_CDATA_2 (`]]]>`)415};416417Tokenizer.prototype._stateBeforeSpecial = function(c){418if(c === "c" || c === "C"){419this._state = BEFORE_SCRIPT_1;420} else if(c === "t" || c === "T"){421this._state = BEFORE_STYLE_1;422} else {423this._state = IN_TAG_NAME;424this._index--; //consume the token again425}426};427428Tokenizer.prototype._stateBeforeSpecialEnd = function(c){429if(this._special === SPECIAL_SCRIPT && (c === "c" || c === "C")){430this._state = AFTER_SCRIPT_1;431} else if(this._special === SPECIAL_STYLE && (c === "t" || c === "T")){432this._state = AFTER_STYLE_1;433}434else this._state = TEXT;435};436437Tokenizer.prototype._stateBeforeScript1 = consumeSpecialNameChar("R", BEFORE_SCRIPT_2);438Tokenizer.prototype._stateBeforeScript2 = consumeSpecialNameChar("I", BEFORE_SCRIPT_3);439Tokenizer.prototype._stateBeforeScript3 = consumeSpecialNameChar("P", BEFORE_SCRIPT_4);440Tokenizer.prototype._stateBeforeScript4 = consumeSpecialNameChar("T", BEFORE_SCRIPT_5);441442Tokenizer.prototype._stateBeforeScript5 = function(c){443if(c === "/" || c === ">" || whitespace(c)){444this._special = SPECIAL_SCRIPT;445}446this._state = IN_TAG_NAME;447this._index--; //consume the token again448};449450Tokenizer.prototype._stateAfterScript1 = ifElseState("R", AFTER_SCRIPT_2, TEXT);451Tokenizer.prototype._stateAfterScript2 = ifElseState("I", AFTER_SCRIPT_3, TEXT);452Tokenizer.prototype._stateAfterScript3 = ifElseState("P", AFTER_SCRIPT_4, TEXT);453Tokenizer.prototype._stateAfterScript4 = ifElseState("T", AFTER_SCRIPT_5, TEXT);454455Tokenizer.prototype._stateAfterScript5 = function(c){456if(c === ">" || whitespace(c)){457this._special = SPECIAL_NONE;458this._state = IN_CLOSING_TAG_NAME;459this._sectionStart = this._index - 6;460this._index--; //reconsume the token461}462else this._state = TEXT;463};464465Tokenizer.prototype._stateBeforeStyle1 = consumeSpecialNameChar("Y", BEFORE_STYLE_2);466Tokenizer.prototype._stateBeforeStyle2 = consumeSpecialNameChar("L", BEFORE_STYLE_3);467Tokenizer.prototype._stateBeforeStyle3 = consumeSpecialNameChar("E", BEFORE_STYLE_4);468469Tokenizer.prototype._stateBeforeStyle4 = function(c){470if(c === "/" || c === ">" || whitespace(c)){471this._special = SPECIAL_STYLE;472}473this._state = IN_TAG_NAME;474this._index--; //consume the token again475};476477Tokenizer.prototype._stateAfterStyle1 = ifElseState("Y", AFTER_STYLE_2, TEXT);478Tokenizer.prototype._stateAfterStyle2 = ifElseState("L", AFTER_STYLE_3, TEXT);479Tokenizer.prototype._stateAfterStyle3 = ifElseState("E", AFTER_STYLE_4, TEXT);480481Tokenizer.prototype._stateAfterStyle4 = function(c){482if(c === ">" || whitespace(c)){483this._special = SPECIAL_NONE;484this._state = IN_CLOSING_TAG_NAME;485this._sectionStart = this._index - 5;486this._index--; //reconsume the token487}488else this._state = TEXT;489};490491Tokenizer.prototype._stateBeforeEntity = ifElseState("#", BEFORE_NUMERIC_ENTITY, IN_NAMED_ENTITY);492Tokenizer.prototype._stateBeforeNumericEntity = ifElseState("X", IN_HEX_ENTITY, IN_NUMERIC_ENTITY);493494//for entities terminated with a semicolon495Tokenizer.prototype._parseNamedEntityStrict = function(){496//offset = 1497if(this._sectionStart + 1 < this._index){498var entity = this._buffer.substring(this._sectionStart + 1, this._index),499map = this._xmlMode ? xmlMap : entityMap;500501if(map.hasOwnProperty(entity)){502this._emitPartial(map[entity]);503this._sectionStart = this._index + 1;504}505}506};507508509//parses legacy entities (without trailing semicolon)510Tokenizer.prototype._parseLegacyEntity = function(){511var start = this._sectionStart + 1,512limit = this._index - start;513514if(limit > 6) limit = 6; //the max length of legacy entities is 6515516while(limit >= 2){ //the min length of legacy entities is 2517var entity = this._buffer.substr(start, limit);518519if(legacyMap.hasOwnProperty(entity)){520this._emitPartial(legacyMap[entity]);521this._sectionStart += limit + 1;522return;523} else {524limit--;525}526}527};528529Tokenizer.prototype._stateInNamedEntity = function(c){530if(c === ";"){531this._parseNamedEntityStrict();532if(this._sectionStart + 1 < this._index && !this._xmlMode){533this._parseLegacyEntity();534}535this._state = this._baseState;536} else if((c < "a" || c > "z") && (c < "A" || c > "Z") && (c < "0" || c > "9")){537if(this._xmlMode);538else if(this._sectionStart + 1 === this._index);539else if(this._baseState !== TEXT){540if(c !== "="){541this._parseNamedEntityStrict();542}543} else {544this._parseLegacyEntity();545}546547this._state = this._baseState;548this._index--;549}550};551552Tokenizer.prototype._decodeNumericEntity = function(offset, base){553var sectionStart = this._sectionStart + offset;554555if(sectionStart !== this._index){556//parse entity557var entity = this._buffer.substring(sectionStart, this._index);558var parsed = parseInt(entity, base);559560this._emitPartial(decodeCodePoint(parsed));561this._sectionStart = this._index;562} else {563this._sectionStart--;564}565566this._state = this._baseState;567};568569Tokenizer.prototype._stateInNumericEntity = function(c){570if(c === ";"){571this._decodeNumericEntity(2, 10);572this._sectionStart++;573} else if(c < "0" || c > "9"){574if(!this._xmlMode){575this._decodeNumericEntity(2, 10);576} else {577this._state = this._baseState;578}579this._index--;580}581};582583Tokenizer.prototype._stateInHexEntity = function(c){584if(c === ";"){585this._decodeNumericEntity(3, 16);586this._sectionStart++;587} else if((c < "a" || c > "f") && (c < "A" || c > "F") && (c < "0" || c > "9")){588if(!this._xmlMode){589this._decodeNumericEntity(3, 16);590} else {591this._state = this._baseState;592}593this._index--;594}595};596597Tokenizer.prototype._cleanup = function (){598if(this._sectionStart < 0){599this._buffer = "";600this._index = 0;601this._bufferOffset += this._index;602} else if(this._running){603if(this._state === TEXT){604if(this._sectionStart !== this._index){605this._cbs.ontext(this._buffer.substr(this._sectionStart));606}607this._buffer = "";608this._index = 0;609this._bufferOffset += this._index;610} else if(this._sectionStart === this._index){611//the section just started612this._buffer = "";613this._index = 0;614this._bufferOffset += this._index;615} else {616//remove everything unnecessary617this._buffer = this._buffer.substr(this._sectionStart);618this._index -= this._sectionStart;619this._bufferOffset += this._sectionStart;620}621622this._sectionStart = 0;623}624};625626//TODO make events conditional627Tokenizer.prototype.write = function(chunk){628if(this._ended) this._cbs.onerror(Error(".write() after done!"));629630this._buffer += chunk;631this._parse();632};633634Tokenizer.prototype._parse = function(){635while(this._index < this._buffer.length && this._running){636var c = this._buffer.charAt(this._index);637if(this._state === TEXT) {638this._stateText(c);639} else if(this._state === BEFORE_TAG_NAME){640this._stateBeforeTagName(c);641} else if(this._state === IN_TAG_NAME) {642this._stateInTagName(c);643} else if(this._state === BEFORE_CLOSING_TAG_NAME){644this._stateBeforeCloseingTagName(c);645} else if(this._state === IN_CLOSING_TAG_NAME){646this._stateInCloseingTagName(c);647} else if(this._state === AFTER_CLOSING_TAG_NAME){648this._stateAfterCloseingTagName(c);649} else if(this._state === IN_SELF_CLOSING_TAG){650this._stateInSelfClosingTag(c);651}652653/*654* attributes655*/656else if(this._state === BEFORE_ATTRIBUTE_NAME){657this._stateBeforeAttributeName(c);658} else if(this._state === IN_ATTRIBUTE_NAME){659this._stateInAttributeName(c);660} else if(this._state === AFTER_ATTRIBUTE_NAME){661this._stateAfterAttributeName(c);662} else if(this._state === BEFORE_ATTRIBUTE_VALUE){663this._stateBeforeAttributeValue(c);664} else if(this._state === IN_ATTRIBUTE_VALUE_DQ){665this._stateInAttributeValueDoubleQuotes(c);666} else if(this._state === IN_ATTRIBUTE_VALUE_SQ){667this._stateInAttributeValueSingleQuotes(c);668} else if(this._state === IN_ATTRIBUTE_VALUE_NQ){669this._stateInAttributeValueNoQuotes(c);670}671672/*673* declarations674*/675else if(this._state === BEFORE_DECLARATION){676this._stateBeforeDeclaration(c);677} else if(this._state === IN_DECLARATION){678this._stateInDeclaration(c);679}680681/*682* processing instructions683*/684else if(this._state === IN_PROCESSING_INSTRUCTION){685this._stateInProcessingInstruction(c);686}687688/*689* comments690*/691else if(this._state === BEFORE_COMMENT){692this._stateBeforeComment(c);693} else if(this._state === IN_COMMENT){694this._stateInComment(c);695} else if(this._state === AFTER_COMMENT_1){696this._stateAfterComment1(c);697} else if(this._state === AFTER_COMMENT_2){698this._stateAfterComment2(c);699}700701/*702* cdata703*/704else if(this._state === BEFORE_CDATA_1){705this._stateBeforeCdata1(c);706} else if(this._state === BEFORE_CDATA_2){707this._stateBeforeCdata2(c);708} else if(this._state === BEFORE_CDATA_3){709this._stateBeforeCdata3(c);710} else if(this._state === BEFORE_CDATA_4){711this._stateBeforeCdata4(c);712} else if(this._state === BEFORE_CDATA_5){713this._stateBeforeCdata5(c);714} else if(this._state === BEFORE_CDATA_6){715this._stateBeforeCdata6(c);716} else if(this._state === IN_CDATA){717this._stateInCdata(c);718} else if(this._state === AFTER_CDATA_1){719this._stateAfterCdata1(c);720} else if(this._state === AFTER_CDATA_2){721this._stateAfterCdata2(c);722}723724/*725* special tags726*/727else if(this._state === BEFORE_SPECIAL){728this._stateBeforeSpecial(c);729} else if(this._state === BEFORE_SPECIAL_END){730this._stateBeforeSpecialEnd(c);731}732733/*734* script735*/736else if(this._state === BEFORE_SCRIPT_1){737this._stateBeforeScript1(c);738} else if(this._state === BEFORE_SCRIPT_2){739this._stateBeforeScript2(c);740} else if(this._state === BEFORE_SCRIPT_3){741this._stateBeforeScript3(c);742} else if(this._state === BEFORE_SCRIPT_4){743this._stateBeforeScript4(c);744} else if(this._state === BEFORE_SCRIPT_5){745this._stateBeforeScript5(c);746}747748else if(this._state === AFTER_SCRIPT_1){749this._stateAfterScript1(c);750} else if(this._state === AFTER_SCRIPT_2){751this._stateAfterScript2(c);752} else if(this._state === AFTER_SCRIPT_3){753this._stateAfterScript3(c);754} else if(this._state === AFTER_SCRIPT_4){755this._stateAfterScript4(c);756} else if(this._state === AFTER_SCRIPT_5){757this._stateAfterScript5(c);758}759760/*761* style762*/763else if(this._state === BEFORE_STYLE_1){764this._stateBeforeStyle1(c);765} else if(this._state === BEFORE_STYLE_2){766this._stateBeforeStyle2(c);767} else if(this._state === BEFORE_STYLE_3){768this._stateBeforeStyle3(c);769} else if(this._state === BEFORE_STYLE_4){770this._stateBeforeStyle4(c);771}772773else if(this._state === AFTER_STYLE_1){774this._stateAfterStyle1(c);775} else if(this._state === AFTER_STYLE_2){776this._stateAfterStyle2(c);777} else if(this._state === AFTER_STYLE_3){778this._stateAfterStyle3(c);779} else if(this._state === AFTER_STYLE_4){780this._stateAfterStyle4(c);781}782783/*784* entities785*/786else if(this._state === BEFORE_ENTITY){787this._stateBeforeEntity(c);788} else if(this._state === BEFORE_NUMERIC_ENTITY){789this._stateBeforeNumericEntity(c);790} else if(this._state === IN_NAMED_ENTITY){791this._stateInNamedEntity(c);792} else if(this._state === IN_NUMERIC_ENTITY){793this._stateInNumericEntity(c);794} else if(this._state === IN_HEX_ENTITY){795this._stateInHexEntity(c);796}797798else {799this._cbs.onerror(Error("unknown _state"), this._state);800}801802this._index++;803}804805this._cleanup();806};807808Tokenizer.prototype.pause = function(){809this._running = false;810};811Tokenizer.prototype.resume = function(){812this._running = true;813814if(this._index < this._buffer.length){815this._parse();816}817if(this._ended){818this._finish();819}820};821822Tokenizer.prototype.end = function(chunk){823if(this._ended) this._cbs.onerror(Error(".end() after done!"));824if(chunk) this.write(chunk);825826this._ended = true;827828if(this._running) this._finish();829};830831Tokenizer.prototype._finish = function(){832//if there is remaining data, emit it in a reasonable way833if(this._sectionStart < this._index){834this._handleTrailingData();835}836837this._cbs.onend();838};839840Tokenizer.prototype._handleTrailingData = function(){841var data = this._buffer.substr(this._sectionStart);842843if(this._state === IN_CDATA || this._state === AFTER_CDATA_1 || this._state === AFTER_CDATA_2){844this._cbs.oncdata(data);845} else if(this._state === IN_COMMENT || this._state === AFTER_COMMENT_1 || this._state === AFTER_COMMENT_2){846this._cbs.oncomment(data);847} else if(this._state === IN_NAMED_ENTITY && !this._xmlMode){848this._parseLegacyEntity();849if(this._sectionStart < this._index){850this._state = this._baseState;851this._handleTrailingData();852}853} else if(this._state === IN_NUMERIC_ENTITY && !this._xmlMode){854this._decodeNumericEntity(2, 10);855if(this._sectionStart < this._index){856this._state = this._baseState;857this._handleTrailingData();858}859} else if(this._state === IN_HEX_ENTITY && !this._xmlMode){860this._decodeNumericEntity(3, 16);861if(this._sectionStart < this._index){862this._state = this._baseState;863this._handleTrailingData();864}865} else if(866this._state !== IN_TAG_NAME &&867this._state !== BEFORE_ATTRIBUTE_NAME &&868this._state !== BEFORE_ATTRIBUTE_VALUE &&869this._state !== AFTER_ATTRIBUTE_NAME &&870this._state !== IN_ATTRIBUTE_NAME &&871this._state !== IN_ATTRIBUTE_VALUE_SQ &&872this._state !== IN_ATTRIBUTE_VALUE_DQ &&873this._state !== IN_ATTRIBUTE_VALUE_NQ &&874this._state !== IN_CLOSING_TAG_NAME875){876this._cbs.ontext(data);877}878//else, ignore remaining data879//TODO add a way to remove current tag880};881882Tokenizer.prototype.reset = function(){883Tokenizer.call(this, {xmlMode: this._xmlMode, decodeEntities: this._decodeEntities}, this._cbs);884};885886Tokenizer.prototype.getAbsoluteIndex = function(){887return this._bufferOffset + this._index;888};889890Tokenizer.prototype._getSection = function(){891return this._buffer.substring(this._sectionStart, this._index);892};893894Tokenizer.prototype._emitToken = function(name){895this._cbs[name](this._getSection());896this._sectionStart = -1;897};898899Tokenizer.prototype._emitPartial = function(value){900if(this._baseState !== TEXT){901this._cbs.onattribdata(value); //TODO implement the new event902} else {903this._cbs.ontext(value);904}905};906907908