123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196 |
- 'use strict';
- const Preprocessor = require('./preprocessor');
- const unicode = require('../common/unicode');
- const neTree = require('./named-entity-data');
- const ERR = require('../common/error-codes');
- //Aliases
- const $ = unicode.CODE_POINTS;
- const $$ = unicode.CODE_POINT_SEQUENCES;
- //C1 Unicode control character reference replacements
- const C1_CONTROLS_REFERENCE_REPLACEMENTS = {
- 0x80: 0x20ac,
- 0x82: 0x201a,
- 0x83: 0x0192,
- 0x84: 0x201e,
- 0x85: 0x2026,
- 0x86: 0x2020,
- 0x87: 0x2021,
- 0x88: 0x02c6,
- 0x89: 0x2030,
- 0x8a: 0x0160,
- 0x8b: 0x2039,
- 0x8c: 0x0152,
- 0x8e: 0x017d,
- 0x91: 0x2018,
- 0x92: 0x2019,
- 0x93: 0x201c,
- 0x94: 0x201d,
- 0x95: 0x2022,
- 0x96: 0x2013,
- 0x97: 0x2014,
- 0x98: 0x02dc,
- 0x99: 0x2122,
- 0x9a: 0x0161,
- 0x9b: 0x203a,
- 0x9c: 0x0153,
- 0x9e: 0x017e,
- 0x9f: 0x0178
- };
- // Named entity tree flags
- const HAS_DATA_FLAG = 1 << 0;
- const DATA_DUPLET_FLAG = 1 << 1;
- const HAS_BRANCHES_FLAG = 1 << 2;
- const MAX_BRANCH_MARKER_VALUE = HAS_DATA_FLAG | DATA_DUPLET_FLAG | HAS_BRANCHES_FLAG;
- //States
- const DATA_STATE = 'DATA_STATE';
- const RCDATA_STATE = 'RCDATA_STATE';
- const RAWTEXT_STATE = 'RAWTEXT_STATE';
- const SCRIPT_DATA_STATE = 'SCRIPT_DATA_STATE';
- const PLAINTEXT_STATE = 'PLAINTEXT_STATE';
- const TAG_OPEN_STATE = 'TAG_OPEN_STATE';
- const END_TAG_OPEN_STATE = 'END_TAG_OPEN_STATE';
- const TAG_NAME_STATE = 'TAG_NAME_STATE';
- const RCDATA_LESS_THAN_SIGN_STATE = 'RCDATA_LESS_THAN_SIGN_STATE';
- const RCDATA_END_TAG_OPEN_STATE = 'RCDATA_END_TAG_OPEN_STATE';
- const RCDATA_END_TAG_NAME_STATE = 'RCDATA_END_TAG_NAME_STATE';
- const RAWTEXT_LESS_THAN_SIGN_STATE = 'RAWTEXT_LESS_THAN_SIGN_STATE';
- const RAWTEXT_END_TAG_OPEN_STATE = 'RAWTEXT_END_TAG_OPEN_STATE';
- const RAWTEXT_END_TAG_NAME_STATE = 'RAWTEXT_END_TAG_NAME_STATE';
- const SCRIPT_DATA_LESS_THAN_SIGN_STATE = 'SCRIPT_DATA_LESS_THAN_SIGN_STATE';
- const SCRIPT_DATA_END_TAG_OPEN_STATE = 'SCRIPT_DATA_END_TAG_OPEN_STATE';
- const SCRIPT_DATA_END_TAG_NAME_STATE = 'SCRIPT_DATA_END_TAG_NAME_STATE';
- const SCRIPT_DATA_ESCAPE_START_STATE = 'SCRIPT_DATA_ESCAPE_START_STATE';
- const SCRIPT_DATA_ESCAPE_START_DASH_STATE = 'SCRIPT_DATA_ESCAPE_START_DASH_STATE';
- const SCRIPT_DATA_ESCAPED_STATE = 'SCRIPT_DATA_ESCAPED_STATE';
- const SCRIPT_DATA_ESCAPED_DASH_STATE = 'SCRIPT_DATA_ESCAPED_DASH_STATE';
- const SCRIPT_DATA_ESCAPED_DASH_DASH_STATE = 'SCRIPT_DATA_ESCAPED_DASH_DASH_STATE';
- const SCRIPT_DATA_ESCAPED_LESS_THAN_SIGN_STATE = 'SCRIPT_DATA_ESCAPED_LESS_THAN_SIGN_STATE';
- const SCRIPT_DATA_ESCAPED_END_TAG_OPEN_STATE = 'SCRIPT_DATA_ESCAPED_END_TAG_OPEN_STATE';
- const SCRIPT_DATA_ESCAPED_END_TAG_NAME_STATE = 'SCRIPT_DATA_ESCAPED_END_TAG_NAME_STATE';
- const SCRIPT_DATA_DOUBLE_ESCAPE_START_STATE = 'SCRIPT_DATA_DOUBLE_ESCAPE_START_STATE';
- const SCRIPT_DATA_DOUBLE_ESCAPED_STATE = 'SCRIPT_DATA_DOUBLE_ESCAPED_STATE';
- const SCRIPT_DATA_DOUBLE_ESCAPED_DASH_STATE = 'SCRIPT_DATA_DOUBLE_ESCAPED_DASH_STATE';
- const SCRIPT_DATA_DOUBLE_ESCAPED_DASH_DASH_STATE = 'SCRIPT_DATA_DOUBLE_ESCAPED_DASH_DASH_STATE';
- const SCRIPT_DATA_DOUBLE_ESCAPED_LESS_THAN_SIGN_STATE = 'SCRIPT_DATA_DOUBLE_ESCAPED_LESS_THAN_SIGN_STATE';
- const SCRIPT_DATA_DOUBLE_ESCAPE_END_STATE = 'SCRIPT_DATA_DOUBLE_ESCAPE_END_STATE';
- const BEFORE_ATTRIBUTE_NAME_STATE = 'BEFORE_ATTRIBUTE_NAME_STATE';
- const ATTRIBUTE_NAME_STATE = 'ATTRIBUTE_NAME_STATE';
- const AFTER_ATTRIBUTE_NAME_STATE = 'AFTER_ATTRIBUTE_NAME_STATE';
- const BEFORE_ATTRIBUTE_VALUE_STATE = 'BEFORE_ATTRIBUTE_VALUE_STATE';
- const ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE = 'ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE';
- const ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE = 'ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE';
- const ATTRIBUTE_VALUE_UNQUOTED_STATE = 'ATTRIBUTE_VALUE_UNQUOTED_STATE';
- const AFTER_ATTRIBUTE_VALUE_QUOTED_STATE = 'AFTER_ATTRIBUTE_VALUE_QUOTED_STATE';
- const SELF_CLOSING_START_TAG_STATE = 'SELF_CLOSING_START_TAG_STATE';
- const BOGUS_COMMENT_STATE = 'BOGUS_COMMENT_STATE';
- const MARKUP_DECLARATION_OPEN_STATE = 'MARKUP_DECLARATION_OPEN_STATE';
- const COMMENT_START_STATE = 'COMMENT_START_STATE';
- const COMMENT_START_DASH_STATE = 'COMMENT_START_DASH_STATE';
- const COMMENT_STATE = 'COMMENT_STATE';
- const COMMENT_LESS_THAN_SIGN_STATE = 'COMMENT_LESS_THAN_SIGN_STATE';
- const COMMENT_LESS_THAN_SIGN_BANG_STATE = 'COMMENT_LESS_THAN_SIGN_BANG_STATE';
- const COMMENT_LESS_THAN_SIGN_BANG_DASH_STATE = 'COMMENT_LESS_THAN_SIGN_BANG_DASH_STATE';
- const COMMENT_LESS_THAN_SIGN_BANG_DASH_DASH_STATE = 'COMMENT_LESS_THAN_SIGN_BANG_DASH_DASH_STATE';
- const COMMENT_END_DASH_STATE = 'COMMENT_END_DASH_STATE';
- const COMMENT_END_STATE = 'COMMENT_END_STATE';
- const COMMENT_END_BANG_STATE = 'COMMENT_END_BANG_STATE';
- const DOCTYPE_STATE = 'DOCTYPE_STATE';
- const BEFORE_DOCTYPE_NAME_STATE = 'BEFORE_DOCTYPE_NAME_STATE';
- const DOCTYPE_NAME_STATE = 'DOCTYPE_NAME_STATE';
- const AFTER_DOCTYPE_NAME_STATE = 'AFTER_DOCTYPE_NAME_STATE';
- const AFTER_DOCTYPE_PUBLIC_KEYWORD_STATE = 'AFTER_DOCTYPE_PUBLIC_KEYWORD_STATE';
- const BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE = 'BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE';
- const DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE = 'DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE';
- const DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE = 'DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE';
- const AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE = 'AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE';
- const BETWEEN_DOCTYPE_PUBLIC_AND_SYSTEM_IDENTIFIERS_STATE = 'BETWEEN_DOCTYPE_PUBLIC_AND_SYSTEM_IDENTIFIERS_STATE';
- const AFTER_DOCTYPE_SYSTEM_KEYWORD_STATE = 'AFTER_DOCTYPE_SYSTEM_KEYWORD_STATE';
- const BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE = 'BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE';
- const DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE = 'DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE';
- const DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE = 'DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE';
- const AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE = 'AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE';
- const BOGUS_DOCTYPE_STATE = 'BOGUS_DOCTYPE_STATE';
- const CDATA_SECTION_STATE = 'CDATA_SECTION_STATE';
- const CDATA_SECTION_BRACKET_STATE = 'CDATA_SECTION_BRACKET_STATE';
- const CDATA_SECTION_END_STATE = 'CDATA_SECTION_END_STATE';
- const CHARACTER_REFERENCE_STATE = 'CHARACTER_REFERENCE_STATE';
- const NAMED_CHARACTER_REFERENCE_STATE = 'NAMED_CHARACTER_REFERENCE_STATE';
- const AMBIGUOUS_AMPERSAND_STATE = 'AMBIGUOS_AMPERSAND_STATE';
- const NUMERIC_CHARACTER_REFERENCE_STATE = 'NUMERIC_CHARACTER_REFERENCE_STATE';
- const HEXADEMICAL_CHARACTER_REFERENCE_START_STATE = 'HEXADEMICAL_CHARACTER_REFERENCE_START_STATE';
- const DECIMAL_CHARACTER_REFERENCE_START_STATE = 'DECIMAL_CHARACTER_REFERENCE_START_STATE';
- const HEXADEMICAL_CHARACTER_REFERENCE_STATE = 'HEXADEMICAL_CHARACTER_REFERENCE_STATE';
- const DECIMAL_CHARACTER_REFERENCE_STATE = 'DECIMAL_CHARACTER_REFERENCE_STATE';
- const NUMERIC_CHARACTER_REFERENCE_END_STATE = 'NUMERIC_CHARACTER_REFERENCE_END_STATE';
- //Utils
- //OPTIMIZATION: these utility functions should not be moved out of this module. V8 Crankshaft will not inline
- //this functions if they will be situated in another module due to context switch.
- //Always perform inlining check before modifying this functions ('node --trace-inlining').
- function isWhitespace(cp) {
- return cp === $.SPACE || cp === $.LINE_FEED || cp === $.TABULATION || cp === $.FORM_FEED;
- }
- function isAsciiDigit(cp) {
- return cp >= $.DIGIT_0 && cp <= $.DIGIT_9;
- }
- function isAsciiUpper(cp) {
- return cp >= $.LATIN_CAPITAL_A && cp <= $.LATIN_CAPITAL_Z;
- }
- function isAsciiLower(cp) {
- return cp >= $.LATIN_SMALL_A && cp <= $.LATIN_SMALL_Z;
- }
- function isAsciiLetter(cp) {
- return isAsciiLower(cp) || isAsciiUpper(cp);
- }
- function isAsciiAlphaNumeric(cp) {
- return isAsciiLetter(cp) || isAsciiDigit(cp);
- }
- function isAsciiUpperHexDigit(cp) {
- return cp >= $.LATIN_CAPITAL_A && cp <= $.LATIN_CAPITAL_F;
- }
- function isAsciiLowerHexDigit(cp) {
- return cp >= $.LATIN_SMALL_A && cp <= $.LATIN_SMALL_F;
- }
- function isAsciiHexDigit(cp) {
- return isAsciiDigit(cp) || isAsciiUpperHexDigit(cp) || isAsciiLowerHexDigit(cp);
- }
- function toAsciiLowerCodePoint(cp) {
- return cp + 0x0020;
- }
- //NOTE: String.fromCharCode() function can handle only characters from BMP subset.
- //So, we need to workaround this manually.
- //(see: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/fromCharCode#Getting_it_to_work_with_higher_values)
- function toChar(cp) {
- if (cp <= 0xffff) {
- return String.fromCharCode(cp);
- }
- cp -= 0x10000;
- return String.fromCharCode(((cp >>> 10) & 0x3ff) | 0xd800) + String.fromCharCode(0xdc00 | (cp & 0x3ff));
- }
- function toAsciiLowerChar(cp) {
- return String.fromCharCode(toAsciiLowerCodePoint(cp));
- }
- function findNamedEntityTreeBranch(nodeIx, cp) {
- const branchCount = neTree[++nodeIx];
- let lo = ++nodeIx;
- let hi = lo + branchCount - 1;
- while (lo <= hi) {
- const mid = (lo + hi) >>> 1;
- const midCp = neTree[mid];
- if (midCp < cp) {
- lo = mid + 1;
- } else if (midCp > cp) {
- hi = mid - 1;
- } else {
- return neTree[mid + branchCount];
- }
- }
- return -1;
- }
- //Tokenizer
- class Tokenizer {
- constructor() {
- this.preprocessor = new Preprocessor();
- this.tokenQueue = [];
- this.allowCDATA = false;
- this.state = DATA_STATE;
- this.returnState = '';
- this.charRefCode = -1;
- this.tempBuff = [];
- this.lastStartTagName = '';
- this.consumedAfterSnapshot = -1;
- this.active = false;
- this.currentCharacterToken = null;
- this.currentToken = null;
- this.currentAttr = null;
- }
- //Errors
- _err() {
- // NOTE: err reporting is noop by default. Enabled by mixin.
- }
- _errOnNextCodePoint(err) {
- this._consume();
- this._err(err);
- this._unconsume();
- }
- //API
- getNextToken() {
- while (!this.tokenQueue.length && this.active) {
- this.consumedAfterSnapshot = 0;
- const cp = this._consume();
- if (!this._ensureHibernation()) {
- this[this.state](cp);
- }
- }
- return this.tokenQueue.shift();
- }
- write(chunk, isLastChunk) {
- this.active = true;
- this.preprocessor.write(chunk, isLastChunk);
- }
- insertHtmlAtCurrentPos(chunk) {
- this.active = true;
- this.preprocessor.insertHtmlAtCurrentPos(chunk);
- }
- //Hibernation
- _ensureHibernation() {
- if (this.preprocessor.endOfChunkHit) {
- for (; this.consumedAfterSnapshot > 0; this.consumedAfterSnapshot--) {
- this.preprocessor.retreat();
- }
- this.active = false;
- this.tokenQueue.push({ type: Tokenizer.HIBERNATION_TOKEN });
- return true;
- }
- return false;
- }
- //Consumption
- _consume() {
- this.consumedAfterSnapshot++;
- return this.preprocessor.advance();
- }
- _unconsume() {
- this.consumedAfterSnapshot--;
- this.preprocessor.retreat();
- }
- _reconsumeInState(state) {
- this.state = state;
- this._unconsume();
- }
- _consumeSequenceIfMatch(pattern, startCp, caseSensitive) {
- let consumedCount = 0;
- let isMatch = true;
- const patternLength = pattern.length;
- let patternPos = 0;
- let cp = startCp;
- let patternCp = void 0;
- for (; patternPos < patternLength; patternPos++) {
- if (patternPos > 0) {
- cp = this._consume();
- consumedCount++;
- }
- if (cp === $.EOF) {
- isMatch = false;
- break;
- }
- patternCp = pattern[patternPos];
- if (cp !== patternCp && (caseSensitive || cp !== toAsciiLowerCodePoint(patternCp))) {
- isMatch = false;
- break;
- }
- }
- if (!isMatch) {
- while (consumedCount--) {
- this._unconsume();
- }
- }
- return isMatch;
- }
- //Temp buffer
- _isTempBufferEqualToScriptString() {
- if (this.tempBuff.length !== $$.SCRIPT_STRING.length) {
- return false;
- }
- for (let i = 0; i < this.tempBuff.length; i++) {
- if (this.tempBuff[i] !== $$.SCRIPT_STRING[i]) {
- return false;
- }
- }
- return true;
- }
- //Token creation
- _createStartTagToken() {
- this.currentToken = {
- type: Tokenizer.START_TAG_TOKEN,
- tagName: '',
- selfClosing: false,
- ackSelfClosing: false,
- attrs: []
- };
- }
- _createEndTagToken() {
- this.currentToken = {
- type: Tokenizer.END_TAG_TOKEN,
- tagName: '',
- selfClosing: false,
- attrs: []
- };
- }
- _createCommentToken() {
- this.currentToken = {
- type: Tokenizer.COMMENT_TOKEN,
- data: ''
- };
- }
- _createDoctypeToken(initialName) {
- this.currentToken = {
- type: Tokenizer.DOCTYPE_TOKEN,
- name: initialName,
- forceQuirks: false,
- publicId: null,
- systemId: null
- };
- }
- _createCharacterToken(type, ch) {
- this.currentCharacterToken = {
- type: type,
- chars: ch
- };
- }
- _createEOFToken() {
- this.currentToken = { type: Tokenizer.EOF_TOKEN };
- }
- //Tag attributes
- _createAttr(attrNameFirstCh) {
- this.currentAttr = {
- name: attrNameFirstCh,
- value: ''
- };
- }
- _leaveAttrName(toState) {
- if (Tokenizer.getTokenAttr(this.currentToken, this.currentAttr.name) === null) {
- this.currentToken.attrs.push(this.currentAttr);
- } else {
- this._err(ERR.duplicateAttribute);
- }
- this.state = toState;
- }
- _leaveAttrValue(toState) {
- this.state = toState;
- }
- //Token emission
- _emitCurrentToken() {
- this._emitCurrentCharacterToken();
- const ct = this.currentToken;
- this.currentToken = null;
- //NOTE: store emited start tag's tagName to determine is the following end tag token is appropriate.
- if (ct.type === Tokenizer.START_TAG_TOKEN) {
- this.lastStartTagName = ct.tagName;
- } else if (ct.type === Tokenizer.END_TAG_TOKEN) {
- if (ct.attrs.length > 0) {
- this._err(ERR.endTagWithAttributes);
- }
- if (ct.selfClosing) {
- this._err(ERR.endTagWithTrailingSolidus);
- }
- }
- this.tokenQueue.push(ct);
- }
- _emitCurrentCharacterToken() {
- if (this.currentCharacterToken) {
- this.tokenQueue.push(this.currentCharacterToken);
- this.currentCharacterToken = null;
- }
- }
- _emitEOFToken() {
- this._createEOFToken();
- this._emitCurrentToken();
- }
- //Characters emission
- //OPTIMIZATION: specification uses only one type of character tokens (one token per character).
- //This causes a huge memory overhead and a lot of unnecessary parser loops. parse5 uses 3 groups of characters.
- //If we have a sequence of characters that belong to the same group, parser can process it
- //as a single solid character token.
- //So, there are 3 types of character tokens in parse5:
- //1)NULL_CHARACTER_TOKEN - \u0000-character sequences (e.g. '\u0000\u0000\u0000')
- //2)WHITESPACE_CHARACTER_TOKEN - any whitespace/new-line character sequences (e.g. '\n \r\t \f')
- //3)CHARACTER_TOKEN - any character sequence which don't belong to groups 1 and 2 (e.g. 'abcdef1234@@#$%^')
- _appendCharToCurrentCharacterToken(type, ch) {
- if (this.currentCharacterToken && this.currentCharacterToken.type !== type) {
- this._emitCurrentCharacterToken();
- }
- if (this.currentCharacterToken) {
- this.currentCharacterToken.chars += ch;
- } else {
- this._createCharacterToken(type, ch);
- }
- }
- _emitCodePoint(cp) {
- let type = Tokenizer.CHARACTER_TOKEN;
- if (isWhitespace(cp)) {
- type = Tokenizer.WHITESPACE_CHARACTER_TOKEN;
- } else if (cp === $.NULL) {
- type = Tokenizer.NULL_CHARACTER_TOKEN;
- }
- this._appendCharToCurrentCharacterToken(type, toChar(cp));
- }
- _emitSeveralCodePoints(codePoints) {
- for (let i = 0; i < codePoints.length; i++) {
- this._emitCodePoint(codePoints[i]);
- }
- }
- //NOTE: used then we emit character explicitly. This is always a non-whitespace and a non-null character.
- //So we can avoid additional checks here.
- _emitChars(ch) {
- this._appendCharToCurrentCharacterToken(Tokenizer.CHARACTER_TOKEN, ch);
- }
- // Character reference helpers
- _matchNamedCharacterReference(startCp) {
- let result = null;
- let excess = 1;
- let i = findNamedEntityTreeBranch(0, startCp);
- this.tempBuff.push(startCp);
- while (i > -1) {
- const current = neTree[i];
- const inNode = current < MAX_BRANCH_MARKER_VALUE;
- const nodeWithData = inNode && current & HAS_DATA_FLAG;
- if (nodeWithData) {
- //NOTE: we use greedy search, so we continue lookup at this point
- result = current & DATA_DUPLET_FLAG ? [neTree[++i], neTree[++i]] : [neTree[++i]];
- excess = 0;
- }
- const cp = this._consume();
- this.tempBuff.push(cp);
- excess++;
- if (cp === $.EOF) {
- break;
- }
- if (inNode) {
- i = current & HAS_BRANCHES_FLAG ? findNamedEntityTreeBranch(i, cp) : -1;
- } else {
- i = cp === current ? ++i : -1;
- }
- }
- while (excess--) {
- this.tempBuff.pop();
- this._unconsume();
- }
- return result;
- }
- _isCharacterReferenceInAttribute() {
- return (
- this.returnState === ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE ||
- this.returnState === ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE ||
- this.returnState === ATTRIBUTE_VALUE_UNQUOTED_STATE
- );
- }
- _isCharacterReferenceAttributeQuirk(withSemicolon) {
- if (!withSemicolon && this._isCharacterReferenceInAttribute()) {
- const nextCp = this._consume();
- this._unconsume();
- return nextCp === $.EQUALS_SIGN || isAsciiAlphaNumeric(nextCp);
- }
- return false;
- }
- _flushCodePointsConsumedAsCharacterReference() {
- if (this._isCharacterReferenceInAttribute()) {
- for (let i = 0; i < this.tempBuff.length; i++) {
- this.currentAttr.value += toChar(this.tempBuff[i]);
- }
- } else {
- this._emitSeveralCodePoints(this.tempBuff);
- }
- this.tempBuff = [];
- }
- // State machine
- // Data state
- //------------------------------------------------------------------
- [DATA_STATE](cp) {
- this.preprocessor.dropParsedChunk();
- if (cp === $.LESS_THAN_SIGN) {
- this.state = TAG_OPEN_STATE;
- } else if (cp === $.AMPERSAND) {
- this.returnState = DATA_STATE;
- this.state = CHARACTER_REFERENCE_STATE;
- } else if (cp === $.NULL) {
- this._err(ERR.unexpectedNullCharacter);
- this._emitCodePoint(cp);
- } else if (cp === $.EOF) {
- this._emitEOFToken();
- } else {
- this._emitCodePoint(cp);
- }
- }
- // RCDATA state
- //------------------------------------------------------------------
- [RCDATA_STATE](cp) {
- this.preprocessor.dropParsedChunk();
- if (cp === $.AMPERSAND) {
- this.returnState = RCDATA_STATE;
- this.state = CHARACTER_REFERENCE_STATE;
- } else if (cp === $.LESS_THAN_SIGN) {
- this.state = RCDATA_LESS_THAN_SIGN_STATE;
- } else if (cp === $.NULL) {
- this._err(ERR.unexpectedNullCharacter);
- this._emitChars(unicode.REPLACEMENT_CHARACTER);
- } else if (cp === $.EOF) {
- this._emitEOFToken();
- } else {
- this._emitCodePoint(cp);
- }
- }
- // RAWTEXT state
- //------------------------------------------------------------------
- [RAWTEXT_STATE](cp) {
- this.preprocessor.dropParsedChunk();
- if (cp === $.LESS_THAN_SIGN) {
- this.state = RAWTEXT_LESS_THAN_SIGN_STATE;
- } else if (cp === $.NULL) {
- this._err(ERR.unexpectedNullCharacter);
- this._emitChars(unicode.REPLACEMENT_CHARACTER);
- } else if (cp === $.EOF) {
- this._emitEOFToken();
- } else {
- this._emitCodePoint(cp);
- }
- }
- // Script data state
- //------------------------------------------------------------------
- [SCRIPT_DATA_STATE](cp) {
- this.preprocessor.dropParsedChunk();
- if (cp === $.LESS_THAN_SIGN) {
- this.state = SCRIPT_DATA_LESS_THAN_SIGN_STATE;
- } else if (cp === $.NULL) {
- this._err(ERR.unexpectedNullCharacter);
- this._emitChars(unicode.REPLACEMENT_CHARACTER);
- } else if (cp === $.EOF) {
- this._emitEOFToken();
- } else {
- this._emitCodePoint(cp);
- }
- }
- // PLAINTEXT state
- //------------------------------------------------------------------
- [PLAINTEXT_STATE](cp) {
- this.preprocessor.dropParsedChunk();
- if (cp === $.NULL) {
- this._err(ERR.unexpectedNullCharacter);
- this._emitChars(unicode.REPLACEMENT_CHARACTER);
- } else if (cp === $.EOF) {
- this._emitEOFToken();
- } else {
- this._emitCodePoint(cp);
- }
- }
- // Tag open state
- //------------------------------------------------------------------
- [TAG_OPEN_STATE](cp) {
- if (cp === $.EXCLAMATION_MARK) {
- this.state = MARKUP_DECLARATION_OPEN_STATE;
- } else if (cp === $.SOLIDUS) {
- this.state = END_TAG_OPEN_STATE;
- } else if (isAsciiLetter(cp)) {
- this._createStartTagToken();
- this._reconsumeInState(TAG_NAME_STATE);
- } else if (cp === $.QUESTION_MARK) {
- this._err(ERR.unexpectedQuestionMarkInsteadOfTagName);
- this._createCommentToken();
- this._reconsumeInState(BOGUS_COMMENT_STATE);
- } else if (cp === $.EOF) {
- this._err(ERR.eofBeforeTagName);
- this._emitChars('<');
- this._emitEOFToken();
- } else {
- this._err(ERR.invalidFirstCharacterOfTagName);
- this._emitChars('<');
- this._reconsumeInState(DATA_STATE);
- }
- }
- // End tag open state
- //------------------------------------------------------------------
- [END_TAG_OPEN_STATE](cp) {
- if (isAsciiLetter(cp)) {
- this._createEndTagToken();
- this._reconsumeInState(TAG_NAME_STATE);
- } else if (cp === $.GREATER_THAN_SIGN) {
- this._err(ERR.missingEndTagName);
- this.state = DATA_STATE;
- } else if (cp === $.EOF) {
- this._err(ERR.eofBeforeTagName);
- this._emitChars('</');
- this._emitEOFToken();
- } else {
- this._err(ERR.invalidFirstCharacterOfTagName);
- this._createCommentToken();
- this._reconsumeInState(BOGUS_COMMENT_STATE);
- }
- }
- // Tag name state
- //------------------------------------------------------------------
- [TAG_NAME_STATE](cp) {
- if (isWhitespace(cp)) {
- this.state = BEFORE_ATTRIBUTE_NAME_STATE;
- } else if (cp === $.SOLIDUS) {
- this.state = SELF_CLOSING_START_TAG_STATE;
- } else if (cp === $.GREATER_THAN_SIGN) {
- this.state = DATA_STATE;
- this._emitCurrentToken();
- } else if (isAsciiUpper(cp)) {
- this.currentToken.tagName += toAsciiLowerChar(cp);
- } else if (cp === $.NULL) {
- this._err(ERR.unexpectedNullCharacter);
- this.currentToken.tagName += unicode.REPLACEMENT_CHARACTER;
- } else if (cp === $.EOF) {
- this._err(ERR.eofInTag);
- this._emitEOFToken();
- } else {
- this.currentToken.tagName += toChar(cp);
- }
- }
- // RCDATA less-than sign state
- //------------------------------------------------------------------
- [RCDATA_LESS_THAN_SIGN_STATE](cp) {
- if (cp === $.SOLIDUS) {
- this.tempBuff = [];
- this.state = RCDATA_END_TAG_OPEN_STATE;
- } else {
- this._emitChars('<');
- this._reconsumeInState(RCDATA_STATE);
- }
- }
- // RCDATA end tag open state
- //------------------------------------------------------------------
- [RCDATA_END_TAG_OPEN_STATE](cp) {
- if (isAsciiLetter(cp)) {
- this._createEndTagToken();
- this._reconsumeInState(RCDATA_END_TAG_NAME_STATE);
- } else {
- this._emitChars('</');
- this._reconsumeInState(RCDATA_STATE);
- }
- }
- // RCDATA end tag name state
- //------------------------------------------------------------------
- [RCDATA_END_TAG_NAME_STATE](cp) {
- if (isAsciiUpper(cp)) {
- this.currentToken.tagName += toAsciiLowerChar(cp);
- this.tempBuff.push(cp);
- } else if (isAsciiLower(cp)) {
- this.currentToken.tagName += toChar(cp);
- this.tempBuff.push(cp);
- } else {
- if (this.lastStartTagName === this.currentToken.tagName) {
- if (isWhitespace(cp)) {
- this.state = BEFORE_ATTRIBUTE_NAME_STATE;
- return;
- }
- if (cp === $.SOLIDUS) {
- this.state = SELF_CLOSING_START_TAG_STATE;
- return;
- }
- if (cp === $.GREATER_THAN_SIGN) {
- this.state = DATA_STATE;
- this._emitCurrentToken();
- return;
- }
- }
- this._emitChars('</');
- this._emitSeveralCodePoints(this.tempBuff);
- this._reconsumeInState(RCDATA_STATE);
- }
- }
- // RAWTEXT less-than sign state
- //------------------------------------------------------------------
- [RAWTEXT_LESS_THAN_SIGN_STATE](cp) {
- if (cp === $.SOLIDUS) {
- this.tempBuff = [];
- this.state = RAWTEXT_END_TAG_OPEN_STATE;
- } else {
- this._emitChars('<');
- this._reconsumeInState(RAWTEXT_STATE);
- }
- }
- // RAWTEXT end tag open state
- //------------------------------------------------------------------
- [RAWTEXT_END_TAG_OPEN_STATE](cp) {
- if (isAsciiLetter(cp)) {
- this._createEndTagToken();
- this._reconsumeInState(RAWTEXT_END_TAG_NAME_STATE);
- } else {
- this._emitChars('</');
- this._reconsumeInState(RAWTEXT_STATE);
- }
- }
- // RAWTEXT end tag name state
- //------------------------------------------------------------------
- [RAWTEXT_END_TAG_NAME_STATE](cp) {
- if (isAsciiUpper(cp)) {
- this.currentToken.tagName += toAsciiLowerChar(cp);
- this.tempBuff.push(cp);
- } else if (isAsciiLower(cp)) {
- this.currentToken.tagName += toChar(cp);
- this.tempBuff.push(cp);
- } else {
- if (this.lastStartTagName === this.currentToken.tagName) {
- if (isWhitespace(cp)) {
- this.state = BEFORE_ATTRIBUTE_NAME_STATE;
- return;
- }
- if (cp === $.SOLIDUS) {
- this.state = SELF_CLOSING_START_TAG_STATE;
- return;
- }
- if (cp === $.GREATER_THAN_SIGN) {
- this._emitCurrentToken();
- this.state = DATA_STATE;
- return;
- }
- }
- this._emitChars('</');
- this._emitSeveralCodePoints(this.tempBuff);
- this._reconsumeInState(RAWTEXT_STATE);
- }
- }
- // Script data less-than sign state
- //------------------------------------------------------------------
- [SCRIPT_DATA_LESS_THAN_SIGN_STATE](cp) {
- if (cp === $.SOLIDUS) {
- this.tempBuff = [];
- this.state = SCRIPT_DATA_END_TAG_OPEN_STATE;
- } else if (cp === $.EXCLAMATION_MARK) {
- this.state = SCRIPT_DATA_ESCAPE_START_STATE;
- this._emitChars('<!');
- } else {
- this._emitChars('<');
- this._reconsumeInState(SCRIPT_DATA_STATE);
- }
- }
- // Script data end tag open state
- //------------------------------------------------------------------
- [SCRIPT_DATA_END_TAG_OPEN_STATE](cp) {
- if (isAsciiLetter(cp)) {
- this._createEndTagToken();
- this._reconsumeInState(SCRIPT_DATA_END_TAG_NAME_STATE);
- } else {
- this._emitChars('</');
- this._reconsumeInState(SCRIPT_DATA_STATE);
- }
- }
- // Script data end tag name state
- //------------------------------------------------------------------
- [SCRIPT_DATA_END_TAG_NAME_STATE](cp) {
- if (isAsciiUpper(cp)) {
- this.currentToken.tagName += toAsciiLowerChar(cp);
- this.tempBuff.push(cp);
- } else if (isAsciiLower(cp)) {
- this.currentToken.tagName += toChar(cp);
- this.tempBuff.push(cp);
- } else {
- if (this.lastStartTagName === this.currentToken.tagName) {
- if (isWhitespace(cp)) {
- this.state = BEFORE_ATTRIBUTE_NAME_STATE;
- return;
- } else if (cp === $.SOLIDUS) {
- this.state = SELF_CLOSING_START_TAG_STATE;
- return;
- } else if (cp === $.GREATER_THAN_SIGN) {
- this._emitCurrentToken();
- this.state = DATA_STATE;
- return;
- }
- }
- this._emitChars('</');
- this._emitSeveralCodePoints(this.tempBuff);
- this._reconsumeInState(SCRIPT_DATA_STATE);
- }
- }
- // Script data escape start state
- //------------------------------------------------------------------
- [SCRIPT_DATA_ESCAPE_START_STATE](cp) {
- if (cp === $.HYPHEN_MINUS) {
- this.state = SCRIPT_DATA_ESCAPE_START_DASH_STATE;
- this._emitChars('-');
- } else {
- this._reconsumeInState(SCRIPT_DATA_STATE);
- }
- }
- // Script data escape start dash state
- //------------------------------------------------------------------
- [SCRIPT_DATA_ESCAPE_START_DASH_STATE](cp) {
- if (cp === $.HYPHEN_MINUS) {
- this.state = SCRIPT_DATA_ESCAPED_DASH_DASH_STATE;
- this._emitChars('-');
- } else {
- this._reconsumeInState(SCRIPT_DATA_STATE);
- }
- }
- // Script data escaped state
- //------------------------------------------------------------------
- [SCRIPT_DATA_ESCAPED_STATE](cp) {
- if (cp === $.HYPHEN_MINUS) {
- this.state = SCRIPT_DATA_ESCAPED_DASH_STATE;
- this._emitChars('-');
- } else if (cp === $.LESS_THAN_SIGN) {
- this.state = SCRIPT_DATA_ESCAPED_LESS_THAN_SIGN_STATE;
- } else if (cp === $.NULL) {
- this._err(ERR.unexpectedNullCharacter);
- this._emitChars(unicode.REPLACEMENT_CHARACTER);
- } else if (cp === $.EOF) {
- this._err(ERR.eofInScriptHtmlCommentLikeText);
- this._emitEOFToken();
- } else {
- this._emitCodePoint(cp);
- }
- }
- // Script data escaped dash state
- //------------------------------------------------------------------
- [SCRIPT_DATA_ESCAPED_DASH_STATE](cp) {
- if (cp === $.HYPHEN_MINUS) {
- this.state = SCRIPT_DATA_ESCAPED_DASH_DASH_STATE;
- this._emitChars('-');
- } else if (cp === $.LESS_THAN_SIGN) {
- this.state = SCRIPT_DATA_ESCAPED_LESS_THAN_SIGN_STATE;
- } else if (cp === $.NULL) {
- this._err(ERR.unexpectedNullCharacter);
- this.state = SCRIPT_DATA_ESCAPED_STATE;
- this._emitChars(unicode.REPLACEMENT_CHARACTER);
- } else if (cp === $.EOF) {
- this._err(ERR.eofInScriptHtmlCommentLikeText);
- this._emitEOFToken();
- } else {
- this.state = SCRIPT_DATA_ESCAPED_STATE;
- this._emitCodePoint(cp);
- }
- }
- // Script data escaped dash dash state
- //------------------------------------------------------------------
- [SCRIPT_DATA_ESCAPED_DASH_DASH_STATE](cp) {
- if (cp === $.HYPHEN_MINUS) {
- this._emitChars('-');
- } else if (cp === $.LESS_THAN_SIGN) {
- this.state = SCRIPT_DATA_ESCAPED_LESS_THAN_SIGN_STATE;
- } else if (cp === $.GREATER_THAN_SIGN) {
- this.state = SCRIPT_DATA_STATE;
- this._emitChars('>');
- } else if (cp === $.NULL) {
- this._err(ERR.unexpectedNullCharacter);
- this.state = SCRIPT_DATA_ESCAPED_STATE;
- this._emitChars(unicode.REPLACEMENT_CHARACTER);
- } else if (cp === $.EOF) {
- this._err(ERR.eofInScriptHtmlCommentLikeText);
- this._emitEOFToken();
- } else {
- this.state = SCRIPT_DATA_ESCAPED_STATE;
- this._emitCodePoint(cp);
- }
- }
- // Script data escaped less-than sign state
- //------------------------------------------------------------------
- [SCRIPT_DATA_ESCAPED_LESS_THAN_SIGN_STATE](cp) {
- if (cp === $.SOLIDUS) {
- this.tempBuff = [];
- this.state = SCRIPT_DATA_ESCAPED_END_TAG_OPEN_STATE;
- } else if (isAsciiLetter(cp)) {
- this.tempBuff = [];
- this._emitChars('<');
- this._reconsumeInState(SCRIPT_DATA_DOUBLE_ESCAPE_START_STATE);
- } else {
- this._emitChars('<');
- this._reconsumeInState(SCRIPT_DATA_ESCAPED_STATE);
- }
- }
- // Script data escaped end tag open state
- //------------------------------------------------------------------
- [SCRIPT_DATA_ESCAPED_END_TAG_OPEN_STATE](cp) {
- if (isAsciiLetter(cp)) {
- this._createEndTagToken();
- this._reconsumeInState(SCRIPT_DATA_ESCAPED_END_TAG_NAME_STATE);
- } else {
- this._emitChars('</');
- this._reconsumeInState(SCRIPT_DATA_ESCAPED_STATE);
- }
- }
- // Script data escaped end tag name state
- //------------------------------------------------------------------
- [SCRIPT_DATA_ESCAPED_END_TAG_NAME_STATE](cp) {
- if (isAsciiUpper(cp)) {
- this.currentToken.tagName += toAsciiLowerChar(cp);
- this.tempBuff.push(cp);
- } else if (isAsciiLower(cp)) {
- this.currentToken.tagName += toChar(cp);
- this.tempBuff.push(cp);
- } else {
- if (this.lastStartTagName === this.currentToken.tagName) {
- if (isWhitespace(cp)) {
- this.state = BEFORE_ATTRIBUTE_NAME_STATE;
- return;
- }
- if (cp === $.SOLIDUS) {
- this.state = SELF_CLOSING_START_TAG_STATE;
- return;
- }
- if (cp === $.GREATER_THAN_SIGN) {
- this._emitCurrentToken();
- this.state = DATA_STATE;
- return;
- }
- }
- this._emitChars('</');
- this._emitSeveralCodePoints(this.tempBuff);
- this._reconsumeInState(SCRIPT_DATA_ESCAPED_STATE);
- }
- }
- // Script data double escape start state
- //------------------------------------------------------------------
- [SCRIPT_DATA_DOUBLE_ESCAPE_START_STATE](cp) {
- if (isWhitespace(cp) || cp === $.SOLIDUS || cp === $.GREATER_THAN_SIGN) {
- this.state = this._isTempBufferEqualToScriptString()
- ? SCRIPT_DATA_DOUBLE_ESCAPED_STATE
- : SCRIPT_DATA_ESCAPED_STATE;
- this._emitCodePoint(cp);
- } else if (isAsciiUpper(cp)) {
- this.tempBuff.push(toAsciiLowerCodePoint(cp));
- this._emitCodePoint(cp);
- } else if (isAsciiLower(cp)) {
- this.tempBuff.push(cp);
- this._emitCodePoint(cp);
- } else {
- this._reconsumeInState(SCRIPT_DATA_ESCAPED_STATE);
- }
- }
- // Script data double escaped state
- //------------------------------------------------------------------
- [SCRIPT_DATA_DOUBLE_ESCAPED_STATE](cp) {
- if (cp === $.HYPHEN_MINUS) {
- this.state = SCRIPT_DATA_DOUBLE_ESCAPED_DASH_STATE;
- this._emitChars('-');
- } else if (cp === $.LESS_THAN_SIGN) {
- this.state = SCRIPT_DATA_DOUBLE_ESCAPED_LESS_THAN_SIGN_STATE;
- this._emitChars('<');
- } else if (cp === $.NULL) {
- this._err(ERR.unexpectedNullCharacter);
- this._emitChars(unicode.REPLACEMENT_CHARACTER);
- } else if (cp === $.EOF) {
- this._err(ERR.eofInScriptHtmlCommentLikeText);
- this._emitEOFToken();
- } else {
- this._emitCodePoint(cp);
- }
- }
- // Script data double escaped dash state
- //------------------------------------------------------------------
- [SCRIPT_DATA_DOUBLE_ESCAPED_DASH_STATE](cp) {
- if (cp === $.HYPHEN_MINUS) {
- this.state = SCRIPT_DATA_DOUBLE_ESCAPED_DASH_DASH_STATE;
- this._emitChars('-');
- } else if (cp === $.LESS_THAN_SIGN) {
- this.state = SCRIPT_DATA_DOUBLE_ESCAPED_LESS_THAN_SIGN_STATE;
- this._emitChars('<');
- } else if (cp === $.NULL) {
- this._err(ERR.unexpectedNullCharacter);
- this.state = SCRIPT_DATA_DOUBLE_ESCAPED_STATE;
- this._emitChars(unicode.REPLACEMENT_CHARACTER);
- } else if (cp === $.EOF) {
- this._err(ERR.eofInScriptHtmlCommentLikeText);
- this._emitEOFToken();
- } else {
- this.state = SCRIPT_DATA_DOUBLE_ESCAPED_STATE;
- this._emitCodePoint(cp);
- }
- }
- // Script data double escaped dash dash state
- //------------------------------------------------------------------
- [SCRIPT_DATA_DOUBLE_ESCAPED_DASH_DASH_STATE](cp) {
- if (cp === $.HYPHEN_MINUS) {
- this._emitChars('-');
- } else if (cp === $.LESS_THAN_SIGN) {
- this.state = SCRIPT_DATA_DOUBLE_ESCAPED_LESS_THAN_SIGN_STATE;
- this._emitChars('<');
- } else if (cp === $.GREATER_THAN_SIGN) {
- this.state = SCRIPT_DATA_STATE;
- this._emitChars('>');
- } else if (cp === $.NULL) {
- this._err(ERR.unexpectedNullCharacter);
- this.state = SCRIPT_DATA_DOUBLE_ESCAPED_STATE;
- this._emitChars(unicode.REPLACEMENT_CHARACTER);
- } else if (cp === $.EOF) {
- this._err(ERR.eofInScriptHtmlCommentLikeText);
- this._emitEOFToken();
- } else {
- this.state = SCRIPT_DATA_DOUBLE_ESCAPED_STATE;
- this._emitCodePoint(cp);
- }
- }
- // Script data double escaped less-than sign state
- //------------------------------------------------------------------
- [SCRIPT_DATA_DOUBLE_ESCAPED_LESS_THAN_SIGN_STATE](cp) {
- if (cp === $.SOLIDUS) {
- this.tempBuff = [];
- this.state = SCRIPT_DATA_DOUBLE_ESCAPE_END_STATE;
- this._emitChars('/');
- } else {
- this._reconsumeInState(SCRIPT_DATA_DOUBLE_ESCAPED_STATE);
- }
- }
- // Script data double escape end state
- //------------------------------------------------------------------
- [SCRIPT_DATA_DOUBLE_ESCAPE_END_STATE](cp) {
- if (isWhitespace(cp) || cp === $.SOLIDUS || cp === $.GREATER_THAN_SIGN) {
- this.state = this._isTempBufferEqualToScriptString()
- ? SCRIPT_DATA_ESCAPED_STATE
- : SCRIPT_DATA_DOUBLE_ESCAPED_STATE;
- this._emitCodePoint(cp);
- } else if (isAsciiUpper(cp)) {
- this.tempBuff.push(toAsciiLowerCodePoint(cp));
- this._emitCodePoint(cp);
- } else if (isAsciiLower(cp)) {
- this.tempBuff.push(cp);
- this._emitCodePoint(cp);
- } else {
- this._reconsumeInState(SCRIPT_DATA_DOUBLE_ESCAPED_STATE);
- }
- }
- // Before attribute name state
- //------------------------------------------------------------------
- [BEFORE_ATTRIBUTE_NAME_STATE](cp) {
- if (isWhitespace(cp)) {
- return;
- }
- if (cp === $.SOLIDUS || cp === $.GREATER_THAN_SIGN || cp === $.EOF) {
- this._reconsumeInState(AFTER_ATTRIBUTE_NAME_STATE);
- } else if (cp === $.EQUALS_SIGN) {
- this._err(ERR.unexpectedEqualsSignBeforeAttributeName);
- this._createAttr('=');
- this.state = ATTRIBUTE_NAME_STATE;
- } else {
- this._createAttr('');
- this._reconsumeInState(ATTRIBUTE_NAME_STATE);
- }
- }
- // Attribute name state
- //------------------------------------------------------------------
- [ATTRIBUTE_NAME_STATE](cp) {
- if (isWhitespace(cp) || cp === $.SOLIDUS || cp === $.GREATER_THAN_SIGN || cp === $.EOF) {
- this._leaveAttrName(AFTER_ATTRIBUTE_NAME_STATE);
- this._unconsume();
- } else if (cp === $.EQUALS_SIGN) {
- this._leaveAttrName(BEFORE_ATTRIBUTE_VALUE_STATE);
- } else if (isAsciiUpper(cp)) {
- this.currentAttr.name += toAsciiLowerChar(cp);
- } else if (cp === $.QUOTATION_MARK || cp === $.APOSTROPHE || cp === $.LESS_THAN_SIGN) {
- this._err(ERR.unexpectedCharacterInAttributeName);
- this.currentAttr.name += toChar(cp);
- } else if (cp === $.NULL) {
- this._err(ERR.unexpectedNullCharacter);
- this.currentAttr.name += unicode.REPLACEMENT_CHARACTER;
- } else {
- this.currentAttr.name += toChar(cp);
- }
- }
- // After attribute name state
- //------------------------------------------------------------------
- [AFTER_ATTRIBUTE_NAME_STATE](cp) {
- if (isWhitespace(cp)) {
- return;
- }
- if (cp === $.SOLIDUS) {
- this.state = SELF_CLOSING_START_TAG_STATE;
- } else if (cp === $.EQUALS_SIGN) {
- this.state = BEFORE_ATTRIBUTE_VALUE_STATE;
- } else if (cp === $.GREATER_THAN_SIGN) {
- this.state = DATA_STATE;
- this._emitCurrentToken();
- } else if (cp === $.EOF) {
- this._err(ERR.eofInTag);
- this._emitEOFToken();
- } else {
- this._createAttr('');
- this._reconsumeInState(ATTRIBUTE_NAME_STATE);
- }
- }
- // Before attribute value state
- //------------------------------------------------------------------
- [BEFORE_ATTRIBUTE_VALUE_STATE](cp) {
- if (isWhitespace(cp)) {
- return;
- }
- if (cp === $.QUOTATION_MARK) {
- this.state = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
- } else if (cp === $.APOSTROPHE) {
- this.state = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
- } else if (cp === $.GREATER_THAN_SIGN) {
- this._err(ERR.missingAttributeValue);
- this.state = DATA_STATE;
- this._emitCurrentToken();
- } else {
- this._reconsumeInState(ATTRIBUTE_VALUE_UNQUOTED_STATE);
- }
- }
- // Attribute value (double-quoted) state
- //------------------------------------------------------------------
- [ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE](cp) {
- if (cp === $.QUOTATION_MARK) {
- this.state = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
- } else if (cp === $.AMPERSAND) {
- this.returnState = ATTRIBUTE_VALUE_DOUBLE_QUOTED_STATE;
- this.state = CHARACTER_REFERENCE_STATE;
- } else if (cp === $.NULL) {
- this._err(ERR.unexpectedNullCharacter);
- this.currentAttr.value += unicode.REPLACEMENT_CHARACTER;
- } else if (cp === $.EOF) {
- this._err(ERR.eofInTag);
- this._emitEOFToken();
- } else {
- this.currentAttr.value += toChar(cp);
- }
- }
- // Attribute value (single-quoted) state
- //------------------------------------------------------------------
- [ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE](cp) {
- if (cp === $.APOSTROPHE) {
- this.state = AFTER_ATTRIBUTE_VALUE_QUOTED_STATE;
- } else if (cp === $.AMPERSAND) {
- this.returnState = ATTRIBUTE_VALUE_SINGLE_QUOTED_STATE;
- this.state = CHARACTER_REFERENCE_STATE;
- } else if (cp === $.NULL) {
- this._err(ERR.unexpectedNullCharacter);
- this.currentAttr.value += unicode.REPLACEMENT_CHARACTER;
- } else if (cp === $.EOF) {
- this._err(ERR.eofInTag);
- this._emitEOFToken();
- } else {
- this.currentAttr.value += toChar(cp);
- }
- }
- // Attribute value (unquoted) state
- //------------------------------------------------------------------
- [ATTRIBUTE_VALUE_UNQUOTED_STATE](cp) {
- if (isWhitespace(cp)) {
- this._leaveAttrValue(BEFORE_ATTRIBUTE_NAME_STATE);
- } else if (cp === $.AMPERSAND) {
- this.returnState = ATTRIBUTE_VALUE_UNQUOTED_STATE;
- this.state = CHARACTER_REFERENCE_STATE;
- } else if (cp === $.GREATER_THAN_SIGN) {
- this._leaveAttrValue(DATA_STATE);
- this._emitCurrentToken();
- } else if (cp === $.NULL) {
- this._err(ERR.unexpectedNullCharacter);
- this.currentAttr.value += unicode.REPLACEMENT_CHARACTER;
- } else if (
- cp === $.QUOTATION_MARK ||
- cp === $.APOSTROPHE ||
- cp === $.LESS_THAN_SIGN ||
- cp === $.EQUALS_SIGN ||
- cp === $.GRAVE_ACCENT
- ) {
- this._err(ERR.unexpectedCharacterInUnquotedAttributeValue);
- this.currentAttr.value += toChar(cp);
- } else if (cp === $.EOF) {
- this._err(ERR.eofInTag);
- this._emitEOFToken();
- } else {
- this.currentAttr.value += toChar(cp);
- }
- }
- // After attribute value (quoted) state
- //------------------------------------------------------------------
- [AFTER_ATTRIBUTE_VALUE_QUOTED_STATE](cp) {
- if (isWhitespace(cp)) {
- this._leaveAttrValue(BEFORE_ATTRIBUTE_NAME_STATE);
- } else if (cp === $.SOLIDUS) {
- this._leaveAttrValue(SELF_CLOSING_START_TAG_STATE);
- } else if (cp === $.GREATER_THAN_SIGN) {
- this._leaveAttrValue(DATA_STATE);
- this._emitCurrentToken();
- } else if (cp === $.EOF) {
- this._err(ERR.eofInTag);
- this._emitEOFToken();
- } else {
- this._err(ERR.missingWhitespaceBetweenAttributes);
- this._reconsumeInState(BEFORE_ATTRIBUTE_NAME_STATE);
- }
- }
- // Self-closing start tag state
- //------------------------------------------------------------------
- [SELF_CLOSING_START_TAG_STATE](cp) {
- if (cp === $.GREATER_THAN_SIGN) {
- this.currentToken.selfClosing = true;
- this.state = DATA_STATE;
- this._emitCurrentToken();
- } else if (cp === $.EOF) {
- this._err(ERR.eofInTag);
- this._emitEOFToken();
- } else {
- this._err(ERR.unexpectedSolidusInTag);
- this._reconsumeInState(BEFORE_ATTRIBUTE_NAME_STATE);
- }
- }
- // Bogus comment state
- //------------------------------------------------------------------
- [BOGUS_COMMENT_STATE](cp) {
- if (cp === $.GREATER_THAN_SIGN) {
- this.state = DATA_STATE;
- this._emitCurrentToken();
- } else if (cp === $.EOF) {
- this._emitCurrentToken();
- this._emitEOFToken();
- } else if (cp === $.NULL) {
- this._err(ERR.unexpectedNullCharacter);
- this.currentToken.data += unicode.REPLACEMENT_CHARACTER;
- } else {
- this.currentToken.data += toChar(cp);
- }
- }
- // Markup declaration open state
- //------------------------------------------------------------------
- [MARKUP_DECLARATION_OPEN_STATE](cp) {
- if (this._consumeSequenceIfMatch($$.DASH_DASH_STRING, cp, true)) {
- this._createCommentToken();
- this.state = COMMENT_START_STATE;
- } else if (this._consumeSequenceIfMatch($$.DOCTYPE_STRING, cp, false)) {
- this.state = DOCTYPE_STATE;
- } else if (this._consumeSequenceIfMatch($$.CDATA_START_STRING, cp, true)) {
- if (this.allowCDATA) {
- this.state = CDATA_SECTION_STATE;
- } else {
- this._err(ERR.cdataInHtmlContent);
- this._createCommentToken();
- this.currentToken.data = '[CDATA[';
- this.state = BOGUS_COMMENT_STATE;
- }
- }
- //NOTE: sequence lookup can be abrupted by hibernation. In that case lookup
- //results are no longer valid and we will need to start over.
- else if (!this._ensureHibernation()) {
- this._err(ERR.incorrectlyOpenedComment);
- this._createCommentToken();
- this._reconsumeInState(BOGUS_COMMENT_STATE);
- }
- }
- // Comment start state
- //------------------------------------------------------------------
- [COMMENT_START_STATE](cp) {
- if (cp === $.HYPHEN_MINUS) {
- this.state = COMMENT_START_DASH_STATE;
- } else if (cp === $.GREATER_THAN_SIGN) {
- this._err(ERR.abruptClosingOfEmptyComment);
- this.state = DATA_STATE;
- this._emitCurrentToken();
- } else {
- this._reconsumeInState(COMMENT_STATE);
- }
- }
- // Comment start dash state
- //------------------------------------------------------------------
- [COMMENT_START_DASH_STATE](cp) {
- if (cp === $.HYPHEN_MINUS) {
- this.state = COMMENT_END_STATE;
- } else if (cp === $.GREATER_THAN_SIGN) {
- this._err(ERR.abruptClosingOfEmptyComment);
- this.state = DATA_STATE;
- this._emitCurrentToken();
- } else if (cp === $.EOF) {
- this._err(ERR.eofInComment);
- this._emitCurrentToken();
- this._emitEOFToken();
- } else {
- this.currentToken.data += '-';
- this._reconsumeInState(COMMENT_STATE);
- }
- }
- // Comment state
- //------------------------------------------------------------------
- [COMMENT_STATE](cp) {
- if (cp === $.HYPHEN_MINUS) {
- this.state = COMMENT_END_DASH_STATE;
- } else if (cp === $.LESS_THAN_SIGN) {
- this.currentToken.data += '<';
- this.state = COMMENT_LESS_THAN_SIGN_STATE;
- } else if (cp === $.NULL) {
- this._err(ERR.unexpectedNullCharacter);
- this.currentToken.data += unicode.REPLACEMENT_CHARACTER;
- } else if (cp === $.EOF) {
- this._err(ERR.eofInComment);
- this._emitCurrentToken();
- this._emitEOFToken();
- } else {
- this.currentToken.data += toChar(cp);
- }
- }
- // Comment less-than sign state
- //------------------------------------------------------------------
- [COMMENT_LESS_THAN_SIGN_STATE](cp) {
- if (cp === $.EXCLAMATION_MARK) {
- this.currentToken.data += '!';
- this.state = COMMENT_LESS_THAN_SIGN_BANG_STATE;
- } else if (cp === $.LESS_THAN_SIGN) {
- this.currentToken.data += '!';
- } else {
- this._reconsumeInState(COMMENT_STATE);
- }
- }
- // Comment less-than sign bang state
- //------------------------------------------------------------------
- [COMMENT_LESS_THAN_SIGN_BANG_STATE](cp) {
- if (cp === $.HYPHEN_MINUS) {
- this.state = COMMENT_LESS_THAN_SIGN_BANG_DASH_STATE;
- } else {
- this._reconsumeInState(COMMENT_STATE);
- }
- }
- // Comment less-than sign bang dash state
- //------------------------------------------------------------------
- [COMMENT_LESS_THAN_SIGN_BANG_DASH_STATE](cp) {
- if (cp === $.HYPHEN_MINUS) {
- this.state = COMMENT_LESS_THAN_SIGN_BANG_DASH_DASH_STATE;
- } else {
- this._reconsumeInState(COMMENT_END_DASH_STATE);
- }
- }
- // Comment less-than sign bang dash dash state
- //------------------------------------------------------------------
- [COMMENT_LESS_THAN_SIGN_BANG_DASH_DASH_STATE](cp) {
- if (cp !== $.GREATER_THAN_SIGN && cp !== $.EOF) {
- this._err(ERR.nestedComment);
- }
- this._reconsumeInState(COMMENT_END_STATE);
- }
- // Comment end dash state
- //------------------------------------------------------------------
- [COMMENT_END_DASH_STATE](cp) {
- if (cp === $.HYPHEN_MINUS) {
- this.state = COMMENT_END_STATE;
- } else if (cp === $.EOF) {
- this._err(ERR.eofInComment);
- this._emitCurrentToken();
- this._emitEOFToken();
- } else {
- this.currentToken.data += '-';
- this._reconsumeInState(COMMENT_STATE);
- }
- }
- // Comment end state
- //------------------------------------------------------------------
- [COMMENT_END_STATE](cp) {
- if (cp === $.GREATER_THAN_SIGN) {
- this.state = DATA_STATE;
- this._emitCurrentToken();
- } else if (cp === $.EXCLAMATION_MARK) {
- this.state = COMMENT_END_BANG_STATE;
- } else if (cp === $.HYPHEN_MINUS) {
- this.currentToken.data += '-';
- } else if (cp === $.EOF) {
- this._err(ERR.eofInComment);
- this._emitCurrentToken();
- this._emitEOFToken();
- } else {
- this.currentToken.data += '--';
- this._reconsumeInState(COMMENT_STATE);
- }
- }
- // Comment end bang state
- //------------------------------------------------------------------
- [COMMENT_END_BANG_STATE](cp) {
- if (cp === $.HYPHEN_MINUS) {
- this.currentToken.data += '--!';
- this.state = COMMENT_END_DASH_STATE;
- } else if (cp === $.GREATER_THAN_SIGN) {
- this._err(ERR.incorrectlyClosedComment);
- this.state = DATA_STATE;
- this._emitCurrentToken();
- } else if (cp === $.EOF) {
- this._err(ERR.eofInComment);
- this._emitCurrentToken();
- this._emitEOFToken();
- } else {
- this.currentToken.data += '--!';
- this._reconsumeInState(COMMENT_STATE);
- }
- }
- // DOCTYPE state
- //------------------------------------------------------------------
- [DOCTYPE_STATE](cp) {
- if (isWhitespace(cp)) {
- this.state = BEFORE_DOCTYPE_NAME_STATE;
- } else if (cp === $.GREATER_THAN_SIGN) {
- this._reconsumeInState(BEFORE_DOCTYPE_NAME_STATE);
- } else if (cp === $.EOF) {
- this._err(ERR.eofInDoctype);
- this._createDoctypeToken(null);
- this.currentToken.forceQuirks = true;
- this._emitCurrentToken();
- this._emitEOFToken();
- } else {
- this._err(ERR.missingWhitespaceBeforeDoctypeName);
- this._reconsumeInState(BEFORE_DOCTYPE_NAME_STATE);
- }
- }
- // Before DOCTYPE name state
- //------------------------------------------------------------------
- [BEFORE_DOCTYPE_NAME_STATE](cp) {
- if (isWhitespace(cp)) {
- return;
- }
- if (isAsciiUpper(cp)) {
- this._createDoctypeToken(toAsciiLowerChar(cp));
- this.state = DOCTYPE_NAME_STATE;
- } else if (cp === $.NULL) {
- this._err(ERR.unexpectedNullCharacter);
- this._createDoctypeToken(unicode.REPLACEMENT_CHARACTER);
- this.state = DOCTYPE_NAME_STATE;
- } else if (cp === $.GREATER_THAN_SIGN) {
- this._err(ERR.missingDoctypeName);
- this._createDoctypeToken(null);
- this.currentToken.forceQuirks = true;
- this._emitCurrentToken();
- this.state = DATA_STATE;
- } else if (cp === $.EOF) {
- this._err(ERR.eofInDoctype);
- this._createDoctypeToken(null);
- this.currentToken.forceQuirks = true;
- this._emitCurrentToken();
- this._emitEOFToken();
- } else {
- this._createDoctypeToken(toChar(cp));
- this.state = DOCTYPE_NAME_STATE;
- }
- }
- // DOCTYPE name state
- //------------------------------------------------------------------
- [DOCTYPE_NAME_STATE](cp) {
- if (isWhitespace(cp)) {
- this.state = AFTER_DOCTYPE_NAME_STATE;
- } else if (cp === $.GREATER_THAN_SIGN) {
- this.state = DATA_STATE;
- this._emitCurrentToken();
- } else if (isAsciiUpper(cp)) {
- this.currentToken.name += toAsciiLowerChar(cp);
- } else if (cp === $.NULL) {
- this._err(ERR.unexpectedNullCharacter);
- this.currentToken.name += unicode.REPLACEMENT_CHARACTER;
- } else if (cp === $.EOF) {
- this._err(ERR.eofInDoctype);
- this.currentToken.forceQuirks = true;
- this._emitCurrentToken();
- this._emitEOFToken();
- } else {
- this.currentToken.name += toChar(cp);
- }
- }
- // After DOCTYPE name state
- //------------------------------------------------------------------
- [AFTER_DOCTYPE_NAME_STATE](cp) {
- if (isWhitespace(cp)) {
- return;
- }
- if (cp === $.GREATER_THAN_SIGN) {
- this.state = DATA_STATE;
- this._emitCurrentToken();
- } else if (cp === $.EOF) {
- this._err(ERR.eofInDoctype);
- this.currentToken.forceQuirks = true;
- this._emitCurrentToken();
- this._emitEOFToken();
- } else if (this._consumeSequenceIfMatch($$.PUBLIC_STRING, cp, false)) {
- this.state = AFTER_DOCTYPE_PUBLIC_KEYWORD_STATE;
- } else if (this._consumeSequenceIfMatch($$.SYSTEM_STRING, cp, false)) {
- this.state = AFTER_DOCTYPE_SYSTEM_KEYWORD_STATE;
- }
- //NOTE: sequence lookup can be abrupted by hibernation. In that case lookup
- //results are no longer valid and we will need to start over.
- else if (!this._ensureHibernation()) {
- this._err(ERR.invalidCharacterSequenceAfterDoctypeName);
- this.currentToken.forceQuirks = true;
- this._reconsumeInState(BOGUS_DOCTYPE_STATE);
- }
- }
- // After DOCTYPE public keyword state
- //------------------------------------------------------------------
- [AFTER_DOCTYPE_PUBLIC_KEYWORD_STATE](cp) {
- if (isWhitespace(cp)) {
- this.state = BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
- } else if (cp === $.QUOTATION_MARK) {
- this._err(ERR.missingWhitespaceAfterDoctypePublicKeyword);
- this.currentToken.publicId = '';
- this.state = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
- } else if (cp === $.APOSTROPHE) {
- this._err(ERR.missingWhitespaceAfterDoctypePublicKeyword);
- this.currentToken.publicId = '';
- this.state = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
- } else if (cp === $.GREATER_THAN_SIGN) {
- this._err(ERR.missingDoctypePublicIdentifier);
- this.currentToken.forceQuirks = true;
- this.state = DATA_STATE;
- this._emitCurrentToken();
- } else if (cp === $.EOF) {
- this._err(ERR.eofInDoctype);
- this.currentToken.forceQuirks = true;
- this._emitCurrentToken();
- this._emitEOFToken();
- } else {
- this._err(ERR.missingQuoteBeforeDoctypePublicIdentifier);
- this.currentToken.forceQuirks = true;
- this._reconsumeInState(BOGUS_DOCTYPE_STATE);
- }
- }
- // Before DOCTYPE public identifier state
- //------------------------------------------------------------------
- [BEFORE_DOCTYPE_PUBLIC_IDENTIFIER_STATE](cp) {
- if (isWhitespace(cp)) {
- return;
- }
- if (cp === $.QUOTATION_MARK) {
- this.currentToken.publicId = '';
- this.state = DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE;
- } else if (cp === $.APOSTROPHE) {
- this.currentToken.publicId = '';
- this.state = DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE;
- } else if (cp === $.GREATER_THAN_SIGN) {
- this._err(ERR.missingDoctypePublicIdentifier);
- this.currentToken.forceQuirks = true;
- this.state = DATA_STATE;
- this._emitCurrentToken();
- } else if (cp === $.EOF) {
- this._err(ERR.eofInDoctype);
- this.currentToken.forceQuirks = true;
- this._emitCurrentToken();
- this._emitEOFToken();
- } else {
- this._err(ERR.missingQuoteBeforeDoctypePublicIdentifier);
- this.currentToken.forceQuirks = true;
- this._reconsumeInState(BOGUS_DOCTYPE_STATE);
- }
- }
- // DOCTYPE public identifier (double-quoted) state
- //------------------------------------------------------------------
- [DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED_STATE](cp) {
- if (cp === $.QUOTATION_MARK) {
- this.state = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
- } else if (cp === $.NULL) {
- this._err(ERR.unexpectedNullCharacter);
- this.currentToken.publicId += unicode.REPLACEMENT_CHARACTER;
- } else if (cp === $.GREATER_THAN_SIGN) {
- this._err(ERR.abruptDoctypePublicIdentifier);
- this.currentToken.forceQuirks = true;
- this._emitCurrentToken();
- this.state = DATA_STATE;
- } else if (cp === $.EOF) {
- this._err(ERR.eofInDoctype);
- this.currentToken.forceQuirks = true;
- this._emitCurrentToken();
- this._emitEOFToken();
- } else {
- this.currentToken.publicId += toChar(cp);
- }
- }
- // DOCTYPE public identifier (single-quoted) state
- //------------------------------------------------------------------
- [DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED_STATE](cp) {
- if (cp === $.APOSTROPHE) {
- this.state = AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE;
- } else if (cp === $.NULL) {
- this._err(ERR.unexpectedNullCharacter);
- this.currentToken.publicId += unicode.REPLACEMENT_CHARACTER;
- } else if (cp === $.GREATER_THAN_SIGN) {
- this._err(ERR.abruptDoctypePublicIdentifier);
- this.currentToken.forceQuirks = true;
- this._emitCurrentToken();
- this.state = DATA_STATE;
- } else if (cp === $.EOF) {
- this._err(ERR.eofInDoctype);
- this.currentToken.forceQuirks = true;
- this._emitCurrentToken();
- this._emitEOFToken();
- } else {
- this.currentToken.publicId += toChar(cp);
- }
- }
- // After DOCTYPE public identifier state
- //------------------------------------------------------------------
- [AFTER_DOCTYPE_PUBLIC_IDENTIFIER_STATE](cp) {
- if (isWhitespace(cp)) {
- this.state = BETWEEN_DOCTYPE_PUBLIC_AND_SYSTEM_IDENTIFIERS_STATE;
- } else if (cp === $.GREATER_THAN_SIGN) {
- this.state = DATA_STATE;
- this._emitCurrentToken();
- } else if (cp === $.QUOTATION_MARK) {
- this._err(ERR.missingWhitespaceBetweenDoctypePublicAndSystemIdentifiers);
- this.currentToken.systemId = '';
- this.state = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
- } else if (cp === $.APOSTROPHE) {
- this._err(ERR.missingWhitespaceBetweenDoctypePublicAndSystemIdentifiers);
- this.currentToken.systemId = '';
- this.state = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
- } else if (cp === $.EOF) {
- this._err(ERR.eofInDoctype);
- this.currentToken.forceQuirks = true;
- this._emitCurrentToken();
- this._emitEOFToken();
- } else {
- this._err(ERR.missingQuoteBeforeDoctypeSystemIdentifier);
- this.currentToken.forceQuirks = true;
- this._reconsumeInState(BOGUS_DOCTYPE_STATE);
- }
- }
- // Between DOCTYPE public and system identifiers state
- //------------------------------------------------------------------
- [BETWEEN_DOCTYPE_PUBLIC_AND_SYSTEM_IDENTIFIERS_STATE](cp) {
- if (isWhitespace(cp)) {
- return;
- }
- if (cp === $.GREATER_THAN_SIGN) {
- this._emitCurrentToken();
- this.state = DATA_STATE;
- } else if (cp === $.QUOTATION_MARK) {
- this.currentToken.systemId = '';
- this.state = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
- } else if (cp === $.APOSTROPHE) {
- this.currentToken.systemId = '';
- this.state = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
- } else if (cp === $.EOF) {
- this._err(ERR.eofInDoctype);
- this.currentToken.forceQuirks = true;
- this._emitCurrentToken();
- this._emitEOFToken();
- } else {
- this._err(ERR.missingQuoteBeforeDoctypeSystemIdentifier);
- this.currentToken.forceQuirks = true;
- this._reconsumeInState(BOGUS_DOCTYPE_STATE);
- }
- }
- // After DOCTYPE system keyword state
- //------------------------------------------------------------------
- [AFTER_DOCTYPE_SYSTEM_KEYWORD_STATE](cp) {
- if (isWhitespace(cp)) {
- this.state = BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
- } else if (cp === $.QUOTATION_MARK) {
- this._err(ERR.missingWhitespaceAfterDoctypeSystemKeyword);
- this.currentToken.systemId = '';
- this.state = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
- } else if (cp === $.APOSTROPHE) {
- this._err(ERR.missingWhitespaceAfterDoctypeSystemKeyword);
- this.currentToken.systemId = '';
- this.state = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
- } else if (cp === $.GREATER_THAN_SIGN) {
- this._err(ERR.missingDoctypeSystemIdentifier);
- this.currentToken.forceQuirks = true;
- this.state = DATA_STATE;
- this._emitCurrentToken();
- } else if (cp === $.EOF) {
- this._err(ERR.eofInDoctype);
- this.currentToken.forceQuirks = true;
- this._emitCurrentToken();
- this._emitEOFToken();
- } else {
- this._err(ERR.missingQuoteBeforeDoctypeSystemIdentifier);
- this.currentToken.forceQuirks = true;
- this._reconsumeInState(BOGUS_DOCTYPE_STATE);
- }
- }
- // Before DOCTYPE system identifier state
- //------------------------------------------------------------------
- [BEFORE_DOCTYPE_SYSTEM_IDENTIFIER_STATE](cp) {
- if (isWhitespace(cp)) {
- return;
- }
- if (cp === $.QUOTATION_MARK) {
- this.currentToken.systemId = '';
- this.state = DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE;
- } else if (cp === $.APOSTROPHE) {
- this.currentToken.systemId = '';
- this.state = DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE;
- } else if (cp === $.GREATER_THAN_SIGN) {
- this._err(ERR.missingDoctypeSystemIdentifier);
- this.currentToken.forceQuirks = true;
- this.state = DATA_STATE;
- this._emitCurrentToken();
- } else if (cp === $.EOF) {
- this._err(ERR.eofInDoctype);
- this.currentToken.forceQuirks = true;
- this._emitCurrentToken();
- this._emitEOFToken();
- } else {
- this._err(ERR.missingQuoteBeforeDoctypeSystemIdentifier);
- this.currentToken.forceQuirks = true;
- this._reconsumeInState(BOGUS_DOCTYPE_STATE);
- }
- }
- // DOCTYPE system identifier (double-quoted) state
- //------------------------------------------------------------------
- [DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED_STATE](cp) {
- if (cp === $.QUOTATION_MARK) {
- this.state = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
- } else if (cp === $.NULL) {
- this._err(ERR.unexpectedNullCharacter);
- this.currentToken.systemId += unicode.REPLACEMENT_CHARACTER;
- } else if (cp === $.GREATER_THAN_SIGN) {
- this._err(ERR.abruptDoctypeSystemIdentifier);
- this.currentToken.forceQuirks = true;
- this._emitCurrentToken();
- this.state = DATA_STATE;
- } else if (cp === $.EOF) {
- this._err(ERR.eofInDoctype);
- this.currentToken.forceQuirks = true;
- this._emitCurrentToken();
- this._emitEOFToken();
- } else {
- this.currentToken.systemId += toChar(cp);
- }
- }
- // DOCTYPE system identifier (single-quoted) state
- //------------------------------------------------------------------
- [DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED_STATE](cp) {
- if (cp === $.APOSTROPHE) {
- this.state = AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE;
- } else if (cp === $.NULL) {
- this._err(ERR.unexpectedNullCharacter);
- this.currentToken.systemId += unicode.REPLACEMENT_CHARACTER;
- } else if (cp === $.GREATER_THAN_SIGN) {
- this._err(ERR.abruptDoctypeSystemIdentifier);
- this.currentToken.forceQuirks = true;
- this._emitCurrentToken();
- this.state = DATA_STATE;
- } else if (cp === $.EOF) {
- this._err(ERR.eofInDoctype);
- this.currentToken.forceQuirks = true;
- this._emitCurrentToken();
- this._emitEOFToken();
- } else {
- this.currentToken.systemId += toChar(cp);
- }
- }
- // After DOCTYPE system identifier state
- //------------------------------------------------------------------
- [AFTER_DOCTYPE_SYSTEM_IDENTIFIER_STATE](cp) {
- if (isWhitespace(cp)) {
- return;
- }
- if (cp === $.GREATER_THAN_SIGN) {
- this._emitCurrentToken();
- this.state = DATA_STATE;
- } else if (cp === $.EOF) {
- this._err(ERR.eofInDoctype);
- this.currentToken.forceQuirks = true;
- this._emitCurrentToken();
- this._emitEOFToken();
- } else {
- this._err(ERR.unexpectedCharacterAfterDoctypeSystemIdentifier);
- this._reconsumeInState(BOGUS_DOCTYPE_STATE);
- }
- }
- // Bogus DOCTYPE state
- //------------------------------------------------------------------
- [BOGUS_DOCTYPE_STATE](cp) {
- if (cp === $.GREATER_THAN_SIGN) {
- this._emitCurrentToken();
- this.state = DATA_STATE;
- } else if (cp === $.NULL) {
- this._err(ERR.unexpectedNullCharacter);
- } else if (cp === $.EOF) {
- this._emitCurrentToken();
- this._emitEOFToken();
- }
- }
- // CDATA section state
- //------------------------------------------------------------------
- [CDATA_SECTION_STATE](cp) {
- if (cp === $.RIGHT_SQUARE_BRACKET) {
- this.state = CDATA_SECTION_BRACKET_STATE;
- } else if (cp === $.EOF) {
- this._err(ERR.eofInCdata);
- this._emitEOFToken();
- } else {
- this._emitCodePoint(cp);
- }
- }
- // CDATA section bracket state
- //------------------------------------------------------------------
- [CDATA_SECTION_BRACKET_STATE](cp) {
- if (cp === $.RIGHT_SQUARE_BRACKET) {
- this.state = CDATA_SECTION_END_STATE;
- } else {
- this._emitChars(']');
- this._reconsumeInState(CDATA_SECTION_STATE);
- }
- }
- // CDATA section end state
- //------------------------------------------------------------------
- [CDATA_SECTION_END_STATE](cp) {
- if (cp === $.GREATER_THAN_SIGN) {
- this.state = DATA_STATE;
- } else if (cp === $.RIGHT_SQUARE_BRACKET) {
- this._emitChars(']');
- } else {
- this._emitChars(']]');
- this._reconsumeInState(CDATA_SECTION_STATE);
- }
- }
- // Character reference state
- //------------------------------------------------------------------
- [CHARACTER_REFERENCE_STATE](cp) {
- this.tempBuff = [$.AMPERSAND];
- if (cp === $.NUMBER_SIGN) {
- this.tempBuff.push(cp);
- this.state = NUMERIC_CHARACTER_REFERENCE_STATE;
- } else if (isAsciiAlphaNumeric(cp)) {
- this._reconsumeInState(NAMED_CHARACTER_REFERENCE_STATE);
- } else {
- this._flushCodePointsConsumedAsCharacterReference();
- this._reconsumeInState(this.returnState);
- }
- }
- // Named character reference state
- //------------------------------------------------------------------
- [NAMED_CHARACTER_REFERENCE_STATE](cp) {
- const matchResult = this._matchNamedCharacterReference(cp);
- //NOTE: matching can be abrupted by hibernation. In that case match
- //results are no longer valid and we will need to start over.
- if (this._ensureHibernation()) {
- this.tempBuff = [$.AMPERSAND];
- } else if (matchResult) {
- const withSemicolon = this.tempBuff[this.tempBuff.length - 1] === $.SEMICOLON;
- if (!this._isCharacterReferenceAttributeQuirk(withSemicolon)) {
- if (!withSemicolon) {
- this._errOnNextCodePoint(ERR.missingSemicolonAfterCharacterReference);
- }
- this.tempBuff = matchResult;
- }
- this._flushCodePointsConsumedAsCharacterReference();
- this.state = this.returnState;
- } else {
- this._flushCodePointsConsumedAsCharacterReference();
- this.state = AMBIGUOUS_AMPERSAND_STATE;
- }
- }
- // Ambiguos ampersand state
- //------------------------------------------------------------------
- [AMBIGUOUS_AMPERSAND_STATE](cp) {
- if (isAsciiAlphaNumeric(cp)) {
- if (this._isCharacterReferenceInAttribute()) {
- this.currentAttr.value += toChar(cp);
- } else {
- this._emitCodePoint(cp);
- }
- } else {
- if (cp === $.SEMICOLON) {
- this._err(ERR.unknownNamedCharacterReference);
- }
- this._reconsumeInState(this.returnState);
- }
- }
- // Numeric character reference state
- //------------------------------------------------------------------
- [NUMERIC_CHARACTER_REFERENCE_STATE](cp) {
- this.charRefCode = 0;
- if (cp === $.LATIN_SMALL_X || cp === $.LATIN_CAPITAL_X) {
- this.tempBuff.push(cp);
- this.state = HEXADEMICAL_CHARACTER_REFERENCE_START_STATE;
- } else {
- this._reconsumeInState(DECIMAL_CHARACTER_REFERENCE_START_STATE);
- }
- }
- // Hexademical character reference start state
- //------------------------------------------------------------------
- [HEXADEMICAL_CHARACTER_REFERENCE_START_STATE](cp) {
- if (isAsciiHexDigit(cp)) {
- this._reconsumeInState(HEXADEMICAL_CHARACTER_REFERENCE_STATE);
- } else {
- this._err(ERR.absenceOfDigitsInNumericCharacterReference);
- this._flushCodePointsConsumedAsCharacterReference();
- this._reconsumeInState(this.returnState);
- }
- }
- // Decimal character reference start state
- //------------------------------------------------------------------
- [DECIMAL_CHARACTER_REFERENCE_START_STATE](cp) {
- if (isAsciiDigit(cp)) {
- this._reconsumeInState(DECIMAL_CHARACTER_REFERENCE_STATE);
- } else {
- this._err(ERR.absenceOfDigitsInNumericCharacterReference);
- this._flushCodePointsConsumedAsCharacterReference();
- this._reconsumeInState(this.returnState);
- }
- }
- // Hexademical character reference state
- //------------------------------------------------------------------
- [HEXADEMICAL_CHARACTER_REFERENCE_STATE](cp) {
- if (isAsciiUpperHexDigit(cp)) {
- this.charRefCode = this.charRefCode * 16 + cp - 0x37;
- } else if (isAsciiLowerHexDigit(cp)) {
- this.charRefCode = this.charRefCode * 16 + cp - 0x57;
- } else if (isAsciiDigit(cp)) {
- this.charRefCode = this.charRefCode * 16 + cp - 0x30;
- } else if (cp === $.SEMICOLON) {
- this.state = NUMERIC_CHARACTER_REFERENCE_END_STATE;
- } else {
- this._err(ERR.missingSemicolonAfterCharacterReference);
- this._reconsumeInState(NUMERIC_CHARACTER_REFERENCE_END_STATE);
- }
- }
- // Decimal character reference state
- //------------------------------------------------------------------
- [DECIMAL_CHARACTER_REFERENCE_STATE](cp) {
- if (isAsciiDigit(cp)) {
- this.charRefCode = this.charRefCode * 10 + cp - 0x30;
- } else if (cp === $.SEMICOLON) {
- this.state = NUMERIC_CHARACTER_REFERENCE_END_STATE;
- } else {
- this._err(ERR.missingSemicolonAfterCharacterReference);
- this._reconsumeInState(NUMERIC_CHARACTER_REFERENCE_END_STATE);
- }
- }
- // Numeric character reference end state
- //------------------------------------------------------------------
- [NUMERIC_CHARACTER_REFERENCE_END_STATE]() {
- if (this.charRefCode === $.NULL) {
- this._err(ERR.nullCharacterReference);
- this.charRefCode = $.REPLACEMENT_CHARACTER;
- } else if (this.charRefCode > 0x10ffff) {
- this._err(ERR.characterReferenceOutsideUnicodeRange);
- this.charRefCode = $.REPLACEMENT_CHARACTER;
- } else if (unicode.isSurrogate(this.charRefCode)) {
- this._err(ERR.surrogateCharacterReference);
- this.charRefCode = $.REPLACEMENT_CHARACTER;
- } else if (unicode.isUndefinedCodePoint(this.charRefCode)) {
- this._err(ERR.noncharacterCharacterReference);
- } else if (unicode.isControlCodePoint(this.charRefCode) || this.charRefCode === $.CARRIAGE_RETURN) {
- this._err(ERR.controlCharacterReference);
- const replacement = C1_CONTROLS_REFERENCE_REPLACEMENTS[this.charRefCode];
- if (replacement) {
- this.charRefCode = replacement;
- }
- }
- this.tempBuff = [this.charRefCode];
- this._flushCodePointsConsumedAsCharacterReference();
- this._reconsumeInState(this.returnState);
- }
- }
- //Token types
- Tokenizer.CHARACTER_TOKEN = 'CHARACTER_TOKEN';
- Tokenizer.NULL_CHARACTER_TOKEN = 'NULL_CHARACTER_TOKEN';
- Tokenizer.WHITESPACE_CHARACTER_TOKEN = 'WHITESPACE_CHARACTER_TOKEN';
- Tokenizer.START_TAG_TOKEN = 'START_TAG_TOKEN';
- Tokenizer.END_TAG_TOKEN = 'END_TAG_TOKEN';
- Tokenizer.COMMENT_TOKEN = 'COMMENT_TOKEN';
- Tokenizer.DOCTYPE_TOKEN = 'DOCTYPE_TOKEN';
- Tokenizer.EOF_TOKEN = 'EOF_TOKEN';
- Tokenizer.HIBERNATION_TOKEN = 'HIBERNATION_TOKEN';
- //Tokenizer initial states for different modes
- Tokenizer.MODE = {
- DATA: DATA_STATE,
- RCDATA: RCDATA_STATE,
- RAWTEXT: RAWTEXT_STATE,
- SCRIPT_DATA: SCRIPT_DATA_STATE,
- PLAINTEXT: PLAINTEXT_STATE
- };
- //Static
- Tokenizer.getTokenAttr = function(token, attrName) {
- for (let i = token.attrs.length - 1; i >= 0; i--) {
- if (token.attrs[i].name === attrName) {
- return token.attrs[i].value;
- }
- }
- return null;
- };
- module.exports = Tokenizer;
|