index.js 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819
  1. 'use strict';
  2. var Tokenizer = require('../tokenizer'),
  3. OpenElementStack = require('./open_element_stack'),
  4. FormattingElementList = require('./formatting_element_list'),
  5. LocationInfoParserMixin = require('../extensions/location_info/parser_mixin'),
  6. defaultTreeAdapter = require('../tree_adapters/default'),
  7. mergeOptions = require('../utils/merge_options'),
  8. doctype = require('../common/doctype'),
  9. foreignContent = require('../common/foreign_content'),
  10. UNICODE = require('../common/unicode'),
  11. HTML = require('../common/html');
  12. //Aliases
  13. var $ = HTML.TAG_NAMES,
  14. NS = HTML.NAMESPACES,
  15. ATTRS = HTML.ATTRS;
  16. var DEFAULT_OPTIONS = {
  17. locationInfo: false,
  18. treeAdapter: defaultTreeAdapter
  19. };
  20. //Misc constants
  21. var HIDDEN_INPUT_TYPE = 'hidden';
  22. //Adoption agency loops iteration count
  23. var AA_OUTER_LOOP_ITER = 8,
  24. AA_INNER_LOOP_ITER = 3;
  25. //Insertion modes
  26. var INITIAL_MODE = 'INITIAL_MODE',
  27. BEFORE_HTML_MODE = 'BEFORE_HTML_MODE',
  28. BEFORE_HEAD_MODE = 'BEFORE_HEAD_MODE',
  29. IN_HEAD_MODE = 'IN_HEAD_MODE',
  30. AFTER_HEAD_MODE = 'AFTER_HEAD_MODE',
  31. IN_BODY_MODE = 'IN_BODY_MODE',
  32. TEXT_MODE = 'TEXT_MODE',
  33. IN_TABLE_MODE = 'IN_TABLE_MODE',
  34. IN_TABLE_TEXT_MODE = 'IN_TABLE_TEXT_MODE',
  35. IN_CAPTION_MODE = 'IN_CAPTION_MODE',
  36. IN_COLUMN_GROUP_MODE = 'IN_COLUMN_GROUP_MODE',
  37. IN_TABLE_BODY_MODE = 'IN_TABLE_BODY_MODE',
  38. IN_ROW_MODE = 'IN_ROW_MODE',
  39. IN_CELL_MODE = 'IN_CELL_MODE',
  40. IN_SELECT_MODE = 'IN_SELECT_MODE',
  41. IN_SELECT_IN_TABLE_MODE = 'IN_SELECT_IN_TABLE_MODE',
  42. IN_TEMPLATE_MODE = 'IN_TEMPLATE_MODE',
  43. AFTER_BODY_MODE = 'AFTER_BODY_MODE',
  44. IN_FRAMESET_MODE = 'IN_FRAMESET_MODE',
  45. AFTER_FRAMESET_MODE = 'AFTER_FRAMESET_MODE',
  46. AFTER_AFTER_BODY_MODE = 'AFTER_AFTER_BODY_MODE',
  47. AFTER_AFTER_FRAMESET_MODE = 'AFTER_AFTER_FRAMESET_MODE';
  48. //Insertion mode reset map
  49. var INSERTION_MODE_RESET_MAP = Object.create(null);
  50. INSERTION_MODE_RESET_MAP[$.TR] = IN_ROW_MODE;
  51. INSERTION_MODE_RESET_MAP[$.TBODY] =
  52. INSERTION_MODE_RESET_MAP[$.THEAD] =
  53. INSERTION_MODE_RESET_MAP[$.TFOOT] = IN_TABLE_BODY_MODE;
  54. INSERTION_MODE_RESET_MAP[$.CAPTION] = IN_CAPTION_MODE;
  55. INSERTION_MODE_RESET_MAP[$.COLGROUP] = IN_COLUMN_GROUP_MODE;
  56. INSERTION_MODE_RESET_MAP[$.TABLE] = IN_TABLE_MODE;
  57. INSERTION_MODE_RESET_MAP[$.BODY] = IN_BODY_MODE;
  58. INSERTION_MODE_RESET_MAP[$.FRAMESET] = IN_FRAMESET_MODE;
  59. //Template insertion mode switch map
  60. var TEMPLATE_INSERTION_MODE_SWITCH_MAP = Object.create(null);
  61. TEMPLATE_INSERTION_MODE_SWITCH_MAP[$.CAPTION] =
  62. TEMPLATE_INSERTION_MODE_SWITCH_MAP[$.COLGROUP] =
  63. TEMPLATE_INSERTION_MODE_SWITCH_MAP[$.TBODY] =
  64. TEMPLATE_INSERTION_MODE_SWITCH_MAP[$.TFOOT] =
  65. TEMPLATE_INSERTION_MODE_SWITCH_MAP[$.THEAD] = IN_TABLE_MODE;
  66. TEMPLATE_INSERTION_MODE_SWITCH_MAP[$.COL] = IN_COLUMN_GROUP_MODE;
  67. TEMPLATE_INSERTION_MODE_SWITCH_MAP[$.TR] = IN_TABLE_BODY_MODE;
  68. TEMPLATE_INSERTION_MODE_SWITCH_MAP[$.TD] =
  69. TEMPLATE_INSERTION_MODE_SWITCH_MAP[$.TH] = IN_ROW_MODE;
  70. //Token handlers map for insertion modes
  71. var _ = Object.create(null);
  72. _[INITIAL_MODE] = Object.create(null);
  73. _[INITIAL_MODE][Tokenizer.CHARACTER_TOKEN] =
  74. _[INITIAL_MODE][Tokenizer.NULL_CHARACTER_TOKEN] = tokenInInitialMode;
  75. _[INITIAL_MODE][Tokenizer.WHITESPACE_CHARACTER_TOKEN] = ignoreToken;
  76. _[INITIAL_MODE][Tokenizer.COMMENT_TOKEN] = appendComment;
  77. _[INITIAL_MODE][Tokenizer.DOCTYPE_TOKEN] = doctypeInInitialMode;
  78. _[INITIAL_MODE][Tokenizer.START_TAG_TOKEN] =
  79. _[INITIAL_MODE][Tokenizer.END_TAG_TOKEN] =
  80. _[INITIAL_MODE][Tokenizer.EOF_TOKEN] = tokenInInitialMode;
  81. _[BEFORE_HTML_MODE] = Object.create(null);
  82. _[BEFORE_HTML_MODE][Tokenizer.CHARACTER_TOKEN] =
  83. _[BEFORE_HTML_MODE][Tokenizer.NULL_CHARACTER_TOKEN] = tokenBeforeHtml;
  84. _[BEFORE_HTML_MODE][Tokenizer.WHITESPACE_CHARACTER_TOKEN] = ignoreToken;
  85. _[BEFORE_HTML_MODE][Tokenizer.COMMENT_TOKEN] = appendComment;
  86. _[BEFORE_HTML_MODE][Tokenizer.DOCTYPE_TOKEN] = ignoreToken;
  87. _[BEFORE_HTML_MODE][Tokenizer.START_TAG_TOKEN] = startTagBeforeHtml;
  88. _[BEFORE_HTML_MODE][Tokenizer.END_TAG_TOKEN] = endTagBeforeHtml;
  89. _[BEFORE_HTML_MODE][Tokenizer.EOF_TOKEN] = tokenBeforeHtml;
  90. _[BEFORE_HEAD_MODE] = Object.create(null);
  91. _[BEFORE_HEAD_MODE][Tokenizer.CHARACTER_TOKEN] =
  92. _[BEFORE_HEAD_MODE][Tokenizer.NULL_CHARACTER_TOKEN] = tokenBeforeHead;
  93. _[BEFORE_HEAD_MODE][Tokenizer.WHITESPACE_CHARACTER_TOKEN] = ignoreToken;
  94. _[BEFORE_HEAD_MODE][Tokenizer.COMMENT_TOKEN] = appendComment;
  95. _[BEFORE_HEAD_MODE][Tokenizer.DOCTYPE_TOKEN] = ignoreToken;
  96. _[BEFORE_HEAD_MODE][Tokenizer.START_TAG_TOKEN] = startTagBeforeHead;
  97. _[BEFORE_HEAD_MODE][Tokenizer.END_TAG_TOKEN] = endTagBeforeHead;
  98. _[BEFORE_HEAD_MODE][Tokenizer.EOF_TOKEN] = tokenBeforeHead;
  99. _[IN_HEAD_MODE] = Object.create(null);
  100. _[IN_HEAD_MODE][Tokenizer.CHARACTER_TOKEN] =
  101. _[IN_HEAD_MODE][Tokenizer.NULL_CHARACTER_TOKEN] = tokenInHead;
  102. _[IN_HEAD_MODE][Tokenizer.WHITESPACE_CHARACTER_TOKEN] = insertCharacters;
  103. _[IN_HEAD_MODE][Tokenizer.COMMENT_TOKEN] = appendComment;
  104. _[IN_HEAD_MODE][Tokenizer.DOCTYPE_TOKEN] = ignoreToken;
  105. _[IN_HEAD_MODE][Tokenizer.START_TAG_TOKEN] = startTagInHead;
  106. _[IN_HEAD_MODE][Tokenizer.END_TAG_TOKEN] = endTagInHead;
  107. _[IN_HEAD_MODE][Tokenizer.EOF_TOKEN] = tokenInHead;
  108. _[AFTER_HEAD_MODE] = Object.create(null);
  109. _[AFTER_HEAD_MODE][Tokenizer.CHARACTER_TOKEN] =
  110. _[AFTER_HEAD_MODE][Tokenizer.NULL_CHARACTER_TOKEN] = tokenAfterHead;
  111. _[AFTER_HEAD_MODE][Tokenizer.WHITESPACE_CHARACTER_TOKEN] = insertCharacters;
  112. _[AFTER_HEAD_MODE][Tokenizer.COMMENT_TOKEN] = appendComment;
  113. _[AFTER_HEAD_MODE][Tokenizer.DOCTYPE_TOKEN] = ignoreToken;
  114. _[AFTER_HEAD_MODE][Tokenizer.START_TAG_TOKEN] = startTagAfterHead;
  115. _[AFTER_HEAD_MODE][Tokenizer.END_TAG_TOKEN] = endTagAfterHead;
  116. _[AFTER_HEAD_MODE][Tokenizer.EOF_TOKEN] = tokenAfterHead;
  117. _[IN_BODY_MODE] = Object.create(null);
  118. _[IN_BODY_MODE][Tokenizer.CHARACTER_TOKEN] = characterInBody;
  119. _[IN_BODY_MODE][Tokenizer.NULL_CHARACTER_TOKEN] = ignoreToken;
  120. _[IN_BODY_MODE][Tokenizer.WHITESPACE_CHARACTER_TOKEN] = whitespaceCharacterInBody;
  121. _[IN_BODY_MODE][Tokenizer.COMMENT_TOKEN] = appendComment;
  122. _[IN_BODY_MODE][Tokenizer.DOCTYPE_TOKEN] = ignoreToken;
  123. _[IN_BODY_MODE][Tokenizer.START_TAG_TOKEN] = startTagInBody;
  124. _[IN_BODY_MODE][Tokenizer.END_TAG_TOKEN] = endTagInBody;
  125. _[IN_BODY_MODE][Tokenizer.EOF_TOKEN] = eofInBody;
  126. _[TEXT_MODE] = Object.create(null);
  127. _[TEXT_MODE][Tokenizer.CHARACTER_TOKEN] =
  128. _[TEXT_MODE][Tokenizer.NULL_CHARACTER_TOKEN] =
  129. _[TEXT_MODE][Tokenizer.WHITESPACE_CHARACTER_TOKEN] = insertCharacters;
  130. _[TEXT_MODE][Tokenizer.COMMENT_TOKEN] =
  131. _[TEXT_MODE][Tokenizer.DOCTYPE_TOKEN] =
  132. _[TEXT_MODE][Tokenizer.START_TAG_TOKEN] = ignoreToken;
  133. _[TEXT_MODE][Tokenizer.END_TAG_TOKEN] = endTagInText;
  134. _[TEXT_MODE][Tokenizer.EOF_TOKEN] = eofInText;
  135. _[IN_TABLE_MODE] = Object.create(null);
  136. _[IN_TABLE_MODE][Tokenizer.CHARACTER_TOKEN] =
  137. _[IN_TABLE_MODE][Tokenizer.NULL_CHARACTER_TOKEN] =
  138. _[IN_TABLE_MODE][Tokenizer.WHITESPACE_CHARACTER_TOKEN] = characterInTable;
  139. _[IN_TABLE_MODE][Tokenizer.COMMENT_TOKEN] = appendComment;
  140. _[IN_TABLE_MODE][Tokenizer.DOCTYPE_TOKEN] = ignoreToken;
  141. _[IN_TABLE_MODE][Tokenizer.START_TAG_TOKEN] = startTagInTable;
  142. _[IN_TABLE_MODE][Tokenizer.END_TAG_TOKEN] = endTagInTable;
  143. _[IN_TABLE_MODE][Tokenizer.EOF_TOKEN] = eofInBody;
  144. _[IN_TABLE_TEXT_MODE] = Object.create(null);
  145. _[IN_TABLE_TEXT_MODE][Tokenizer.CHARACTER_TOKEN] = characterInTableText;
  146. _[IN_TABLE_TEXT_MODE][Tokenizer.NULL_CHARACTER_TOKEN] = ignoreToken;
  147. _[IN_TABLE_TEXT_MODE][Tokenizer.WHITESPACE_CHARACTER_TOKEN] = whitespaceCharacterInTableText;
  148. _[IN_TABLE_TEXT_MODE][Tokenizer.COMMENT_TOKEN] =
  149. _[IN_TABLE_TEXT_MODE][Tokenizer.DOCTYPE_TOKEN] =
  150. _[IN_TABLE_TEXT_MODE][Tokenizer.START_TAG_TOKEN] =
  151. _[IN_TABLE_TEXT_MODE][Tokenizer.END_TAG_TOKEN] =
  152. _[IN_TABLE_TEXT_MODE][Tokenizer.EOF_TOKEN] = tokenInTableText;
  153. _[IN_CAPTION_MODE] = Object.create(null);
  154. _[IN_CAPTION_MODE][Tokenizer.CHARACTER_TOKEN] = characterInBody;
  155. _[IN_CAPTION_MODE][Tokenizer.NULL_CHARACTER_TOKEN] = ignoreToken;
  156. _[IN_CAPTION_MODE][Tokenizer.WHITESPACE_CHARACTER_TOKEN] = whitespaceCharacterInBody;
  157. _[IN_CAPTION_MODE][Tokenizer.COMMENT_TOKEN] = appendComment;
  158. _[IN_CAPTION_MODE][Tokenizer.DOCTYPE_TOKEN] = ignoreToken;
  159. _[IN_CAPTION_MODE][Tokenizer.START_TAG_TOKEN] = startTagInCaption;
  160. _[IN_CAPTION_MODE][Tokenizer.END_TAG_TOKEN] = endTagInCaption;
  161. _[IN_CAPTION_MODE][Tokenizer.EOF_TOKEN] = eofInBody;
  162. _[IN_COLUMN_GROUP_MODE] = Object.create(null);
  163. _[IN_COLUMN_GROUP_MODE][Tokenizer.CHARACTER_TOKEN] =
  164. _[IN_COLUMN_GROUP_MODE][Tokenizer.NULL_CHARACTER_TOKEN] = tokenInColumnGroup;
  165. _[IN_COLUMN_GROUP_MODE][Tokenizer.WHITESPACE_CHARACTER_TOKEN] = insertCharacters;
  166. _[IN_COLUMN_GROUP_MODE][Tokenizer.COMMENT_TOKEN] = appendComment;
  167. _[IN_COLUMN_GROUP_MODE][Tokenizer.DOCTYPE_TOKEN] = ignoreToken;
  168. _[IN_COLUMN_GROUP_MODE][Tokenizer.START_TAG_TOKEN] = startTagInColumnGroup;
  169. _[IN_COLUMN_GROUP_MODE][Tokenizer.END_TAG_TOKEN] = endTagInColumnGroup;
  170. _[IN_COLUMN_GROUP_MODE][Tokenizer.EOF_TOKEN] = eofInBody;
  171. _[IN_TABLE_BODY_MODE] = Object.create(null);
  172. _[IN_TABLE_BODY_MODE][Tokenizer.CHARACTER_TOKEN] =
  173. _[IN_TABLE_BODY_MODE][Tokenizer.NULL_CHARACTER_TOKEN] =
  174. _[IN_TABLE_BODY_MODE][Tokenizer.WHITESPACE_CHARACTER_TOKEN] = characterInTable;
  175. _[IN_TABLE_BODY_MODE][Tokenizer.COMMENT_TOKEN] = appendComment;
  176. _[IN_TABLE_BODY_MODE][Tokenizer.DOCTYPE_TOKEN] = ignoreToken;
  177. _[IN_TABLE_BODY_MODE][Tokenizer.START_TAG_TOKEN] = startTagInTableBody;
  178. _[IN_TABLE_BODY_MODE][Tokenizer.END_TAG_TOKEN] = endTagInTableBody;
  179. _[IN_TABLE_BODY_MODE][Tokenizer.EOF_TOKEN] = eofInBody;
  180. _[IN_ROW_MODE] = Object.create(null);
  181. _[IN_ROW_MODE][Tokenizer.CHARACTER_TOKEN] =
  182. _[IN_ROW_MODE][Tokenizer.NULL_CHARACTER_TOKEN] =
  183. _[IN_ROW_MODE][Tokenizer.WHITESPACE_CHARACTER_TOKEN] = characterInTable;
  184. _[IN_ROW_MODE][Tokenizer.COMMENT_TOKEN] = appendComment;
  185. _[IN_ROW_MODE][Tokenizer.DOCTYPE_TOKEN] = ignoreToken;
  186. _[IN_ROW_MODE][Tokenizer.START_TAG_TOKEN] = startTagInRow;
  187. _[IN_ROW_MODE][Tokenizer.END_TAG_TOKEN] = endTagInRow;
  188. _[IN_ROW_MODE][Tokenizer.EOF_TOKEN] = eofInBody;
  189. _[IN_CELL_MODE] = Object.create(null);
  190. _[IN_CELL_MODE][Tokenizer.CHARACTER_TOKEN] = characterInBody;
  191. _[IN_CELL_MODE][Tokenizer.NULL_CHARACTER_TOKEN] = ignoreToken;
  192. _[IN_CELL_MODE][Tokenizer.WHITESPACE_CHARACTER_TOKEN] = whitespaceCharacterInBody;
  193. _[IN_CELL_MODE][Tokenizer.COMMENT_TOKEN] = appendComment;
  194. _[IN_CELL_MODE][Tokenizer.DOCTYPE_TOKEN] = ignoreToken;
  195. _[IN_CELL_MODE][Tokenizer.START_TAG_TOKEN] = startTagInCell;
  196. _[IN_CELL_MODE][Tokenizer.END_TAG_TOKEN] = endTagInCell;
  197. _[IN_CELL_MODE][Tokenizer.EOF_TOKEN] = eofInBody;
  198. _[IN_SELECT_MODE] = Object.create(null);
  199. _[IN_SELECT_MODE][Tokenizer.CHARACTER_TOKEN] = insertCharacters;
  200. _[IN_SELECT_MODE][Tokenizer.NULL_CHARACTER_TOKEN] = ignoreToken;
  201. _[IN_SELECT_MODE][Tokenizer.WHITESPACE_CHARACTER_TOKEN] = insertCharacters;
  202. _[IN_SELECT_MODE][Tokenizer.COMMENT_TOKEN] = appendComment;
  203. _[IN_SELECT_MODE][Tokenizer.DOCTYPE_TOKEN] = ignoreToken;
  204. _[IN_SELECT_MODE][Tokenizer.START_TAG_TOKEN] = startTagInSelect;
  205. _[IN_SELECT_MODE][Tokenizer.END_TAG_TOKEN] = endTagInSelect;
  206. _[IN_SELECT_MODE][Tokenizer.EOF_TOKEN] = eofInBody;
  207. _[IN_SELECT_IN_TABLE_MODE] = Object.create(null);
  208. _[IN_SELECT_IN_TABLE_MODE][Tokenizer.CHARACTER_TOKEN] = insertCharacters;
  209. _[IN_SELECT_IN_TABLE_MODE][Tokenizer.NULL_CHARACTER_TOKEN] = ignoreToken;
  210. _[IN_SELECT_IN_TABLE_MODE][Tokenizer.WHITESPACE_CHARACTER_TOKEN] = insertCharacters;
  211. _[IN_SELECT_IN_TABLE_MODE][Tokenizer.COMMENT_TOKEN] = appendComment;
  212. _[IN_SELECT_IN_TABLE_MODE][Tokenizer.DOCTYPE_TOKEN] = ignoreToken;
  213. _[IN_SELECT_IN_TABLE_MODE][Tokenizer.START_TAG_TOKEN] = startTagInSelectInTable;
  214. _[IN_SELECT_IN_TABLE_MODE][Tokenizer.END_TAG_TOKEN] = endTagInSelectInTable;
  215. _[IN_SELECT_IN_TABLE_MODE][Tokenizer.EOF_TOKEN] = eofInBody;
  216. _[IN_TEMPLATE_MODE] = Object.create(null);
  217. _[IN_TEMPLATE_MODE][Tokenizer.CHARACTER_TOKEN] = characterInBody;
  218. _[IN_TEMPLATE_MODE][Tokenizer.NULL_CHARACTER_TOKEN] = ignoreToken;
  219. _[IN_TEMPLATE_MODE][Tokenizer.WHITESPACE_CHARACTER_TOKEN] = whitespaceCharacterInBody;
  220. _[IN_TEMPLATE_MODE][Tokenizer.COMMENT_TOKEN] = appendComment;
  221. _[IN_TEMPLATE_MODE][Tokenizer.DOCTYPE_TOKEN] = ignoreToken;
  222. _[IN_TEMPLATE_MODE][Tokenizer.START_TAG_TOKEN] = startTagInTemplate;
  223. _[IN_TEMPLATE_MODE][Tokenizer.END_TAG_TOKEN] = endTagInTemplate;
  224. _[IN_TEMPLATE_MODE][Tokenizer.EOF_TOKEN] = eofInTemplate;
  225. _[AFTER_BODY_MODE] = Object.create(null);
  226. _[AFTER_BODY_MODE][Tokenizer.CHARACTER_TOKEN] =
  227. _[AFTER_BODY_MODE][Tokenizer.NULL_CHARACTER_TOKEN] = tokenAfterBody;
  228. _[AFTER_BODY_MODE][Tokenizer.WHITESPACE_CHARACTER_TOKEN] = whitespaceCharacterInBody;
  229. _[AFTER_BODY_MODE][Tokenizer.COMMENT_TOKEN] = appendCommentToRootHtmlElement;
  230. _[AFTER_BODY_MODE][Tokenizer.DOCTYPE_TOKEN] = ignoreToken;
  231. _[AFTER_BODY_MODE][Tokenizer.START_TAG_TOKEN] = startTagAfterBody;
  232. _[AFTER_BODY_MODE][Tokenizer.END_TAG_TOKEN] = endTagAfterBody;
  233. _[AFTER_BODY_MODE][Tokenizer.EOF_TOKEN] = stopParsing;
  234. _[IN_FRAMESET_MODE] = Object.create(null);
  235. _[IN_FRAMESET_MODE][Tokenizer.CHARACTER_TOKEN] =
  236. _[IN_FRAMESET_MODE][Tokenizer.NULL_CHARACTER_TOKEN] = ignoreToken;
  237. _[IN_FRAMESET_MODE][Tokenizer.WHITESPACE_CHARACTER_TOKEN] = insertCharacters;
  238. _[IN_FRAMESET_MODE][Tokenizer.COMMENT_TOKEN] = appendComment;
  239. _[IN_FRAMESET_MODE][Tokenizer.DOCTYPE_TOKEN] = ignoreToken;
  240. _[IN_FRAMESET_MODE][Tokenizer.START_TAG_TOKEN] = startTagInFrameset;
  241. _[IN_FRAMESET_MODE][Tokenizer.END_TAG_TOKEN] = endTagInFrameset;
  242. _[IN_FRAMESET_MODE][Tokenizer.EOF_TOKEN] = stopParsing;
  243. _[AFTER_FRAMESET_MODE] = Object.create(null);
  244. _[AFTER_FRAMESET_MODE][Tokenizer.CHARACTER_TOKEN] =
  245. _[AFTER_FRAMESET_MODE][Tokenizer.NULL_CHARACTER_TOKEN] = ignoreToken;
  246. _[AFTER_FRAMESET_MODE][Tokenizer.WHITESPACE_CHARACTER_TOKEN] = insertCharacters;
  247. _[AFTER_FRAMESET_MODE][Tokenizer.COMMENT_TOKEN] = appendComment;
  248. _[AFTER_FRAMESET_MODE][Tokenizer.DOCTYPE_TOKEN] = ignoreToken;
  249. _[AFTER_FRAMESET_MODE][Tokenizer.START_TAG_TOKEN] = startTagAfterFrameset;
  250. _[AFTER_FRAMESET_MODE][Tokenizer.END_TAG_TOKEN] = endTagAfterFrameset;
  251. _[AFTER_FRAMESET_MODE][Tokenizer.EOF_TOKEN] = stopParsing;
  252. _[AFTER_AFTER_BODY_MODE] = Object.create(null);
  253. _[AFTER_AFTER_BODY_MODE][Tokenizer.CHARACTER_TOKEN] = tokenAfterAfterBody;
  254. _[AFTER_AFTER_BODY_MODE][Tokenizer.NULL_CHARACTER_TOKEN] = tokenAfterAfterBody;
  255. _[AFTER_AFTER_BODY_MODE][Tokenizer.WHITESPACE_CHARACTER_TOKEN] = whitespaceCharacterInBody;
  256. _[AFTER_AFTER_BODY_MODE][Tokenizer.COMMENT_TOKEN] = appendCommentToDocument;
  257. _[AFTER_AFTER_BODY_MODE][Tokenizer.DOCTYPE_TOKEN] = ignoreToken;
  258. _[AFTER_AFTER_BODY_MODE][Tokenizer.START_TAG_TOKEN] = startTagAfterAfterBody;
  259. _[AFTER_AFTER_BODY_MODE][Tokenizer.END_TAG_TOKEN] = tokenAfterAfterBody;
  260. _[AFTER_AFTER_BODY_MODE][Tokenizer.EOF_TOKEN] = stopParsing;
  261. _[AFTER_AFTER_FRAMESET_MODE] = Object.create(null);
  262. _[AFTER_AFTER_FRAMESET_MODE][Tokenizer.CHARACTER_TOKEN] =
  263. _[AFTER_AFTER_FRAMESET_MODE][Tokenizer.NULL_CHARACTER_TOKEN] = ignoreToken;
  264. _[AFTER_AFTER_FRAMESET_MODE][Tokenizer.WHITESPACE_CHARACTER_TOKEN] = whitespaceCharacterInBody;
  265. _[AFTER_AFTER_FRAMESET_MODE][Tokenizer.COMMENT_TOKEN] = appendCommentToDocument;
  266. _[AFTER_AFTER_FRAMESET_MODE][Tokenizer.DOCTYPE_TOKEN] = ignoreToken;
  267. _[AFTER_AFTER_FRAMESET_MODE][Tokenizer.START_TAG_TOKEN] = startTagAfterAfterFrameset;
  268. _[AFTER_AFTER_FRAMESET_MODE][Tokenizer.END_TAG_TOKEN] = ignoreToken;
  269. _[AFTER_AFTER_FRAMESET_MODE][Tokenizer.EOF_TOKEN] = stopParsing;
  270. //Parser
  271. var Parser = module.exports = function (options) {
  272. this.options = mergeOptions(DEFAULT_OPTIONS, options);
  273. this.treeAdapter = this.options.treeAdapter;
  274. this.pendingScript = null;
  275. if (this.options.locationInfo)
  276. new LocationInfoParserMixin(this);
  277. };
  278. // API
  279. Parser.prototype.parse = function (html) {
  280. var document = this.treeAdapter.createDocument();
  281. this._bootstrap(document, null);
  282. this.tokenizer.write(html, true);
  283. this._runParsingLoop(null);
  284. return document;
  285. };
  286. Parser.prototype.parseFragment = function (html, fragmentContext) {
  287. //NOTE: use <template> element as a fragment context if context element was not provided,
  288. //so we will parse in "forgiving" manner
  289. if (!fragmentContext)
  290. fragmentContext = this.treeAdapter.createElement($.TEMPLATE, NS.HTML, []);
  291. //NOTE: create fake element which will be used as 'document' for fragment parsing.
  292. //This is important for jsdom there 'document' can't be recreated, therefore
  293. //fragment parsing causes messing of the main `document`.
  294. var documentMock = this.treeAdapter.createElement('documentmock', NS.HTML, []);
  295. this._bootstrap(documentMock, fragmentContext);
  296. if (this.treeAdapter.getTagName(fragmentContext) === $.TEMPLATE)
  297. this._pushTmplInsertionMode(IN_TEMPLATE_MODE);
  298. this._initTokenizerForFragmentParsing();
  299. this._insertFakeRootElement();
  300. this._resetInsertionMode();
  301. this._findFormInFragmentContext();
  302. this.tokenizer.write(html, true);
  303. this._runParsingLoop(null);
  304. var rootElement = this.treeAdapter.getFirstChild(documentMock),
  305. fragment = this.treeAdapter.createDocumentFragment();
  306. this._adoptNodes(rootElement, fragment);
  307. return fragment;
  308. };
  309. //Bootstrap parser
  310. Parser.prototype._bootstrap = function (document, fragmentContext) {
  311. this.tokenizer = new Tokenizer(this.options);
  312. this.stopped = false;
  313. this.insertionMode = INITIAL_MODE;
  314. this.originalInsertionMode = '';
  315. this.document = document;
  316. this.fragmentContext = fragmentContext;
  317. this.headElement = null;
  318. this.formElement = null;
  319. this.openElements = new OpenElementStack(this.document, this.treeAdapter);
  320. this.activeFormattingElements = new FormattingElementList(this.treeAdapter);
  321. this.tmplInsertionModeStack = [];
  322. this.tmplInsertionModeStackTop = -1;
  323. this.currentTmplInsertionMode = null;
  324. this.pendingCharacterTokens = [];
  325. this.hasNonWhitespacePendingCharacterToken = false;
  326. this.framesetOk = true;
  327. this.skipNextNewLine = false;
  328. this.fosterParentingEnabled = false;
  329. };
  330. //Parsing loop
  331. Parser.prototype._runParsingLoop = function (scriptHandler) {
  332. while (!this.stopped) {
  333. this._setupTokenizerCDATAMode();
  334. var token = this.tokenizer.getNextToken();
  335. if (token.type === Tokenizer.HIBERNATION_TOKEN)
  336. break;
  337. if (this.skipNextNewLine) {
  338. this.skipNextNewLine = false;
  339. if (token.type === Tokenizer.WHITESPACE_CHARACTER_TOKEN && token.chars[0] === '\n') {
  340. if (token.chars.length === 1)
  341. continue;
  342. token.chars = token.chars.substr(1);
  343. }
  344. }
  345. this._processInputToken(token);
  346. if (scriptHandler && this.pendingScript)
  347. break;
  348. }
  349. };
  350. Parser.prototype.runParsingLoopForCurrentChunk = function (writeCallback, scriptHandler) {
  351. this._runParsingLoop(scriptHandler);
  352. if (scriptHandler && this.pendingScript) {
  353. var script = this.pendingScript;
  354. this.pendingScript = null;
  355. scriptHandler(script);
  356. return;
  357. }
  358. if (writeCallback)
  359. writeCallback();
  360. };
  361. //Text parsing
  362. Parser.prototype._setupTokenizerCDATAMode = function () {
  363. var current = this._getAdjustedCurrentElement();
  364. this.tokenizer.allowCDATA = current && current !== this.document &&
  365. this.treeAdapter.getNamespaceURI(current) !== NS.HTML && !this._isIntegrationPoint(current);
  366. };
  367. Parser.prototype._switchToTextParsing = function (currentToken, nextTokenizerState) {
  368. this._insertElement(currentToken, NS.HTML);
  369. this.tokenizer.state = nextTokenizerState;
  370. this.originalInsertionMode = this.insertionMode;
  371. this.insertionMode = TEXT_MODE;
  372. };
  373. Parser.prototype.switchToPlaintextParsing = function () {
  374. this.insertionMode = TEXT_MODE;
  375. this.originalInsertionMode = IN_BODY_MODE;
  376. this.tokenizer.state = Tokenizer.MODE.PLAINTEXT;
  377. };
  378. //Fragment parsing
  379. Parser.prototype._getAdjustedCurrentElement = function () {
  380. return this.openElements.stackTop === 0 && this.fragmentContext ?
  381. this.fragmentContext :
  382. this.openElements.current;
  383. };
  384. Parser.prototype._findFormInFragmentContext = function () {
  385. var node = this.fragmentContext;
  386. do {
  387. if (this.treeAdapter.getTagName(node) === $.FORM) {
  388. this.formElement = node;
  389. break;
  390. }
  391. node = this.treeAdapter.getParentNode(node);
  392. } while (node);
  393. };
  394. Parser.prototype._initTokenizerForFragmentParsing = function () {
  395. if (this.treeAdapter.getNamespaceURI(this.fragmentContext) === NS.HTML) {
  396. var tn = this.treeAdapter.getTagName(this.fragmentContext);
  397. if (tn === $.TITLE || tn === $.TEXTAREA)
  398. this.tokenizer.state = Tokenizer.MODE.RCDATA;
  399. else if (tn === $.STYLE || tn === $.XMP || tn === $.IFRAME ||
  400. tn === $.NOEMBED || tn === $.NOFRAMES || tn === $.NOSCRIPT)
  401. this.tokenizer.state = Tokenizer.MODE.RAWTEXT;
  402. else if (tn === $.SCRIPT)
  403. this.tokenizer.state = Tokenizer.MODE.SCRIPT_DATA;
  404. else if (tn === $.PLAINTEXT)
  405. this.tokenizer.state = Tokenizer.MODE.PLAINTEXT;
  406. }
  407. };
  408. //Tree mutation
  409. Parser.prototype._setDocumentType = function (token) {
  410. this.treeAdapter.setDocumentType(this.document, token.name, token.publicId, token.systemId);
  411. };
  412. Parser.prototype._attachElementToTree = function (element) {
  413. if (this._shouldFosterParentOnInsertion())
  414. this._fosterParentElement(element);
  415. else {
  416. var parent = this.openElements.currentTmplContent || this.openElements.current;
  417. this.treeAdapter.appendChild(parent, element);
  418. }
  419. };
  420. Parser.prototype._appendElement = function (token, namespaceURI) {
  421. var element = this.treeAdapter.createElement(token.tagName, namespaceURI, token.attrs);
  422. this._attachElementToTree(element);
  423. };
  424. Parser.prototype._insertElement = function (token, namespaceURI) {
  425. var element = this.treeAdapter.createElement(token.tagName, namespaceURI, token.attrs);
  426. this._attachElementToTree(element);
  427. this.openElements.push(element);
  428. };
  429. Parser.prototype._insertFakeElement = function (tagName) {
  430. var element = this.treeAdapter.createElement(tagName, NS.HTML, []);
  431. this._attachElementToTree(element);
  432. this.openElements.push(element);
  433. };
  434. Parser.prototype._insertTemplate = function (token) {
  435. var tmpl = this.treeAdapter.createElement(token.tagName, NS.HTML, token.attrs),
  436. content = this.treeAdapter.createDocumentFragment();
  437. this.treeAdapter.setTemplateContent(tmpl, content);
  438. this._attachElementToTree(tmpl);
  439. this.openElements.push(tmpl);
  440. };
  441. Parser.prototype._insertFakeRootElement = function () {
  442. var element = this.treeAdapter.createElement($.HTML, NS.HTML, []);
  443. this.treeAdapter.appendChild(this.openElements.current, element);
  444. this.openElements.push(element);
  445. };
  446. Parser.prototype._appendCommentNode = function (token, parent) {
  447. var commentNode = this.treeAdapter.createCommentNode(token.data);
  448. this.treeAdapter.appendChild(parent, commentNode);
  449. };
  450. Parser.prototype._insertCharacters = function (token) {
  451. if (this._shouldFosterParentOnInsertion())
  452. this._fosterParentText(token.chars);
  453. else {
  454. var parent = this.openElements.currentTmplContent || this.openElements.current;
  455. this.treeAdapter.insertText(parent, token.chars);
  456. }
  457. };
  458. Parser.prototype._adoptNodes = function (donor, recipient) {
  459. while (true) {
  460. var child = this.treeAdapter.getFirstChild(donor);
  461. if (!child)
  462. break;
  463. this.treeAdapter.detachNode(child);
  464. this.treeAdapter.appendChild(recipient, child);
  465. }
  466. };
  467. //Token processing
  468. Parser.prototype._shouldProcessTokenInForeignContent = function (token) {
  469. var current = this._getAdjustedCurrentElement();
  470. if (!current || current === this.document)
  471. return false;
  472. var ns = this.treeAdapter.getNamespaceURI(current);
  473. if (ns === NS.HTML)
  474. return false;
  475. if (this.treeAdapter.getTagName(current) === $.ANNOTATION_XML && ns === NS.MATHML &&
  476. token.type === Tokenizer.START_TAG_TOKEN && token.tagName === $.SVG)
  477. return false;
  478. var isCharacterToken = token.type === Tokenizer.CHARACTER_TOKEN ||
  479. token.type === Tokenizer.NULL_CHARACTER_TOKEN ||
  480. token.type === Tokenizer.WHITESPACE_CHARACTER_TOKEN,
  481. isMathMLTextStartTag = token.type === Tokenizer.START_TAG_TOKEN &&
  482. token.tagName !== $.MGLYPH &&
  483. token.tagName !== $.MALIGNMARK;
  484. if ((isMathMLTextStartTag || isCharacterToken) && this._isIntegrationPoint(current, NS.MATHML))
  485. return false;
  486. if ((token.type === Tokenizer.START_TAG_TOKEN || isCharacterToken) && this._isIntegrationPoint(current, NS.HTML))
  487. return false;
  488. return token.type !== Tokenizer.EOF_TOKEN;
  489. };
  490. Parser.prototype._processToken = function (token) {
  491. _[this.insertionMode][token.type](this, token);
  492. };
  493. Parser.prototype._processTokenInBodyMode = function (token) {
  494. _[IN_BODY_MODE][token.type](this, token);
  495. };
  496. Parser.prototype._processTokenInForeignContent = function (token) {
  497. if (token.type === Tokenizer.CHARACTER_TOKEN)
  498. characterInForeignContent(this, token);
  499. else if (token.type === Tokenizer.NULL_CHARACTER_TOKEN)
  500. nullCharacterInForeignContent(this, token);
  501. else if (token.type === Tokenizer.WHITESPACE_CHARACTER_TOKEN)
  502. insertCharacters(this, token);
  503. else if (token.type === Tokenizer.COMMENT_TOKEN)
  504. appendComment(this, token);
  505. else if (token.type === Tokenizer.START_TAG_TOKEN)
  506. startTagInForeignContent(this, token);
  507. else if (token.type === Tokenizer.END_TAG_TOKEN)
  508. endTagInForeignContent(this, token);
  509. };
  510. Parser.prototype._processInputToken = function (token) {
  511. if (this._shouldProcessTokenInForeignContent(token))
  512. this._processTokenInForeignContent(token);
  513. else
  514. this._processToken(token);
  515. };
  516. //Integration points
  517. Parser.prototype._isIntegrationPoint = function (element, foreignNS) {
  518. var tn = this.treeAdapter.getTagName(element),
  519. ns = this.treeAdapter.getNamespaceURI(element),
  520. attrs = this.treeAdapter.getAttrList(element);
  521. return foreignContent.isIntegrationPoint(tn, ns, attrs, foreignNS);
  522. };
  523. //Active formatting elements reconstruction
  524. Parser.prototype._reconstructActiveFormattingElements = function () {
  525. var listLength = this.activeFormattingElements.length;
  526. if (listLength) {
  527. var unopenIdx = listLength,
  528. entry = null;
  529. do {
  530. unopenIdx--;
  531. entry = this.activeFormattingElements.entries[unopenIdx];
  532. if (entry.type === FormattingElementList.MARKER_ENTRY || this.openElements.contains(entry.element)) {
  533. unopenIdx++;
  534. break;
  535. }
  536. } while (unopenIdx > 0);
  537. for (var i = unopenIdx; i < listLength; i++) {
  538. entry = this.activeFormattingElements.entries[i];
  539. this._insertElement(entry.token, this.treeAdapter.getNamespaceURI(entry.element));
  540. entry.element = this.openElements.current;
  541. }
  542. }
  543. };
  544. //Close elements
  545. Parser.prototype._closeTableCell = function () {
  546. this.openElements.generateImpliedEndTags();
  547. this.openElements.popUntilTableCellPopped();
  548. this.activeFormattingElements.clearToLastMarker();
  549. this.insertionMode = IN_ROW_MODE;
  550. };
  551. Parser.prototype._closePElement = function () {
  552. this.openElements.generateImpliedEndTagsWithExclusion($.P);
  553. this.openElements.popUntilTagNamePopped($.P);
  554. };
  555. //Insertion modes
  556. Parser.prototype._resetInsertionMode = function () {
  557. for (var i = this.openElements.stackTop, last = false; i >= 0; i--) {
  558. var element = this.openElements.items[i];
  559. if (i === 0) {
  560. last = true;
  561. if (this.fragmentContext)
  562. element = this.fragmentContext;
  563. }
  564. var tn = this.treeAdapter.getTagName(element),
  565. newInsertionMode = INSERTION_MODE_RESET_MAP[tn];
  566. if (newInsertionMode) {
  567. this.insertionMode = newInsertionMode;
  568. break;
  569. }
  570. else if (!last && (tn === $.TD || tn === $.TH)) {
  571. this.insertionMode = IN_CELL_MODE;
  572. break;
  573. }
  574. else if (!last && tn === $.HEAD) {
  575. this.insertionMode = IN_HEAD_MODE;
  576. break;
  577. }
  578. else if (tn === $.SELECT) {
  579. this._resetInsertionModeForSelect(i);
  580. break;
  581. }
  582. else if (tn === $.TEMPLATE) {
  583. this.insertionMode = this.currentTmplInsertionMode;
  584. break;
  585. }
  586. else if (tn === $.HTML) {
  587. this.insertionMode = this.headElement ? AFTER_HEAD_MODE : BEFORE_HEAD_MODE;
  588. break;
  589. }
  590. else if (last) {
  591. this.insertionMode = IN_BODY_MODE;
  592. break;
  593. }
  594. }
  595. };
  596. Parser.prototype._resetInsertionModeForSelect = function (selectIdx) {
  597. if (selectIdx > 0) {
  598. for (var i = selectIdx - 1; i > 0; i--) {
  599. var ancestor = this.openElements.items[i],
  600. tn = this.treeAdapter.getTagName(ancestor);
  601. if (tn === $.TEMPLATE)
  602. break;
  603. else if (tn === $.TABLE) {
  604. this.insertionMode = IN_SELECT_IN_TABLE_MODE;
  605. return;
  606. }
  607. }
  608. }
  609. this.insertionMode = IN_SELECT_MODE;
  610. };
  611. Parser.prototype._pushTmplInsertionMode = function (mode) {
  612. this.tmplInsertionModeStack.push(mode);
  613. this.tmplInsertionModeStackTop++;
  614. this.currentTmplInsertionMode = mode;
  615. };
  616. Parser.prototype._popTmplInsertionMode = function () {
  617. this.tmplInsertionModeStack.pop();
  618. this.tmplInsertionModeStackTop--;
  619. this.currentTmplInsertionMode = this.tmplInsertionModeStack[this.tmplInsertionModeStackTop];
  620. };
  621. //Foster parenting
  622. Parser.prototype._isElementCausesFosterParenting = function (element) {
  623. var tn = this.treeAdapter.getTagName(element);
  624. return tn === $.TABLE || tn === $.TBODY || tn === $.TFOOT || tn === $.THEAD || tn === $.TR;
  625. };
  626. Parser.prototype._shouldFosterParentOnInsertion = function () {
  627. return this.fosterParentingEnabled && this._isElementCausesFosterParenting(this.openElements.current);
  628. };
  629. Parser.prototype._findFosterParentingLocation = function () {
  630. var location = {
  631. parent: null,
  632. beforeElement: null
  633. };
  634. for (var i = this.openElements.stackTop; i >= 0; i--) {
  635. var openElement = this.openElements.items[i],
  636. tn = this.treeAdapter.getTagName(openElement),
  637. ns = this.treeAdapter.getNamespaceURI(openElement);
  638. if (tn === $.TEMPLATE && ns === NS.HTML) {
  639. location.parent = this.treeAdapter.getTemplateContent(openElement);
  640. break;
  641. }
  642. else if (tn === $.TABLE) {
  643. location.parent = this.treeAdapter.getParentNode(openElement);
  644. if (location.parent)
  645. location.beforeElement = openElement;
  646. else
  647. location.parent = this.openElements.items[i - 1];
  648. break;
  649. }
  650. }
  651. if (!location.parent)
  652. location.parent = this.openElements.items[0];
  653. return location;
  654. };
  655. Parser.prototype._fosterParentElement = function (element) {
  656. var location = this._findFosterParentingLocation();
  657. if (location.beforeElement)
  658. this.treeAdapter.insertBefore(location.parent, element, location.beforeElement);
  659. else
  660. this.treeAdapter.appendChild(location.parent, element);
  661. };
  662. Parser.prototype._fosterParentText = function (chars) {
  663. var location = this._findFosterParentingLocation();
  664. if (location.beforeElement)
  665. this.treeAdapter.insertTextBefore(location.parent, chars, location.beforeElement);
  666. else
  667. this.treeAdapter.insertText(location.parent, chars);
  668. };
  669. //Special elements
  670. Parser.prototype._isSpecialElement = function (element) {
  671. var tn = this.treeAdapter.getTagName(element),
  672. ns = this.treeAdapter.getNamespaceURI(element);
  673. return HTML.SPECIAL_ELEMENTS[ns][tn];
  674. };
  675. //Adoption agency algorithm
  676. //(see: http://www.whatwg.org/specs/web-apps/current-work/multipage/tree-construction.html#adoptionAgency)
  677. //------------------------------------------------------------------
  678. //Steps 5-8 of the algorithm
  679. function aaObtainFormattingElementEntry(p, token) {
  680. var formattingElementEntry = p.activeFormattingElements.getElementEntryInScopeWithTagName(token.tagName);
  681. if (formattingElementEntry) {
  682. if (!p.openElements.contains(formattingElementEntry.element)) {
  683. p.activeFormattingElements.removeEntry(formattingElementEntry);
  684. formattingElementEntry = null;
  685. }
  686. else if (!p.openElements.hasInScope(token.tagName))
  687. formattingElementEntry = null;
  688. }
  689. else
  690. genericEndTagInBody(p, token);
  691. return formattingElementEntry;
  692. }
  693. //Steps 9 and 10 of the algorithm
  694. function aaObtainFurthestBlock(p, formattingElementEntry) {
  695. var furthestBlock = null;
  696. for (var i = p.openElements.stackTop; i >= 0; i--) {
  697. var element = p.openElements.items[i];
  698. if (element === formattingElementEntry.element)
  699. break;
  700. if (p._isSpecialElement(element))
  701. furthestBlock = element;
  702. }
  703. if (!furthestBlock) {
  704. p.openElements.popUntilElementPopped(formattingElementEntry.element);
  705. p.activeFormattingElements.removeEntry(formattingElementEntry);
  706. }
  707. return furthestBlock;
  708. }
  709. //Step 13 of the algorithm
  710. function aaInnerLoop(p, furthestBlock, formattingElement) {
  711. var lastElement = furthestBlock,
  712. nextElement = p.openElements.getCommonAncestor(furthestBlock);
  713. for (var i = 0, element = nextElement; element !== formattingElement; i++, element = nextElement) {
  714. //NOTE: store next element for the next loop iteration (it may be deleted from the stack by step 9.5)
  715. nextElement = p.openElements.getCommonAncestor(element);
  716. var elementEntry = p.activeFormattingElements.getElementEntry(element),
  717. counterOverflow = elementEntry && i >= AA_INNER_LOOP_ITER,
  718. shouldRemoveFromOpenElements = !elementEntry || counterOverflow;
  719. if (shouldRemoveFromOpenElements) {
  720. if (counterOverflow)
  721. p.activeFormattingElements.removeEntry(elementEntry);
  722. p.openElements.remove(element);
  723. }
  724. else {
  725. element = aaRecreateElementFromEntry(p, elementEntry);
  726. if (lastElement === furthestBlock)
  727. p.activeFormattingElements.bookmark = elementEntry;
  728. p.treeAdapter.detachNode(lastElement);
  729. p.treeAdapter.appendChild(element, lastElement);
  730. lastElement = element;
  731. }
  732. }
  733. return lastElement;
  734. }
  735. //Step 13.7 of the algorithm
  736. function aaRecreateElementFromEntry(p, elementEntry) {
  737. var ns = p.treeAdapter.getNamespaceURI(elementEntry.element),
  738. newElement = p.treeAdapter.createElement(elementEntry.token.tagName, ns, elementEntry.token.attrs);
  739. p.openElements.replace(elementEntry.element, newElement);
  740. elementEntry.element = newElement;
  741. return newElement;
  742. }
  743. //Step 14 of the algorithm
  744. function aaInsertLastNodeInCommonAncestor(p, commonAncestor, lastElement) {
  745. if (p._isElementCausesFosterParenting(commonAncestor))
  746. p._fosterParentElement(lastElement);
  747. else {
  748. var tn = p.treeAdapter.getTagName(commonAncestor),
  749. ns = p.treeAdapter.getNamespaceURI(commonAncestor);
  750. if (tn === $.TEMPLATE && ns === NS.HTML)
  751. commonAncestor = p.treeAdapter.getTemplateContent(commonAncestor);
  752. p.treeAdapter.appendChild(commonAncestor, lastElement);
  753. }
  754. }
  755. //Steps 15-19 of the algorithm
  756. function aaReplaceFormattingElement(p, furthestBlock, formattingElementEntry) {
  757. var ns = p.treeAdapter.getNamespaceURI(formattingElementEntry.element),
  758. token = formattingElementEntry.token,
  759. newElement = p.treeAdapter.createElement(token.tagName, ns, token.attrs);
  760. p._adoptNodes(furthestBlock, newElement);
  761. p.treeAdapter.appendChild(furthestBlock, newElement);
  762. p.activeFormattingElements.insertElementAfterBookmark(newElement, formattingElementEntry.token);
  763. p.activeFormattingElements.removeEntry(formattingElementEntry);
  764. p.openElements.remove(formattingElementEntry.element);
  765. p.openElements.insertAfter(furthestBlock, newElement);
  766. }
  767. //Algorithm entry point
  768. function callAdoptionAgency(p, token) {
  769. var formattingElementEntry;
  770. for (var i = 0; i < AA_OUTER_LOOP_ITER; i++) {
  771. formattingElementEntry = aaObtainFormattingElementEntry(p, token, formattingElementEntry);
  772. if (!formattingElementEntry)
  773. break;
  774. var furthestBlock = aaObtainFurthestBlock(p, formattingElementEntry);
  775. if (!furthestBlock)
  776. break;
  777. p.activeFormattingElements.bookmark = formattingElementEntry;
  778. var lastElement = aaInnerLoop(p, furthestBlock, formattingElementEntry.element),
  779. commonAncestor = p.openElements.getCommonAncestor(formattingElementEntry.element);
  780. p.treeAdapter.detachNode(lastElement);
  781. aaInsertLastNodeInCommonAncestor(p, commonAncestor, lastElement);
  782. aaReplaceFormattingElement(p, furthestBlock, formattingElementEntry);
  783. }
  784. }
  785. //Generic token handlers
  786. //------------------------------------------------------------------
  787. function ignoreToken() {
  788. //NOTE: do nothing =)
  789. }
  790. function appendComment(p, token) {
  791. p._appendCommentNode(token, p.openElements.currentTmplContent || p.openElements.current);
  792. }
  793. function appendCommentToRootHtmlElement(p, token) {
  794. p._appendCommentNode(token, p.openElements.items[0]);
  795. }
  796. function appendCommentToDocument(p, token) {
  797. p._appendCommentNode(token, p.document);
  798. }
  799. function insertCharacters(p, token) {
  800. p._insertCharacters(token);
  801. }
  802. function stopParsing(p) {
  803. p.stopped = true;
  804. }
  805. //12.2.5.4.1 The "initial" insertion mode
  806. //------------------------------------------------------------------
  807. function doctypeInInitialMode(p, token) {
  808. p._setDocumentType(token);
  809. var mode = token.forceQuirks ?
  810. HTML.DOCUMENT_MODE.QUIRKS :
  811. doctype.getDocumentMode(token.name, token.publicId, token.systemId);
  812. p.treeAdapter.setDocumentMode(p.document, mode);
  813. p.insertionMode = BEFORE_HTML_MODE;
  814. }
  815. function tokenInInitialMode(p, token) {
  816. p.treeAdapter.setDocumentMode(p.document, HTML.DOCUMENT_MODE.QUIRKS);
  817. p.insertionMode = BEFORE_HTML_MODE;
  818. p._processToken(token);
  819. }
  820. //12.2.5.4.2 The "before html" insertion mode
  821. //------------------------------------------------------------------
  822. function startTagBeforeHtml(p, token) {
  823. if (token.tagName === $.HTML) {
  824. p._insertElement(token, NS.HTML);
  825. p.insertionMode = BEFORE_HEAD_MODE;
  826. }
  827. else
  828. tokenBeforeHtml(p, token);
  829. }
  830. function endTagBeforeHtml(p, token) {
  831. var tn = token.tagName;
  832. if (tn === $.HTML || tn === $.HEAD || tn === $.BODY || tn === $.BR)
  833. tokenBeforeHtml(p, token);
  834. }
  835. function tokenBeforeHtml(p, token) {
  836. p._insertFakeRootElement();
  837. p.insertionMode = BEFORE_HEAD_MODE;
  838. p._processToken(token);
  839. }
  840. //12.2.5.4.3 The "before head" insertion mode
  841. //------------------------------------------------------------------
  842. function startTagBeforeHead(p, token) {
  843. var tn = token.tagName;
  844. if (tn === $.HTML)
  845. startTagInBody(p, token);
  846. else if (tn === $.HEAD) {
  847. p._insertElement(token, NS.HTML);
  848. p.headElement = p.openElements.current;
  849. p.insertionMode = IN_HEAD_MODE;
  850. }
  851. else
  852. tokenBeforeHead(p, token);
  853. }
  854. function endTagBeforeHead(p, token) {
  855. var tn = token.tagName;
  856. if (tn === $.HEAD || tn === $.BODY || tn === $.HTML || tn === $.BR)
  857. tokenBeforeHead(p, token);
  858. }
  859. function tokenBeforeHead(p, token) {
  860. p._insertFakeElement($.HEAD);
  861. p.headElement = p.openElements.current;
  862. p.insertionMode = IN_HEAD_MODE;
  863. p._processToken(token);
  864. }
  865. //12.2.5.4.4 The "in head" insertion mode
  866. //------------------------------------------------------------------
  867. function startTagInHead(p, token) {
  868. var tn = token.tagName;
  869. if (tn === $.HTML)
  870. startTagInBody(p, token);
  871. else if (tn === $.BASE || tn === $.BASEFONT || tn === $.BGSOUND || tn === $.LINK || tn === $.META)
  872. p._appendElement(token, NS.HTML);
  873. else if (tn === $.TITLE)
  874. p._switchToTextParsing(token, Tokenizer.MODE.RCDATA);
  875. //NOTE: here we assume that we always act as an interactive user agent with enabled scripting, so we parse
  876. //<noscript> as a rawtext.
  877. else if (tn === $.NOSCRIPT || tn === $.NOFRAMES || tn === $.STYLE)
  878. p._switchToTextParsing(token, Tokenizer.MODE.RAWTEXT);
  879. else if (tn === $.SCRIPT)
  880. p._switchToTextParsing(token, Tokenizer.MODE.SCRIPT_DATA);
  881. else if (tn === $.TEMPLATE) {
  882. p._insertTemplate(token, NS.HTML);
  883. p.activeFormattingElements.insertMarker();
  884. p.framesetOk = false;
  885. p.insertionMode = IN_TEMPLATE_MODE;
  886. p._pushTmplInsertionMode(IN_TEMPLATE_MODE);
  887. }
  888. else if (tn !== $.HEAD)
  889. tokenInHead(p, token);
  890. }
  891. function endTagInHead(p, token) {
  892. var tn = token.tagName;
  893. if (tn === $.HEAD) {
  894. p.openElements.pop();
  895. p.insertionMode = AFTER_HEAD_MODE;
  896. }
  897. else if (tn === $.BODY || tn === $.BR || tn === $.HTML)
  898. tokenInHead(p, token);
  899. else if (tn === $.TEMPLATE && p.openElements.tmplCount > 0) {
  900. p.openElements.generateImpliedEndTags();
  901. p.openElements.popUntilTagNamePopped($.TEMPLATE);
  902. p.activeFormattingElements.clearToLastMarker();
  903. p._popTmplInsertionMode();
  904. p._resetInsertionMode();
  905. }
  906. }
  907. function tokenInHead(p, token) {
  908. p.openElements.pop();
  909. p.insertionMode = AFTER_HEAD_MODE;
  910. p._processToken(token);
  911. }
  912. //12.2.5.4.6 The "after head" insertion mode
  913. //------------------------------------------------------------------
  914. function startTagAfterHead(p, token) {
  915. var tn = token.tagName;
  916. if (tn === $.HTML)
  917. startTagInBody(p, token);
  918. else if (tn === $.BODY) {
  919. p._insertElement(token, NS.HTML);
  920. p.framesetOk = false;
  921. p.insertionMode = IN_BODY_MODE;
  922. }
  923. else if (tn === $.FRAMESET) {
  924. p._insertElement(token, NS.HTML);
  925. p.insertionMode = IN_FRAMESET_MODE;
  926. }
  927. else if (tn === $.BASE || tn === $.BASEFONT || tn === $.BGSOUND || tn === $.LINK || tn === $.META ||
  928. tn === $.NOFRAMES || tn === $.SCRIPT || tn === $.STYLE || tn === $.TEMPLATE || tn === $.TITLE) {
  929. p.openElements.push(p.headElement);
  930. startTagInHead(p, token);
  931. p.openElements.remove(p.headElement);
  932. }
  933. else if (tn !== $.HEAD)
  934. tokenAfterHead(p, token);
  935. }
  936. function endTagAfterHead(p, token) {
  937. var tn = token.tagName;
  938. if (tn === $.BODY || tn === $.HTML || tn === $.BR)
  939. tokenAfterHead(p, token);
  940. else if (tn === $.TEMPLATE)
  941. endTagInHead(p, token);
  942. }
  943. function tokenAfterHead(p, token) {
  944. p._insertFakeElement($.BODY);
  945. p.insertionMode = IN_BODY_MODE;
  946. p._processToken(token);
  947. }
  948. //12.2.5.4.7 The "in body" insertion mode
  949. //------------------------------------------------------------------
  950. function whitespaceCharacterInBody(p, token) {
  951. p._reconstructActiveFormattingElements();
  952. p._insertCharacters(token);
  953. }
  954. function characterInBody(p, token) {
  955. p._reconstructActiveFormattingElements();
  956. p._insertCharacters(token);
  957. p.framesetOk = false;
  958. }
  959. function htmlStartTagInBody(p, token) {
  960. if (p.openElements.tmplCount === 0)
  961. p.treeAdapter.adoptAttributes(p.openElements.items[0], token.attrs);
  962. }
  963. function bodyStartTagInBody(p, token) {
  964. var bodyElement = p.openElements.tryPeekProperlyNestedBodyElement();
  965. if (bodyElement && p.openElements.tmplCount === 0) {
  966. p.framesetOk = false;
  967. p.treeAdapter.adoptAttributes(bodyElement, token.attrs);
  968. }
  969. }
  970. function framesetStartTagInBody(p, token) {
  971. var bodyElement = p.openElements.tryPeekProperlyNestedBodyElement();
  972. if (p.framesetOk && bodyElement) {
  973. p.treeAdapter.detachNode(bodyElement);
  974. p.openElements.popAllUpToHtmlElement();
  975. p._insertElement(token, NS.HTML);
  976. p.insertionMode = IN_FRAMESET_MODE;
  977. }
  978. }
  979. function addressStartTagInBody(p, token) {
  980. if (p.openElements.hasInButtonScope($.P))
  981. p._closePElement();
  982. p._insertElement(token, NS.HTML);
  983. }
  984. function numberedHeaderStartTagInBody(p, token) {
  985. if (p.openElements.hasInButtonScope($.P))
  986. p._closePElement();
  987. var tn = p.openElements.currentTagName;
  988. if (tn === $.H1 || tn === $.H2 || tn === $.H3 || tn === $.H4 || tn === $.H5 || tn === $.H6)
  989. p.openElements.pop();
  990. p._insertElement(token, NS.HTML);
  991. }
  992. function preStartTagInBody(p, token) {
  993. if (p.openElements.hasInButtonScope($.P))
  994. p._closePElement();
  995. p._insertElement(token, NS.HTML);
  996. //NOTE: If the next token is a U+000A LINE FEED (LF) character token, then ignore that token and move
  997. //on to the next one. (Newlines at the start of pre blocks are ignored as an authoring convenience.)
  998. p.skipNextNewLine = true;
  999. p.framesetOk = false;
  1000. }
  1001. function formStartTagInBody(p, token) {
  1002. var inTemplate = p.openElements.tmplCount > 0;
  1003. if (!p.formElement || inTemplate) {
  1004. if (p.openElements.hasInButtonScope($.P))
  1005. p._closePElement();
  1006. p._insertElement(token, NS.HTML);
  1007. if (!inTemplate)
  1008. p.formElement = p.openElements.current;
  1009. }
  1010. }
  1011. function listItemStartTagInBody(p, token) {
  1012. p.framesetOk = false;
  1013. var tn = token.tagName;
  1014. for (var i = p.openElements.stackTop; i >= 0; i--) {
  1015. var element = p.openElements.items[i],
  1016. elementTn = p.treeAdapter.getTagName(element),
  1017. closeTn = null;
  1018. if (tn === $.LI && elementTn === $.LI)
  1019. closeTn = $.LI;
  1020. else if ((tn === $.DD || tn === $.DT) && (elementTn === $.DD || elementTn === $.DT))
  1021. closeTn = elementTn;
  1022. if (closeTn) {
  1023. p.openElements.generateImpliedEndTagsWithExclusion(closeTn);
  1024. p.openElements.popUntilTagNamePopped(closeTn);
  1025. break;
  1026. }
  1027. if (elementTn !== $.ADDRESS && elementTn !== $.DIV && elementTn !== $.P && p._isSpecialElement(element))
  1028. break;
  1029. }
  1030. if (p.openElements.hasInButtonScope($.P))
  1031. p._closePElement();
  1032. p._insertElement(token, NS.HTML);
  1033. }
  1034. function plaintextStartTagInBody(p, token) {
  1035. if (p.openElements.hasInButtonScope($.P))
  1036. p._closePElement();
  1037. p._insertElement(token, NS.HTML);
  1038. p.tokenizer.state = Tokenizer.MODE.PLAINTEXT;
  1039. }
  1040. function buttonStartTagInBody(p, token) {
  1041. if (p.openElements.hasInScope($.BUTTON)) {
  1042. p.openElements.generateImpliedEndTags();
  1043. p.openElements.popUntilTagNamePopped($.BUTTON);
  1044. }
  1045. p._reconstructActiveFormattingElements();
  1046. p._insertElement(token, NS.HTML);
  1047. p.framesetOk = false;
  1048. }
  1049. function aStartTagInBody(p, token) {
  1050. var activeElementEntry = p.activeFormattingElements.getElementEntryInScopeWithTagName($.A);
  1051. if (activeElementEntry) {
  1052. callAdoptionAgency(p, token);
  1053. p.openElements.remove(activeElementEntry.element);
  1054. p.activeFormattingElements.removeEntry(activeElementEntry);
  1055. }
  1056. p._reconstructActiveFormattingElements();
  1057. p._insertElement(token, NS.HTML);
  1058. p.activeFormattingElements.pushElement(p.openElements.current, token);
  1059. }
  1060. function bStartTagInBody(p, token) {
  1061. p._reconstructActiveFormattingElements();
  1062. p._insertElement(token, NS.HTML);
  1063. p.activeFormattingElements.pushElement(p.openElements.current, token);
  1064. }
  1065. function nobrStartTagInBody(p, token) {
  1066. p._reconstructActiveFormattingElements();
  1067. if (p.openElements.hasInScope($.NOBR)) {
  1068. callAdoptionAgency(p, token);
  1069. p._reconstructActiveFormattingElements();
  1070. }
  1071. p._insertElement(token, NS.HTML);
  1072. p.activeFormattingElements.pushElement(p.openElements.current, token);
  1073. }
  1074. function appletStartTagInBody(p, token) {
  1075. p._reconstructActiveFormattingElements();
  1076. p._insertElement(token, NS.HTML);
  1077. p.activeFormattingElements.insertMarker();
  1078. p.framesetOk = false;
  1079. }
  1080. function tableStartTagInBody(p, token) {
  1081. if (p.treeAdapter.getDocumentMode(p.document) !== HTML.DOCUMENT_MODE.QUIRKS && p.openElements.hasInButtonScope($.P))
  1082. p._closePElement();
  1083. p._insertElement(token, NS.HTML);
  1084. p.framesetOk = false;
  1085. p.insertionMode = IN_TABLE_MODE;
  1086. }
  1087. function areaStartTagInBody(p, token) {
  1088. p._reconstructActiveFormattingElements();
  1089. p._appendElement(token, NS.HTML);
  1090. p.framesetOk = false;
  1091. }
  1092. function inputStartTagInBody(p, token) {
  1093. p._reconstructActiveFormattingElements();
  1094. p._appendElement(token, NS.HTML);
  1095. var inputType = Tokenizer.getTokenAttr(token, ATTRS.TYPE);
  1096. if (!inputType || inputType.toLowerCase() !== HIDDEN_INPUT_TYPE)
  1097. p.framesetOk = false;
  1098. }
  1099. function paramStartTagInBody(p, token) {
  1100. p._appendElement(token, NS.HTML);
  1101. }
  1102. function hrStartTagInBody(p, token) {
  1103. if (p.openElements.hasInButtonScope($.P))
  1104. p._closePElement();
  1105. if (p.openElements.currentTagName === $.MENUITEM)
  1106. p.openElements.pop();
  1107. p._appendElement(token, NS.HTML);
  1108. p.framesetOk = false;
  1109. }
  1110. function imageStartTagInBody(p, token) {
  1111. token.tagName = $.IMG;
  1112. areaStartTagInBody(p, token);
  1113. }
  1114. function textareaStartTagInBody(p, token) {
  1115. p._insertElement(token, NS.HTML);
  1116. //NOTE: If the next token is a U+000A LINE FEED (LF) character token, then ignore that token and move
  1117. //on to the next one. (Newlines at the start of textarea elements are ignored as an authoring convenience.)
  1118. p.skipNextNewLine = true;
  1119. p.tokenizer.state = Tokenizer.MODE.RCDATA;
  1120. p.originalInsertionMode = p.insertionMode;
  1121. p.framesetOk = false;
  1122. p.insertionMode = TEXT_MODE;
  1123. }
  1124. function xmpStartTagInBody(p, token) {
  1125. if (p.openElements.hasInButtonScope($.P))
  1126. p._closePElement();
  1127. p._reconstructActiveFormattingElements();
  1128. p.framesetOk = false;
  1129. p._switchToTextParsing(token, Tokenizer.MODE.RAWTEXT);
  1130. }
  1131. function iframeStartTagInBody(p, token) {
  1132. p.framesetOk = false;
  1133. p._switchToTextParsing(token, Tokenizer.MODE.RAWTEXT);
  1134. }
  1135. //NOTE: here we assume that we always act as an user agent with enabled plugins, so we parse
  1136. //<noembed> as a rawtext.
  1137. function noembedStartTagInBody(p, token) {
  1138. p._switchToTextParsing(token, Tokenizer.MODE.RAWTEXT);
  1139. }
  1140. function selectStartTagInBody(p, token) {
  1141. p._reconstructActiveFormattingElements();
  1142. p._insertElement(token, NS.HTML);
  1143. p.framesetOk = false;
  1144. if (p.insertionMode === IN_TABLE_MODE ||
  1145. p.insertionMode === IN_CAPTION_MODE ||
  1146. p.insertionMode === IN_TABLE_BODY_MODE ||
  1147. p.insertionMode === IN_ROW_MODE ||
  1148. p.insertionMode === IN_CELL_MODE)
  1149. p.insertionMode = IN_SELECT_IN_TABLE_MODE;
  1150. else
  1151. p.insertionMode = IN_SELECT_MODE;
  1152. }
  1153. function optgroupStartTagInBody(p, token) {
  1154. if (p.openElements.currentTagName === $.OPTION)
  1155. p.openElements.pop();
  1156. p._reconstructActiveFormattingElements();
  1157. p._insertElement(token, NS.HTML);
  1158. }
  1159. function rbStartTagInBody(p, token) {
  1160. if (p.openElements.hasInScope($.RUBY))
  1161. p.openElements.generateImpliedEndTags();
  1162. p._insertElement(token, NS.HTML);
  1163. }
  1164. function rtStartTagInBody(p, token) {
  1165. if (p.openElements.hasInScope($.RUBY))
  1166. p.openElements.generateImpliedEndTagsWithExclusion($.RTC);
  1167. p._insertElement(token, NS.HTML);
  1168. }
  1169. function menuitemStartTagInBody(p, token) {
  1170. if (p.openElements.currentTagName === $.MENUITEM)
  1171. p.openElements.pop();
  1172. // TODO needs clarification, see https://github.com/whatwg/html/pull/907/files#r73505877
  1173. p._reconstructActiveFormattingElements();
  1174. p._insertElement(token, NS.HTML);
  1175. }
  1176. function menuStartTagInBody(p, token) {
  1177. if (p.openElements.hasInButtonScope($.P))
  1178. p._closePElement();
  1179. if (p.openElements.currentTagName === $.MENUITEM)
  1180. p.openElements.pop();
  1181. p._insertElement(token, NS.HTML);
  1182. }
  1183. function mathStartTagInBody(p, token) {
  1184. p._reconstructActiveFormattingElements();
  1185. foreignContent.adjustTokenMathMLAttrs(token);
  1186. foreignContent.adjustTokenXMLAttrs(token);
  1187. if (token.selfClosing)
  1188. p._appendElement(token, NS.MATHML);
  1189. else
  1190. p._insertElement(token, NS.MATHML);
  1191. }
  1192. function svgStartTagInBody(p, token) {
  1193. p._reconstructActiveFormattingElements();
  1194. foreignContent.adjustTokenSVGAttrs(token);
  1195. foreignContent.adjustTokenXMLAttrs(token);
  1196. if (token.selfClosing)
  1197. p._appendElement(token, NS.SVG);
  1198. else
  1199. p._insertElement(token, NS.SVG);
  1200. }
  1201. function genericStartTagInBody(p, token) {
  1202. p._reconstructActiveFormattingElements();
  1203. p._insertElement(token, NS.HTML);
  1204. }
  1205. //OPTIMIZATION: Integer comparisons are low-cost, so we can use very fast tag name length filters here.
  1206. //It's faster than using dictionary.
  1207. function startTagInBody(p, token) {
  1208. var tn = token.tagName;
  1209. switch (tn.length) {
  1210. case 1:
  1211. if (tn === $.I || tn === $.S || tn === $.B || tn === $.U)
  1212. bStartTagInBody(p, token);
  1213. else if (tn === $.P)
  1214. addressStartTagInBody(p, token);
  1215. else if (tn === $.A)
  1216. aStartTagInBody(p, token);
  1217. else
  1218. genericStartTagInBody(p, token);
  1219. break;
  1220. case 2:
  1221. if (tn === $.DL || tn === $.OL || tn === $.UL)
  1222. addressStartTagInBody(p, token);
  1223. else if (tn === $.H1 || tn === $.H2 || tn === $.H3 || tn === $.H4 || tn === $.H5 || tn === $.H6)
  1224. numberedHeaderStartTagInBody(p, token);
  1225. else if (tn === $.LI || tn === $.DD || tn === $.DT)
  1226. listItemStartTagInBody(p, token);
  1227. else if (tn === $.EM || tn === $.TT)
  1228. bStartTagInBody(p, token);
  1229. else if (tn === $.BR)
  1230. areaStartTagInBody(p, token);
  1231. else if (tn === $.HR)
  1232. hrStartTagInBody(p, token);
  1233. else if (tn === $.RB)
  1234. rbStartTagInBody(p, token);
  1235. else if (tn === $.RT || tn === $.RP)
  1236. rtStartTagInBody(p, token);
  1237. else if (tn !== $.TH && tn !== $.TD && tn !== $.TR)
  1238. genericStartTagInBody(p, token);
  1239. break;
  1240. case 3:
  1241. if (tn === $.DIV || tn === $.DIR || tn === $.NAV)
  1242. addressStartTagInBody(p, token);
  1243. else if (tn === $.PRE)
  1244. preStartTagInBody(p, token);
  1245. else if (tn === $.BIG)
  1246. bStartTagInBody(p, token);
  1247. else if (tn === $.IMG || tn === $.WBR)
  1248. areaStartTagInBody(p, token);
  1249. else if (tn === $.XMP)
  1250. xmpStartTagInBody(p, token);
  1251. else if (tn === $.SVG)
  1252. svgStartTagInBody(p, token);
  1253. else if (tn === $.RTC)
  1254. rbStartTagInBody(p, token);
  1255. else if (tn !== $.COL)
  1256. genericStartTagInBody(p, token);
  1257. break;
  1258. case 4:
  1259. if (tn === $.HTML)
  1260. htmlStartTagInBody(p, token);
  1261. else if (tn === $.BASE || tn === $.LINK || tn === $.META)
  1262. startTagInHead(p, token);
  1263. else if (tn === $.BODY)
  1264. bodyStartTagInBody(p, token);
  1265. else if (tn === $.MAIN)
  1266. addressStartTagInBody(p, token);
  1267. else if (tn === $.FORM)
  1268. formStartTagInBody(p, token);
  1269. else if (tn === $.CODE || tn === $.FONT)
  1270. bStartTagInBody(p, token);
  1271. else if (tn === $.NOBR)
  1272. nobrStartTagInBody(p, token);
  1273. else if (tn === $.AREA)
  1274. areaStartTagInBody(p, token);
  1275. else if (tn === $.MATH)
  1276. mathStartTagInBody(p, token);
  1277. else if (tn === $.MENU)
  1278. menuStartTagInBody(p, token);
  1279. else if (tn !== $.HEAD)
  1280. genericStartTagInBody(p, token);
  1281. break;
  1282. case 5:
  1283. if (tn === $.STYLE || tn === $.TITLE)
  1284. startTagInHead(p, token);
  1285. else if (tn === $.ASIDE)
  1286. addressStartTagInBody(p, token);
  1287. else if (tn === $.SMALL)
  1288. bStartTagInBody(p, token);
  1289. else if (tn === $.TABLE)
  1290. tableStartTagInBody(p, token);
  1291. else if (tn === $.EMBED)
  1292. areaStartTagInBody(p, token);
  1293. else if (tn === $.INPUT)
  1294. inputStartTagInBody(p, token);
  1295. else if (tn === $.PARAM || tn === $.TRACK)
  1296. paramStartTagInBody(p, token);
  1297. else if (tn === $.IMAGE)
  1298. imageStartTagInBody(p, token);
  1299. else if (tn !== $.FRAME && tn !== $.TBODY && tn !== $.TFOOT && tn !== $.THEAD)
  1300. genericStartTagInBody(p, token);
  1301. break;
  1302. case 6:
  1303. if (tn === $.SCRIPT)
  1304. startTagInHead(p, token);
  1305. else if (tn === $.CENTER || tn === $.FIGURE || tn === $.FOOTER || tn === $.HEADER || tn === $.HGROUP)
  1306. addressStartTagInBody(p, token);
  1307. else if (tn === $.BUTTON)
  1308. buttonStartTagInBody(p, token);
  1309. else if (tn === $.STRIKE || tn === $.STRONG)
  1310. bStartTagInBody(p, token);
  1311. else if (tn === $.APPLET || tn === $.OBJECT)
  1312. appletStartTagInBody(p, token);
  1313. else if (tn === $.KEYGEN)
  1314. areaStartTagInBody(p, token);
  1315. else if (tn === $.SOURCE)
  1316. paramStartTagInBody(p, token);
  1317. else if (tn === $.IFRAME)
  1318. iframeStartTagInBody(p, token);
  1319. else if (tn === $.SELECT)
  1320. selectStartTagInBody(p, token);
  1321. else if (tn === $.OPTION)
  1322. optgroupStartTagInBody(p, token);
  1323. else
  1324. genericStartTagInBody(p, token);
  1325. break;
  1326. case 7:
  1327. if (tn === $.BGSOUND)
  1328. startTagInHead(p, token);
  1329. else if (tn === $.DETAILS || tn === $.ADDRESS || tn === $.ARTICLE || tn === $.SECTION || tn === $.SUMMARY)
  1330. addressStartTagInBody(p, token);
  1331. else if (tn === $.LISTING)
  1332. preStartTagInBody(p, token);
  1333. else if (tn === $.MARQUEE)
  1334. appletStartTagInBody(p, token);
  1335. else if (tn === $.NOEMBED)
  1336. noembedStartTagInBody(p, token);
  1337. else if (tn !== $.CAPTION)
  1338. genericStartTagInBody(p, token);
  1339. break;
  1340. case 8:
  1341. if (tn === $.BASEFONT)
  1342. startTagInHead(p, token);
  1343. else if (tn === $.MENUITEM)
  1344. menuitemStartTagInBody(p, token);
  1345. else if (tn === $.FRAMESET)
  1346. framesetStartTagInBody(p, token);
  1347. else if (tn === $.FIELDSET)
  1348. addressStartTagInBody(p, token);
  1349. else if (tn === $.TEXTAREA)
  1350. textareaStartTagInBody(p, token);
  1351. else if (tn === $.TEMPLATE)
  1352. startTagInHead(p, token);
  1353. else if (tn === $.NOSCRIPT)
  1354. noembedStartTagInBody(p, token);
  1355. else if (tn === $.OPTGROUP)
  1356. optgroupStartTagInBody(p, token);
  1357. else if (tn !== $.COLGROUP)
  1358. genericStartTagInBody(p, token);
  1359. break;
  1360. case 9:
  1361. if (tn === $.PLAINTEXT)
  1362. plaintextStartTagInBody(p, token);
  1363. else
  1364. genericStartTagInBody(p, token);
  1365. break;
  1366. case 10:
  1367. if (tn === $.BLOCKQUOTE || tn === $.FIGCAPTION)
  1368. addressStartTagInBody(p, token);
  1369. else
  1370. genericStartTagInBody(p, token);
  1371. break;
  1372. default:
  1373. genericStartTagInBody(p, token);
  1374. }
  1375. }
  1376. function bodyEndTagInBody(p) {
  1377. if (p.openElements.hasInScope($.BODY))
  1378. p.insertionMode = AFTER_BODY_MODE;
  1379. }
  1380. function htmlEndTagInBody(p, token) {
  1381. if (p.openElements.hasInScope($.BODY)) {
  1382. p.insertionMode = AFTER_BODY_MODE;
  1383. p._processToken(token);
  1384. }
  1385. }
  1386. function addressEndTagInBody(p, token) {
  1387. var tn = token.tagName;
  1388. if (p.openElements.hasInScope(tn)) {
  1389. p.openElements.generateImpliedEndTags();
  1390. p.openElements.popUntilTagNamePopped(tn);
  1391. }
  1392. }
  1393. function formEndTagInBody(p) {
  1394. var inTemplate = p.openElements.tmplCount > 0,
  1395. formElement = p.formElement;
  1396. if (!inTemplate)
  1397. p.formElement = null;
  1398. if ((formElement || inTemplate) && p.openElements.hasInScope($.FORM)) {
  1399. p.openElements.generateImpliedEndTags();
  1400. if (inTemplate)
  1401. p.openElements.popUntilTagNamePopped($.FORM);
  1402. else
  1403. p.openElements.remove(formElement);
  1404. }
  1405. }
  1406. function pEndTagInBody(p) {
  1407. if (!p.openElements.hasInButtonScope($.P))
  1408. p._insertFakeElement($.P);
  1409. p._closePElement();
  1410. }
  1411. function liEndTagInBody(p) {
  1412. if (p.openElements.hasInListItemScope($.LI)) {
  1413. p.openElements.generateImpliedEndTagsWithExclusion($.LI);
  1414. p.openElements.popUntilTagNamePopped($.LI);
  1415. }
  1416. }
  1417. function ddEndTagInBody(p, token) {
  1418. var tn = token.tagName;
  1419. if (p.openElements.hasInScope(tn)) {
  1420. p.openElements.generateImpliedEndTagsWithExclusion(tn);
  1421. p.openElements.popUntilTagNamePopped(tn);
  1422. }
  1423. }
  1424. function numberedHeaderEndTagInBody(p) {
  1425. if (p.openElements.hasNumberedHeaderInScope()) {
  1426. p.openElements.generateImpliedEndTags();
  1427. p.openElements.popUntilNumberedHeaderPopped();
  1428. }
  1429. }
  1430. function appletEndTagInBody(p, token) {
  1431. var tn = token.tagName;
  1432. if (p.openElements.hasInScope(tn)) {
  1433. p.openElements.generateImpliedEndTags();
  1434. p.openElements.popUntilTagNamePopped(tn);
  1435. p.activeFormattingElements.clearToLastMarker();
  1436. }
  1437. }
  1438. function brEndTagInBody(p) {
  1439. p._reconstructActiveFormattingElements();
  1440. p._insertFakeElement($.BR);
  1441. p.openElements.pop();
  1442. p.framesetOk = false;
  1443. }
  1444. function genericEndTagInBody(p, token) {
  1445. var tn = token.tagName;
  1446. for (var i = p.openElements.stackTop; i > 0; i--) {
  1447. var element = p.openElements.items[i];
  1448. if (p.treeAdapter.getTagName(element) === tn) {
  1449. p.openElements.generateImpliedEndTagsWithExclusion(tn);
  1450. p.openElements.popUntilElementPopped(element);
  1451. break;
  1452. }
  1453. if (p._isSpecialElement(element))
  1454. break;
  1455. }
  1456. }
  1457. //OPTIMIZATION: Integer comparisons are low-cost, so we can use very fast tag name length filters here.
  1458. //It's faster than using dictionary.
  1459. function endTagInBody(p, token) {
  1460. var tn = token.tagName;
  1461. switch (tn.length) {
  1462. case 1:
  1463. if (tn === $.A || tn === $.B || tn === $.I || tn === $.S || tn === $.U)
  1464. callAdoptionAgency(p, token);
  1465. else if (tn === $.P)
  1466. pEndTagInBody(p, token);
  1467. else
  1468. genericEndTagInBody(p, token);
  1469. break;
  1470. case 2:
  1471. if (tn === $.DL || tn === $.UL || tn === $.OL)
  1472. addressEndTagInBody(p, token);
  1473. else if (tn === $.LI)
  1474. liEndTagInBody(p, token);
  1475. else if (tn === $.DD || tn === $.DT)
  1476. ddEndTagInBody(p, token);
  1477. else if (tn === $.H1 || tn === $.H2 || tn === $.H3 || tn === $.H4 || tn === $.H5 || tn === $.H6)
  1478. numberedHeaderEndTagInBody(p, token);
  1479. else if (tn === $.BR)
  1480. brEndTagInBody(p, token);
  1481. else if (tn === $.EM || tn === $.TT)
  1482. callAdoptionAgency(p, token);
  1483. else
  1484. genericEndTagInBody(p, token);
  1485. break;
  1486. case 3:
  1487. if (tn === $.BIG)
  1488. callAdoptionAgency(p, token);
  1489. else if (tn === $.DIR || tn === $.DIV || tn === $.NAV)
  1490. addressEndTagInBody(p, token);
  1491. else
  1492. genericEndTagInBody(p, token);
  1493. break;
  1494. case 4:
  1495. if (tn === $.BODY)
  1496. bodyEndTagInBody(p, token);
  1497. else if (tn === $.HTML)
  1498. htmlEndTagInBody(p, token);
  1499. else if (tn === $.FORM)
  1500. formEndTagInBody(p, token);
  1501. else if (tn === $.CODE || tn === $.FONT || tn === $.NOBR)
  1502. callAdoptionAgency(p, token);
  1503. else if (tn === $.MAIN || tn === $.MENU)
  1504. addressEndTagInBody(p, token);
  1505. else
  1506. genericEndTagInBody(p, token);
  1507. break;
  1508. case 5:
  1509. if (tn === $.ASIDE)
  1510. addressEndTagInBody(p, token);
  1511. else if (tn === $.SMALL)
  1512. callAdoptionAgency(p, token);
  1513. else
  1514. genericEndTagInBody(p, token);
  1515. break;
  1516. case 6:
  1517. if (tn === $.CENTER || tn === $.FIGURE || tn === $.FOOTER || tn === $.HEADER || tn === $.HGROUP)
  1518. addressEndTagInBody(p, token);
  1519. else if (tn === $.APPLET || tn === $.OBJECT)
  1520. appletEndTagInBody(p, token);
  1521. else if (tn === $.STRIKE || tn === $.STRONG)
  1522. callAdoptionAgency(p, token);
  1523. else
  1524. genericEndTagInBody(p, token);
  1525. break;
  1526. case 7:
  1527. if (tn === $.ADDRESS || tn === $.ARTICLE || tn === $.DETAILS || tn === $.SECTION || tn === $.SUMMARY)
  1528. addressEndTagInBody(p, token);
  1529. else if (tn === $.MARQUEE)
  1530. appletEndTagInBody(p, token);
  1531. else
  1532. genericEndTagInBody(p, token);
  1533. break;
  1534. case 8:
  1535. if (tn === $.FIELDSET)
  1536. addressEndTagInBody(p, token);
  1537. else if (tn === $.TEMPLATE)
  1538. endTagInHead(p, token);
  1539. else
  1540. genericEndTagInBody(p, token);
  1541. break;
  1542. case 10:
  1543. if (tn === $.BLOCKQUOTE || tn === $.FIGCAPTION)
  1544. addressEndTagInBody(p, token);
  1545. else
  1546. genericEndTagInBody(p, token);
  1547. break;
  1548. default :
  1549. genericEndTagInBody(p, token);
  1550. }
  1551. }
  1552. function eofInBody(p, token) {
  1553. if (p.tmplInsertionModeStackTop > -1)
  1554. eofInTemplate(p, token);
  1555. else
  1556. p.stopped = true;
  1557. }
  1558. //12.2.5.4.8 The "text" insertion mode
  1559. //------------------------------------------------------------------
  1560. function endTagInText(p, token) {
  1561. if (token.tagName === $.SCRIPT)
  1562. p.pendingScript = p.openElements.current;
  1563. p.openElements.pop();
  1564. p.insertionMode = p.originalInsertionMode;
  1565. }
  1566. function eofInText(p, token) {
  1567. p.openElements.pop();
  1568. p.insertionMode = p.originalInsertionMode;
  1569. p._processToken(token);
  1570. }
  1571. //12.2.5.4.9 The "in table" insertion mode
  1572. //------------------------------------------------------------------
  1573. function characterInTable(p, token) {
  1574. var curTn = p.openElements.currentTagName;
  1575. if (curTn === $.TABLE || curTn === $.TBODY || curTn === $.TFOOT || curTn === $.THEAD || curTn === $.TR) {
  1576. p.pendingCharacterTokens = [];
  1577. p.hasNonWhitespacePendingCharacterToken = false;
  1578. p.originalInsertionMode = p.insertionMode;
  1579. p.insertionMode = IN_TABLE_TEXT_MODE;
  1580. p._processToken(token);
  1581. }
  1582. else
  1583. tokenInTable(p, token);
  1584. }
  1585. function captionStartTagInTable(p, token) {
  1586. p.openElements.clearBackToTableContext();
  1587. p.activeFormattingElements.insertMarker();
  1588. p._insertElement(token, NS.HTML);
  1589. p.insertionMode = IN_CAPTION_MODE;
  1590. }
  1591. function colgroupStartTagInTable(p, token) {
  1592. p.openElements.clearBackToTableContext();
  1593. p._insertElement(token, NS.HTML);
  1594. p.insertionMode = IN_COLUMN_GROUP_MODE;
  1595. }
  1596. function colStartTagInTable(p, token) {
  1597. p.openElements.clearBackToTableContext();
  1598. p._insertFakeElement($.COLGROUP);
  1599. p.insertionMode = IN_COLUMN_GROUP_MODE;
  1600. p._processToken(token);
  1601. }
  1602. function tbodyStartTagInTable(p, token) {
  1603. p.openElements.clearBackToTableContext();
  1604. p._insertElement(token, NS.HTML);
  1605. p.insertionMode = IN_TABLE_BODY_MODE;
  1606. }
  1607. function tdStartTagInTable(p, token) {
  1608. p.openElements.clearBackToTableContext();
  1609. p._insertFakeElement($.TBODY);
  1610. p.insertionMode = IN_TABLE_BODY_MODE;
  1611. p._processToken(token);
  1612. }
  1613. function tableStartTagInTable(p, token) {
  1614. if (p.openElements.hasInTableScope($.TABLE)) {
  1615. p.openElements.popUntilTagNamePopped($.TABLE);
  1616. p._resetInsertionMode();
  1617. p._processToken(token);
  1618. }
  1619. }
  1620. function inputStartTagInTable(p, token) {
  1621. var inputType = Tokenizer.getTokenAttr(token, ATTRS.TYPE);
  1622. if (inputType && inputType.toLowerCase() === HIDDEN_INPUT_TYPE)
  1623. p._appendElement(token, NS.HTML);
  1624. else
  1625. tokenInTable(p, token);
  1626. }
  1627. function formStartTagInTable(p, token) {
  1628. if (!p.formElement && p.openElements.tmplCount === 0) {
  1629. p._insertElement(token, NS.HTML);
  1630. p.formElement = p.openElements.current;
  1631. p.openElements.pop();
  1632. }
  1633. }
  1634. function startTagInTable(p, token) {
  1635. var tn = token.tagName;
  1636. switch (tn.length) {
  1637. case 2:
  1638. if (tn === $.TD || tn === $.TH || tn === $.TR)
  1639. tdStartTagInTable(p, token);
  1640. else
  1641. tokenInTable(p, token);
  1642. break;
  1643. case 3:
  1644. if (tn === $.COL)
  1645. colStartTagInTable(p, token);
  1646. else
  1647. tokenInTable(p, token);
  1648. break;
  1649. case 4:
  1650. if (tn === $.FORM)
  1651. formStartTagInTable(p, token);
  1652. else
  1653. tokenInTable(p, token);
  1654. break;
  1655. case 5:
  1656. if (tn === $.TABLE)
  1657. tableStartTagInTable(p, token);
  1658. else if (tn === $.STYLE)
  1659. startTagInHead(p, token);
  1660. else if (tn === $.TBODY || tn === $.TFOOT || tn === $.THEAD)
  1661. tbodyStartTagInTable(p, token);
  1662. else if (tn === $.INPUT)
  1663. inputStartTagInTable(p, token);
  1664. else
  1665. tokenInTable(p, token);
  1666. break;
  1667. case 6:
  1668. if (tn === $.SCRIPT)
  1669. startTagInHead(p, token);
  1670. else
  1671. tokenInTable(p, token);
  1672. break;
  1673. case 7:
  1674. if (tn === $.CAPTION)
  1675. captionStartTagInTable(p, token);
  1676. else
  1677. tokenInTable(p, token);
  1678. break;
  1679. case 8:
  1680. if (tn === $.COLGROUP)
  1681. colgroupStartTagInTable(p, token);
  1682. else if (tn === $.TEMPLATE)
  1683. startTagInHead(p, token);
  1684. else
  1685. tokenInTable(p, token);
  1686. break;
  1687. default:
  1688. tokenInTable(p, token);
  1689. }
  1690. }
  1691. function endTagInTable(p, token) {
  1692. var tn = token.tagName;
  1693. if (tn === $.TABLE) {
  1694. if (p.openElements.hasInTableScope($.TABLE)) {
  1695. p.openElements.popUntilTagNamePopped($.TABLE);
  1696. p._resetInsertionMode();
  1697. }
  1698. }
  1699. else if (tn === $.TEMPLATE)
  1700. endTagInHead(p, token);
  1701. else if (tn !== $.BODY && tn !== $.CAPTION && tn !== $.COL && tn !== $.COLGROUP && tn !== $.HTML &&
  1702. tn !== $.TBODY && tn !== $.TD && tn !== $.TFOOT && tn !== $.TH && tn !== $.THEAD && tn !== $.TR)
  1703. tokenInTable(p, token);
  1704. }
  1705. function tokenInTable(p, token) {
  1706. var savedFosterParentingState = p.fosterParentingEnabled;
  1707. p.fosterParentingEnabled = true;
  1708. p._processTokenInBodyMode(token);
  1709. p.fosterParentingEnabled = savedFosterParentingState;
  1710. }
  1711. //12.2.5.4.10 The "in table text" insertion mode
  1712. //------------------------------------------------------------------
  1713. function whitespaceCharacterInTableText(p, token) {
  1714. p.pendingCharacterTokens.push(token);
  1715. }
  1716. function characterInTableText(p, token) {
  1717. p.pendingCharacterTokens.push(token);
  1718. p.hasNonWhitespacePendingCharacterToken = true;
  1719. }
  1720. function tokenInTableText(p, token) {
  1721. var i = 0;
  1722. if (p.hasNonWhitespacePendingCharacterToken) {
  1723. for (; i < p.pendingCharacterTokens.length; i++)
  1724. tokenInTable(p, p.pendingCharacterTokens[i]);
  1725. }
  1726. else {
  1727. for (; i < p.pendingCharacterTokens.length; i++)
  1728. p._insertCharacters(p.pendingCharacterTokens[i]);
  1729. }
  1730. p.insertionMode = p.originalInsertionMode;
  1731. p._processToken(token);
  1732. }
  1733. //12.2.5.4.11 The "in caption" insertion mode
  1734. //------------------------------------------------------------------
  1735. function startTagInCaption(p, token) {
  1736. var tn = token.tagName;
  1737. if (tn === $.CAPTION || tn === $.COL || tn === $.COLGROUP || tn === $.TBODY ||
  1738. tn === $.TD || tn === $.TFOOT || tn === $.TH || tn === $.THEAD || tn === $.TR) {
  1739. if (p.openElements.hasInTableScope($.CAPTION)) {
  1740. p.openElements.generateImpliedEndTags();
  1741. p.openElements.popUntilTagNamePopped($.CAPTION);
  1742. p.activeFormattingElements.clearToLastMarker();
  1743. p.insertionMode = IN_TABLE_MODE;
  1744. p._processToken(token);
  1745. }
  1746. }
  1747. else
  1748. startTagInBody(p, token);
  1749. }
  1750. function endTagInCaption(p, token) {
  1751. var tn = token.tagName;
  1752. if (tn === $.CAPTION || tn === $.TABLE) {
  1753. if (p.openElements.hasInTableScope($.CAPTION)) {
  1754. p.openElements.generateImpliedEndTags();
  1755. p.openElements.popUntilTagNamePopped($.CAPTION);
  1756. p.activeFormattingElements.clearToLastMarker();
  1757. p.insertionMode = IN_TABLE_MODE;
  1758. if (tn === $.TABLE)
  1759. p._processToken(token);
  1760. }
  1761. }
  1762. else if (tn !== $.BODY && tn !== $.COL && tn !== $.COLGROUP && tn !== $.HTML && tn !== $.TBODY &&
  1763. tn !== $.TD && tn !== $.TFOOT && tn !== $.TH && tn !== $.THEAD && tn !== $.TR)
  1764. endTagInBody(p, token);
  1765. }
  1766. //12.2.5.4.12 The "in column group" insertion mode
  1767. //------------------------------------------------------------------
  1768. function startTagInColumnGroup(p, token) {
  1769. var tn = token.tagName;
  1770. if (tn === $.HTML)
  1771. startTagInBody(p, token);
  1772. else if (tn === $.COL)
  1773. p._appendElement(token, NS.HTML);
  1774. else if (tn === $.TEMPLATE)
  1775. startTagInHead(p, token);
  1776. else
  1777. tokenInColumnGroup(p, token);
  1778. }
  1779. function endTagInColumnGroup(p, token) {
  1780. var tn = token.tagName;
  1781. if (tn === $.COLGROUP) {
  1782. if (p.openElements.currentTagName === $.COLGROUP) {
  1783. p.openElements.pop();
  1784. p.insertionMode = IN_TABLE_MODE;
  1785. }
  1786. }
  1787. else if (tn === $.TEMPLATE)
  1788. endTagInHead(p, token);
  1789. else if (tn !== $.COL)
  1790. tokenInColumnGroup(p, token);
  1791. }
  1792. function tokenInColumnGroup(p, token) {
  1793. if (p.openElements.currentTagName === $.COLGROUP) {
  1794. p.openElements.pop();
  1795. p.insertionMode = IN_TABLE_MODE;
  1796. p._processToken(token);
  1797. }
  1798. }
  1799. //12.2.5.4.13 The "in table body" insertion mode
  1800. //------------------------------------------------------------------
  1801. function startTagInTableBody(p, token) {
  1802. var tn = token.tagName;
  1803. if (tn === $.TR) {
  1804. p.openElements.clearBackToTableBodyContext();
  1805. p._insertElement(token, NS.HTML);
  1806. p.insertionMode = IN_ROW_MODE;
  1807. }
  1808. else if (tn === $.TH || tn === $.TD) {
  1809. p.openElements.clearBackToTableBodyContext();
  1810. p._insertFakeElement($.TR);
  1811. p.insertionMode = IN_ROW_MODE;
  1812. p._processToken(token);
  1813. }
  1814. else if (tn === $.CAPTION || tn === $.COL || tn === $.COLGROUP ||
  1815. tn === $.TBODY || tn === $.TFOOT || tn === $.THEAD) {
  1816. if (p.openElements.hasTableBodyContextInTableScope()) {
  1817. p.openElements.clearBackToTableBodyContext();
  1818. p.openElements.pop();
  1819. p.insertionMode = IN_TABLE_MODE;
  1820. p._processToken(token);
  1821. }
  1822. }
  1823. else
  1824. startTagInTable(p, token);
  1825. }
  1826. function endTagInTableBody(p, token) {
  1827. var tn = token.tagName;
  1828. if (tn === $.TBODY || tn === $.TFOOT || tn === $.THEAD) {
  1829. if (p.openElements.hasInTableScope(tn)) {
  1830. p.openElements.clearBackToTableBodyContext();
  1831. p.openElements.pop();
  1832. p.insertionMode = IN_TABLE_MODE;
  1833. }
  1834. }
  1835. else if (tn === $.TABLE) {
  1836. if (p.openElements.hasTableBodyContextInTableScope()) {
  1837. p.openElements.clearBackToTableBodyContext();
  1838. p.openElements.pop();
  1839. p.insertionMode = IN_TABLE_MODE;
  1840. p._processToken(token);
  1841. }
  1842. }
  1843. else if (tn !== $.BODY && tn !== $.CAPTION && tn !== $.COL && tn !== $.COLGROUP ||
  1844. tn !== $.HTML && tn !== $.TD && tn !== $.TH && tn !== $.TR)
  1845. endTagInTable(p, token);
  1846. }
  1847. //12.2.5.4.14 The "in row" insertion mode
  1848. //------------------------------------------------------------------
  1849. function startTagInRow(p, token) {
  1850. var tn = token.tagName;
  1851. if (tn === $.TH || tn === $.TD) {
  1852. p.openElements.clearBackToTableRowContext();
  1853. p._insertElement(token, NS.HTML);
  1854. p.insertionMode = IN_CELL_MODE;
  1855. p.activeFormattingElements.insertMarker();
  1856. }
  1857. else if (tn === $.CAPTION || tn === $.COL || tn === $.COLGROUP || tn === $.TBODY ||
  1858. tn === $.TFOOT || tn === $.THEAD || tn === $.TR) {
  1859. if (p.openElements.hasInTableScope($.TR)) {
  1860. p.openElements.clearBackToTableRowContext();
  1861. p.openElements.pop();
  1862. p.insertionMode = IN_TABLE_BODY_MODE;
  1863. p._processToken(token);
  1864. }
  1865. }
  1866. else
  1867. startTagInTable(p, token);
  1868. }
  1869. function endTagInRow(p, token) {
  1870. var tn = token.tagName;
  1871. if (tn === $.TR) {
  1872. if (p.openElements.hasInTableScope($.TR)) {
  1873. p.openElements.clearBackToTableRowContext();
  1874. p.openElements.pop();
  1875. p.insertionMode = IN_TABLE_BODY_MODE;
  1876. }
  1877. }
  1878. else if (tn === $.TABLE) {
  1879. if (p.openElements.hasInTableScope($.TR)) {
  1880. p.openElements.clearBackToTableRowContext();
  1881. p.openElements.pop();
  1882. p.insertionMode = IN_TABLE_BODY_MODE;
  1883. p._processToken(token);
  1884. }
  1885. }
  1886. else if (tn === $.TBODY || tn === $.TFOOT || tn === $.THEAD) {
  1887. if (p.openElements.hasInTableScope(tn) || p.openElements.hasInTableScope($.TR)) {
  1888. p.openElements.clearBackToTableRowContext();
  1889. p.openElements.pop();
  1890. p.insertionMode = IN_TABLE_BODY_MODE;
  1891. p._processToken(token);
  1892. }
  1893. }
  1894. else if (tn !== $.BODY && tn !== $.CAPTION && tn !== $.COL && tn !== $.COLGROUP ||
  1895. tn !== $.HTML && tn !== $.TD && tn !== $.TH)
  1896. endTagInTable(p, token);
  1897. }
  1898. //12.2.5.4.15 The "in cell" insertion mode
  1899. //------------------------------------------------------------------
  1900. function startTagInCell(p, token) {
  1901. var tn = token.tagName;
  1902. if (tn === $.CAPTION || tn === $.COL || tn === $.COLGROUP || tn === $.TBODY ||
  1903. tn === $.TD || tn === $.TFOOT || tn === $.TH || tn === $.THEAD || tn === $.TR) {
  1904. if (p.openElements.hasInTableScope($.TD) || p.openElements.hasInTableScope($.TH)) {
  1905. p._closeTableCell();
  1906. p._processToken(token);
  1907. }
  1908. }
  1909. else
  1910. startTagInBody(p, token);
  1911. }
  1912. function endTagInCell(p, token) {
  1913. var tn = token.tagName;
  1914. if (tn === $.TD || tn === $.TH) {
  1915. if (p.openElements.hasInTableScope(tn)) {
  1916. p.openElements.generateImpliedEndTags();
  1917. p.openElements.popUntilTagNamePopped(tn);
  1918. p.activeFormattingElements.clearToLastMarker();
  1919. p.insertionMode = IN_ROW_MODE;
  1920. }
  1921. }
  1922. else if (tn === $.TABLE || tn === $.TBODY || tn === $.TFOOT || tn === $.THEAD || tn === $.TR) {
  1923. if (p.openElements.hasInTableScope(tn)) {
  1924. p._closeTableCell();
  1925. p._processToken(token);
  1926. }
  1927. }
  1928. else if (tn !== $.BODY && tn !== $.CAPTION && tn !== $.COL && tn !== $.COLGROUP && tn !== $.HTML)
  1929. endTagInBody(p, token);
  1930. }
  1931. //12.2.5.4.16 The "in select" insertion mode
  1932. //------------------------------------------------------------------
  1933. function startTagInSelect(p, token) {
  1934. var tn = token.tagName;
  1935. if (tn === $.HTML)
  1936. startTagInBody(p, token);
  1937. else if (tn === $.OPTION) {
  1938. if (p.openElements.currentTagName === $.OPTION)
  1939. p.openElements.pop();
  1940. p._insertElement(token, NS.HTML);
  1941. }
  1942. else if (tn === $.OPTGROUP) {
  1943. if (p.openElements.currentTagName === $.OPTION)
  1944. p.openElements.pop();
  1945. if (p.openElements.currentTagName === $.OPTGROUP)
  1946. p.openElements.pop();
  1947. p._insertElement(token, NS.HTML);
  1948. }
  1949. else if (tn === $.INPUT || tn === $.KEYGEN || tn === $.TEXTAREA || tn === $.SELECT) {
  1950. if (p.openElements.hasInSelectScope($.SELECT)) {
  1951. p.openElements.popUntilTagNamePopped($.SELECT);
  1952. p._resetInsertionMode();
  1953. if (tn !== $.SELECT)
  1954. p._processToken(token);
  1955. }
  1956. }
  1957. else if (tn === $.SCRIPT || tn === $.TEMPLATE)
  1958. startTagInHead(p, token);
  1959. }
  1960. function endTagInSelect(p, token) {
  1961. var tn = token.tagName;
  1962. if (tn === $.OPTGROUP) {
  1963. var prevOpenElement = p.openElements.items[p.openElements.stackTop - 1],
  1964. prevOpenElementTn = prevOpenElement && p.treeAdapter.getTagName(prevOpenElement);
  1965. if (p.openElements.currentTagName === $.OPTION && prevOpenElementTn === $.OPTGROUP)
  1966. p.openElements.pop();
  1967. if (p.openElements.currentTagName === $.OPTGROUP)
  1968. p.openElements.pop();
  1969. }
  1970. else if (tn === $.OPTION) {
  1971. if (p.openElements.currentTagName === $.OPTION)
  1972. p.openElements.pop();
  1973. }
  1974. else if (tn === $.SELECT && p.openElements.hasInSelectScope($.SELECT)) {
  1975. p.openElements.popUntilTagNamePopped($.SELECT);
  1976. p._resetInsertionMode();
  1977. }
  1978. else if (tn === $.TEMPLATE)
  1979. endTagInHead(p, token);
  1980. }
  1981. //12.2.5.4.17 The "in select in table" insertion mode
  1982. //------------------------------------------------------------------
  1983. function startTagInSelectInTable(p, token) {
  1984. var tn = token.tagName;
  1985. if (tn === $.CAPTION || tn === $.TABLE || tn === $.TBODY || tn === $.TFOOT ||
  1986. tn === $.THEAD || tn === $.TR || tn === $.TD || tn === $.TH) {
  1987. p.openElements.popUntilTagNamePopped($.SELECT);
  1988. p._resetInsertionMode();
  1989. p._processToken(token);
  1990. }
  1991. else
  1992. startTagInSelect(p, token);
  1993. }
  1994. function endTagInSelectInTable(p, token) {
  1995. var tn = token.tagName;
  1996. if (tn === $.CAPTION || tn === $.TABLE || tn === $.TBODY || tn === $.TFOOT ||
  1997. tn === $.THEAD || tn === $.TR || tn === $.TD || tn === $.TH) {
  1998. if (p.openElements.hasInTableScope(tn)) {
  1999. p.openElements.popUntilTagNamePopped($.SELECT);
  2000. p._resetInsertionMode();
  2001. p._processToken(token);
  2002. }
  2003. }
  2004. else
  2005. endTagInSelect(p, token);
  2006. }
  2007. //12.2.5.4.18 The "in template" insertion mode
  2008. //------------------------------------------------------------------
  2009. function startTagInTemplate(p, token) {
  2010. var tn = token.tagName;
  2011. if (tn === $.BASE || tn === $.BASEFONT || tn === $.BGSOUND || tn === $.LINK || tn === $.META ||
  2012. tn === $.NOFRAMES || tn === $.SCRIPT || tn === $.STYLE || tn === $.TEMPLATE || tn === $.TITLE)
  2013. startTagInHead(p, token);
  2014. else {
  2015. var newInsertionMode = TEMPLATE_INSERTION_MODE_SWITCH_MAP[tn] || IN_BODY_MODE;
  2016. p._popTmplInsertionMode();
  2017. p._pushTmplInsertionMode(newInsertionMode);
  2018. p.insertionMode = newInsertionMode;
  2019. p._processToken(token);
  2020. }
  2021. }
  2022. function endTagInTemplate(p, token) {
  2023. if (token.tagName === $.TEMPLATE)
  2024. endTagInHead(p, token);
  2025. }
  2026. function eofInTemplate(p, token) {
  2027. if (p.openElements.tmplCount > 0) {
  2028. p.openElements.popUntilTagNamePopped($.TEMPLATE);
  2029. p.activeFormattingElements.clearToLastMarker();
  2030. p._popTmplInsertionMode();
  2031. p._resetInsertionMode();
  2032. p._processToken(token);
  2033. }
  2034. else
  2035. p.stopped = true;
  2036. }
  2037. //12.2.5.4.19 The "after body" insertion mode
  2038. //------------------------------------------------------------------
  2039. function startTagAfterBody(p, token) {
  2040. if (token.tagName === $.HTML)
  2041. startTagInBody(p, token);
  2042. else
  2043. tokenAfterBody(p, token);
  2044. }
  2045. function endTagAfterBody(p, token) {
  2046. if (token.tagName === $.HTML) {
  2047. if (!p.fragmentContext)
  2048. p.insertionMode = AFTER_AFTER_BODY_MODE;
  2049. }
  2050. else
  2051. tokenAfterBody(p, token);
  2052. }
  2053. function tokenAfterBody(p, token) {
  2054. p.insertionMode = IN_BODY_MODE;
  2055. p._processToken(token);
  2056. }
  2057. //12.2.5.4.20 The "in frameset" insertion mode
  2058. //------------------------------------------------------------------
  2059. function startTagInFrameset(p, token) {
  2060. var tn = token.tagName;
  2061. if (tn === $.HTML)
  2062. startTagInBody(p, token);
  2063. else if (tn === $.FRAMESET)
  2064. p._insertElement(token, NS.HTML);
  2065. else if (tn === $.FRAME)
  2066. p._appendElement(token, NS.HTML);
  2067. else if (tn === $.NOFRAMES)
  2068. startTagInHead(p, token);
  2069. }
  2070. function endTagInFrameset(p, token) {
  2071. if (token.tagName === $.FRAMESET && !p.openElements.isRootHtmlElementCurrent()) {
  2072. p.openElements.pop();
  2073. if (!p.fragmentContext && p.openElements.currentTagName !== $.FRAMESET)
  2074. p.insertionMode = AFTER_FRAMESET_MODE;
  2075. }
  2076. }
  2077. //12.2.5.4.21 The "after frameset" insertion mode
  2078. //------------------------------------------------------------------
  2079. function startTagAfterFrameset(p, token) {
  2080. var tn = token.tagName;
  2081. if (tn === $.HTML)
  2082. startTagInBody(p, token);
  2083. else if (tn === $.NOFRAMES)
  2084. startTagInHead(p, token);
  2085. }
  2086. function endTagAfterFrameset(p, token) {
  2087. if (token.tagName === $.HTML)
  2088. p.insertionMode = AFTER_AFTER_FRAMESET_MODE;
  2089. }
  2090. //12.2.5.4.22 The "after after body" insertion mode
  2091. //------------------------------------------------------------------
  2092. function startTagAfterAfterBody(p, token) {
  2093. if (token.tagName === $.HTML)
  2094. startTagInBody(p, token);
  2095. else
  2096. tokenAfterAfterBody(p, token);
  2097. }
  2098. function tokenAfterAfterBody(p, token) {
  2099. p.insertionMode = IN_BODY_MODE;
  2100. p._processToken(token);
  2101. }
  2102. //12.2.5.4.23 The "after after frameset" insertion mode
  2103. //------------------------------------------------------------------
  2104. function startTagAfterAfterFrameset(p, token) {
  2105. var tn = token.tagName;
  2106. if (tn === $.HTML)
  2107. startTagInBody(p, token);
  2108. else if (tn === $.NOFRAMES)
  2109. startTagInHead(p, token);
  2110. }
  2111. //12.2.5.5 The rules for parsing tokens in foreign content
  2112. //------------------------------------------------------------------
  2113. function nullCharacterInForeignContent(p, token) {
  2114. token.chars = UNICODE.REPLACEMENT_CHARACTER;
  2115. p._insertCharacters(token);
  2116. }
  2117. function characterInForeignContent(p, token) {
  2118. p._insertCharacters(token);
  2119. p.framesetOk = false;
  2120. }
  2121. function startTagInForeignContent(p, token) {
  2122. if (foreignContent.causesExit(token) && !p.fragmentContext) {
  2123. while (p.treeAdapter.getNamespaceURI(p.openElements.current) !== NS.HTML && !p._isIntegrationPoint(p.openElements.current))
  2124. p.openElements.pop();
  2125. p._processToken(token);
  2126. }
  2127. else {
  2128. var current = p._getAdjustedCurrentElement(),
  2129. currentNs = p.treeAdapter.getNamespaceURI(current);
  2130. if (currentNs === NS.MATHML)
  2131. foreignContent.adjustTokenMathMLAttrs(token);
  2132. else if (currentNs === NS.SVG) {
  2133. foreignContent.adjustTokenSVGTagName(token);
  2134. foreignContent.adjustTokenSVGAttrs(token);
  2135. }
  2136. foreignContent.adjustTokenXMLAttrs(token);
  2137. if (token.selfClosing)
  2138. p._appendElement(token, currentNs);
  2139. else
  2140. p._insertElement(token, currentNs);
  2141. }
  2142. }
  2143. function endTagInForeignContent(p, token) {
  2144. for (var i = p.openElements.stackTop; i > 0; i--) {
  2145. var element = p.openElements.items[i];
  2146. if (p.treeAdapter.getNamespaceURI(element) === NS.HTML) {
  2147. p._processToken(token);
  2148. break;
  2149. }
  2150. if (p.treeAdapter.getTagName(element).toLowerCase() === token.tagName) {
  2151. p.openElements.popUntilElementPopped(element);
  2152. break;
  2153. }
  2154. }
  2155. }