index.js 96 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710
  1. import { Preprocessor } from './preprocessor.js';
  2. import { CODE_POINTS as $, SEQUENCES as $$, REPLACEMENT_CHARACTER, isSurrogate, isUndefinedCodePoint, isControlCodePoint, } from '../common/unicode.js';
  3. import { TokenType, getTokenAttr, } from '../common/token.js';
  4. import { htmlDecodeTree, EntityDecoder, DecodingMode } from 'entities/lib/decode.js';
  5. import { ERR } from '../common/error-codes.js';
  6. import { TAG_ID, getTagID } from '../common/html.js';
  7. //States
  8. var State;
  9. (function (State) {
  10. State[State["DATA"] = 0] = "DATA";
  11. State[State["RCDATA"] = 1] = "RCDATA";
  12. State[State["RAWTEXT"] = 2] = "RAWTEXT";
  13. State[State["SCRIPT_DATA"] = 3] = "SCRIPT_DATA";
  14. State[State["PLAINTEXT"] = 4] = "PLAINTEXT";
  15. State[State["TAG_OPEN"] = 5] = "TAG_OPEN";
  16. State[State["END_TAG_OPEN"] = 6] = "END_TAG_OPEN";
  17. State[State["TAG_NAME"] = 7] = "TAG_NAME";
  18. State[State["RCDATA_LESS_THAN_SIGN"] = 8] = "RCDATA_LESS_THAN_SIGN";
  19. State[State["RCDATA_END_TAG_OPEN"] = 9] = "RCDATA_END_TAG_OPEN";
  20. State[State["RCDATA_END_TAG_NAME"] = 10] = "RCDATA_END_TAG_NAME";
  21. State[State["RAWTEXT_LESS_THAN_SIGN"] = 11] = "RAWTEXT_LESS_THAN_SIGN";
  22. State[State["RAWTEXT_END_TAG_OPEN"] = 12] = "RAWTEXT_END_TAG_OPEN";
  23. State[State["RAWTEXT_END_TAG_NAME"] = 13] = "RAWTEXT_END_TAG_NAME";
  24. State[State["SCRIPT_DATA_LESS_THAN_SIGN"] = 14] = "SCRIPT_DATA_LESS_THAN_SIGN";
  25. State[State["SCRIPT_DATA_END_TAG_OPEN"] = 15] = "SCRIPT_DATA_END_TAG_OPEN";
  26. State[State["SCRIPT_DATA_END_TAG_NAME"] = 16] = "SCRIPT_DATA_END_TAG_NAME";
  27. State[State["SCRIPT_DATA_ESCAPE_START"] = 17] = "SCRIPT_DATA_ESCAPE_START";
  28. State[State["SCRIPT_DATA_ESCAPE_START_DASH"] = 18] = "SCRIPT_DATA_ESCAPE_START_DASH";
  29. State[State["SCRIPT_DATA_ESCAPED"] = 19] = "SCRIPT_DATA_ESCAPED";
  30. State[State["SCRIPT_DATA_ESCAPED_DASH"] = 20] = "SCRIPT_DATA_ESCAPED_DASH";
  31. State[State["SCRIPT_DATA_ESCAPED_DASH_DASH"] = 21] = "SCRIPT_DATA_ESCAPED_DASH_DASH";
  32. State[State["SCRIPT_DATA_ESCAPED_LESS_THAN_SIGN"] = 22] = "SCRIPT_DATA_ESCAPED_LESS_THAN_SIGN";
  33. State[State["SCRIPT_DATA_ESCAPED_END_TAG_OPEN"] = 23] = "SCRIPT_DATA_ESCAPED_END_TAG_OPEN";
  34. State[State["SCRIPT_DATA_ESCAPED_END_TAG_NAME"] = 24] = "SCRIPT_DATA_ESCAPED_END_TAG_NAME";
  35. State[State["SCRIPT_DATA_DOUBLE_ESCAPE_START"] = 25] = "SCRIPT_DATA_DOUBLE_ESCAPE_START";
  36. State[State["SCRIPT_DATA_DOUBLE_ESCAPED"] = 26] = "SCRIPT_DATA_DOUBLE_ESCAPED";
  37. State[State["SCRIPT_DATA_DOUBLE_ESCAPED_DASH"] = 27] = "SCRIPT_DATA_DOUBLE_ESCAPED_DASH";
  38. State[State["SCRIPT_DATA_DOUBLE_ESCAPED_DASH_DASH"] = 28] = "SCRIPT_DATA_DOUBLE_ESCAPED_DASH_DASH";
  39. State[State["SCRIPT_DATA_DOUBLE_ESCAPED_LESS_THAN_SIGN"] = 29] = "SCRIPT_DATA_DOUBLE_ESCAPED_LESS_THAN_SIGN";
  40. State[State["SCRIPT_DATA_DOUBLE_ESCAPE_END"] = 30] = "SCRIPT_DATA_DOUBLE_ESCAPE_END";
  41. State[State["BEFORE_ATTRIBUTE_NAME"] = 31] = "BEFORE_ATTRIBUTE_NAME";
  42. State[State["ATTRIBUTE_NAME"] = 32] = "ATTRIBUTE_NAME";
  43. State[State["AFTER_ATTRIBUTE_NAME"] = 33] = "AFTER_ATTRIBUTE_NAME";
  44. State[State["BEFORE_ATTRIBUTE_VALUE"] = 34] = "BEFORE_ATTRIBUTE_VALUE";
  45. State[State["ATTRIBUTE_VALUE_DOUBLE_QUOTED"] = 35] = "ATTRIBUTE_VALUE_DOUBLE_QUOTED";
  46. State[State["ATTRIBUTE_VALUE_SINGLE_QUOTED"] = 36] = "ATTRIBUTE_VALUE_SINGLE_QUOTED";
  47. State[State["ATTRIBUTE_VALUE_UNQUOTED"] = 37] = "ATTRIBUTE_VALUE_UNQUOTED";
  48. State[State["AFTER_ATTRIBUTE_VALUE_QUOTED"] = 38] = "AFTER_ATTRIBUTE_VALUE_QUOTED";
  49. State[State["SELF_CLOSING_START_TAG"] = 39] = "SELF_CLOSING_START_TAG";
  50. State[State["BOGUS_COMMENT"] = 40] = "BOGUS_COMMENT";
  51. State[State["MARKUP_DECLARATION_OPEN"] = 41] = "MARKUP_DECLARATION_OPEN";
  52. State[State["COMMENT_START"] = 42] = "COMMENT_START";
  53. State[State["COMMENT_START_DASH"] = 43] = "COMMENT_START_DASH";
  54. State[State["COMMENT"] = 44] = "COMMENT";
  55. State[State["COMMENT_LESS_THAN_SIGN"] = 45] = "COMMENT_LESS_THAN_SIGN";
  56. State[State["COMMENT_LESS_THAN_SIGN_BANG"] = 46] = "COMMENT_LESS_THAN_SIGN_BANG";
  57. State[State["COMMENT_LESS_THAN_SIGN_BANG_DASH"] = 47] = "COMMENT_LESS_THAN_SIGN_BANG_DASH";
  58. State[State["COMMENT_LESS_THAN_SIGN_BANG_DASH_DASH"] = 48] = "COMMENT_LESS_THAN_SIGN_BANG_DASH_DASH";
  59. State[State["COMMENT_END_DASH"] = 49] = "COMMENT_END_DASH";
  60. State[State["COMMENT_END"] = 50] = "COMMENT_END";
  61. State[State["COMMENT_END_BANG"] = 51] = "COMMENT_END_BANG";
  62. State[State["DOCTYPE"] = 52] = "DOCTYPE";
  63. State[State["BEFORE_DOCTYPE_NAME"] = 53] = "BEFORE_DOCTYPE_NAME";
  64. State[State["DOCTYPE_NAME"] = 54] = "DOCTYPE_NAME";
  65. State[State["AFTER_DOCTYPE_NAME"] = 55] = "AFTER_DOCTYPE_NAME";
  66. State[State["AFTER_DOCTYPE_PUBLIC_KEYWORD"] = 56] = "AFTER_DOCTYPE_PUBLIC_KEYWORD";
  67. State[State["BEFORE_DOCTYPE_PUBLIC_IDENTIFIER"] = 57] = "BEFORE_DOCTYPE_PUBLIC_IDENTIFIER";
  68. State[State["DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED"] = 58] = "DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED";
  69. State[State["DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED"] = 59] = "DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED";
  70. State[State["AFTER_DOCTYPE_PUBLIC_IDENTIFIER"] = 60] = "AFTER_DOCTYPE_PUBLIC_IDENTIFIER";
  71. State[State["BETWEEN_DOCTYPE_PUBLIC_AND_SYSTEM_IDENTIFIERS"] = 61] = "BETWEEN_DOCTYPE_PUBLIC_AND_SYSTEM_IDENTIFIERS";
  72. State[State["AFTER_DOCTYPE_SYSTEM_KEYWORD"] = 62] = "AFTER_DOCTYPE_SYSTEM_KEYWORD";
  73. State[State["BEFORE_DOCTYPE_SYSTEM_IDENTIFIER"] = 63] = "BEFORE_DOCTYPE_SYSTEM_IDENTIFIER";
  74. State[State["DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED"] = 64] = "DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED";
  75. State[State["DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED"] = 65] = "DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED";
  76. State[State["AFTER_DOCTYPE_SYSTEM_IDENTIFIER"] = 66] = "AFTER_DOCTYPE_SYSTEM_IDENTIFIER";
  77. State[State["BOGUS_DOCTYPE"] = 67] = "BOGUS_DOCTYPE";
  78. State[State["CDATA_SECTION"] = 68] = "CDATA_SECTION";
  79. State[State["CDATA_SECTION_BRACKET"] = 69] = "CDATA_SECTION_BRACKET";
  80. State[State["CDATA_SECTION_END"] = 70] = "CDATA_SECTION_END";
  81. State[State["CHARACTER_REFERENCE"] = 71] = "CHARACTER_REFERENCE";
  82. State[State["AMBIGUOUS_AMPERSAND"] = 72] = "AMBIGUOUS_AMPERSAND";
  83. })(State || (State = {}));
  84. //Tokenizer initial states for different modes
  85. export const TokenizerMode = {
  86. DATA: State.DATA,
  87. RCDATA: State.RCDATA,
  88. RAWTEXT: State.RAWTEXT,
  89. SCRIPT_DATA: State.SCRIPT_DATA,
  90. PLAINTEXT: State.PLAINTEXT,
  91. CDATA_SECTION: State.CDATA_SECTION,
  92. };
  93. //Utils
  94. //OPTIMIZATION: these utility functions should not be moved out of this module. V8 Crankshaft will not inline
  95. //this functions if they will be situated in another module due to context switch.
  96. //Always perform inlining check before modifying this functions ('node --trace-inlining').
  97. function isAsciiDigit(cp) {
  98. return cp >= $.DIGIT_0 && cp <= $.DIGIT_9;
  99. }
  100. function isAsciiUpper(cp) {
  101. return cp >= $.LATIN_CAPITAL_A && cp <= $.LATIN_CAPITAL_Z;
  102. }
  103. function isAsciiLower(cp) {
  104. return cp >= $.LATIN_SMALL_A && cp <= $.LATIN_SMALL_Z;
  105. }
  106. function isAsciiLetter(cp) {
  107. return isAsciiLower(cp) || isAsciiUpper(cp);
  108. }
  109. function isAsciiAlphaNumeric(cp) {
  110. return isAsciiLetter(cp) || isAsciiDigit(cp);
  111. }
  112. function toAsciiLower(cp) {
  113. return cp + 32;
  114. }
  115. function isWhitespace(cp) {
  116. return cp === $.SPACE || cp === $.LINE_FEED || cp === $.TABULATION || cp === $.FORM_FEED;
  117. }
  118. function isScriptDataDoubleEscapeSequenceEnd(cp) {
  119. return isWhitespace(cp) || cp === $.SOLIDUS || cp === $.GREATER_THAN_SIGN;
  120. }
  121. function getErrorForNumericCharacterReference(code) {
  122. if (code === $.NULL) {
  123. return ERR.nullCharacterReference;
  124. }
  125. else if (code > 1114111) {
  126. return ERR.characterReferenceOutsideUnicodeRange;
  127. }
  128. else if (isSurrogate(code)) {
  129. return ERR.surrogateCharacterReference;
  130. }
  131. else if (isUndefinedCodePoint(code)) {
  132. return ERR.noncharacterCharacterReference;
  133. }
  134. else if (isControlCodePoint(code) || code === $.CARRIAGE_RETURN) {
  135. return ERR.controlCharacterReference;
  136. }
  137. return null;
  138. }
  139. //Tokenizer
  140. export class Tokenizer {
  141. constructor(options, handler) {
  142. this.options = options;
  143. this.handler = handler;
  144. this.paused = false;
  145. /** Ensures that the parsing loop isn't run multiple times at once. */
  146. this.inLoop = false;
  147. /**
  148. * Indicates that the current adjusted node exists, is not an element in the HTML namespace,
  149. * and that it is not an integration point for either MathML or HTML.
  150. *
  151. * @see {@link https://html.spec.whatwg.org/multipage/parsing.html#tree-construction}
  152. */
  153. this.inForeignNode = false;
  154. this.lastStartTagName = '';
  155. this.active = false;
  156. this.state = State.DATA;
  157. this.returnState = State.DATA;
  158. this.entityStartPos = 0;
  159. this.consumedAfterSnapshot = -1;
  160. this.currentCharacterToken = null;
  161. this.currentToken = null;
  162. this.currentAttr = { name: '', value: '' };
  163. this.preprocessor = new Preprocessor(handler);
  164. this.currentLocation = this.getCurrentLocation(-1);
  165. this.entityDecoder = new EntityDecoder(htmlDecodeTree, (cp, consumed) => {
  166. // Note: Set `pos` _before_ flushing, as flushing might drop
  167. // the current chunk and invalidate `entityStartPos`.
  168. this.preprocessor.pos = this.entityStartPos + consumed - 1;
  169. this._flushCodePointConsumedAsCharacterReference(cp);
  170. }, handler.onParseError
  171. ? {
  172. missingSemicolonAfterCharacterReference: () => {
  173. this._err(ERR.missingSemicolonAfterCharacterReference, 1);
  174. },
  175. absenceOfDigitsInNumericCharacterReference: (consumed) => {
  176. this._err(ERR.absenceOfDigitsInNumericCharacterReference, this.entityStartPos - this.preprocessor.pos + consumed);
  177. },
  178. validateNumericCharacterReference: (code) => {
  179. const error = getErrorForNumericCharacterReference(code);
  180. if (error)
  181. this._err(error, 1);
  182. },
  183. }
  184. : undefined);
  185. }
  186. //Errors
  187. _err(code, cpOffset = 0) {
  188. var _a, _b;
  189. (_b = (_a = this.handler).onParseError) === null || _b === void 0 ? void 0 : _b.call(_a, this.preprocessor.getError(code, cpOffset));
  190. }
  191. // NOTE: `offset` may never run across line boundaries.
  192. getCurrentLocation(offset) {
  193. if (!this.options.sourceCodeLocationInfo) {
  194. return null;
  195. }
  196. return {
  197. startLine: this.preprocessor.line,
  198. startCol: this.preprocessor.col - offset,
  199. startOffset: this.preprocessor.offset - offset,
  200. endLine: -1,
  201. endCol: -1,
  202. endOffset: -1,
  203. };
  204. }
  205. _runParsingLoop() {
  206. if (this.inLoop)
  207. return;
  208. this.inLoop = true;
  209. while (this.active && !this.paused) {
  210. this.consumedAfterSnapshot = 0;
  211. const cp = this._consume();
  212. if (!this._ensureHibernation()) {
  213. this._callState(cp);
  214. }
  215. }
  216. this.inLoop = false;
  217. }
  218. //API
  219. pause() {
  220. this.paused = true;
  221. }
  222. resume(writeCallback) {
  223. if (!this.paused) {
  224. throw new Error('Parser was already resumed');
  225. }
  226. this.paused = false;
  227. // Necessary for synchronous resume.
  228. if (this.inLoop)
  229. return;
  230. this._runParsingLoop();
  231. if (!this.paused) {
  232. writeCallback === null || writeCallback === void 0 ? void 0 : writeCallback();
  233. }
  234. }
  235. write(chunk, isLastChunk, writeCallback) {
  236. this.active = true;
  237. this.preprocessor.write(chunk, isLastChunk);
  238. this._runParsingLoop();
  239. if (!this.paused) {
  240. writeCallback === null || writeCallback === void 0 ? void 0 : writeCallback();
  241. }
  242. }
  243. insertHtmlAtCurrentPos(chunk) {
  244. this.active = true;
  245. this.preprocessor.insertHtmlAtCurrentPos(chunk);
  246. this._runParsingLoop();
  247. }
  248. //Hibernation
  249. _ensureHibernation() {
  250. if (this.preprocessor.endOfChunkHit) {
  251. this.preprocessor.retreat(this.consumedAfterSnapshot);
  252. this.consumedAfterSnapshot = 0;
  253. this.active = false;
  254. return true;
  255. }
  256. return false;
  257. }
  258. //Consumption
  259. _consume() {
  260. this.consumedAfterSnapshot++;
  261. return this.preprocessor.advance();
  262. }
  263. _advanceBy(count) {
  264. this.consumedAfterSnapshot += count;
  265. for (let i = 0; i < count; i++) {
  266. this.preprocessor.advance();
  267. }
  268. }
  269. _consumeSequenceIfMatch(pattern, caseSensitive) {
  270. if (this.preprocessor.startsWith(pattern, caseSensitive)) {
  271. // We will already have consumed one character before calling this method.
  272. this._advanceBy(pattern.length - 1);
  273. return true;
  274. }
  275. return false;
  276. }
  277. //Token creation
  278. _createStartTagToken() {
  279. this.currentToken = {
  280. type: TokenType.START_TAG,
  281. tagName: '',
  282. tagID: TAG_ID.UNKNOWN,
  283. selfClosing: false,
  284. ackSelfClosing: false,
  285. attrs: [],
  286. location: this.getCurrentLocation(1),
  287. };
  288. }
  289. _createEndTagToken() {
  290. this.currentToken = {
  291. type: TokenType.END_TAG,
  292. tagName: '',
  293. tagID: TAG_ID.UNKNOWN,
  294. selfClosing: false,
  295. ackSelfClosing: false,
  296. attrs: [],
  297. location: this.getCurrentLocation(2),
  298. };
  299. }
  300. _createCommentToken(offset) {
  301. this.currentToken = {
  302. type: TokenType.COMMENT,
  303. data: '',
  304. location: this.getCurrentLocation(offset),
  305. };
  306. }
  307. _createDoctypeToken(initialName) {
  308. this.currentToken = {
  309. type: TokenType.DOCTYPE,
  310. name: initialName,
  311. forceQuirks: false,
  312. publicId: null,
  313. systemId: null,
  314. location: this.currentLocation,
  315. };
  316. }
  317. _createCharacterToken(type, chars) {
  318. this.currentCharacterToken = {
  319. type,
  320. chars,
  321. location: this.currentLocation,
  322. };
  323. }
  324. //Tag attributes
  325. _createAttr(attrNameFirstCh) {
  326. this.currentAttr = {
  327. name: attrNameFirstCh,
  328. value: '',
  329. };
  330. this.currentLocation = this.getCurrentLocation(0);
  331. }
  332. _leaveAttrName() {
  333. var _a;
  334. var _b;
  335. const token = this.currentToken;
  336. if (getTokenAttr(token, this.currentAttr.name) === null) {
  337. token.attrs.push(this.currentAttr);
  338. if (token.location && this.currentLocation) {
  339. const attrLocations = ((_a = (_b = token.location).attrs) !== null && _a !== void 0 ? _a : (_b.attrs = Object.create(null)));
  340. attrLocations[this.currentAttr.name] = this.currentLocation;
  341. // Set end location
  342. this._leaveAttrValue();
  343. }
  344. }
  345. else {
  346. this._err(ERR.duplicateAttribute);
  347. }
  348. }
  349. _leaveAttrValue() {
  350. if (this.currentLocation) {
  351. this.currentLocation.endLine = this.preprocessor.line;
  352. this.currentLocation.endCol = this.preprocessor.col;
  353. this.currentLocation.endOffset = this.preprocessor.offset;
  354. }
  355. }
  356. //Token emission
  357. prepareToken(ct) {
  358. this._emitCurrentCharacterToken(ct.location);
  359. this.currentToken = null;
  360. if (ct.location) {
  361. ct.location.endLine = this.preprocessor.line;
  362. ct.location.endCol = this.preprocessor.col + 1;
  363. ct.location.endOffset = this.preprocessor.offset + 1;
  364. }
  365. this.currentLocation = this.getCurrentLocation(-1);
  366. }
  367. emitCurrentTagToken() {
  368. const ct = this.currentToken;
  369. this.prepareToken(ct);
  370. ct.tagID = getTagID(ct.tagName);
  371. if (ct.type === TokenType.START_TAG) {
  372. this.lastStartTagName = ct.tagName;
  373. this.handler.onStartTag(ct);
  374. }
  375. else {
  376. if (ct.attrs.length > 0) {
  377. this._err(ERR.endTagWithAttributes);
  378. }
  379. if (ct.selfClosing) {
  380. this._err(ERR.endTagWithTrailingSolidus);
  381. }
  382. this.handler.onEndTag(ct);
  383. }
  384. this.preprocessor.dropParsedChunk();
  385. }
  386. emitCurrentComment(ct) {
  387. this.prepareToken(ct);
  388. this.handler.onComment(ct);
  389. this.preprocessor.dropParsedChunk();
  390. }
  391. emitCurrentDoctype(ct) {
  392. this.prepareToken(ct);
  393. this.handler.onDoctype(ct);
  394. this.preprocessor.dropParsedChunk();
  395. }
  396. _emitCurrentCharacterToken(nextLocation) {
  397. if (this.currentCharacterToken) {
  398. //NOTE: if we have a pending character token, make it's end location equal to the
  399. //current token's start location.
  400. if (nextLocation && this.currentCharacterToken.location) {
  401. this.currentCharacterToken.location.endLine = nextLocation.startLine;
  402. this.currentCharacterToken.location.endCol = nextLocation.startCol;
  403. this.currentCharacterToken.location.endOffset = nextLocation.startOffset;
  404. }
  405. switch (this.currentCharacterToken.type) {
  406. case TokenType.CHARACTER: {
  407. this.handler.onCharacter(this.currentCharacterToken);
  408. break;
  409. }
  410. case TokenType.NULL_CHARACTER: {
  411. this.handler.onNullCharacter(this.currentCharacterToken);
  412. break;
  413. }
  414. case TokenType.WHITESPACE_CHARACTER: {
  415. this.handler.onWhitespaceCharacter(this.currentCharacterToken);
  416. break;
  417. }
  418. }
  419. this.currentCharacterToken = null;
  420. }
  421. }
  422. _emitEOFToken() {
  423. const location = this.getCurrentLocation(0);
  424. if (location) {
  425. location.endLine = location.startLine;
  426. location.endCol = location.startCol;
  427. location.endOffset = location.startOffset;
  428. }
  429. this._emitCurrentCharacterToken(location);
  430. this.handler.onEof({ type: TokenType.EOF, location });
  431. this.active = false;
  432. }
  433. //Characters emission
  434. //OPTIMIZATION: The specification uses only one type of character token (one token per character).
  435. //This causes a huge memory overhead and a lot of unnecessary parser loops. parse5 uses 3 groups of characters.
  436. //If we have a sequence of characters that belong to the same group, the parser can process it
  437. //as a single solid character token.
  438. //So, there are 3 types of character tokens in parse5:
  439. //1)TokenType.NULL_CHARACTER - \u0000-character sequences (e.g. '\u0000\u0000\u0000')
  440. //2)TokenType.WHITESPACE_CHARACTER - any whitespace/new-line character sequences (e.g. '\n \r\t \f')
  441. //3)TokenType.CHARACTER - any character sequence which don't belong to groups 1 and 2 (e.g. 'abcdef1234@@#$%^')
  442. _appendCharToCurrentCharacterToken(type, ch) {
  443. if (this.currentCharacterToken) {
  444. if (this.currentCharacterToken.type === type) {
  445. this.currentCharacterToken.chars += ch;
  446. return;
  447. }
  448. else {
  449. this.currentLocation = this.getCurrentLocation(0);
  450. this._emitCurrentCharacterToken(this.currentLocation);
  451. this.preprocessor.dropParsedChunk();
  452. }
  453. }
  454. this._createCharacterToken(type, ch);
  455. }
  456. _emitCodePoint(cp) {
  457. const type = isWhitespace(cp)
  458. ? TokenType.WHITESPACE_CHARACTER
  459. : cp === $.NULL
  460. ? TokenType.NULL_CHARACTER
  461. : TokenType.CHARACTER;
  462. this._appendCharToCurrentCharacterToken(type, String.fromCodePoint(cp));
  463. }
  464. //NOTE: used when we emit characters explicitly.
  465. //This is always for non-whitespace and non-null characters, which allows us to avoid additional checks.
  466. _emitChars(ch) {
  467. this._appendCharToCurrentCharacterToken(TokenType.CHARACTER, ch);
  468. }
  469. // Character reference helpers
  470. _startCharacterReference() {
  471. this.returnState = this.state;
  472. this.state = State.CHARACTER_REFERENCE;
  473. this.entityStartPos = this.preprocessor.pos;
  474. this.entityDecoder.startEntity(this._isCharacterReferenceInAttribute() ? DecodingMode.Attribute : DecodingMode.Legacy);
  475. }
  476. _isCharacterReferenceInAttribute() {
  477. return (this.returnState === State.ATTRIBUTE_VALUE_DOUBLE_QUOTED ||
  478. this.returnState === State.ATTRIBUTE_VALUE_SINGLE_QUOTED ||
  479. this.returnState === State.ATTRIBUTE_VALUE_UNQUOTED);
  480. }
  481. _flushCodePointConsumedAsCharacterReference(cp) {
  482. if (this._isCharacterReferenceInAttribute()) {
  483. this.currentAttr.value += String.fromCodePoint(cp);
  484. }
  485. else {
  486. this._emitCodePoint(cp);
  487. }
  488. }
  489. // Calling states this way turns out to be much faster than any other approach.
  490. _callState(cp) {
  491. switch (this.state) {
  492. case State.DATA: {
  493. this._stateData(cp);
  494. break;
  495. }
  496. case State.RCDATA: {
  497. this._stateRcdata(cp);
  498. break;
  499. }
  500. case State.RAWTEXT: {
  501. this._stateRawtext(cp);
  502. break;
  503. }
  504. case State.SCRIPT_DATA: {
  505. this._stateScriptData(cp);
  506. break;
  507. }
  508. case State.PLAINTEXT: {
  509. this._statePlaintext(cp);
  510. break;
  511. }
  512. case State.TAG_OPEN: {
  513. this._stateTagOpen(cp);
  514. break;
  515. }
  516. case State.END_TAG_OPEN: {
  517. this._stateEndTagOpen(cp);
  518. break;
  519. }
  520. case State.TAG_NAME: {
  521. this._stateTagName(cp);
  522. break;
  523. }
  524. case State.RCDATA_LESS_THAN_SIGN: {
  525. this._stateRcdataLessThanSign(cp);
  526. break;
  527. }
  528. case State.RCDATA_END_TAG_OPEN: {
  529. this._stateRcdataEndTagOpen(cp);
  530. break;
  531. }
  532. case State.RCDATA_END_TAG_NAME: {
  533. this._stateRcdataEndTagName(cp);
  534. break;
  535. }
  536. case State.RAWTEXT_LESS_THAN_SIGN: {
  537. this._stateRawtextLessThanSign(cp);
  538. break;
  539. }
  540. case State.RAWTEXT_END_TAG_OPEN: {
  541. this._stateRawtextEndTagOpen(cp);
  542. break;
  543. }
  544. case State.RAWTEXT_END_TAG_NAME: {
  545. this._stateRawtextEndTagName(cp);
  546. break;
  547. }
  548. case State.SCRIPT_DATA_LESS_THAN_SIGN: {
  549. this._stateScriptDataLessThanSign(cp);
  550. break;
  551. }
  552. case State.SCRIPT_DATA_END_TAG_OPEN: {
  553. this._stateScriptDataEndTagOpen(cp);
  554. break;
  555. }
  556. case State.SCRIPT_DATA_END_TAG_NAME: {
  557. this._stateScriptDataEndTagName(cp);
  558. break;
  559. }
  560. case State.SCRIPT_DATA_ESCAPE_START: {
  561. this._stateScriptDataEscapeStart(cp);
  562. break;
  563. }
  564. case State.SCRIPT_DATA_ESCAPE_START_DASH: {
  565. this._stateScriptDataEscapeStartDash(cp);
  566. break;
  567. }
  568. case State.SCRIPT_DATA_ESCAPED: {
  569. this._stateScriptDataEscaped(cp);
  570. break;
  571. }
  572. case State.SCRIPT_DATA_ESCAPED_DASH: {
  573. this._stateScriptDataEscapedDash(cp);
  574. break;
  575. }
  576. case State.SCRIPT_DATA_ESCAPED_DASH_DASH: {
  577. this._stateScriptDataEscapedDashDash(cp);
  578. break;
  579. }
  580. case State.SCRIPT_DATA_ESCAPED_LESS_THAN_SIGN: {
  581. this._stateScriptDataEscapedLessThanSign(cp);
  582. break;
  583. }
  584. case State.SCRIPT_DATA_ESCAPED_END_TAG_OPEN: {
  585. this._stateScriptDataEscapedEndTagOpen(cp);
  586. break;
  587. }
  588. case State.SCRIPT_DATA_ESCAPED_END_TAG_NAME: {
  589. this._stateScriptDataEscapedEndTagName(cp);
  590. break;
  591. }
  592. case State.SCRIPT_DATA_DOUBLE_ESCAPE_START: {
  593. this._stateScriptDataDoubleEscapeStart(cp);
  594. break;
  595. }
  596. case State.SCRIPT_DATA_DOUBLE_ESCAPED: {
  597. this._stateScriptDataDoubleEscaped(cp);
  598. break;
  599. }
  600. case State.SCRIPT_DATA_DOUBLE_ESCAPED_DASH: {
  601. this._stateScriptDataDoubleEscapedDash(cp);
  602. break;
  603. }
  604. case State.SCRIPT_DATA_DOUBLE_ESCAPED_DASH_DASH: {
  605. this._stateScriptDataDoubleEscapedDashDash(cp);
  606. break;
  607. }
  608. case State.SCRIPT_DATA_DOUBLE_ESCAPED_LESS_THAN_SIGN: {
  609. this._stateScriptDataDoubleEscapedLessThanSign(cp);
  610. break;
  611. }
  612. case State.SCRIPT_DATA_DOUBLE_ESCAPE_END: {
  613. this._stateScriptDataDoubleEscapeEnd(cp);
  614. break;
  615. }
  616. case State.BEFORE_ATTRIBUTE_NAME: {
  617. this._stateBeforeAttributeName(cp);
  618. break;
  619. }
  620. case State.ATTRIBUTE_NAME: {
  621. this._stateAttributeName(cp);
  622. break;
  623. }
  624. case State.AFTER_ATTRIBUTE_NAME: {
  625. this._stateAfterAttributeName(cp);
  626. break;
  627. }
  628. case State.BEFORE_ATTRIBUTE_VALUE: {
  629. this._stateBeforeAttributeValue(cp);
  630. break;
  631. }
  632. case State.ATTRIBUTE_VALUE_DOUBLE_QUOTED: {
  633. this._stateAttributeValueDoubleQuoted(cp);
  634. break;
  635. }
  636. case State.ATTRIBUTE_VALUE_SINGLE_QUOTED: {
  637. this._stateAttributeValueSingleQuoted(cp);
  638. break;
  639. }
  640. case State.ATTRIBUTE_VALUE_UNQUOTED: {
  641. this._stateAttributeValueUnquoted(cp);
  642. break;
  643. }
  644. case State.AFTER_ATTRIBUTE_VALUE_QUOTED: {
  645. this._stateAfterAttributeValueQuoted(cp);
  646. break;
  647. }
  648. case State.SELF_CLOSING_START_TAG: {
  649. this._stateSelfClosingStartTag(cp);
  650. break;
  651. }
  652. case State.BOGUS_COMMENT: {
  653. this._stateBogusComment(cp);
  654. break;
  655. }
  656. case State.MARKUP_DECLARATION_OPEN: {
  657. this._stateMarkupDeclarationOpen(cp);
  658. break;
  659. }
  660. case State.COMMENT_START: {
  661. this._stateCommentStart(cp);
  662. break;
  663. }
  664. case State.COMMENT_START_DASH: {
  665. this._stateCommentStartDash(cp);
  666. break;
  667. }
  668. case State.COMMENT: {
  669. this._stateComment(cp);
  670. break;
  671. }
  672. case State.COMMENT_LESS_THAN_SIGN: {
  673. this._stateCommentLessThanSign(cp);
  674. break;
  675. }
  676. case State.COMMENT_LESS_THAN_SIGN_BANG: {
  677. this._stateCommentLessThanSignBang(cp);
  678. break;
  679. }
  680. case State.COMMENT_LESS_THAN_SIGN_BANG_DASH: {
  681. this._stateCommentLessThanSignBangDash(cp);
  682. break;
  683. }
  684. case State.COMMENT_LESS_THAN_SIGN_BANG_DASH_DASH: {
  685. this._stateCommentLessThanSignBangDashDash(cp);
  686. break;
  687. }
  688. case State.COMMENT_END_DASH: {
  689. this._stateCommentEndDash(cp);
  690. break;
  691. }
  692. case State.COMMENT_END: {
  693. this._stateCommentEnd(cp);
  694. break;
  695. }
  696. case State.COMMENT_END_BANG: {
  697. this._stateCommentEndBang(cp);
  698. break;
  699. }
  700. case State.DOCTYPE: {
  701. this._stateDoctype(cp);
  702. break;
  703. }
  704. case State.BEFORE_DOCTYPE_NAME: {
  705. this._stateBeforeDoctypeName(cp);
  706. break;
  707. }
  708. case State.DOCTYPE_NAME: {
  709. this._stateDoctypeName(cp);
  710. break;
  711. }
  712. case State.AFTER_DOCTYPE_NAME: {
  713. this._stateAfterDoctypeName(cp);
  714. break;
  715. }
  716. case State.AFTER_DOCTYPE_PUBLIC_KEYWORD: {
  717. this._stateAfterDoctypePublicKeyword(cp);
  718. break;
  719. }
  720. case State.BEFORE_DOCTYPE_PUBLIC_IDENTIFIER: {
  721. this._stateBeforeDoctypePublicIdentifier(cp);
  722. break;
  723. }
  724. case State.DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED: {
  725. this._stateDoctypePublicIdentifierDoubleQuoted(cp);
  726. break;
  727. }
  728. case State.DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED: {
  729. this._stateDoctypePublicIdentifierSingleQuoted(cp);
  730. break;
  731. }
  732. case State.AFTER_DOCTYPE_PUBLIC_IDENTIFIER: {
  733. this._stateAfterDoctypePublicIdentifier(cp);
  734. break;
  735. }
  736. case State.BETWEEN_DOCTYPE_PUBLIC_AND_SYSTEM_IDENTIFIERS: {
  737. this._stateBetweenDoctypePublicAndSystemIdentifiers(cp);
  738. break;
  739. }
  740. case State.AFTER_DOCTYPE_SYSTEM_KEYWORD: {
  741. this._stateAfterDoctypeSystemKeyword(cp);
  742. break;
  743. }
  744. case State.BEFORE_DOCTYPE_SYSTEM_IDENTIFIER: {
  745. this._stateBeforeDoctypeSystemIdentifier(cp);
  746. break;
  747. }
  748. case State.DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED: {
  749. this._stateDoctypeSystemIdentifierDoubleQuoted(cp);
  750. break;
  751. }
  752. case State.DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED: {
  753. this._stateDoctypeSystemIdentifierSingleQuoted(cp);
  754. break;
  755. }
  756. case State.AFTER_DOCTYPE_SYSTEM_IDENTIFIER: {
  757. this._stateAfterDoctypeSystemIdentifier(cp);
  758. break;
  759. }
  760. case State.BOGUS_DOCTYPE: {
  761. this._stateBogusDoctype(cp);
  762. break;
  763. }
  764. case State.CDATA_SECTION: {
  765. this._stateCdataSection(cp);
  766. break;
  767. }
  768. case State.CDATA_SECTION_BRACKET: {
  769. this._stateCdataSectionBracket(cp);
  770. break;
  771. }
  772. case State.CDATA_SECTION_END: {
  773. this._stateCdataSectionEnd(cp);
  774. break;
  775. }
  776. case State.CHARACTER_REFERENCE: {
  777. this._stateCharacterReference();
  778. break;
  779. }
  780. case State.AMBIGUOUS_AMPERSAND: {
  781. this._stateAmbiguousAmpersand(cp);
  782. break;
  783. }
  784. default: {
  785. throw new Error('Unknown state');
  786. }
  787. }
  788. }
  789. // State machine
  790. // Data state
  791. //------------------------------------------------------------------
  792. _stateData(cp) {
  793. switch (cp) {
  794. case $.LESS_THAN_SIGN: {
  795. this.state = State.TAG_OPEN;
  796. break;
  797. }
  798. case $.AMPERSAND: {
  799. this._startCharacterReference();
  800. break;
  801. }
  802. case $.NULL: {
  803. this._err(ERR.unexpectedNullCharacter);
  804. this._emitCodePoint(cp);
  805. break;
  806. }
  807. case $.EOF: {
  808. this._emitEOFToken();
  809. break;
  810. }
  811. default: {
  812. this._emitCodePoint(cp);
  813. }
  814. }
  815. }
  816. // RCDATA state
  817. //------------------------------------------------------------------
  818. _stateRcdata(cp) {
  819. switch (cp) {
  820. case $.AMPERSAND: {
  821. this._startCharacterReference();
  822. break;
  823. }
  824. case $.LESS_THAN_SIGN: {
  825. this.state = State.RCDATA_LESS_THAN_SIGN;
  826. break;
  827. }
  828. case $.NULL: {
  829. this._err(ERR.unexpectedNullCharacter);
  830. this._emitChars(REPLACEMENT_CHARACTER);
  831. break;
  832. }
  833. case $.EOF: {
  834. this._emitEOFToken();
  835. break;
  836. }
  837. default: {
  838. this._emitCodePoint(cp);
  839. }
  840. }
  841. }
  842. // RAWTEXT state
  843. //------------------------------------------------------------------
  844. _stateRawtext(cp) {
  845. switch (cp) {
  846. case $.LESS_THAN_SIGN: {
  847. this.state = State.RAWTEXT_LESS_THAN_SIGN;
  848. break;
  849. }
  850. case $.NULL: {
  851. this._err(ERR.unexpectedNullCharacter);
  852. this._emitChars(REPLACEMENT_CHARACTER);
  853. break;
  854. }
  855. case $.EOF: {
  856. this._emitEOFToken();
  857. break;
  858. }
  859. default: {
  860. this._emitCodePoint(cp);
  861. }
  862. }
  863. }
  864. // Script data state
  865. //------------------------------------------------------------------
  866. _stateScriptData(cp) {
  867. switch (cp) {
  868. case $.LESS_THAN_SIGN: {
  869. this.state = State.SCRIPT_DATA_LESS_THAN_SIGN;
  870. break;
  871. }
  872. case $.NULL: {
  873. this._err(ERR.unexpectedNullCharacter);
  874. this._emitChars(REPLACEMENT_CHARACTER);
  875. break;
  876. }
  877. case $.EOF: {
  878. this._emitEOFToken();
  879. break;
  880. }
  881. default: {
  882. this._emitCodePoint(cp);
  883. }
  884. }
  885. }
  886. // PLAINTEXT state
  887. //------------------------------------------------------------------
  888. _statePlaintext(cp) {
  889. switch (cp) {
  890. case $.NULL: {
  891. this._err(ERR.unexpectedNullCharacter);
  892. this._emitChars(REPLACEMENT_CHARACTER);
  893. break;
  894. }
  895. case $.EOF: {
  896. this._emitEOFToken();
  897. break;
  898. }
  899. default: {
  900. this._emitCodePoint(cp);
  901. }
  902. }
  903. }
  904. // Tag open state
  905. //------------------------------------------------------------------
  906. _stateTagOpen(cp) {
  907. if (isAsciiLetter(cp)) {
  908. this._createStartTagToken();
  909. this.state = State.TAG_NAME;
  910. this._stateTagName(cp);
  911. }
  912. else
  913. switch (cp) {
  914. case $.EXCLAMATION_MARK: {
  915. this.state = State.MARKUP_DECLARATION_OPEN;
  916. break;
  917. }
  918. case $.SOLIDUS: {
  919. this.state = State.END_TAG_OPEN;
  920. break;
  921. }
  922. case $.QUESTION_MARK: {
  923. this._err(ERR.unexpectedQuestionMarkInsteadOfTagName);
  924. this._createCommentToken(1);
  925. this.state = State.BOGUS_COMMENT;
  926. this._stateBogusComment(cp);
  927. break;
  928. }
  929. case $.EOF: {
  930. this._err(ERR.eofBeforeTagName);
  931. this._emitChars('<');
  932. this._emitEOFToken();
  933. break;
  934. }
  935. default: {
  936. this._err(ERR.invalidFirstCharacterOfTagName);
  937. this._emitChars('<');
  938. this.state = State.DATA;
  939. this._stateData(cp);
  940. }
  941. }
  942. }
  943. // End tag open state
  944. //------------------------------------------------------------------
  945. _stateEndTagOpen(cp) {
  946. if (isAsciiLetter(cp)) {
  947. this._createEndTagToken();
  948. this.state = State.TAG_NAME;
  949. this._stateTagName(cp);
  950. }
  951. else
  952. switch (cp) {
  953. case $.GREATER_THAN_SIGN: {
  954. this._err(ERR.missingEndTagName);
  955. this.state = State.DATA;
  956. break;
  957. }
  958. case $.EOF: {
  959. this._err(ERR.eofBeforeTagName);
  960. this._emitChars('</');
  961. this._emitEOFToken();
  962. break;
  963. }
  964. default: {
  965. this._err(ERR.invalidFirstCharacterOfTagName);
  966. this._createCommentToken(2);
  967. this.state = State.BOGUS_COMMENT;
  968. this._stateBogusComment(cp);
  969. }
  970. }
  971. }
  972. // Tag name state
  973. //------------------------------------------------------------------
  974. _stateTagName(cp) {
  975. const token = this.currentToken;
  976. switch (cp) {
  977. case $.SPACE:
  978. case $.LINE_FEED:
  979. case $.TABULATION:
  980. case $.FORM_FEED: {
  981. this.state = State.BEFORE_ATTRIBUTE_NAME;
  982. break;
  983. }
  984. case $.SOLIDUS: {
  985. this.state = State.SELF_CLOSING_START_TAG;
  986. break;
  987. }
  988. case $.GREATER_THAN_SIGN: {
  989. this.state = State.DATA;
  990. this.emitCurrentTagToken();
  991. break;
  992. }
  993. case $.NULL: {
  994. this._err(ERR.unexpectedNullCharacter);
  995. token.tagName += REPLACEMENT_CHARACTER;
  996. break;
  997. }
  998. case $.EOF: {
  999. this._err(ERR.eofInTag);
  1000. this._emitEOFToken();
  1001. break;
  1002. }
  1003. default: {
  1004. token.tagName += String.fromCodePoint(isAsciiUpper(cp) ? toAsciiLower(cp) : cp);
  1005. }
  1006. }
  1007. }
  1008. // RCDATA less-than sign state
  1009. //------------------------------------------------------------------
  1010. _stateRcdataLessThanSign(cp) {
  1011. if (cp === $.SOLIDUS) {
  1012. this.state = State.RCDATA_END_TAG_OPEN;
  1013. }
  1014. else {
  1015. this._emitChars('<');
  1016. this.state = State.RCDATA;
  1017. this._stateRcdata(cp);
  1018. }
  1019. }
  1020. // RCDATA end tag open state
  1021. //------------------------------------------------------------------
  1022. _stateRcdataEndTagOpen(cp) {
  1023. if (isAsciiLetter(cp)) {
  1024. this.state = State.RCDATA_END_TAG_NAME;
  1025. this._stateRcdataEndTagName(cp);
  1026. }
  1027. else {
  1028. this._emitChars('</');
  1029. this.state = State.RCDATA;
  1030. this._stateRcdata(cp);
  1031. }
  1032. }
  1033. handleSpecialEndTag(_cp) {
  1034. if (!this.preprocessor.startsWith(this.lastStartTagName, false)) {
  1035. return !this._ensureHibernation();
  1036. }
  1037. this._createEndTagToken();
  1038. const token = this.currentToken;
  1039. token.tagName = this.lastStartTagName;
  1040. const cp = this.preprocessor.peek(this.lastStartTagName.length);
  1041. switch (cp) {
  1042. case $.SPACE:
  1043. case $.LINE_FEED:
  1044. case $.TABULATION:
  1045. case $.FORM_FEED: {
  1046. this._advanceBy(this.lastStartTagName.length);
  1047. this.state = State.BEFORE_ATTRIBUTE_NAME;
  1048. return false;
  1049. }
  1050. case $.SOLIDUS: {
  1051. this._advanceBy(this.lastStartTagName.length);
  1052. this.state = State.SELF_CLOSING_START_TAG;
  1053. return false;
  1054. }
  1055. case $.GREATER_THAN_SIGN: {
  1056. this._advanceBy(this.lastStartTagName.length);
  1057. this.emitCurrentTagToken();
  1058. this.state = State.DATA;
  1059. return false;
  1060. }
  1061. default: {
  1062. return !this._ensureHibernation();
  1063. }
  1064. }
  1065. }
  1066. // RCDATA end tag name state
  1067. //------------------------------------------------------------------
  1068. _stateRcdataEndTagName(cp) {
  1069. if (this.handleSpecialEndTag(cp)) {
  1070. this._emitChars('</');
  1071. this.state = State.RCDATA;
  1072. this._stateRcdata(cp);
  1073. }
  1074. }
  1075. // RAWTEXT less-than sign state
  1076. //------------------------------------------------------------------
  1077. _stateRawtextLessThanSign(cp) {
  1078. if (cp === $.SOLIDUS) {
  1079. this.state = State.RAWTEXT_END_TAG_OPEN;
  1080. }
  1081. else {
  1082. this._emitChars('<');
  1083. this.state = State.RAWTEXT;
  1084. this._stateRawtext(cp);
  1085. }
  1086. }
  1087. // RAWTEXT end tag open state
  1088. //------------------------------------------------------------------
  1089. _stateRawtextEndTagOpen(cp) {
  1090. if (isAsciiLetter(cp)) {
  1091. this.state = State.RAWTEXT_END_TAG_NAME;
  1092. this._stateRawtextEndTagName(cp);
  1093. }
  1094. else {
  1095. this._emitChars('</');
  1096. this.state = State.RAWTEXT;
  1097. this._stateRawtext(cp);
  1098. }
  1099. }
  1100. // RAWTEXT end tag name state
  1101. //------------------------------------------------------------------
  1102. _stateRawtextEndTagName(cp) {
  1103. if (this.handleSpecialEndTag(cp)) {
  1104. this._emitChars('</');
  1105. this.state = State.RAWTEXT;
  1106. this._stateRawtext(cp);
  1107. }
  1108. }
  1109. // Script data less-than sign state
  1110. //------------------------------------------------------------------
  1111. _stateScriptDataLessThanSign(cp) {
  1112. switch (cp) {
  1113. case $.SOLIDUS: {
  1114. this.state = State.SCRIPT_DATA_END_TAG_OPEN;
  1115. break;
  1116. }
  1117. case $.EXCLAMATION_MARK: {
  1118. this.state = State.SCRIPT_DATA_ESCAPE_START;
  1119. this._emitChars('<!');
  1120. break;
  1121. }
  1122. default: {
  1123. this._emitChars('<');
  1124. this.state = State.SCRIPT_DATA;
  1125. this._stateScriptData(cp);
  1126. }
  1127. }
  1128. }
  1129. // Script data end tag open state
  1130. //------------------------------------------------------------------
  1131. _stateScriptDataEndTagOpen(cp) {
  1132. if (isAsciiLetter(cp)) {
  1133. this.state = State.SCRIPT_DATA_END_TAG_NAME;
  1134. this._stateScriptDataEndTagName(cp);
  1135. }
  1136. else {
  1137. this._emitChars('</');
  1138. this.state = State.SCRIPT_DATA;
  1139. this._stateScriptData(cp);
  1140. }
  1141. }
  1142. // Script data end tag name state
  1143. //------------------------------------------------------------------
  1144. _stateScriptDataEndTagName(cp) {
  1145. if (this.handleSpecialEndTag(cp)) {
  1146. this._emitChars('</');
  1147. this.state = State.SCRIPT_DATA;
  1148. this._stateScriptData(cp);
  1149. }
  1150. }
  1151. // Script data escape start state
  1152. //------------------------------------------------------------------
  1153. _stateScriptDataEscapeStart(cp) {
  1154. if (cp === $.HYPHEN_MINUS) {
  1155. this.state = State.SCRIPT_DATA_ESCAPE_START_DASH;
  1156. this._emitChars('-');
  1157. }
  1158. else {
  1159. this.state = State.SCRIPT_DATA;
  1160. this._stateScriptData(cp);
  1161. }
  1162. }
  1163. // Script data escape start dash state
  1164. //------------------------------------------------------------------
  1165. _stateScriptDataEscapeStartDash(cp) {
  1166. if (cp === $.HYPHEN_MINUS) {
  1167. this.state = State.SCRIPT_DATA_ESCAPED_DASH_DASH;
  1168. this._emitChars('-');
  1169. }
  1170. else {
  1171. this.state = State.SCRIPT_DATA;
  1172. this._stateScriptData(cp);
  1173. }
  1174. }
  1175. // Script data escaped state
  1176. //------------------------------------------------------------------
  1177. _stateScriptDataEscaped(cp) {
  1178. switch (cp) {
  1179. case $.HYPHEN_MINUS: {
  1180. this.state = State.SCRIPT_DATA_ESCAPED_DASH;
  1181. this._emitChars('-');
  1182. break;
  1183. }
  1184. case $.LESS_THAN_SIGN: {
  1185. this.state = State.SCRIPT_DATA_ESCAPED_LESS_THAN_SIGN;
  1186. break;
  1187. }
  1188. case $.NULL: {
  1189. this._err(ERR.unexpectedNullCharacter);
  1190. this._emitChars(REPLACEMENT_CHARACTER);
  1191. break;
  1192. }
  1193. case $.EOF: {
  1194. this._err(ERR.eofInScriptHtmlCommentLikeText);
  1195. this._emitEOFToken();
  1196. break;
  1197. }
  1198. default: {
  1199. this._emitCodePoint(cp);
  1200. }
  1201. }
  1202. }
  1203. // Script data escaped dash state
  1204. //------------------------------------------------------------------
  1205. _stateScriptDataEscapedDash(cp) {
  1206. switch (cp) {
  1207. case $.HYPHEN_MINUS: {
  1208. this.state = State.SCRIPT_DATA_ESCAPED_DASH_DASH;
  1209. this._emitChars('-');
  1210. break;
  1211. }
  1212. case $.LESS_THAN_SIGN: {
  1213. this.state = State.SCRIPT_DATA_ESCAPED_LESS_THAN_SIGN;
  1214. break;
  1215. }
  1216. case $.NULL: {
  1217. this._err(ERR.unexpectedNullCharacter);
  1218. this.state = State.SCRIPT_DATA_ESCAPED;
  1219. this._emitChars(REPLACEMENT_CHARACTER);
  1220. break;
  1221. }
  1222. case $.EOF: {
  1223. this._err(ERR.eofInScriptHtmlCommentLikeText);
  1224. this._emitEOFToken();
  1225. break;
  1226. }
  1227. default: {
  1228. this.state = State.SCRIPT_DATA_ESCAPED;
  1229. this._emitCodePoint(cp);
  1230. }
  1231. }
  1232. }
  1233. // Script data escaped dash dash state
  1234. //------------------------------------------------------------------
  1235. _stateScriptDataEscapedDashDash(cp) {
  1236. switch (cp) {
  1237. case $.HYPHEN_MINUS: {
  1238. this._emitChars('-');
  1239. break;
  1240. }
  1241. case $.LESS_THAN_SIGN: {
  1242. this.state = State.SCRIPT_DATA_ESCAPED_LESS_THAN_SIGN;
  1243. break;
  1244. }
  1245. case $.GREATER_THAN_SIGN: {
  1246. this.state = State.SCRIPT_DATA;
  1247. this._emitChars('>');
  1248. break;
  1249. }
  1250. case $.NULL: {
  1251. this._err(ERR.unexpectedNullCharacter);
  1252. this.state = State.SCRIPT_DATA_ESCAPED;
  1253. this._emitChars(REPLACEMENT_CHARACTER);
  1254. break;
  1255. }
  1256. case $.EOF: {
  1257. this._err(ERR.eofInScriptHtmlCommentLikeText);
  1258. this._emitEOFToken();
  1259. break;
  1260. }
  1261. default: {
  1262. this.state = State.SCRIPT_DATA_ESCAPED;
  1263. this._emitCodePoint(cp);
  1264. }
  1265. }
  1266. }
  1267. // Script data escaped less-than sign state
  1268. //------------------------------------------------------------------
  1269. _stateScriptDataEscapedLessThanSign(cp) {
  1270. if (cp === $.SOLIDUS) {
  1271. this.state = State.SCRIPT_DATA_ESCAPED_END_TAG_OPEN;
  1272. }
  1273. else if (isAsciiLetter(cp)) {
  1274. this._emitChars('<');
  1275. this.state = State.SCRIPT_DATA_DOUBLE_ESCAPE_START;
  1276. this._stateScriptDataDoubleEscapeStart(cp);
  1277. }
  1278. else {
  1279. this._emitChars('<');
  1280. this.state = State.SCRIPT_DATA_ESCAPED;
  1281. this._stateScriptDataEscaped(cp);
  1282. }
  1283. }
  1284. // Script data escaped end tag open state
  1285. //------------------------------------------------------------------
  1286. _stateScriptDataEscapedEndTagOpen(cp) {
  1287. if (isAsciiLetter(cp)) {
  1288. this.state = State.SCRIPT_DATA_ESCAPED_END_TAG_NAME;
  1289. this._stateScriptDataEscapedEndTagName(cp);
  1290. }
  1291. else {
  1292. this._emitChars('</');
  1293. this.state = State.SCRIPT_DATA_ESCAPED;
  1294. this._stateScriptDataEscaped(cp);
  1295. }
  1296. }
  1297. // Script data escaped end tag name state
  1298. //------------------------------------------------------------------
  1299. _stateScriptDataEscapedEndTagName(cp) {
  1300. if (this.handleSpecialEndTag(cp)) {
  1301. this._emitChars('</');
  1302. this.state = State.SCRIPT_DATA_ESCAPED;
  1303. this._stateScriptDataEscaped(cp);
  1304. }
  1305. }
  1306. // Script data double escape start state
  1307. //------------------------------------------------------------------
  1308. _stateScriptDataDoubleEscapeStart(cp) {
  1309. if (this.preprocessor.startsWith($$.SCRIPT, false) &&
  1310. isScriptDataDoubleEscapeSequenceEnd(this.preprocessor.peek($$.SCRIPT.length))) {
  1311. this._emitCodePoint(cp);
  1312. for (let i = 0; i < $$.SCRIPT.length; i++) {
  1313. this._emitCodePoint(this._consume());
  1314. }
  1315. this.state = State.SCRIPT_DATA_DOUBLE_ESCAPED;
  1316. }
  1317. else if (!this._ensureHibernation()) {
  1318. this.state = State.SCRIPT_DATA_ESCAPED;
  1319. this._stateScriptDataEscaped(cp);
  1320. }
  1321. }
  1322. // Script data double escaped state
  1323. //------------------------------------------------------------------
  1324. _stateScriptDataDoubleEscaped(cp) {
  1325. switch (cp) {
  1326. case $.HYPHEN_MINUS: {
  1327. this.state = State.SCRIPT_DATA_DOUBLE_ESCAPED_DASH;
  1328. this._emitChars('-');
  1329. break;
  1330. }
  1331. case $.LESS_THAN_SIGN: {
  1332. this.state = State.SCRIPT_DATA_DOUBLE_ESCAPED_LESS_THAN_SIGN;
  1333. this._emitChars('<');
  1334. break;
  1335. }
  1336. case $.NULL: {
  1337. this._err(ERR.unexpectedNullCharacter);
  1338. this._emitChars(REPLACEMENT_CHARACTER);
  1339. break;
  1340. }
  1341. case $.EOF: {
  1342. this._err(ERR.eofInScriptHtmlCommentLikeText);
  1343. this._emitEOFToken();
  1344. break;
  1345. }
  1346. default: {
  1347. this._emitCodePoint(cp);
  1348. }
  1349. }
  1350. }
  1351. // Script data double escaped dash state
  1352. //------------------------------------------------------------------
  1353. _stateScriptDataDoubleEscapedDash(cp) {
  1354. switch (cp) {
  1355. case $.HYPHEN_MINUS: {
  1356. this.state = State.SCRIPT_DATA_DOUBLE_ESCAPED_DASH_DASH;
  1357. this._emitChars('-');
  1358. break;
  1359. }
  1360. case $.LESS_THAN_SIGN: {
  1361. this.state = State.SCRIPT_DATA_DOUBLE_ESCAPED_LESS_THAN_SIGN;
  1362. this._emitChars('<');
  1363. break;
  1364. }
  1365. case $.NULL: {
  1366. this._err(ERR.unexpectedNullCharacter);
  1367. this.state = State.SCRIPT_DATA_DOUBLE_ESCAPED;
  1368. this._emitChars(REPLACEMENT_CHARACTER);
  1369. break;
  1370. }
  1371. case $.EOF: {
  1372. this._err(ERR.eofInScriptHtmlCommentLikeText);
  1373. this._emitEOFToken();
  1374. break;
  1375. }
  1376. default: {
  1377. this.state = State.SCRIPT_DATA_DOUBLE_ESCAPED;
  1378. this._emitCodePoint(cp);
  1379. }
  1380. }
  1381. }
  1382. // Script data double escaped dash dash state
  1383. //------------------------------------------------------------------
  1384. _stateScriptDataDoubleEscapedDashDash(cp) {
  1385. switch (cp) {
  1386. case $.HYPHEN_MINUS: {
  1387. this._emitChars('-');
  1388. break;
  1389. }
  1390. case $.LESS_THAN_SIGN: {
  1391. this.state = State.SCRIPT_DATA_DOUBLE_ESCAPED_LESS_THAN_SIGN;
  1392. this._emitChars('<');
  1393. break;
  1394. }
  1395. case $.GREATER_THAN_SIGN: {
  1396. this.state = State.SCRIPT_DATA;
  1397. this._emitChars('>');
  1398. break;
  1399. }
  1400. case $.NULL: {
  1401. this._err(ERR.unexpectedNullCharacter);
  1402. this.state = State.SCRIPT_DATA_DOUBLE_ESCAPED;
  1403. this._emitChars(REPLACEMENT_CHARACTER);
  1404. break;
  1405. }
  1406. case $.EOF: {
  1407. this._err(ERR.eofInScriptHtmlCommentLikeText);
  1408. this._emitEOFToken();
  1409. break;
  1410. }
  1411. default: {
  1412. this.state = State.SCRIPT_DATA_DOUBLE_ESCAPED;
  1413. this._emitCodePoint(cp);
  1414. }
  1415. }
  1416. }
  1417. // Script data double escaped less-than sign state
  1418. //------------------------------------------------------------------
  1419. _stateScriptDataDoubleEscapedLessThanSign(cp) {
  1420. if (cp === $.SOLIDUS) {
  1421. this.state = State.SCRIPT_DATA_DOUBLE_ESCAPE_END;
  1422. this._emitChars('/');
  1423. }
  1424. else {
  1425. this.state = State.SCRIPT_DATA_DOUBLE_ESCAPED;
  1426. this._stateScriptDataDoubleEscaped(cp);
  1427. }
  1428. }
  1429. // Script data double escape end state
  1430. //------------------------------------------------------------------
  1431. _stateScriptDataDoubleEscapeEnd(cp) {
  1432. if (this.preprocessor.startsWith($$.SCRIPT, false) &&
  1433. isScriptDataDoubleEscapeSequenceEnd(this.preprocessor.peek($$.SCRIPT.length))) {
  1434. this._emitCodePoint(cp);
  1435. for (let i = 0; i < $$.SCRIPT.length; i++) {
  1436. this._emitCodePoint(this._consume());
  1437. }
  1438. this.state = State.SCRIPT_DATA_ESCAPED;
  1439. }
  1440. else if (!this._ensureHibernation()) {
  1441. this.state = State.SCRIPT_DATA_DOUBLE_ESCAPED;
  1442. this._stateScriptDataDoubleEscaped(cp);
  1443. }
  1444. }
  1445. // Before attribute name state
  1446. //------------------------------------------------------------------
  1447. _stateBeforeAttributeName(cp) {
  1448. switch (cp) {
  1449. case $.SPACE:
  1450. case $.LINE_FEED:
  1451. case $.TABULATION:
  1452. case $.FORM_FEED: {
  1453. // Ignore whitespace
  1454. break;
  1455. }
  1456. case $.SOLIDUS:
  1457. case $.GREATER_THAN_SIGN:
  1458. case $.EOF: {
  1459. this.state = State.AFTER_ATTRIBUTE_NAME;
  1460. this._stateAfterAttributeName(cp);
  1461. break;
  1462. }
  1463. case $.EQUALS_SIGN: {
  1464. this._err(ERR.unexpectedEqualsSignBeforeAttributeName);
  1465. this._createAttr('=');
  1466. this.state = State.ATTRIBUTE_NAME;
  1467. break;
  1468. }
  1469. default: {
  1470. this._createAttr('');
  1471. this.state = State.ATTRIBUTE_NAME;
  1472. this._stateAttributeName(cp);
  1473. }
  1474. }
  1475. }
  1476. // Attribute name state
  1477. //------------------------------------------------------------------
  1478. _stateAttributeName(cp) {
  1479. switch (cp) {
  1480. case $.SPACE:
  1481. case $.LINE_FEED:
  1482. case $.TABULATION:
  1483. case $.FORM_FEED:
  1484. case $.SOLIDUS:
  1485. case $.GREATER_THAN_SIGN:
  1486. case $.EOF: {
  1487. this._leaveAttrName();
  1488. this.state = State.AFTER_ATTRIBUTE_NAME;
  1489. this._stateAfterAttributeName(cp);
  1490. break;
  1491. }
  1492. case $.EQUALS_SIGN: {
  1493. this._leaveAttrName();
  1494. this.state = State.BEFORE_ATTRIBUTE_VALUE;
  1495. break;
  1496. }
  1497. case $.QUOTATION_MARK:
  1498. case $.APOSTROPHE:
  1499. case $.LESS_THAN_SIGN: {
  1500. this._err(ERR.unexpectedCharacterInAttributeName);
  1501. this.currentAttr.name += String.fromCodePoint(cp);
  1502. break;
  1503. }
  1504. case $.NULL: {
  1505. this._err(ERR.unexpectedNullCharacter);
  1506. this.currentAttr.name += REPLACEMENT_CHARACTER;
  1507. break;
  1508. }
  1509. default: {
  1510. this.currentAttr.name += String.fromCodePoint(isAsciiUpper(cp) ? toAsciiLower(cp) : cp);
  1511. }
  1512. }
  1513. }
  1514. // After attribute name state
  1515. //------------------------------------------------------------------
  1516. _stateAfterAttributeName(cp) {
  1517. switch (cp) {
  1518. case $.SPACE:
  1519. case $.LINE_FEED:
  1520. case $.TABULATION:
  1521. case $.FORM_FEED: {
  1522. // Ignore whitespace
  1523. break;
  1524. }
  1525. case $.SOLIDUS: {
  1526. this.state = State.SELF_CLOSING_START_TAG;
  1527. break;
  1528. }
  1529. case $.EQUALS_SIGN: {
  1530. this.state = State.BEFORE_ATTRIBUTE_VALUE;
  1531. break;
  1532. }
  1533. case $.GREATER_THAN_SIGN: {
  1534. this.state = State.DATA;
  1535. this.emitCurrentTagToken();
  1536. break;
  1537. }
  1538. case $.EOF: {
  1539. this._err(ERR.eofInTag);
  1540. this._emitEOFToken();
  1541. break;
  1542. }
  1543. default: {
  1544. this._createAttr('');
  1545. this.state = State.ATTRIBUTE_NAME;
  1546. this._stateAttributeName(cp);
  1547. }
  1548. }
  1549. }
  1550. // Before attribute value state
  1551. //------------------------------------------------------------------
  1552. _stateBeforeAttributeValue(cp) {
  1553. switch (cp) {
  1554. case $.SPACE:
  1555. case $.LINE_FEED:
  1556. case $.TABULATION:
  1557. case $.FORM_FEED: {
  1558. // Ignore whitespace
  1559. break;
  1560. }
  1561. case $.QUOTATION_MARK: {
  1562. this.state = State.ATTRIBUTE_VALUE_DOUBLE_QUOTED;
  1563. break;
  1564. }
  1565. case $.APOSTROPHE: {
  1566. this.state = State.ATTRIBUTE_VALUE_SINGLE_QUOTED;
  1567. break;
  1568. }
  1569. case $.GREATER_THAN_SIGN: {
  1570. this._err(ERR.missingAttributeValue);
  1571. this.state = State.DATA;
  1572. this.emitCurrentTagToken();
  1573. break;
  1574. }
  1575. default: {
  1576. this.state = State.ATTRIBUTE_VALUE_UNQUOTED;
  1577. this._stateAttributeValueUnquoted(cp);
  1578. }
  1579. }
  1580. }
  1581. // Attribute value (double-quoted) state
  1582. //------------------------------------------------------------------
  1583. _stateAttributeValueDoubleQuoted(cp) {
  1584. switch (cp) {
  1585. case $.QUOTATION_MARK: {
  1586. this.state = State.AFTER_ATTRIBUTE_VALUE_QUOTED;
  1587. break;
  1588. }
  1589. case $.AMPERSAND: {
  1590. this._startCharacterReference();
  1591. break;
  1592. }
  1593. case $.NULL: {
  1594. this._err(ERR.unexpectedNullCharacter);
  1595. this.currentAttr.value += REPLACEMENT_CHARACTER;
  1596. break;
  1597. }
  1598. case $.EOF: {
  1599. this._err(ERR.eofInTag);
  1600. this._emitEOFToken();
  1601. break;
  1602. }
  1603. default: {
  1604. this.currentAttr.value += String.fromCodePoint(cp);
  1605. }
  1606. }
  1607. }
  1608. // Attribute value (single-quoted) state
  1609. //------------------------------------------------------------------
  1610. _stateAttributeValueSingleQuoted(cp) {
  1611. switch (cp) {
  1612. case $.APOSTROPHE: {
  1613. this.state = State.AFTER_ATTRIBUTE_VALUE_QUOTED;
  1614. break;
  1615. }
  1616. case $.AMPERSAND: {
  1617. this._startCharacterReference();
  1618. break;
  1619. }
  1620. case $.NULL: {
  1621. this._err(ERR.unexpectedNullCharacter);
  1622. this.currentAttr.value += REPLACEMENT_CHARACTER;
  1623. break;
  1624. }
  1625. case $.EOF: {
  1626. this._err(ERR.eofInTag);
  1627. this._emitEOFToken();
  1628. break;
  1629. }
  1630. default: {
  1631. this.currentAttr.value += String.fromCodePoint(cp);
  1632. }
  1633. }
  1634. }
  1635. // Attribute value (unquoted) state
  1636. //------------------------------------------------------------------
  1637. _stateAttributeValueUnquoted(cp) {
  1638. switch (cp) {
  1639. case $.SPACE:
  1640. case $.LINE_FEED:
  1641. case $.TABULATION:
  1642. case $.FORM_FEED: {
  1643. this._leaveAttrValue();
  1644. this.state = State.BEFORE_ATTRIBUTE_NAME;
  1645. break;
  1646. }
  1647. case $.AMPERSAND: {
  1648. this._startCharacterReference();
  1649. break;
  1650. }
  1651. case $.GREATER_THAN_SIGN: {
  1652. this._leaveAttrValue();
  1653. this.state = State.DATA;
  1654. this.emitCurrentTagToken();
  1655. break;
  1656. }
  1657. case $.NULL: {
  1658. this._err(ERR.unexpectedNullCharacter);
  1659. this.currentAttr.value += REPLACEMENT_CHARACTER;
  1660. break;
  1661. }
  1662. case $.QUOTATION_MARK:
  1663. case $.APOSTROPHE:
  1664. case $.LESS_THAN_SIGN:
  1665. case $.EQUALS_SIGN:
  1666. case $.GRAVE_ACCENT: {
  1667. this._err(ERR.unexpectedCharacterInUnquotedAttributeValue);
  1668. this.currentAttr.value += String.fromCodePoint(cp);
  1669. break;
  1670. }
  1671. case $.EOF: {
  1672. this._err(ERR.eofInTag);
  1673. this._emitEOFToken();
  1674. break;
  1675. }
  1676. default: {
  1677. this.currentAttr.value += String.fromCodePoint(cp);
  1678. }
  1679. }
  1680. }
  1681. // After attribute value (quoted) state
  1682. //------------------------------------------------------------------
  1683. _stateAfterAttributeValueQuoted(cp) {
  1684. switch (cp) {
  1685. case $.SPACE:
  1686. case $.LINE_FEED:
  1687. case $.TABULATION:
  1688. case $.FORM_FEED: {
  1689. this._leaveAttrValue();
  1690. this.state = State.BEFORE_ATTRIBUTE_NAME;
  1691. break;
  1692. }
  1693. case $.SOLIDUS: {
  1694. this._leaveAttrValue();
  1695. this.state = State.SELF_CLOSING_START_TAG;
  1696. break;
  1697. }
  1698. case $.GREATER_THAN_SIGN: {
  1699. this._leaveAttrValue();
  1700. this.state = State.DATA;
  1701. this.emitCurrentTagToken();
  1702. break;
  1703. }
  1704. case $.EOF: {
  1705. this._err(ERR.eofInTag);
  1706. this._emitEOFToken();
  1707. break;
  1708. }
  1709. default: {
  1710. this._err(ERR.missingWhitespaceBetweenAttributes);
  1711. this.state = State.BEFORE_ATTRIBUTE_NAME;
  1712. this._stateBeforeAttributeName(cp);
  1713. }
  1714. }
  1715. }
  1716. // Self-closing start tag state
  1717. //------------------------------------------------------------------
  1718. _stateSelfClosingStartTag(cp) {
  1719. switch (cp) {
  1720. case $.GREATER_THAN_SIGN: {
  1721. const token = this.currentToken;
  1722. token.selfClosing = true;
  1723. this.state = State.DATA;
  1724. this.emitCurrentTagToken();
  1725. break;
  1726. }
  1727. case $.EOF: {
  1728. this._err(ERR.eofInTag);
  1729. this._emitEOFToken();
  1730. break;
  1731. }
  1732. default: {
  1733. this._err(ERR.unexpectedSolidusInTag);
  1734. this.state = State.BEFORE_ATTRIBUTE_NAME;
  1735. this._stateBeforeAttributeName(cp);
  1736. }
  1737. }
  1738. }
  1739. // Bogus comment state
  1740. //------------------------------------------------------------------
  1741. _stateBogusComment(cp) {
  1742. const token = this.currentToken;
  1743. switch (cp) {
  1744. case $.GREATER_THAN_SIGN: {
  1745. this.state = State.DATA;
  1746. this.emitCurrentComment(token);
  1747. break;
  1748. }
  1749. case $.EOF: {
  1750. this.emitCurrentComment(token);
  1751. this._emitEOFToken();
  1752. break;
  1753. }
  1754. case $.NULL: {
  1755. this._err(ERR.unexpectedNullCharacter);
  1756. token.data += REPLACEMENT_CHARACTER;
  1757. break;
  1758. }
  1759. default: {
  1760. token.data += String.fromCodePoint(cp);
  1761. }
  1762. }
  1763. }
  1764. // Markup declaration open state
  1765. //------------------------------------------------------------------
  1766. _stateMarkupDeclarationOpen(cp) {
  1767. if (this._consumeSequenceIfMatch($$.DASH_DASH, true)) {
  1768. this._createCommentToken($$.DASH_DASH.length + 1);
  1769. this.state = State.COMMENT_START;
  1770. }
  1771. else if (this._consumeSequenceIfMatch($$.DOCTYPE, false)) {
  1772. // NOTE: Doctypes tokens are created without fixed offsets. We keep track of the moment a doctype *might* start here.
  1773. this.currentLocation = this.getCurrentLocation($$.DOCTYPE.length + 1);
  1774. this.state = State.DOCTYPE;
  1775. }
  1776. else if (this._consumeSequenceIfMatch($$.CDATA_START, true)) {
  1777. if (this.inForeignNode) {
  1778. this.state = State.CDATA_SECTION;
  1779. }
  1780. else {
  1781. this._err(ERR.cdataInHtmlContent);
  1782. this._createCommentToken($$.CDATA_START.length + 1);
  1783. this.currentToken.data = '[CDATA[';
  1784. this.state = State.BOGUS_COMMENT;
  1785. }
  1786. }
  1787. //NOTE: Sequence lookups can be abrupted by hibernation. In that case, lookup
  1788. //results are no longer valid and we will need to start over.
  1789. else if (!this._ensureHibernation()) {
  1790. this._err(ERR.incorrectlyOpenedComment);
  1791. this._createCommentToken(2);
  1792. this.state = State.BOGUS_COMMENT;
  1793. this._stateBogusComment(cp);
  1794. }
  1795. }
  1796. // Comment start state
  1797. //------------------------------------------------------------------
  1798. _stateCommentStart(cp) {
  1799. switch (cp) {
  1800. case $.HYPHEN_MINUS: {
  1801. this.state = State.COMMENT_START_DASH;
  1802. break;
  1803. }
  1804. case $.GREATER_THAN_SIGN: {
  1805. this._err(ERR.abruptClosingOfEmptyComment);
  1806. this.state = State.DATA;
  1807. const token = this.currentToken;
  1808. this.emitCurrentComment(token);
  1809. break;
  1810. }
  1811. default: {
  1812. this.state = State.COMMENT;
  1813. this._stateComment(cp);
  1814. }
  1815. }
  1816. }
  1817. // Comment start dash state
  1818. //------------------------------------------------------------------
  1819. _stateCommentStartDash(cp) {
  1820. const token = this.currentToken;
  1821. switch (cp) {
  1822. case $.HYPHEN_MINUS: {
  1823. this.state = State.COMMENT_END;
  1824. break;
  1825. }
  1826. case $.GREATER_THAN_SIGN: {
  1827. this._err(ERR.abruptClosingOfEmptyComment);
  1828. this.state = State.DATA;
  1829. this.emitCurrentComment(token);
  1830. break;
  1831. }
  1832. case $.EOF: {
  1833. this._err(ERR.eofInComment);
  1834. this.emitCurrentComment(token);
  1835. this._emitEOFToken();
  1836. break;
  1837. }
  1838. default: {
  1839. token.data += '-';
  1840. this.state = State.COMMENT;
  1841. this._stateComment(cp);
  1842. }
  1843. }
  1844. }
  1845. // Comment state
  1846. //------------------------------------------------------------------
  1847. _stateComment(cp) {
  1848. const token = this.currentToken;
  1849. switch (cp) {
  1850. case $.HYPHEN_MINUS: {
  1851. this.state = State.COMMENT_END_DASH;
  1852. break;
  1853. }
  1854. case $.LESS_THAN_SIGN: {
  1855. token.data += '<';
  1856. this.state = State.COMMENT_LESS_THAN_SIGN;
  1857. break;
  1858. }
  1859. case $.NULL: {
  1860. this._err(ERR.unexpectedNullCharacter);
  1861. token.data += REPLACEMENT_CHARACTER;
  1862. break;
  1863. }
  1864. case $.EOF: {
  1865. this._err(ERR.eofInComment);
  1866. this.emitCurrentComment(token);
  1867. this._emitEOFToken();
  1868. break;
  1869. }
  1870. default: {
  1871. token.data += String.fromCodePoint(cp);
  1872. }
  1873. }
  1874. }
  1875. // Comment less-than sign state
  1876. //------------------------------------------------------------------
  1877. _stateCommentLessThanSign(cp) {
  1878. const token = this.currentToken;
  1879. switch (cp) {
  1880. case $.EXCLAMATION_MARK: {
  1881. token.data += '!';
  1882. this.state = State.COMMENT_LESS_THAN_SIGN_BANG;
  1883. break;
  1884. }
  1885. case $.LESS_THAN_SIGN: {
  1886. token.data += '<';
  1887. break;
  1888. }
  1889. default: {
  1890. this.state = State.COMMENT;
  1891. this._stateComment(cp);
  1892. }
  1893. }
  1894. }
  1895. // Comment less-than sign bang state
  1896. //------------------------------------------------------------------
  1897. _stateCommentLessThanSignBang(cp) {
  1898. if (cp === $.HYPHEN_MINUS) {
  1899. this.state = State.COMMENT_LESS_THAN_SIGN_BANG_DASH;
  1900. }
  1901. else {
  1902. this.state = State.COMMENT;
  1903. this._stateComment(cp);
  1904. }
  1905. }
  1906. // Comment less-than sign bang dash state
  1907. //------------------------------------------------------------------
  1908. _stateCommentLessThanSignBangDash(cp) {
  1909. if (cp === $.HYPHEN_MINUS) {
  1910. this.state = State.COMMENT_LESS_THAN_SIGN_BANG_DASH_DASH;
  1911. }
  1912. else {
  1913. this.state = State.COMMENT_END_DASH;
  1914. this._stateCommentEndDash(cp);
  1915. }
  1916. }
  1917. // Comment less-than sign bang dash dash state
  1918. //------------------------------------------------------------------
  1919. _stateCommentLessThanSignBangDashDash(cp) {
  1920. if (cp !== $.GREATER_THAN_SIGN && cp !== $.EOF) {
  1921. this._err(ERR.nestedComment);
  1922. }
  1923. this.state = State.COMMENT_END;
  1924. this._stateCommentEnd(cp);
  1925. }
  1926. // Comment end dash state
  1927. //------------------------------------------------------------------
  1928. _stateCommentEndDash(cp) {
  1929. const token = this.currentToken;
  1930. switch (cp) {
  1931. case $.HYPHEN_MINUS: {
  1932. this.state = State.COMMENT_END;
  1933. break;
  1934. }
  1935. case $.EOF: {
  1936. this._err(ERR.eofInComment);
  1937. this.emitCurrentComment(token);
  1938. this._emitEOFToken();
  1939. break;
  1940. }
  1941. default: {
  1942. token.data += '-';
  1943. this.state = State.COMMENT;
  1944. this._stateComment(cp);
  1945. }
  1946. }
  1947. }
  1948. // Comment end state
  1949. //------------------------------------------------------------------
  1950. _stateCommentEnd(cp) {
  1951. const token = this.currentToken;
  1952. switch (cp) {
  1953. case $.GREATER_THAN_SIGN: {
  1954. this.state = State.DATA;
  1955. this.emitCurrentComment(token);
  1956. break;
  1957. }
  1958. case $.EXCLAMATION_MARK: {
  1959. this.state = State.COMMENT_END_BANG;
  1960. break;
  1961. }
  1962. case $.HYPHEN_MINUS: {
  1963. token.data += '-';
  1964. break;
  1965. }
  1966. case $.EOF: {
  1967. this._err(ERR.eofInComment);
  1968. this.emitCurrentComment(token);
  1969. this._emitEOFToken();
  1970. break;
  1971. }
  1972. default: {
  1973. token.data += '--';
  1974. this.state = State.COMMENT;
  1975. this._stateComment(cp);
  1976. }
  1977. }
  1978. }
  1979. // Comment end bang state
  1980. //------------------------------------------------------------------
  1981. _stateCommentEndBang(cp) {
  1982. const token = this.currentToken;
  1983. switch (cp) {
  1984. case $.HYPHEN_MINUS: {
  1985. token.data += '--!';
  1986. this.state = State.COMMENT_END_DASH;
  1987. break;
  1988. }
  1989. case $.GREATER_THAN_SIGN: {
  1990. this._err(ERR.incorrectlyClosedComment);
  1991. this.state = State.DATA;
  1992. this.emitCurrentComment(token);
  1993. break;
  1994. }
  1995. case $.EOF: {
  1996. this._err(ERR.eofInComment);
  1997. this.emitCurrentComment(token);
  1998. this._emitEOFToken();
  1999. break;
  2000. }
  2001. default: {
  2002. token.data += '--!';
  2003. this.state = State.COMMENT;
  2004. this._stateComment(cp);
  2005. }
  2006. }
  2007. }
  2008. // DOCTYPE state
  2009. //------------------------------------------------------------------
  2010. _stateDoctype(cp) {
  2011. switch (cp) {
  2012. case $.SPACE:
  2013. case $.LINE_FEED:
  2014. case $.TABULATION:
  2015. case $.FORM_FEED: {
  2016. this.state = State.BEFORE_DOCTYPE_NAME;
  2017. break;
  2018. }
  2019. case $.GREATER_THAN_SIGN: {
  2020. this.state = State.BEFORE_DOCTYPE_NAME;
  2021. this._stateBeforeDoctypeName(cp);
  2022. break;
  2023. }
  2024. case $.EOF: {
  2025. this._err(ERR.eofInDoctype);
  2026. this._createDoctypeToken(null);
  2027. const token = this.currentToken;
  2028. token.forceQuirks = true;
  2029. this.emitCurrentDoctype(token);
  2030. this._emitEOFToken();
  2031. break;
  2032. }
  2033. default: {
  2034. this._err(ERR.missingWhitespaceBeforeDoctypeName);
  2035. this.state = State.BEFORE_DOCTYPE_NAME;
  2036. this._stateBeforeDoctypeName(cp);
  2037. }
  2038. }
  2039. }
  2040. // Before DOCTYPE name state
  2041. //------------------------------------------------------------------
  2042. _stateBeforeDoctypeName(cp) {
  2043. if (isAsciiUpper(cp)) {
  2044. this._createDoctypeToken(String.fromCharCode(toAsciiLower(cp)));
  2045. this.state = State.DOCTYPE_NAME;
  2046. }
  2047. else
  2048. switch (cp) {
  2049. case $.SPACE:
  2050. case $.LINE_FEED:
  2051. case $.TABULATION:
  2052. case $.FORM_FEED: {
  2053. // Ignore whitespace
  2054. break;
  2055. }
  2056. case $.NULL: {
  2057. this._err(ERR.unexpectedNullCharacter);
  2058. this._createDoctypeToken(REPLACEMENT_CHARACTER);
  2059. this.state = State.DOCTYPE_NAME;
  2060. break;
  2061. }
  2062. case $.GREATER_THAN_SIGN: {
  2063. this._err(ERR.missingDoctypeName);
  2064. this._createDoctypeToken(null);
  2065. const token = this.currentToken;
  2066. token.forceQuirks = true;
  2067. this.emitCurrentDoctype(token);
  2068. this.state = State.DATA;
  2069. break;
  2070. }
  2071. case $.EOF: {
  2072. this._err(ERR.eofInDoctype);
  2073. this._createDoctypeToken(null);
  2074. const token = this.currentToken;
  2075. token.forceQuirks = true;
  2076. this.emitCurrentDoctype(token);
  2077. this._emitEOFToken();
  2078. break;
  2079. }
  2080. default: {
  2081. this._createDoctypeToken(String.fromCodePoint(cp));
  2082. this.state = State.DOCTYPE_NAME;
  2083. }
  2084. }
  2085. }
  2086. // DOCTYPE name state
  2087. //------------------------------------------------------------------
  2088. _stateDoctypeName(cp) {
  2089. const token = this.currentToken;
  2090. switch (cp) {
  2091. case $.SPACE:
  2092. case $.LINE_FEED:
  2093. case $.TABULATION:
  2094. case $.FORM_FEED: {
  2095. this.state = State.AFTER_DOCTYPE_NAME;
  2096. break;
  2097. }
  2098. case $.GREATER_THAN_SIGN: {
  2099. this.state = State.DATA;
  2100. this.emitCurrentDoctype(token);
  2101. break;
  2102. }
  2103. case $.NULL: {
  2104. this._err(ERR.unexpectedNullCharacter);
  2105. token.name += REPLACEMENT_CHARACTER;
  2106. break;
  2107. }
  2108. case $.EOF: {
  2109. this._err(ERR.eofInDoctype);
  2110. token.forceQuirks = true;
  2111. this.emitCurrentDoctype(token);
  2112. this._emitEOFToken();
  2113. break;
  2114. }
  2115. default: {
  2116. token.name += String.fromCodePoint(isAsciiUpper(cp) ? toAsciiLower(cp) : cp);
  2117. }
  2118. }
  2119. }
  2120. // After DOCTYPE name state
  2121. //------------------------------------------------------------------
  2122. _stateAfterDoctypeName(cp) {
  2123. const token = this.currentToken;
  2124. switch (cp) {
  2125. case $.SPACE:
  2126. case $.LINE_FEED:
  2127. case $.TABULATION:
  2128. case $.FORM_FEED: {
  2129. // Ignore whitespace
  2130. break;
  2131. }
  2132. case $.GREATER_THAN_SIGN: {
  2133. this.state = State.DATA;
  2134. this.emitCurrentDoctype(token);
  2135. break;
  2136. }
  2137. case $.EOF: {
  2138. this._err(ERR.eofInDoctype);
  2139. token.forceQuirks = true;
  2140. this.emitCurrentDoctype(token);
  2141. this._emitEOFToken();
  2142. break;
  2143. }
  2144. default: {
  2145. if (this._consumeSequenceIfMatch($$.PUBLIC, false)) {
  2146. this.state = State.AFTER_DOCTYPE_PUBLIC_KEYWORD;
  2147. }
  2148. else if (this._consumeSequenceIfMatch($$.SYSTEM, false)) {
  2149. this.state = State.AFTER_DOCTYPE_SYSTEM_KEYWORD;
  2150. }
  2151. //NOTE: sequence lookup can be abrupted by hibernation. In that case lookup
  2152. //results are no longer valid and we will need to start over.
  2153. else if (!this._ensureHibernation()) {
  2154. this._err(ERR.invalidCharacterSequenceAfterDoctypeName);
  2155. token.forceQuirks = true;
  2156. this.state = State.BOGUS_DOCTYPE;
  2157. this._stateBogusDoctype(cp);
  2158. }
  2159. }
  2160. }
  2161. }
  2162. // After DOCTYPE public keyword state
  2163. //------------------------------------------------------------------
  2164. _stateAfterDoctypePublicKeyword(cp) {
  2165. const token = this.currentToken;
  2166. switch (cp) {
  2167. case $.SPACE:
  2168. case $.LINE_FEED:
  2169. case $.TABULATION:
  2170. case $.FORM_FEED: {
  2171. this.state = State.BEFORE_DOCTYPE_PUBLIC_IDENTIFIER;
  2172. break;
  2173. }
  2174. case $.QUOTATION_MARK: {
  2175. this._err(ERR.missingWhitespaceAfterDoctypePublicKeyword);
  2176. token.publicId = '';
  2177. this.state = State.DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED;
  2178. break;
  2179. }
  2180. case $.APOSTROPHE: {
  2181. this._err(ERR.missingWhitespaceAfterDoctypePublicKeyword);
  2182. token.publicId = '';
  2183. this.state = State.DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED;
  2184. break;
  2185. }
  2186. case $.GREATER_THAN_SIGN: {
  2187. this._err(ERR.missingDoctypePublicIdentifier);
  2188. token.forceQuirks = true;
  2189. this.state = State.DATA;
  2190. this.emitCurrentDoctype(token);
  2191. break;
  2192. }
  2193. case $.EOF: {
  2194. this._err(ERR.eofInDoctype);
  2195. token.forceQuirks = true;
  2196. this.emitCurrentDoctype(token);
  2197. this._emitEOFToken();
  2198. break;
  2199. }
  2200. default: {
  2201. this._err(ERR.missingQuoteBeforeDoctypePublicIdentifier);
  2202. token.forceQuirks = true;
  2203. this.state = State.BOGUS_DOCTYPE;
  2204. this._stateBogusDoctype(cp);
  2205. }
  2206. }
  2207. }
  2208. // Before DOCTYPE public identifier state
  2209. //------------------------------------------------------------------
  2210. _stateBeforeDoctypePublicIdentifier(cp) {
  2211. const token = this.currentToken;
  2212. switch (cp) {
  2213. case $.SPACE:
  2214. case $.LINE_FEED:
  2215. case $.TABULATION:
  2216. case $.FORM_FEED: {
  2217. // Ignore whitespace
  2218. break;
  2219. }
  2220. case $.QUOTATION_MARK: {
  2221. token.publicId = '';
  2222. this.state = State.DOCTYPE_PUBLIC_IDENTIFIER_DOUBLE_QUOTED;
  2223. break;
  2224. }
  2225. case $.APOSTROPHE: {
  2226. token.publicId = '';
  2227. this.state = State.DOCTYPE_PUBLIC_IDENTIFIER_SINGLE_QUOTED;
  2228. break;
  2229. }
  2230. case $.GREATER_THAN_SIGN: {
  2231. this._err(ERR.missingDoctypePublicIdentifier);
  2232. token.forceQuirks = true;
  2233. this.state = State.DATA;
  2234. this.emitCurrentDoctype(token);
  2235. break;
  2236. }
  2237. case $.EOF: {
  2238. this._err(ERR.eofInDoctype);
  2239. token.forceQuirks = true;
  2240. this.emitCurrentDoctype(token);
  2241. this._emitEOFToken();
  2242. break;
  2243. }
  2244. default: {
  2245. this._err(ERR.missingQuoteBeforeDoctypePublicIdentifier);
  2246. token.forceQuirks = true;
  2247. this.state = State.BOGUS_DOCTYPE;
  2248. this._stateBogusDoctype(cp);
  2249. }
  2250. }
  2251. }
  2252. // DOCTYPE public identifier (double-quoted) state
  2253. //------------------------------------------------------------------
  2254. _stateDoctypePublicIdentifierDoubleQuoted(cp) {
  2255. const token = this.currentToken;
  2256. switch (cp) {
  2257. case $.QUOTATION_MARK: {
  2258. this.state = State.AFTER_DOCTYPE_PUBLIC_IDENTIFIER;
  2259. break;
  2260. }
  2261. case $.NULL: {
  2262. this._err(ERR.unexpectedNullCharacter);
  2263. token.publicId += REPLACEMENT_CHARACTER;
  2264. break;
  2265. }
  2266. case $.GREATER_THAN_SIGN: {
  2267. this._err(ERR.abruptDoctypePublicIdentifier);
  2268. token.forceQuirks = true;
  2269. this.emitCurrentDoctype(token);
  2270. this.state = State.DATA;
  2271. break;
  2272. }
  2273. case $.EOF: {
  2274. this._err(ERR.eofInDoctype);
  2275. token.forceQuirks = true;
  2276. this.emitCurrentDoctype(token);
  2277. this._emitEOFToken();
  2278. break;
  2279. }
  2280. default: {
  2281. token.publicId += String.fromCodePoint(cp);
  2282. }
  2283. }
  2284. }
  2285. // DOCTYPE public identifier (single-quoted) state
  2286. //------------------------------------------------------------------
  2287. _stateDoctypePublicIdentifierSingleQuoted(cp) {
  2288. const token = this.currentToken;
  2289. switch (cp) {
  2290. case $.APOSTROPHE: {
  2291. this.state = State.AFTER_DOCTYPE_PUBLIC_IDENTIFIER;
  2292. break;
  2293. }
  2294. case $.NULL: {
  2295. this._err(ERR.unexpectedNullCharacter);
  2296. token.publicId += REPLACEMENT_CHARACTER;
  2297. break;
  2298. }
  2299. case $.GREATER_THAN_SIGN: {
  2300. this._err(ERR.abruptDoctypePublicIdentifier);
  2301. token.forceQuirks = true;
  2302. this.emitCurrentDoctype(token);
  2303. this.state = State.DATA;
  2304. break;
  2305. }
  2306. case $.EOF: {
  2307. this._err(ERR.eofInDoctype);
  2308. token.forceQuirks = true;
  2309. this.emitCurrentDoctype(token);
  2310. this._emitEOFToken();
  2311. break;
  2312. }
  2313. default: {
  2314. token.publicId += String.fromCodePoint(cp);
  2315. }
  2316. }
  2317. }
  2318. // After DOCTYPE public identifier state
  2319. //------------------------------------------------------------------
  2320. _stateAfterDoctypePublicIdentifier(cp) {
  2321. const token = this.currentToken;
  2322. switch (cp) {
  2323. case $.SPACE:
  2324. case $.LINE_FEED:
  2325. case $.TABULATION:
  2326. case $.FORM_FEED: {
  2327. this.state = State.BETWEEN_DOCTYPE_PUBLIC_AND_SYSTEM_IDENTIFIERS;
  2328. break;
  2329. }
  2330. case $.GREATER_THAN_SIGN: {
  2331. this.state = State.DATA;
  2332. this.emitCurrentDoctype(token);
  2333. break;
  2334. }
  2335. case $.QUOTATION_MARK: {
  2336. this._err(ERR.missingWhitespaceBetweenDoctypePublicAndSystemIdentifiers);
  2337. token.systemId = '';
  2338. this.state = State.DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED;
  2339. break;
  2340. }
  2341. case $.APOSTROPHE: {
  2342. this._err(ERR.missingWhitespaceBetweenDoctypePublicAndSystemIdentifiers);
  2343. token.systemId = '';
  2344. this.state = State.DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED;
  2345. break;
  2346. }
  2347. case $.EOF: {
  2348. this._err(ERR.eofInDoctype);
  2349. token.forceQuirks = true;
  2350. this.emitCurrentDoctype(token);
  2351. this._emitEOFToken();
  2352. break;
  2353. }
  2354. default: {
  2355. this._err(ERR.missingQuoteBeforeDoctypeSystemIdentifier);
  2356. token.forceQuirks = true;
  2357. this.state = State.BOGUS_DOCTYPE;
  2358. this._stateBogusDoctype(cp);
  2359. }
  2360. }
  2361. }
  2362. // Between DOCTYPE public and system identifiers state
  2363. //------------------------------------------------------------------
  2364. _stateBetweenDoctypePublicAndSystemIdentifiers(cp) {
  2365. const token = this.currentToken;
  2366. switch (cp) {
  2367. case $.SPACE:
  2368. case $.LINE_FEED:
  2369. case $.TABULATION:
  2370. case $.FORM_FEED: {
  2371. // Ignore whitespace
  2372. break;
  2373. }
  2374. case $.GREATER_THAN_SIGN: {
  2375. this.emitCurrentDoctype(token);
  2376. this.state = State.DATA;
  2377. break;
  2378. }
  2379. case $.QUOTATION_MARK: {
  2380. token.systemId = '';
  2381. this.state = State.DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED;
  2382. break;
  2383. }
  2384. case $.APOSTROPHE: {
  2385. token.systemId = '';
  2386. this.state = State.DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED;
  2387. break;
  2388. }
  2389. case $.EOF: {
  2390. this._err(ERR.eofInDoctype);
  2391. token.forceQuirks = true;
  2392. this.emitCurrentDoctype(token);
  2393. this._emitEOFToken();
  2394. break;
  2395. }
  2396. default: {
  2397. this._err(ERR.missingQuoteBeforeDoctypeSystemIdentifier);
  2398. token.forceQuirks = true;
  2399. this.state = State.BOGUS_DOCTYPE;
  2400. this._stateBogusDoctype(cp);
  2401. }
  2402. }
  2403. }
  2404. // After DOCTYPE system keyword state
  2405. //------------------------------------------------------------------
  2406. _stateAfterDoctypeSystemKeyword(cp) {
  2407. const token = this.currentToken;
  2408. switch (cp) {
  2409. case $.SPACE:
  2410. case $.LINE_FEED:
  2411. case $.TABULATION:
  2412. case $.FORM_FEED: {
  2413. this.state = State.BEFORE_DOCTYPE_SYSTEM_IDENTIFIER;
  2414. break;
  2415. }
  2416. case $.QUOTATION_MARK: {
  2417. this._err(ERR.missingWhitespaceAfterDoctypeSystemKeyword);
  2418. token.systemId = '';
  2419. this.state = State.DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED;
  2420. break;
  2421. }
  2422. case $.APOSTROPHE: {
  2423. this._err(ERR.missingWhitespaceAfterDoctypeSystemKeyword);
  2424. token.systemId = '';
  2425. this.state = State.DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED;
  2426. break;
  2427. }
  2428. case $.GREATER_THAN_SIGN: {
  2429. this._err(ERR.missingDoctypeSystemIdentifier);
  2430. token.forceQuirks = true;
  2431. this.state = State.DATA;
  2432. this.emitCurrentDoctype(token);
  2433. break;
  2434. }
  2435. case $.EOF: {
  2436. this._err(ERR.eofInDoctype);
  2437. token.forceQuirks = true;
  2438. this.emitCurrentDoctype(token);
  2439. this._emitEOFToken();
  2440. break;
  2441. }
  2442. default: {
  2443. this._err(ERR.missingQuoteBeforeDoctypeSystemIdentifier);
  2444. token.forceQuirks = true;
  2445. this.state = State.BOGUS_DOCTYPE;
  2446. this._stateBogusDoctype(cp);
  2447. }
  2448. }
  2449. }
  2450. // Before DOCTYPE system identifier state
  2451. //------------------------------------------------------------------
  2452. _stateBeforeDoctypeSystemIdentifier(cp) {
  2453. const token = this.currentToken;
  2454. switch (cp) {
  2455. case $.SPACE:
  2456. case $.LINE_FEED:
  2457. case $.TABULATION:
  2458. case $.FORM_FEED: {
  2459. // Ignore whitespace
  2460. break;
  2461. }
  2462. case $.QUOTATION_MARK: {
  2463. token.systemId = '';
  2464. this.state = State.DOCTYPE_SYSTEM_IDENTIFIER_DOUBLE_QUOTED;
  2465. break;
  2466. }
  2467. case $.APOSTROPHE: {
  2468. token.systemId = '';
  2469. this.state = State.DOCTYPE_SYSTEM_IDENTIFIER_SINGLE_QUOTED;
  2470. break;
  2471. }
  2472. case $.GREATER_THAN_SIGN: {
  2473. this._err(ERR.missingDoctypeSystemIdentifier);
  2474. token.forceQuirks = true;
  2475. this.state = State.DATA;
  2476. this.emitCurrentDoctype(token);
  2477. break;
  2478. }
  2479. case $.EOF: {
  2480. this._err(ERR.eofInDoctype);
  2481. token.forceQuirks = true;
  2482. this.emitCurrentDoctype(token);
  2483. this._emitEOFToken();
  2484. break;
  2485. }
  2486. default: {
  2487. this._err(ERR.missingQuoteBeforeDoctypeSystemIdentifier);
  2488. token.forceQuirks = true;
  2489. this.state = State.BOGUS_DOCTYPE;
  2490. this._stateBogusDoctype(cp);
  2491. }
  2492. }
  2493. }
  2494. // DOCTYPE system identifier (double-quoted) state
  2495. //------------------------------------------------------------------
  2496. _stateDoctypeSystemIdentifierDoubleQuoted(cp) {
  2497. const token = this.currentToken;
  2498. switch (cp) {
  2499. case $.QUOTATION_MARK: {
  2500. this.state = State.AFTER_DOCTYPE_SYSTEM_IDENTIFIER;
  2501. break;
  2502. }
  2503. case $.NULL: {
  2504. this._err(ERR.unexpectedNullCharacter);
  2505. token.systemId += REPLACEMENT_CHARACTER;
  2506. break;
  2507. }
  2508. case $.GREATER_THAN_SIGN: {
  2509. this._err(ERR.abruptDoctypeSystemIdentifier);
  2510. token.forceQuirks = true;
  2511. this.emitCurrentDoctype(token);
  2512. this.state = State.DATA;
  2513. break;
  2514. }
  2515. case $.EOF: {
  2516. this._err(ERR.eofInDoctype);
  2517. token.forceQuirks = true;
  2518. this.emitCurrentDoctype(token);
  2519. this._emitEOFToken();
  2520. break;
  2521. }
  2522. default: {
  2523. token.systemId += String.fromCodePoint(cp);
  2524. }
  2525. }
  2526. }
  2527. // DOCTYPE system identifier (single-quoted) state
  2528. //------------------------------------------------------------------
  2529. _stateDoctypeSystemIdentifierSingleQuoted(cp) {
  2530. const token = this.currentToken;
  2531. switch (cp) {
  2532. case $.APOSTROPHE: {
  2533. this.state = State.AFTER_DOCTYPE_SYSTEM_IDENTIFIER;
  2534. break;
  2535. }
  2536. case $.NULL: {
  2537. this._err(ERR.unexpectedNullCharacter);
  2538. token.systemId += REPLACEMENT_CHARACTER;
  2539. break;
  2540. }
  2541. case $.GREATER_THAN_SIGN: {
  2542. this._err(ERR.abruptDoctypeSystemIdentifier);
  2543. token.forceQuirks = true;
  2544. this.emitCurrentDoctype(token);
  2545. this.state = State.DATA;
  2546. break;
  2547. }
  2548. case $.EOF: {
  2549. this._err(ERR.eofInDoctype);
  2550. token.forceQuirks = true;
  2551. this.emitCurrentDoctype(token);
  2552. this._emitEOFToken();
  2553. break;
  2554. }
  2555. default: {
  2556. token.systemId += String.fromCodePoint(cp);
  2557. }
  2558. }
  2559. }
  2560. // After DOCTYPE system identifier state
  2561. //------------------------------------------------------------------
  2562. _stateAfterDoctypeSystemIdentifier(cp) {
  2563. const token = this.currentToken;
  2564. switch (cp) {
  2565. case $.SPACE:
  2566. case $.LINE_FEED:
  2567. case $.TABULATION:
  2568. case $.FORM_FEED: {
  2569. // Ignore whitespace
  2570. break;
  2571. }
  2572. case $.GREATER_THAN_SIGN: {
  2573. this.emitCurrentDoctype(token);
  2574. this.state = State.DATA;
  2575. break;
  2576. }
  2577. case $.EOF: {
  2578. this._err(ERR.eofInDoctype);
  2579. token.forceQuirks = true;
  2580. this.emitCurrentDoctype(token);
  2581. this._emitEOFToken();
  2582. break;
  2583. }
  2584. default: {
  2585. this._err(ERR.unexpectedCharacterAfterDoctypeSystemIdentifier);
  2586. this.state = State.BOGUS_DOCTYPE;
  2587. this._stateBogusDoctype(cp);
  2588. }
  2589. }
  2590. }
  2591. // Bogus DOCTYPE state
  2592. //------------------------------------------------------------------
  2593. _stateBogusDoctype(cp) {
  2594. const token = this.currentToken;
  2595. switch (cp) {
  2596. case $.GREATER_THAN_SIGN: {
  2597. this.emitCurrentDoctype(token);
  2598. this.state = State.DATA;
  2599. break;
  2600. }
  2601. case $.NULL: {
  2602. this._err(ERR.unexpectedNullCharacter);
  2603. break;
  2604. }
  2605. case $.EOF: {
  2606. this.emitCurrentDoctype(token);
  2607. this._emitEOFToken();
  2608. break;
  2609. }
  2610. default:
  2611. // Do nothing
  2612. }
  2613. }
  2614. // CDATA section state
  2615. //------------------------------------------------------------------
  2616. _stateCdataSection(cp) {
  2617. switch (cp) {
  2618. case $.RIGHT_SQUARE_BRACKET: {
  2619. this.state = State.CDATA_SECTION_BRACKET;
  2620. break;
  2621. }
  2622. case $.EOF: {
  2623. this._err(ERR.eofInCdata);
  2624. this._emitEOFToken();
  2625. break;
  2626. }
  2627. default: {
  2628. this._emitCodePoint(cp);
  2629. }
  2630. }
  2631. }
  2632. // CDATA section bracket state
  2633. //------------------------------------------------------------------
  2634. _stateCdataSectionBracket(cp) {
  2635. if (cp === $.RIGHT_SQUARE_BRACKET) {
  2636. this.state = State.CDATA_SECTION_END;
  2637. }
  2638. else {
  2639. this._emitChars(']');
  2640. this.state = State.CDATA_SECTION;
  2641. this._stateCdataSection(cp);
  2642. }
  2643. }
  2644. // CDATA section end state
  2645. //------------------------------------------------------------------
  2646. _stateCdataSectionEnd(cp) {
  2647. switch (cp) {
  2648. case $.GREATER_THAN_SIGN: {
  2649. this.state = State.DATA;
  2650. break;
  2651. }
  2652. case $.RIGHT_SQUARE_BRACKET: {
  2653. this._emitChars(']');
  2654. break;
  2655. }
  2656. default: {
  2657. this._emitChars(']]');
  2658. this.state = State.CDATA_SECTION;
  2659. this._stateCdataSection(cp);
  2660. }
  2661. }
  2662. }
  2663. // Character reference state
  2664. //------------------------------------------------------------------
  2665. _stateCharacterReference() {
  2666. let length = this.entityDecoder.write(this.preprocessor.html, this.preprocessor.pos);
  2667. if (length < 0) {
  2668. if (this.preprocessor.lastChunkWritten) {
  2669. length = this.entityDecoder.end();
  2670. }
  2671. else {
  2672. // Wait for the rest of the entity.
  2673. this.active = false;
  2674. // Mark the entire buffer as read.
  2675. this.preprocessor.pos = this.preprocessor.html.length - 1;
  2676. this.consumedAfterSnapshot = 0;
  2677. this.preprocessor.endOfChunkHit = true;
  2678. return;
  2679. }
  2680. }
  2681. if (length === 0) {
  2682. // This was not a valid entity. Go back to the beginning, and
  2683. // figure out what to do.
  2684. this.preprocessor.pos = this.entityStartPos;
  2685. this._flushCodePointConsumedAsCharacterReference($.AMPERSAND);
  2686. this.state =
  2687. !this._isCharacterReferenceInAttribute() && isAsciiAlphaNumeric(this.preprocessor.peek(1))
  2688. ? State.AMBIGUOUS_AMPERSAND
  2689. : this.returnState;
  2690. }
  2691. else {
  2692. // We successfully parsed an entity. Switch to the return state.
  2693. this.state = this.returnState;
  2694. }
  2695. }
  2696. // Ambiguos ampersand state
  2697. //------------------------------------------------------------------
  2698. _stateAmbiguousAmpersand(cp) {
  2699. if (isAsciiAlphaNumeric(cp)) {
  2700. this._flushCodePointConsumedAsCharacterReference(cp);
  2701. }
  2702. else {
  2703. if (cp === $.SEMICOLON) {
  2704. this._err(ERR.unknownNamedCharacterReference);
  2705. }
  2706. this.state = this.returnState;
  2707. this._callState(cp);
  2708. }
  2709. }
  2710. }