parser.c 161 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917
  1. // Copyright 2010 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // Author: [email protected] (Jonathan Tang)
  16. #include <assert.h>
  17. #include <ctype.h>
  18. #include <stdarg.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <strings.h>
  22. #include "attribute.h"
  23. #include "error.h"
  24. #include "gumbo.h"
  25. #include "insertion_mode.h"
  26. #include "parser.h"
  27. #include "tokenizer.h"
  28. #include "tokenizer_states.h"
  29. #include "utf8.h"
  30. #include "util.h"
  31. #include "vector.h"
  32. #define AVOID_UNUSED_VARIABLE_WARNING(i) (void)(i)
  33. #define GUMBO_STRING(literal) { literal, sizeof(literal) - 1 }
  34. #define TERMINATOR { "", 0 }
  35. static void* malloc_wrapper(void* unused, size_t size) {
  36. return malloc(size);
  37. }
  38. static void free_wrapper(void* unused, void* ptr) {
  39. free(ptr);
  40. }
  41. const GumboOptions kGumboDefaultOptions = {
  42. &malloc_wrapper,
  43. &free_wrapper,
  44. NULL,
  45. 8,
  46. false,
  47. -1,
  48. };
  49. static const GumboStringPiece kDoctypeHtml = GUMBO_STRING("html");
  50. static const GumboStringPiece kPublicIdHtml4_0 = GUMBO_STRING(
  51. "-//W3C//DTD HTML 4.0//EN");
  52. static const GumboStringPiece kPublicIdHtml4_01 = GUMBO_STRING(
  53. "-//W3C//DTD HTML 4.01//EN");
  54. static const GumboStringPiece kPublicIdXhtml1_0 = GUMBO_STRING(
  55. "-//W3C//DTD XHTML 1.0 Strict//EN");
  56. static const GumboStringPiece kPublicIdXhtml1_1 = GUMBO_STRING(
  57. "-//W3C//DTD XHTML 1.1//EN");
  58. static const GumboStringPiece kSystemIdRecHtml4_0 = GUMBO_STRING(
  59. "http://www.w3.org/TR/REC-html40/strict.dtd");
  60. static const GumboStringPiece kSystemIdHtml4 = GUMBO_STRING(
  61. "http://www.w3.org/TR/html4/strict.dtd");
  62. static const GumboStringPiece kSystemIdXhtmlStrict1_1 = GUMBO_STRING(
  63. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd");
  64. static const GumboStringPiece kSystemIdXhtml1_1 = GUMBO_STRING(
  65. "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd");
  66. static const GumboStringPiece kSystemIdLegacyCompat = GUMBO_STRING(
  67. "about:legacy-compat");
  68. // The doctype arrays have an explicit terminator because we want to pass them
  69. // to a helper function, and passing them as a pointer discards sizeof
  70. // information. The SVG arrays are used only by one-off functions, and so loops
  71. // over them use sizeof directly instead of a terminator.
  72. static const GumboStringPiece kQuirksModePublicIdPrefixes[] = {
  73. GUMBO_STRING("+//Silmaril//dtd html Pro v0r11 19970101//"),
  74. GUMBO_STRING("-//AdvaSoft Ltd//DTD HTML 3.0 asWedit + extensions//"),
  75. GUMBO_STRING("-//AS//DTD HTML 3.0 asWedit + extensions//"),
  76. GUMBO_STRING("-//IETF//DTD HTML 2.0 Level 1//"),
  77. GUMBO_STRING("-//IETF//DTD HTML 2.0 Level 2//"),
  78. GUMBO_STRING("-//IETF//DTD HTML 2.0 Strict Level 1//"),
  79. GUMBO_STRING("-//IETF//DTD HTML 2.0 Strict Level 2//"),
  80. GUMBO_STRING("-//IETF//DTD HTML 2.0 Strict//"),
  81. GUMBO_STRING("-//IETF//DTD HTML 2.0//"),
  82. GUMBO_STRING("-//IETF//DTD HTML 2.1E//"),
  83. GUMBO_STRING("-//IETF//DTD HTML 3.0//"),
  84. GUMBO_STRING("-//IETF//DTD HTML 3.2 Final//"),
  85. GUMBO_STRING("-//IETF//DTD HTML 3.2//"),
  86. GUMBO_STRING("-//IETF//DTD HTML 3//"),
  87. GUMBO_STRING("-//IETF//DTD HTML Level 0//"),
  88. GUMBO_STRING("-//IETF//DTD HTML Level 1//"),
  89. GUMBO_STRING("-//IETF//DTD HTML Level 2//"),
  90. GUMBO_STRING("-//IETF//DTD HTML Level 3//"),
  91. GUMBO_STRING("-//IETF//DTD HTML Strict Level 0//"),
  92. GUMBO_STRING("-//IETF//DTD HTML Strict Level 1//"),
  93. GUMBO_STRING("-//IETF//DTD HTML Strict Level 2//"),
  94. GUMBO_STRING("-//IETF//DTD HTML Strict Level 3//"),
  95. GUMBO_STRING("-//IETF//DTD HTML Strict//"),
  96. GUMBO_STRING("-//IETF//DTD HTML//"),
  97. GUMBO_STRING("-//Metrius//DTD Metrius Presentational//"),
  98. GUMBO_STRING("-//Microsoft//DTD Internet Explorer 2.0 HTML Strict//"),
  99. GUMBO_STRING("-//Microsoft//DTD Internet Explorer 2.0 HTML//"),
  100. GUMBO_STRING("-//Microsoft//DTD Internet Explorer 2.0 Tables//"),
  101. GUMBO_STRING("-//Microsoft//DTD Internet Explorer 3.0 HTML Strict//"),
  102. GUMBO_STRING("-//Microsoft//DTD Internet Explorer 3.0 HTML//"),
  103. GUMBO_STRING("-//Microsoft//DTD Internet Explorer 3.0 Tables//"),
  104. GUMBO_STRING("-//Netscape Comm. Corp.//DTD HTML//"),
  105. GUMBO_STRING("-//Netscape Comm. Corp.//DTD Strict HTML//"),
  106. GUMBO_STRING("-//O'Reilly and Associates//DTD HTML 2.0//"),
  107. GUMBO_STRING("-//O'Reilly and Associates//DTD HTML Extended 1.0//"),
  108. GUMBO_STRING("-//O'Reilly and Associates//DTD HTML Extended Relaxed 1.0//"),
  109. GUMBO_STRING("-//SoftQuad Software//DTD HoTMetaL PRO 6.0::19990601::)"
  110. "extensions to HTML 4.0//"),
  111. GUMBO_STRING("-//SoftQuad//DTD HoTMetaL PRO 4.0::19971010::"
  112. "extensions to HTML 4.0//"),
  113. GUMBO_STRING("-//Spyglass//DTD HTML 2.0 Extended//"),
  114. GUMBO_STRING("-//SQ//DTD HTML 2.0 HoTMetaL + extensions//"),
  115. GUMBO_STRING("-//Sun Microsystems Corp.//DTD HotJava HTML//"),
  116. GUMBO_STRING("-//Sun Microsystems Corp.//DTD HotJava Strict HTML//"),
  117. GUMBO_STRING("-//W3C//DTD HTML 3 1995-03-24//"),
  118. GUMBO_STRING("-//W3C//DTD HTML 3.2 Draft//"),
  119. GUMBO_STRING("-//W3C//DTD HTML 3.2 Final//"),
  120. GUMBO_STRING("-//W3C//DTD HTML 3.2//"),
  121. GUMBO_STRING("-//W3C//DTD HTML 3.2S Draft//"),
  122. GUMBO_STRING("-//W3C//DTD HTML 4.0 Frameset//"),
  123. GUMBO_STRING("-//W3C//DTD HTML 4.0 Transitional//"),
  124. GUMBO_STRING("-//W3C//DTD HTML Experimental 19960712//"),
  125. GUMBO_STRING("-//W3C//DTD HTML Experimental 970421//"),
  126. GUMBO_STRING("-//W3C//DTD W3 HTML//"),
  127. GUMBO_STRING("-//W3O//DTD W3 HTML 3.0//"),
  128. GUMBO_STRING("-//WebTechs//DTD Mozilla HTML 2.0//"),
  129. GUMBO_STRING("-//WebTechs//DTD Mozilla HTML//"),
  130. TERMINATOR
  131. };
  132. static const GumboStringPiece kQuirksModePublicIdExactMatches[] = {
  133. GUMBO_STRING("-//W3O//DTD W3 HTML Strict 3.0//EN//"),
  134. GUMBO_STRING("-/W3C/DTD HTML 4.0 Transitional/EN"),
  135. GUMBO_STRING("HTML"),
  136. TERMINATOR
  137. };
  138. static const GumboStringPiece kQuirksModeSystemIdExactMatches[] = {
  139. GUMBO_STRING("http://www.ibm.com/data/dtd/v11/ibmxhtml1-transitional.dtd"),
  140. TERMINATOR
  141. };
  142. static const GumboStringPiece kLimitedQuirksPublicIdPrefixes[] = {
  143. GUMBO_STRING("-//W3C//DTD XHTML 1.0 Frameset//"),
  144. GUMBO_STRING("-//W3C//DTD XHTML 1.0 Transitional//"),
  145. TERMINATOR
  146. };
  147. static const GumboStringPiece kLimitedQuirksRequiresSystemIdPublicIdPrefixes[] = {
  148. GUMBO_STRING("-//W3C//DTD HTML 4.01 Frameset//"),
  149. GUMBO_STRING("-//W3C//DTD HTML 4.01 Transitional//"),
  150. TERMINATOR
  151. };
  152. // Indexed by GumboNamespaceEnum; keep in sync with that.
  153. static const char* kLegalXmlns[] = {
  154. "http://www.w3.org/1999/xhtml",
  155. "http://www.w3.org/2000/svg",
  156. "http://www.w3.org/1998/Math/MathML"
  157. };
  158. typedef struct _ReplacementEntry {
  159. const GumboStringPiece from;
  160. const GumboStringPiece to;
  161. } ReplacementEntry;
  162. #define REPLACEMENT_ENTRY(from, to) \
  163. { GUMBO_STRING(from), GUMBO_STRING(to) }
  164. // Static data for SVG attribute replacements.
  165. // http://www.whatwg.org/specs/web-apps/current-work/multipage/tree-construction.html#adjust-svg-attributes
  166. static const ReplacementEntry kSvgAttributeReplacements[] = {
  167. REPLACEMENT_ENTRY("attributename", "attributeName"),
  168. REPLACEMENT_ENTRY("attributetype", "attributeType"),
  169. REPLACEMENT_ENTRY("basefrequency", "baseFrequency"),
  170. REPLACEMENT_ENTRY("baseprofile", "baseProfile"),
  171. REPLACEMENT_ENTRY("calcmode", "calcMode"),
  172. REPLACEMENT_ENTRY("clippathunits", "clipPathUnits"),
  173. REPLACEMENT_ENTRY("contentscripttype", "contentScriptType"),
  174. REPLACEMENT_ENTRY("contentstyletype", "contentStyleType"),
  175. REPLACEMENT_ENTRY("diffuseconstant", "diffuseConstant"),
  176. REPLACEMENT_ENTRY("edgemode", "edgeMode"),
  177. REPLACEMENT_ENTRY("externalresourcesrequired", "externalResourcesRequired"),
  178. REPLACEMENT_ENTRY("filterres", "filterRes"),
  179. REPLACEMENT_ENTRY("filterunits", "filterUnits"),
  180. REPLACEMENT_ENTRY("glyphref", "glyphRef"),
  181. REPLACEMENT_ENTRY("gradienttransform", "gradientTransform"),
  182. REPLACEMENT_ENTRY("gradientunits", "gradientUnits"),
  183. REPLACEMENT_ENTRY("kernelmatrix", "kernelMatrix"),
  184. REPLACEMENT_ENTRY("kernelunitlength", "kernelUnitLength"),
  185. REPLACEMENT_ENTRY("keypoints", "keyPoints"),
  186. REPLACEMENT_ENTRY("keysplines", "keySplines"),
  187. REPLACEMENT_ENTRY("keytimes", "keyTimes"),
  188. REPLACEMENT_ENTRY("lengthadjust", "lengthAdjust"),
  189. REPLACEMENT_ENTRY("limitingconeangle", "limitingConeAngle"),
  190. REPLACEMENT_ENTRY("markerheight", "markerHeight"),
  191. REPLACEMENT_ENTRY("markerunits", "markerUnits"),
  192. REPLACEMENT_ENTRY("markerwidth", "markerWidth"),
  193. REPLACEMENT_ENTRY("maskcontentunits", "maskContentUnits"),
  194. REPLACEMENT_ENTRY("maskunits", "maskUnits"),
  195. REPLACEMENT_ENTRY("numoctaves", "numOctaves"),
  196. REPLACEMENT_ENTRY("pathlength", "pathLength"),
  197. REPLACEMENT_ENTRY("patterncontentunits", "patternContentUnits"),
  198. REPLACEMENT_ENTRY("patterntransform", "patternTransform"),
  199. REPLACEMENT_ENTRY("patternunits", "patternUnits"),
  200. REPLACEMENT_ENTRY("pointsatx", "pointsAtX"),
  201. REPLACEMENT_ENTRY("pointsaty", "pointsAtY"),
  202. REPLACEMENT_ENTRY("pointsatz", "pointsAtZ"),
  203. REPLACEMENT_ENTRY("preservealpha", "preserveAlpha"),
  204. REPLACEMENT_ENTRY("preserveaspectratio", "preserveAspectRatio"),
  205. REPLACEMENT_ENTRY("primitiveunits", "primitiveUnits"),
  206. REPLACEMENT_ENTRY("refx", "refX"),
  207. REPLACEMENT_ENTRY("refy", "refY"),
  208. REPLACEMENT_ENTRY("repeatcount", "repeatCount"),
  209. REPLACEMENT_ENTRY("repeatdur", "repeatDur"),
  210. REPLACEMENT_ENTRY("requiredextensions", "requiredExtensions"),
  211. REPLACEMENT_ENTRY("requiredfeatures", "requiredFeatures"),
  212. REPLACEMENT_ENTRY("specularconstant", "specularConstant"),
  213. REPLACEMENT_ENTRY("specularexponent", "specularExponent"),
  214. REPLACEMENT_ENTRY("spreadmethod", "spreadMethod"),
  215. REPLACEMENT_ENTRY("startoffset", "startOffset"),
  216. REPLACEMENT_ENTRY("stddeviation", "stdDeviation"),
  217. REPLACEMENT_ENTRY("stitchtiles", "stitchTiles"),
  218. REPLACEMENT_ENTRY("surfacescale", "surfaceScale"),
  219. REPLACEMENT_ENTRY("systemlanguage", "systemLanguage"),
  220. REPLACEMENT_ENTRY("tablevalues", "tableValues"),
  221. REPLACEMENT_ENTRY("targetx", "targetX"),
  222. REPLACEMENT_ENTRY("targety", "targetY"),
  223. REPLACEMENT_ENTRY("textlength", "textLength"),
  224. REPLACEMENT_ENTRY("viewbox", "viewBox"),
  225. REPLACEMENT_ENTRY("viewtarget", "viewTarget"),
  226. REPLACEMENT_ENTRY("xchannelselector", "xChannelSelector"),
  227. REPLACEMENT_ENTRY("ychannelselector", "yChannelSelector"),
  228. REPLACEMENT_ENTRY("zoomandpan", "zoomAndPan"),
  229. };
  230. static const ReplacementEntry kSvgTagReplacements[] = {
  231. REPLACEMENT_ENTRY("altglyph", "altGlyph"),
  232. REPLACEMENT_ENTRY("altglyphdef", "altGlyphDef"),
  233. REPLACEMENT_ENTRY("altglyphitem", "altGlyphItem"),
  234. REPLACEMENT_ENTRY("animatecolor", "animateColor"),
  235. REPLACEMENT_ENTRY("animatemotion", "animateMotion"),
  236. REPLACEMENT_ENTRY("animatetransform", "animateTransform"),
  237. REPLACEMENT_ENTRY("clippath", "clipPath"),
  238. REPLACEMENT_ENTRY("feblend", "feBlend"),
  239. REPLACEMENT_ENTRY("fecolormatrix", "feColorMatrix"),
  240. REPLACEMENT_ENTRY("fecomponenttransfer", "feComponentTransfer"),
  241. REPLACEMENT_ENTRY("fecomposite", "feComposite"),
  242. REPLACEMENT_ENTRY("feconvolvematrix", "feConvolveMatrix"),
  243. REPLACEMENT_ENTRY("fediffuselighting", "feDiffuseLighting"),
  244. REPLACEMENT_ENTRY("fedisplacementmap", "feDisplacementMap"),
  245. REPLACEMENT_ENTRY("fedistantlight", "feDistantLight"),
  246. REPLACEMENT_ENTRY("feflood", "feFlood"),
  247. REPLACEMENT_ENTRY("fefunca", "feFuncA"),
  248. REPLACEMENT_ENTRY("fefuncb", "feFuncB"),
  249. REPLACEMENT_ENTRY("fefuncg", "feFuncG"),
  250. REPLACEMENT_ENTRY("fefuncr", "feFuncR"),
  251. REPLACEMENT_ENTRY("fegaussianblur", "feGaussianBlur"),
  252. REPLACEMENT_ENTRY("feimage", "feImage"),
  253. REPLACEMENT_ENTRY("femerge", "feMerge"),
  254. REPLACEMENT_ENTRY("femergenode", "feMergeNode"),
  255. REPLACEMENT_ENTRY("femorphology", "feMorphology"),
  256. REPLACEMENT_ENTRY("feoffset", "feOffset"),
  257. REPLACEMENT_ENTRY("fepointlight", "fePointLight"),
  258. REPLACEMENT_ENTRY("fespecularlighting", "feSpecularLighting"),
  259. REPLACEMENT_ENTRY("fespotlight", "feSpotLight"),
  260. REPLACEMENT_ENTRY("fetile", "feTile"),
  261. REPLACEMENT_ENTRY("feturbulence", "feTurbulence"),
  262. REPLACEMENT_ENTRY("foreignobject", "foreignObject"),
  263. REPLACEMENT_ENTRY("glyphref", "glyphRef"),
  264. REPLACEMENT_ENTRY("lineargradient", "linearGradient"),
  265. REPLACEMENT_ENTRY("radialgradient", "radialGradient"),
  266. REPLACEMENT_ENTRY("textpath", "textPath"),
  267. };
  268. typedef struct _NamespacedAttributeReplacement {
  269. const char* from;
  270. const char* local_name;
  271. const GumboAttributeNamespaceEnum attr_namespace;
  272. } NamespacedAttributeReplacement;
  273. static const NamespacedAttributeReplacement kForeignAttributeReplacements[] = {
  274. { "xlink:actuate", "actuate", GUMBO_ATTR_NAMESPACE_XLINK },
  275. { "xlink:actuate", "actuate", GUMBO_ATTR_NAMESPACE_XLINK },
  276. { "xlink:href", "href", GUMBO_ATTR_NAMESPACE_XLINK },
  277. { "xlink:role", "role", GUMBO_ATTR_NAMESPACE_XLINK },
  278. { "xlink:show", "show", GUMBO_ATTR_NAMESPACE_XLINK },
  279. { "xlink:title", "title", GUMBO_ATTR_NAMESPACE_XLINK },
  280. { "xlink:type", "type", GUMBO_ATTR_NAMESPACE_XLINK },
  281. { "xml:base", "base", GUMBO_ATTR_NAMESPACE_XML },
  282. { "xml:lang", "lang", GUMBO_ATTR_NAMESPACE_XML },
  283. { "xml:space", "space", GUMBO_ATTR_NAMESPACE_XML },
  284. { "xmlns", "xmlns", GUMBO_ATTR_NAMESPACE_XMLNS },
  285. { "xmlns:xlink", "xlink", GUMBO_ATTR_NAMESPACE_XMLNS },
  286. };
  287. // The "scope marker" for the list of active formatting elements. We use a
  288. // pointer to this as a generic marker element, since the particular element
  289. // scope doesn't matter.
  290. static const GumboNode kActiveFormattingScopeMarker;
  291. // The tag_is and tag_in function use true & false to denote start & end tags,
  292. // but for readability, we define constants for them here.
  293. static const bool kStartTag = true;
  294. static const bool kEndTag = false;
  295. // Because GumboStringPieces are immutable, we can't insert a character directly
  296. // into a text node. Instead, we accumulate all pending characters here and
  297. // flush them out to a text node whenever a new element is inserted.
  298. //
  299. // http://www.whatwg.org/specs/web-apps/current-work/complete/tokenization.html#insert-a-character
  300. typedef struct _TextNodeBufferState {
  301. // The accumulated text to be inserted into the current text node.
  302. GumboStringBuffer _buffer;
  303. // A pointer to the original text represented by this text node. Note that
  304. // because of foster parenting and other strange DOM manipulations, this may
  305. // include other non-text HTML tags in it; it is defined as the span of
  306. // original text from the first character in this text node to the last
  307. // character in this text node.
  308. const char* _start_original_text;
  309. // The source position of the start of this text node.
  310. GumboSourcePosition _start_position;
  311. // The type of node that will be inserted (TEXT or WHITESPACE).
  312. GumboNodeType _type;
  313. } TextNodeBufferState;
  314. typedef struct GumboInternalParserState {
  315. // http://www.whatwg.org/specs/web-apps/current-work/complete/parsing.html#insertion-mode
  316. GumboInsertionMode _insertion_mode;
  317. // Used for run_generic_parsing_algorithm, which needs to switch back to the
  318. // original insertion mode at its conclusion.
  319. GumboInsertionMode _original_insertion_mode;
  320. // http://www.whatwg.org/specs/web-apps/current-work/complete/parsing.html#the-stack-of-open-elements
  321. GumboVector /*GumboNode*/ _open_elements;
  322. // http://www.whatwg.org/specs/web-apps/current-work/complete/parsing.html#the-list-of-active-formatting-elements
  323. GumboVector /*GumboNode*/ _active_formatting_elements;
  324. // The stack of template insertion modes.
  325. // http://www.whatwg.org/specs/web-apps/current-work/multipage/parsing.html#the-insertion-mode
  326. GumboVector /*InsertionMode*/ _template_insertion_modes;
  327. // http://www.whatwg.org/specs/web-apps/current-work/complete/parsing.html#the-element-pointers
  328. GumboNode* _head_element;
  329. GumboNode* _form_element;
  330. // The flag for when the spec says "Reprocess the current token in..."
  331. bool _reprocess_current_token;
  332. // The flag for "acknowledge the token's self-closing flag".
  333. bool _self_closing_flag_acknowledged;
  334. // The "frameset-ok" flag from the spec.
  335. bool _frameset_ok;
  336. // The flag for "If the next token is a LINE FEED, ignore that token...".
  337. bool _ignore_next_linefeed;
  338. // The flag for "whenever a node would be inserted into the current node, it
  339. // must instead be foster parented". This is used for misnested table
  340. // content, which needs to be handled according to "in body" rules yet foster
  341. // parented outside of the table.
  342. // It would perhaps be more explicit to have this as a parameter to
  343. // handle_in_body and insert_element, but given how special-purpose this is
  344. // and the number of call-sites that would need to take the extra parameter,
  345. // it's easier just to have a state flag.
  346. bool _foster_parent_insertions;
  347. // The accumulated text node buffer state.
  348. TextNodeBufferState _text_node;
  349. // The current token.
  350. GumboToken* _current_token;
  351. // The way that the spec is written, the </body> and </html> tags are *always*
  352. // implicit, because encountering one of those tokens merely switches the
  353. // insertion mode out of "in body". So we have individual state flags for
  354. // those end tags that are then inspected by pop_current_node when the <body>
  355. // and <html> nodes are popped to set the GUMBO_INSERTION_IMPLICIT_END_TAG
  356. // flag appropriately.
  357. bool _closed_body_tag;
  358. bool _closed_html_tag;
  359. } GumboParserState;
  360. static bool token_has_attribute(const GumboToken* token, const char* name) {
  361. assert(token->type == GUMBO_TOKEN_START_TAG);
  362. return gumbo_get_attribute(&token->v.start_tag.attributes, name) != NULL;
  363. }
  364. // Checks if the value of the specified attribute is a case-insensitive match
  365. // for the specified string.
  366. static bool attribute_matches(
  367. const GumboVector* attributes, const char* name, const char* value) {
  368. const GumboAttribute* attr = gumbo_get_attribute(attributes, name);
  369. return attr ? strcasecmp(value, attr->value) == 0 : false;
  370. }
  371. // Checks if the value of the specified attribute is a case-sensitive match
  372. // for the specified string.
  373. static bool attribute_matches_case_sensitive(
  374. const GumboVector* attributes, const char* name, const char* value) {
  375. const GumboAttribute* attr = gumbo_get_attribute(attributes, name);
  376. return attr ? strcmp(value, attr->value) == 0 : false;
  377. }
  378. // Checks if the specified attribute vectors are identical.
  379. static bool all_attributes_match(
  380. const GumboVector* attr1, const GumboVector* attr2) {
  381. int num_unmatched_attr2_elements = attr2->length;
  382. for (int i = 0; i < attr1->length; ++i) {
  383. const GumboAttribute* attr = attr1->data[i];
  384. if (attribute_matches_case_sensitive(attr2, attr->name, attr->value)) {
  385. --num_unmatched_attr2_elements;
  386. } else {
  387. return false;
  388. }
  389. }
  390. return num_unmatched_attr2_elements == 0;
  391. }
  392. static void set_frameset_not_ok(GumboParser* parser) {
  393. gumbo_debug("Setting frameset_ok to false.\n");
  394. parser->_parser_state->_frameset_ok = false;
  395. }
  396. static GumboNode* create_node(GumboParser* parser, GumboNodeType type) {
  397. GumboNode* node = gumbo_parser_allocate(parser, sizeof(GumboNode));
  398. node->parent = NULL;
  399. node->index_within_parent = -1;
  400. node->type = type;
  401. node->parse_flags = GUMBO_INSERTION_NORMAL;
  402. return node;
  403. }
  404. static GumboNode* new_document_node(GumboParser* parser) {
  405. GumboNode* document_node = create_node(parser, GUMBO_NODE_DOCUMENT);
  406. document_node->parse_flags = GUMBO_INSERTION_BY_PARSER;
  407. gumbo_vector_init(
  408. parser, 1, &document_node->v.document.children);
  409. // Must be initialized explicitly, as there's no guarantee that we'll see a
  410. // doc type token.
  411. GumboDocument* document = &document_node->v.document;
  412. document->has_doctype = false;
  413. document->name = NULL;
  414. document->public_identifier = NULL;
  415. document->system_identifier = NULL;
  416. return document_node;
  417. }
  418. static void output_init(GumboParser* parser) {
  419. GumboOutput* output = gumbo_parser_allocate(parser, sizeof(GumboOutput));
  420. output->root = NULL;
  421. output->document = new_document_node(parser);
  422. parser->_output = output;
  423. gumbo_init_errors(parser);
  424. }
  425. static void parser_state_init(GumboParser* parser) {
  426. GumboParserState* parser_state =
  427. gumbo_parser_allocate(parser, sizeof(GumboParserState));
  428. parser_state->_insertion_mode = GUMBO_INSERTION_MODE_INITIAL;
  429. parser_state->_reprocess_current_token = false;
  430. parser_state->_frameset_ok = true;
  431. parser_state->_ignore_next_linefeed = false;
  432. parser_state->_foster_parent_insertions = false;
  433. parser_state->_text_node._type = GUMBO_NODE_WHITESPACE;
  434. gumbo_string_buffer_init(parser, &parser_state->_text_node._buffer);
  435. gumbo_vector_init(parser, 10, &parser_state->_open_elements);
  436. gumbo_vector_init(parser, 5, &parser_state->_active_formatting_elements);
  437. gumbo_vector_init(parser, 5, &parser_state->_template_insertion_modes);
  438. parser_state->_head_element = NULL;
  439. parser_state->_form_element = NULL;
  440. parser_state->_current_token = NULL;
  441. parser_state->_closed_body_tag = false;
  442. parser_state->_closed_html_tag = false;
  443. parser->_parser_state = parser_state;
  444. }
  445. static void parser_state_destroy(GumboParser* parser) {
  446. GumboParserState* state = parser->_parser_state;
  447. gumbo_vector_destroy(parser, &state->_active_formatting_elements);
  448. gumbo_vector_destroy(parser, &state->_open_elements);
  449. gumbo_vector_destroy(parser, &state->_template_insertion_modes);
  450. gumbo_string_buffer_destroy(parser, &state->_text_node._buffer);
  451. gumbo_parser_deallocate(parser, state);
  452. }
  453. static GumboNode* get_document_node(GumboParser* parser) {
  454. return parser->_output->document;
  455. }
  456. // Returns the node at the bottom of the stack of open elements, or NULL if no
  457. // elements have been added yet.
  458. static GumboNode* get_current_node(GumboParser* parser) {
  459. GumboVector* open_elements = &parser->_parser_state->_open_elements;
  460. if (open_elements->length == 0) {
  461. assert(!parser->_output->root);
  462. return NULL;
  463. }
  464. assert(open_elements->length > 0);
  465. assert(open_elements->data != NULL);
  466. return open_elements->data[open_elements->length - 1];
  467. }
  468. // Returns true if the given needle is in the given array of literal
  469. // GumboStringPieces. If exact_match is true, this requires that they match
  470. // exactly; otherwise, this performs a prefix match to check if any of the
  471. // elements in haystack start with needle. This always performs a
  472. // case-insensitive match.
  473. static bool is_in_static_list(
  474. const char* needle, const GumboStringPiece* haystack, bool exact_match) {
  475. for (int i = 0; haystack[i].length > 0; ++i) {
  476. if ((exact_match && !strcmp(needle, haystack[i].data)) ||
  477. (!exact_match && !strcasecmp(needle, haystack[i].data))) {
  478. return true;
  479. }
  480. }
  481. return false;
  482. }
  483. static void set_insertion_mode(GumboParser* parser, GumboInsertionMode mode) {
  484. parser->_parser_state->_insertion_mode = mode;
  485. }
  486. // http://www.whatwg.org/specs/web-apps/current-work/complete/parsing.html#reset-the-insertion-mode-appropriately
  487. // This is a helper function that returns the appropriate insertion mode instead
  488. // of setting it. Returns GUMBO_INSERTION_MODE_INITIAL as a sentinel value to
  489. // indicate that there is no appropriate insertion mode, and the loop should
  490. // continue.
  491. static GumboInsertionMode get_appropriate_insertion_mode(
  492. const GumboNode* node, bool is_last) {
  493. assert(node->type == GUMBO_NODE_ELEMENT);
  494. switch (node->v.element.tag) {
  495. case GUMBO_TAG_SELECT:
  496. return GUMBO_INSERTION_MODE_IN_SELECT;
  497. case GUMBO_TAG_TD:
  498. case GUMBO_TAG_TH:
  499. return is_last ?
  500. GUMBO_INSERTION_MODE_IN_BODY : GUMBO_INSERTION_MODE_IN_CELL;
  501. case GUMBO_TAG_TR:
  502. return GUMBO_INSERTION_MODE_IN_ROW;
  503. case GUMBO_TAG_TBODY:
  504. case GUMBO_TAG_THEAD:
  505. case GUMBO_TAG_TFOOT:
  506. return GUMBO_INSERTION_MODE_IN_TABLE_BODY;
  507. case GUMBO_TAG_CAPTION:
  508. return GUMBO_INSERTION_MODE_IN_CAPTION;
  509. case GUMBO_TAG_COLGROUP:
  510. return GUMBO_INSERTION_MODE_IN_COLUMN_GROUP;
  511. case GUMBO_TAG_TABLE:
  512. return GUMBO_INSERTION_MODE_IN_TABLE;
  513. case GUMBO_TAG_HEAD:
  514. case GUMBO_TAG_BODY:
  515. return GUMBO_INSERTION_MODE_IN_BODY;
  516. case GUMBO_TAG_FRAMESET:
  517. return GUMBO_INSERTION_MODE_IN_FRAMESET;
  518. case GUMBO_TAG_HTML:
  519. return GUMBO_INSERTION_MODE_BEFORE_HEAD;
  520. default:
  521. return is_last ?
  522. GUMBO_INSERTION_MODE_IN_BODY : GUMBO_INSERTION_MODE_INITIAL;
  523. }
  524. }
  525. // This performs the actual "reset the insertion mode" loop.
  526. static void reset_insertion_mode_appropriately(GumboParser* parser) {
  527. const GumboVector* open_elements = &parser->_parser_state->_open_elements;
  528. for (int i = open_elements->length; --i >= 0; ) {
  529. GumboInsertionMode mode =
  530. get_appropriate_insertion_mode(open_elements->data[i], i == 0);
  531. if (mode != GUMBO_INSERTION_MODE_INITIAL) {
  532. set_insertion_mode(parser, mode);
  533. return;
  534. }
  535. }
  536. // Should never get here, because is_last will be set on the last iteration
  537. // and will force GUMBO_INSERTION_MODE_IN_BODY.
  538. assert(0);
  539. }
  540. static GumboError* add_parse_error(GumboParser* parser, const GumboToken* token) {
  541. gumbo_debug("Adding parse error.\n");
  542. GumboError* error = gumbo_add_error(parser);
  543. if (!error) {
  544. return NULL;
  545. }
  546. error->type = GUMBO_ERR_PARSER;
  547. error->position = token->position;
  548. error->original_text = token->original_text.data;
  549. GumboParserError* extra_data = &error->v.parser;
  550. extra_data->input_type = token->type;
  551. extra_data->input_tag = GUMBO_TAG_UNKNOWN;
  552. if (token->type == GUMBO_TOKEN_START_TAG) {
  553. extra_data->input_tag = token->v.start_tag.tag;
  554. } else if (token->type == GUMBO_TOKEN_END_TAG) {
  555. extra_data->input_tag = token->v.end_tag;
  556. }
  557. GumboParserState* state = parser->_parser_state;
  558. extra_data->parser_state = state->_insertion_mode;
  559. gumbo_vector_init(parser, state->_open_elements.length,
  560. &extra_data->tag_stack);
  561. for (int i = 0; i < state->_open_elements.length; ++i) {
  562. const GumboNode* node = state->_open_elements.data[i];
  563. assert(node->type == GUMBO_NODE_ELEMENT);
  564. gumbo_vector_add(parser, (void*) node->v.element.tag,
  565. &extra_data->tag_stack);
  566. }
  567. return error;
  568. }
  569. // Returns true if the specified token is either a start or end tag (specified
  570. // by is_start) with one of the tag types in the varargs list. Terminate the
  571. // list with GUMBO_TAG_LAST; this functions as a sentinel since no portion of
  572. // the spec references tags that are not in the spec.
  573. // TODO(jdtang): A lot of the tag lists for this function are repeated in many
  574. // places in the code. This is how it's written in the spec (and it's done this
  575. // way so it's easy to verify the code against the spec), but it may be worth
  576. // coming up with a notion of a "tag set" that includes a list of tags, and
  577. // using that in many places. It'd probably also help performance, but I want
  578. // to profile before optimizing.
  579. static bool tag_in(const GumboToken* token, bool is_start, ...) {
  580. GumboTag token_tag;
  581. if (is_start && token->type == GUMBO_TOKEN_START_TAG) {
  582. token_tag = token->v.start_tag.tag;
  583. } else if (!is_start && token->type == GUMBO_TOKEN_END_TAG) {
  584. token_tag = token->v.end_tag;
  585. } else {
  586. return false;
  587. }
  588. va_list tags;
  589. va_start(tags, is_start);
  590. bool result = false;
  591. for (GumboTag tag = va_arg(tags, GumboTag); tag != GUMBO_TAG_LAST;
  592. tag = va_arg(tags, GumboTag)) {
  593. if (tag == token_tag) {
  594. result = true;
  595. break;
  596. }
  597. }
  598. va_end(tags);
  599. return result;
  600. }
  601. // Like tag_in, but for the single-tag case.
  602. static bool tag_is(const GumboToken* token, bool is_start, GumboTag tag) {
  603. if (is_start && token->type == GUMBO_TOKEN_START_TAG) {
  604. return token->v.start_tag.tag == tag;
  605. } else if (!is_start && token->type == GUMBO_TOKEN_END_TAG) {
  606. return token->v.end_tag == tag;
  607. } else {
  608. return false;
  609. }
  610. }
  611. // Like tag_in, but checks for the tag of a node, rather than a token.
  612. static bool node_tag_in(const GumboNode* node, ...) {
  613. assert(node != NULL);
  614. if (node->type != GUMBO_NODE_ELEMENT) {
  615. return false;
  616. }
  617. GumboTag node_tag = node->v.element.tag;
  618. va_list tags;
  619. va_start(tags, node);
  620. bool result = false;
  621. for (GumboTag tag = va_arg(tags, GumboTag); tag != GUMBO_TAG_LAST;
  622. tag = va_arg(tags, GumboTag)) {
  623. assert(tag <= GUMBO_TAG_LAST);
  624. if (tag == node_tag) {
  625. result = true;
  626. break;
  627. }
  628. }
  629. va_end(tags);
  630. return result;
  631. }
  632. // Like node_tag_in, but for the single-tag case.
  633. static bool node_tag_is(const GumboNode* node, GumboTag tag) {
  634. return node->type == GUMBO_NODE_ELEMENT && node->v.element.tag == tag;
  635. }
  636. // http://www.whatwg.org/specs/web-apps/current-work/multipage/tree-construction.html#mathml-text-integration-point
  637. static bool is_mathml_integration_point(const GumboNode* node) {
  638. return node_tag_in(node, GUMBO_TAG_MI, GUMBO_TAG_MO, GUMBO_TAG_MN,
  639. GUMBO_TAG_MS, GUMBO_TAG_MTEXT, GUMBO_TAG_LAST) &&
  640. node->v.element.tag_namespace == GUMBO_NAMESPACE_MATHML;
  641. }
  642. // http://www.whatwg.org/specs/web-apps/current-work/multipage/tree-construction.html#html-integration-point
  643. static bool is_html_integration_point(const GumboNode* node) {
  644. return (node_tag_in(node, GUMBO_TAG_FOREIGNOBJECT, GUMBO_TAG_DESC,
  645. GUMBO_TAG_TITLE, GUMBO_TAG_LAST) &&
  646. node->v.element.tag_namespace == GUMBO_NAMESPACE_SVG) ||
  647. (node_tag_is(node, GUMBO_TAG_ANNOTATION_XML) && (
  648. attribute_matches(&node->v.element.attributes,
  649. "encoding", "text/html") ||
  650. attribute_matches(&node->v.element.attributes,
  651. "encoding", "application/xhtml+xml")));
  652. }
  653. // Appends a node to the end of its parent, setting the "parent" and
  654. // "index_within_parent" fields appropriately.
  655. static void append_node(
  656. GumboParser* parser, GumboNode* parent, GumboNode* node) {
  657. assert(node->parent == NULL);
  658. assert(node->index_within_parent == -1);
  659. GumboVector* children;
  660. if (parent->type == GUMBO_NODE_ELEMENT) {
  661. children = &parent->v.element.children;
  662. } else {
  663. assert(parent->type == GUMBO_NODE_DOCUMENT);
  664. children = &parent->v.document.children;
  665. }
  666. node->parent = parent;
  667. node->index_within_parent = children->length;
  668. gumbo_vector_add(parser, (void*) node, children);
  669. assert(node->index_within_parent < children->length);
  670. }
  671. // Inserts a node at the specified index within its parent, updating the
  672. // "parent" and "index_within_parent" fields of it and all its siblings.
  673. static void insert_node(
  674. GumboParser* parser, GumboNode* parent, int index, GumboNode* node) {
  675. assert(node->parent == NULL);
  676. assert(node->index_within_parent == -1);
  677. assert(parent->type == GUMBO_NODE_ELEMENT);
  678. GumboVector* children = &parent->v.element.children;
  679. assert(index >= 0);
  680. assert(index < children->length);
  681. node->parent = parent;
  682. node->index_within_parent = index;
  683. gumbo_vector_insert_at(parser, (void*) node, index, children);
  684. assert(node->index_within_parent < children->length);
  685. for (int i = index + 1; i < children->length; ++i) {
  686. GumboNode* sibling = children->data[i];
  687. sibling->index_within_parent = i;
  688. assert(sibling->index_within_parent < children->length);
  689. }
  690. }
  691. // http://www.whatwg.org/specs/web-apps/current-work/complete/tokenization.html#foster-parenting
  692. static void foster_parent_element(GumboParser* parser, GumboNode* node) {
  693. GumboVector* open_elements = &parser->_parser_state->_open_elements;
  694. assert(open_elements->length > 2);
  695. node->parse_flags |= GUMBO_INSERTION_FOSTER_PARENTED;
  696. GumboNode* foster_parent_element = open_elements->data[0];
  697. assert(foster_parent_element->type == GUMBO_NODE_ELEMENT);
  698. assert(node_tag_is(foster_parent_element, GUMBO_TAG_HTML));
  699. for (int i = open_elements->length; --i > 1; ) {
  700. GumboNode* table_element = open_elements->data[i];
  701. if (node_tag_is(table_element, GUMBO_TAG_TABLE)) {
  702. foster_parent_element = table_element->parent;
  703. if (!foster_parent_element ||
  704. foster_parent_element->type != GUMBO_NODE_ELEMENT) {
  705. // Table has no parent; spec says it's possible if a script manipulated
  706. // the DOM, although I don't think we have to worry about this case.
  707. gumbo_debug("Table has no parent.\n");
  708. foster_parent_element = open_elements->data[i - 1];
  709. break;
  710. }
  711. assert(foster_parent_element->type == GUMBO_NODE_ELEMENT);
  712. gumbo_debug("Found enclosing table (%x) at %d; parent=%s, index=%d.\n",
  713. table_element, i, gumbo_normalized_tagname(
  714. foster_parent_element->v.element.tag),
  715. table_element->index_within_parent);
  716. assert(foster_parent_element->v.element.children.data[
  717. table_element->index_within_parent] == table_element);
  718. insert_node(parser, foster_parent_element,
  719. table_element->index_within_parent, node);
  720. return;
  721. }
  722. }
  723. if (node->type == GUMBO_NODE_ELEMENT) {
  724. gumbo_vector_add(parser, (void*) node, open_elements);
  725. }
  726. append_node(parser, foster_parent_element, node);
  727. }
  728. static void maybe_flush_text_node_buffer(GumboParser* parser) {
  729. GumboParserState* state = parser->_parser_state;
  730. TextNodeBufferState* buffer_state = &state->_text_node;
  731. if (buffer_state->_buffer.length == 0) {
  732. return;
  733. }
  734. assert(buffer_state->_type == GUMBO_NODE_WHITESPACE ||
  735. buffer_state->_type == GUMBO_NODE_TEXT);
  736. GumboNode* text_node = create_node(parser, buffer_state->_type);
  737. GumboText* text_node_data = &text_node->v.text;
  738. text_node_data->text = gumbo_string_buffer_to_string(
  739. parser, &buffer_state->_buffer);
  740. text_node_data->original_text.data = buffer_state->_start_original_text;
  741. text_node_data->original_text.length =
  742. state->_current_token->original_text.data -
  743. buffer_state->_start_original_text;
  744. text_node_data->start_pos = buffer_state->_start_position;
  745. if (state->_foster_parent_insertions && node_tag_in(
  746. get_current_node(parser), GUMBO_TAG_TABLE, GUMBO_TAG_TBODY,
  747. GUMBO_TAG_TFOOT, GUMBO_TAG_THEAD, GUMBO_TAG_TR, GUMBO_TAG_LAST)) {
  748. foster_parent_element(parser, text_node);
  749. } else {
  750. append_node(
  751. parser, parser->_output->root ?
  752. get_current_node(parser) : parser->_output->document, text_node);
  753. }
  754. gumbo_debug("Flushing text node buffer of %.*s.\n",
  755. (int) buffer_state->_buffer.length, buffer_state->_buffer.data);
  756. gumbo_string_buffer_destroy(parser, &buffer_state->_buffer);
  757. gumbo_string_buffer_init(parser, &buffer_state->_buffer);
  758. buffer_state->_type = GUMBO_NODE_WHITESPACE;
  759. assert(buffer_state->_buffer.length == 0);
  760. }
  761. static void record_end_of_element(
  762. GumboToken* current_token, GumboElement* element) {
  763. element->end_pos = current_token->position;
  764. element->original_end_tag =
  765. current_token->type == GUMBO_TOKEN_END_TAG ?
  766. current_token->original_text : kGumboEmptyString;
  767. }
  768. static GumboNode* pop_current_node(GumboParser* parser) {
  769. GumboParserState* state = parser->_parser_state;
  770. maybe_flush_text_node_buffer(parser);
  771. if (state->_open_elements.length > 0) {
  772. assert(node_tag_is(state->_open_elements.data[0], GUMBO_TAG_HTML));
  773. gumbo_debug(
  774. "Popping %s node.\n",
  775. gumbo_normalized_tagname(get_current_node(parser)->v.element.tag));
  776. }
  777. GumboNode* current_node = gumbo_vector_pop(parser, &state->_open_elements);
  778. if (!current_node) {
  779. assert(state->_open_elements.length == 0);
  780. return NULL;
  781. }
  782. assert(current_node->type == GUMBO_NODE_ELEMENT);
  783. bool is_closed_body_or_html_tag =
  784. (node_tag_is(current_node, GUMBO_TAG_BODY) && state->_closed_body_tag) ||
  785. (node_tag_is(current_node, GUMBO_TAG_HTML) && state->_closed_html_tag);
  786. if ((state->_current_token->type != GUMBO_TOKEN_END_TAG ||
  787. !node_tag_is(current_node, state->_current_token->v.end_tag)) &&
  788. !is_closed_body_or_html_tag) {
  789. current_node->parse_flags |= GUMBO_INSERTION_IMPLICIT_END_TAG;
  790. }
  791. if (!is_closed_body_or_html_tag) {
  792. record_end_of_element(state->_current_token, &current_node->v.element);
  793. }
  794. return current_node;
  795. }
  796. static void append_comment_node(
  797. GumboParser* parser, GumboNode* node, const GumboToken* token) {
  798. maybe_flush_text_node_buffer(parser);
  799. GumboNode* comment = create_node(parser, GUMBO_NODE_COMMENT);
  800. comment->type = GUMBO_NODE_COMMENT;
  801. comment->parse_flags = GUMBO_INSERTION_NORMAL;
  802. comment->v.text.text = token->v.text;
  803. comment->v.text.original_text = token->original_text;
  804. comment->v.text.start_pos = token->position;
  805. append_node(parser, node, comment);
  806. }
  807. // http://www.whatwg.org/specs/web-apps/current-work/complete/tokenization.html#clear-the-stack-back-to-a-table-row-context
  808. static void clear_stack_to_table_row_context(GumboParser* parser) {
  809. while (!node_tag_in(get_current_node(parser),
  810. GUMBO_TAG_HTML, GUMBO_TAG_TR, GUMBO_TAG_LAST)) {
  811. pop_current_node(parser);
  812. }
  813. }
  814. // http://www.whatwg.org/specs/web-apps/current-work/complete/tokenization.html#clear-the-stack-back-to-a-table-context
  815. static void clear_stack_to_table_context(GumboParser* parser) {
  816. while (!node_tag_in(get_current_node(parser),
  817. GUMBO_TAG_HTML, GUMBO_TAG_TABLE, GUMBO_TAG_LAST)) {
  818. pop_current_node(parser);
  819. }
  820. }
  821. // http://www.whatwg.org/specs/web-apps/current-work/complete/tokenization.html#clear-the-stack-back-to-a-table-body-context
  822. void clear_stack_to_table_body_context(GumboParser* parser) {
  823. while (!node_tag_in(get_current_node(parser), GUMBO_TAG_HTML,
  824. GUMBO_TAG_TBODY, GUMBO_TAG_TFOOT, GUMBO_TAG_THEAD,
  825. GUMBO_TAG_LAST)) {
  826. pop_current_node(parser);
  827. }
  828. }
  829. // Creates a parser-inserted element in the HTML namespace and returns it.
  830. static GumboNode* create_element(GumboParser* parser, GumboTag tag) {
  831. GumboNode* node = create_node(parser, GUMBO_NODE_ELEMENT);
  832. GumboElement* element = &node->v.element;
  833. gumbo_vector_init(parser, 1, &element->children);
  834. gumbo_vector_init(parser, 0, &element->attributes);
  835. element->tag = tag;
  836. element->tag_namespace = GUMBO_NAMESPACE_HTML;
  837. element->original_tag = kGumboEmptyString;
  838. element->original_end_tag = kGumboEmptyString;
  839. element->start_pos = parser->_parser_state->_current_token->position;
  840. element->end_pos = kGumboEmptySourcePosition;
  841. return node;
  842. }
  843. // Constructs an element from the given start tag token.
  844. static GumboNode* create_element_from_token(
  845. GumboParser* parser, GumboToken* token, GumboNamespaceEnum tag_namespace) {
  846. assert(token->type == GUMBO_TOKEN_START_TAG);
  847. GumboTokenStartTag* start_tag = &token->v.start_tag;
  848. GumboNode* node = create_node(parser, GUMBO_NODE_ELEMENT);
  849. GumboElement* element = &node->v.element;
  850. gumbo_vector_init(parser, 1, &element->children);
  851. element->attributes = start_tag->attributes;
  852. element->tag = start_tag->tag;
  853. element->tag_namespace = tag_namespace;
  854. assert(token->original_text.length >= 2);
  855. assert(token->original_text.data[0] == '<');
  856. assert(token->original_text.data[token->original_text.length - 1] == '>');
  857. element->original_tag = token->original_text;
  858. element->start_pos = token->position;
  859. element->original_end_tag = kGumboEmptyString;
  860. element->end_pos = kGumboEmptySourcePosition;
  861. // The element takes ownership of the attributes from the token, so any
  862. // allocated-memory fields should be nulled out.
  863. start_tag->attributes = kGumboEmptyVector;
  864. return node;
  865. }
  866. // http://www.whatwg.org/specs/web-apps/current-work/complete/tokenization.html#insert-an-html-element
  867. static void insert_element(GumboParser* parser, GumboNode* node,
  868. bool is_reconstructing_formatting_elements) {
  869. GumboParserState* state = parser->_parser_state;
  870. // NOTE(jdtang): The text node buffer must always be flushed before inserting
  871. // a node, otherwise we're handling nodes in a different order than the spec
  872. // mandated. However, one clause of the spec (character tokens in the body)
  873. // requires that we reconstruct the active formatting elements *before* adding
  874. // the character, and reconstructing the active formatting elements may itself
  875. // result in the insertion of new elements (which should be pushed onto the
  876. // stack of open elements before the buffer is flushed). We solve this (for
  877. // the time being, the spec has been rewritten for <template> and the new
  878. // version may be simpler here) with a boolean flag to this method.
  879. if (!is_reconstructing_formatting_elements) {
  880. maybe_flush_text_node_buffer(parser);
  881. }
  882. if (state->_foster_parent_insertions && node_tag_in(
  883. get_current_node(parser), GUMBO_TAG_TABLE, GUMBO_TAG_TBODY,
  884. GUMBO_TAG_TFOOT, GUMBO_TAG_THEAD, GUMBO_TAG_TR, GUMBO_TAG_LAST)) {
  885. foster_parent_element(parser, node);
  886. gumbo_vector_add(parser, (void*) node, &state->_open_elements);
  887. return;
  888. }
  889. // This is called to insert the root HTML element, but get_current_node
  890. // assumes the stack of open elements is non-empty, so we need special
  891. // handling for this case.
  892. append_node(
  893. parser, parser->_output->root ?
  894. get_current_node(parser) : parser->_output->document, node);
  895. gumbo_vector_add(parser, (void*) node, &state->_open_elements);
  896. }
  897. // Convenience method that combines create_element_from_token and
  898. // insert_element, inserting the generated element directly into the current
  899. // node. Returns the node inserted.
  900. static GumboNode* insert_element_from_token(
  901. GumboParser* parser, GumboToken* token) {
  902. GumboNode* element =
  903. create_element_from_token(parser, token, GUMBO_NAMESPACE_HTML);
  904. insert_element(parser, element, false);
  905. gumbo_debug("Inserting <%s> element (@%x) from token.\n",
  906. gumbo_normalized_tagname(element->v.element.tag), element);
  907. return element;
  908. }
  909. // Convenience method that combines create_element and insert_element, inserting
  910. // a parser-generated element of a specific tag type. Returns the node
  911. // inserted.
  912. static GumboNode* insert_element_of_tag_type(
  913. GumboParser* parser, GumboTag tag, GumboParseFlags reason) {
  914. GumboNode* element = create_element(parser, tag);
  915. element->parse_flags |= GUMBO_INSERTION_BY_PARSER | reason;
  916. insert_element(parser, element, false);
  917. gumbo_debug("Inserting %s element (@%x) from tag type.\n",
  918. gumbo_normalized_tagname(tag), element);
  919. return element;
  920. }
  921. // Convenience method for creating foreign namespaced element. Returns the node
  922. // inserted.
  923. static GumboNode* insert_foreign_element(
  924. GumboParser* parser, GumboToken* token, GumboNamespaceEnum tag_namespace) {
  925. assert(token->type == GUMBO_TOKEN_START_TAG);
  926. GumboNode* element = create_element_from_token(parser, token, tag_namespace);
  927. insert_element(parser, element, false);
  928. if (token_has_attribute(token, "xmlns") &&
  929. !attribute_matches_case_sensitive(
  930. &token->v.start_tag.attributes, "xmlns",
  931. kLegalXmlns[tag_namespace])) {
  932. // TODO(jdtang): Since there're multiple possible error codes here, we
  933. // eventually need reason codes to differentiate them.
  934. add_parse_error(parser, token);
  935. }
  936. if (token_has_attribute(token, "xmlns:xlink") &&
  937. !attribute_matches_case_sensitive(
  938. &token->v.start_tag.attributes,
  939. "xmlns:xlink", "http://www.w3.org/1999/xlink")) {
  940. add_parse_error(parser, token);
  941. }
  942. return element;
  943. }
  944. static void insert_text_token(GumboParser* parser, GumboToken* token) {
  945. assert(token->type == GUMBO_TOKEN_WHITESPACE ||
  946. token->type == GUMBO_TOKEN_CHARACTER);
  947. TextNodeBufferState* buffer_state = &parser->_parser_state->_text_node;
  948. if (buffer_state->_buffer.length == 0) {
  949. // Initialize position fields.
  950. buffer_state->_start_original_text = token->original_text.data;
  951. buffer_state->_start_position = token->position;
  952. }
  953. gumbo_string_buffer_append_codepoint(
  954. parser, token->v.character, &buffer_state->_buffer);
  955. if (token->type == GUMBO_TOKEN_CHARACTER) {
  956. buffer_state->_type = GUMBO_NODE_TEXT;
  957. }
  958. gumbo_debug("Inserting text token '%c'.\n", token->v.character);
  959. }
  960. // http://www.whatwg.org/specs/web-apps/current-work/complete/tokenization.html#generic-rcdata-element-parsing-algorithm
  961. static void run_generic_parsing_algorithm(
  962. GumboParser* parser, GumboToken* token, GumboTokenizerEnum lexer_state) {
  963. insert_element_from_token(parser, token);
  964. gumbo_tokenizer_set_state(parser, lexer_state);
  965. parser->_parser_state->_original_insertion_mode =
  966. parser->_parser_state->_insertion_mode;
  967. parser->_parser_state->_insertion_mode = GUMBO_INSERTION_MODE_TEXT;
  968. }
  969. static void acknowledge_self_closing_tag(GumboParser* parser) {
  970. parser->_parser_state->_self_closing_flag_acknowledged = true;
  971. }
  972. // Returns true if there's an anchor tag in the list of active formatting
  973. // elements, and fills in its index if so.
  974. static bool find_last_anchor_index(GumboParser* parser, int* anchor_index) {
  975. GumboVector* elements = &parser->_parser_state->_active_formatting_elements;
  976. for (int i = elements->length; --i >= 0; ) {
  977. GumboNode* node = elements->data[i];
  978. if (node == &kActiveFormattingScopeMarker) {
  979. return false;
  980. }
  981. if (node_tag_is(node, GUMBO_TAG_A)) {
  982. *anchor_index = i;
  983. return true;
  984. }
  985. }
  986. return false;
  987. }
  988. // Counts the number of open formatting elements in the list of active
  989. // formatting elements (after the last active scope marker) that have a specific
  990. // tag. If this is > 0, then earliest_matching_index will be filled in with the
  991. // index of the first such element.
  992. static int count_formatting_elements_of_tag(
  993. GumboParser* parser, const GumboNode* desired_node,
  994. int* earliest_matching_index) {
  995. const GumboElement* desired_element = &desired_node->v.element;
  996. GumboVector* elements = &parser->_parser_state->_active_formatting_elements;
  997. int num_identical_elements = 0;
  998. for (int i = elements->length; --i >= 0; ) {
  999. GumboNode* node = elements->data[i];
  1000. if (node == &kActiveFormattingScopeMarker) {
  1001. break;
  1002. }
  1003. assert(node->type == GUMBO_NODE_ELEMENT);
  1004. GumboElement* element = &node->v.element;
  1005. if (node_tag_is(node, desired_element->tag) &&
  1006. element->tag_namespace == desired_element->tag_namespace &&
  1007. all_attributes_match(&element->attributes,
  1008. &desired_element->attributes)) {
  1009. num_identical_elements++;
  1010. *earliest_matching_index = i;
  1011. }
  1012. }
  1013. return num_identical_elements;
  1014. }
  1015. // http://www.whatwg.org/specs/web-apps/current-work/complete/parsing.html#reconstruct-the-active-formatting-elements
  1016. static void add_formatting_element(GumboParser* parser, const GumboNode* node) {
  1017. assert(node == &kActiveFormattingScopeMarker ||
  1018. node->type == GUMBO_NODE_ELEMENT);
  1019. GumboVector* elements = &parser->_parser_state->_active_formatting_elements;
  1020. if (node == &kActiveFormattingScopeMarker) {
  1021. gumbo_debug("Adding a scope marker.\n");
  1022. } else {
  1023. gumbo_debug("Adding a formatting element.\n");
  1024. }
  1025. // Hunt for identical elements.
  1026. int earliest_identical_element = elements->length;
  1027. int num_identical_elements = count_formatting_elements_of_tag(
  1028. parser, node, &earliest_identical_element);
  1029. // Noah's Ark clause: if there're at least 3, remove the earliest.
  1030. if (num_identical_elements >= 3) {
  1031. gumbo_debug("Noah's ark clause: removing element at %d.\n",
  1032. earliest_identical_element);
  1033. gumbo_vector_remove_at(parser, earliest_identical_element, elements);
  1034. }
  1035. gumbo_vector_add(parser, (void*) node, elements);
  1036. }
  1037. static bool is_open_element(GumboParser* parser, const GumboNode* node) {
  1038. GumboVector* open_elements = &parser->_parser_state->_open_elements;
  1039. for (int i = 0; i < open_elements->length; ++i) {
  1040. if (open_elements->data[i] == node) {
  1041. return true;
  1042. }
  1043. }
  1044. return false;
  1045. }
  1046. // Clones attributes, tags, etc. of a node, but does not copy the content. The
  1047. // clone shares no structure with the original node: all owned strings and
  1048. // values are fresh copies.
  1049. GumboNode* clone_node(
  1050. GumboParser* parser, const GumboNode* node, GumboParseFlags reason) {
  1051. assert(node->type == GUMBO_NODE_ELEMENT);
  1052. GumboNode* new_node = gumbo_parser_allocate(parser, sizeof(GumboNode));
  1053. *new_node = *node;
  1054. new_node->parent = NULL;
  1055. new_node->index_within_parent = -1;
  1056. // Clear the GUMBO_INSERTION_IMPLICIT_END_TAG flag, as the cloned node may
  1057. // have a separate end tag.
  1058. new_node->parse_flags &= ~GUMBO_INSERTION_IMPLICIT_END_TAG;
  1059. new_node->parse_flags |= reason | GUMBO_INSERTION_BY_PARSER;
  1060. GumboElement* element = &new_node->v.element;
  1061. gumbo_vector_init(parser, 1, &element->children);
  1062. const GumboVector* old_attributes = &node->v.element.attributes;
  1063. gumbo_vector_init(parser, old_attributes->length, &element->attributes);
  1064. for (int i = 0; i < old_attributes->length; ++i) {
  1065. const GumboAttribute* old_attr = old_attributes->data[i];
  1066. GumboAttribute* attr =
  1067. gumbo_parser_allocate(parser, sizeof(GumboAttribute));
  1068. *attr = *old_attr;
  1069. attr->name = gumbo_copy_stringz(parser, old_attr->name);
  1070. attr->value = gumbo_copy_stringz(parser, old_attr->value);
  1071. gumbo_vector_add(parser, attr, &element->attributes);
  1072. }
  1073. return new_node;
  1074. }
  1075. // "Reconstruct active formatting elements" part of the spec.
  1076. // This implementation is based on the html5lib translation from the mess of
  1077. // GOTOs in the spec to reasonably structured programming.
  1078. // http://code.google.com/p/html5lib/source/browse/python/html5lib/treebuilders/_base.py
  1079. static void reconstruct_active_formatting_elements(GumboParser* parser) {
  1080. GumboVector* elements = &parser->_parser_state->_active_formatting_elements;
  1081. // Step 1
  1082. if (elements->length == 0) {
  1083. return;
  1084. }
  1085. // Step 2 & 3
  1086. int i = elements->length - 1;
  1087. const GumboNode* element = elements->data[i];
  1088. if (element == &kActiveFormattingScopeMarker ||
  1089. is_open_element(parser, element)) {
  1090. return;
  1091. }
  1092. // Step 6
  1093. do {
  1094. if (i == 0) {
  1095. // Step 4
  1096. i = -1; // Incremented to 0 below.
  1097. break;
  1098. }
  1099. // Step 5
  1100. element = elements->data[--i];
  1101. } while (element != &kActiveFormattingScopeMarker &&
  1102. !is_open_element(parser, element));
  1103. ++i;
  1104. gumbo_debug("Reconstructing elements from %d on %s parent.\n", i,
  1105. gumbo_normalized_tagname(
  1106. get_current_node(parser)->v.element.tag));
  1107. for(; i < elements->length; ++i) {
  1108. // Step 7 & 8.
  1109. assert(elements->length > 0);
  1110. assert(i < elements->length);
  1111. element = elements->data[i];
  1112. assert(element != &kActiveFormattingScopeMarker);
  1113. GumboNode* clone = clone_node(
  1114. parser, element, GUMBO_INSERTION_RECONSTRUCTED_FORMATTING_ELEMENT);
  1115. // Step 9.
  1116. insert_element(parser, clone, true);
  1117. // Step 10.
  1118. elements->data[i] = clone;
  1119. gumbo_debug("Reconstructed %s element at %d.\n",
  1120. gumbo_normalized_tagname(clone->v.element.tag), i);
  1121. }
  1122. }
  1123. static void clear_active_formatting_elements(GumboParser* parser) {
  1124. GumboVector* elements = &parser->_parser_state->_active_formatting_elements;
  1125. int num_elements_cleared = 0;
  1126. const GumboNode* node;
  1127. do {
  1128. node = gumbo_vector_pop(parser, elements);
  1129. ++num_elements_cleared;
  1130. } while(node && node != &kActiveFormattingScopeMarker);
  1131. gumbo_debug("Cleared %d elements from active formatting list.\n",
  1132. num_elements_cleared);
  1133. }
  1134. // http://www.whatwg.org/specs/web-apps/current-work/complete/tokenization.html#the-initial-insertion-mode
  1135. static GumboQuirksModeEnum compute_quirks_mode(
  1136. const GumboTokenDocType* doctype) {
  1137. if (doctype->force_quirks ||
  1138. strcmp(doctype->name, kDoctypeHtml.data) ||
  1139. is_in_static_list(doctype->public_identifier,
  1140. kQuirksModePublicIdPrefixes, false) ||
  1141. is_in_static_list(doctype->public_identifier,
  1142. kQuirksModePublicIdExactMatches, true) ||
  1143. is_in_static_list(doctype->system_identifier,
  1144. kQuirksModeSystemIdExactMatches, true) ||
  1145. (is_in_static_list(doctype->public_identifier,
  1146. kLimitedQuirksRequiresSystemIdPublicIdPrefixes, false)
  1147. && !doctype->has_system_identifier)) {
  1148. return GUMBO_DOCTYPE_QUIRKS;
  1149. } else if (
  1150. is_in_static_list(doctype->public_identifier,
  1151. kLimitedQuirksPublicIdPrefixes, false) ||
  1152. (is_in_static_list(doctype->public_identifier,
  1153. kLimitedQuirksRequiresSystemIdPublicIdPrefixes, false)
  1154. && doctype->has_system_identifier)) {
  1155. return GUMBO_DOCTYPE_LIMITED_QUIRKS;
  1156. }
  1157. return GUMBO_DOCTYPE_NO_QUIRKS;
  1158. }
  1159. // The following functions are all defined by the "has an element in __ scope"
  1160. // sections of the HTML5 spec:
  1161. // http://www.whatwg.org/specs/web-apps/current-work/multipage/parsing.html#has-an-element-in-the-specific-scope
  1162. // The basic idea behind them is that they check for an element of the given tag
  1163. // name, contained within a scope formed by a set of other tag names. For
  1164. // example, "has an element in list scope" looks for an element of the given tag
  1165. // within the nearest enclosing <ol> or <ul>, along with a bunch of generic
  1166. // element types that serve to "firewall" their content from the rest of the
  1167. // document.
  1168. static bool has_an_element_in_specific_scope(
  1169. GumboParser* parser, GumboVector* /* GumboTag */ expected, bool negate, ...) {
  1170. GumboVector* open_elements = &parser->_parser_state->_open_elements;
  1171. va_list args;
  1172. va_start(args, negate);
  1173. // va_arg can only run through the list once, so we copy it to an GumboVector
  1174. // here. I wonder if it'd make more sense to make tags the GumboVector*
  1175. // parameter and 'expected' a vararg list, but that'd require changing a lot
  1176. // of code for unknown benefit. We may want to change the representation of
  1177. // these tag sets anyway, to something more efficient.
  1178. GumboVector tags;
  1179. gumbo_vector_init(parser, 10, &tags);
  1180. for (GumboTag tag = va_arg(args, GumboTag); tag != GUMBO_TAG_LAST;
  1181. tag = va_arg(args, GumboTag)) {
  1182. // We store the tags inline instead of storing pointers to them.
  1183. gumbo_vector_add(parser, (void*) tag, &tags);
  1184. }
  1185. va_end(args);
  1186. bool result = false;
  1187. for (int i = open_elements->length; --i >= 0; ) {
  1188. const GumboNode* node = open_elements->data[i];
  1189. if (node->type != GUMBO_NODE_ELEMENT) {
  1190. continue;
  1191. }
  1192. GumboTag node_tag = node->v.element.tag;
  1193. for (int j = 0; j < expected->length; ++j) {
  1194. GumboTag expected_tag = (GumboTag) expected->data[j];
  1195. if (node_tag == expected_tag) {
  1196. result = true;
  1197. goto cleanup;
  1198. }
  1199. }
  1200. bool found_tag = false;
  1201. for (int j = 0; j < tags.length; ++j) {
  1202. GumboTag tag = (GumboTag) tags.data[j];
  1203. if (tag == node_tag) {
  1204. found_tag = true;
  1205. break;
  1206. }
  1207. }
  1208. if (negate != found_tag) {
  1209. result = false;
  1210. goto cleanup;
  1211. }
  1212. }
  1213. cleanup:
  1214. gumbo_vector_destroy(parser, &tags);
  1215. return result;
  1216. }
  1217. // This is a bit of a hack to stack-allocate a one-element GumboVector name
  1218. // 'varname' containing the 'from_var' variable, since it's used in nearly all
  1219. // the subsequent helper functions. Note the use of void* and casts instead of
  1220. // GumboTag; this is so the alignment requirements are the same as GumboVector
  1221. // and the data inside it can be freely accessed as if it were a normal
  1222. // GumboVector.
  1223. #define DECLARE_ONE_ELEMENT_GUMBO_VECTOR(varname, from_var) \
  1224. void* varname ## _tmp_array[1] = { (void*) from_var }; \
  1225. GumboVector varname = { varname ## _tmp_array, 1, 1 }
  1226. // http://www.whatwg.org/specs/web-apps/current-work/multipage/parsing.html#has-an-element-in-scope
  1227. static bool has_an_element_in_scope(GumboParser* parser, GumboTag tag) {
  1228. DECLARE_ONE_ELEMENT_GUMBO_VECTOR(tags, tag);
  1229. return has_an_element_in_specific_scope(
  1230. parser, &tags, false, GUMBO_TAG_APPLET, GUMBO_TAG_CAPTION, GUMBO_TAG_HTML,
  1231. GUMBO_TAG_TABLE, GUMBO_TAG_TD, GUMBO_TAG_TH, GUMBO_TAG_MARQUEE,
  1232. GUMBO_TAG_OBJECT, GUMBO_TAG_MI, GUMBO_TAG_MO, GUMBO_TAG_MN, GUMBO_TAG_MS,
  1233. GUMBO_TAG_MTEXT, GUMBO_TAG_ANNOTATION_XML, GUMBO_TAG_FOREIGNOBJECT,
  1234. GUMBO_TAG_DESC, GUMBO_TAG_TITLE, GUMBO_TAG_LAST);
  1235. }
  1236. // Like "has an element in scope", but for the specific case of looking for a
  1237. // unique target node, not for any node with a given tag name. This duplicates
  1238. // much of the algorithm from has_an_element_in_specific_scope because the
  1239. // predicate is different when checking for an exact node, and it's easier &
  1240. // faster just to duplicate the code for this one case than to try and
  1241. // parameterize it.
  1242. static bool has_node_in_scope(GumboParser* parser, const GumboNode* node) {
  1243. GumboVector* open_elements = &parser->_parser_state->_open_elements;
  1244. for (int i = open_elements->length; --i >= 0; ) {
  1245. const GumboNode* current = open_elements->data[i];
  1246. if (current == node) {
  1247. return true;
  1248. }
  1249. if (current->type != GUMBO_NODE_ELEMENT) {
  1250. continue;
  1251. }
  1252. if (node_tag_in(
  1253. current, GUMBO_TAG_APPLET, GUMBO_TAG_CAPTION, GUMBO_TAG_HTML,
  1254. GUMBO_TAG_TABLE, GUMBO_TAG_TD, GUMBO_TAG_TH, GUMBO_TAG_MARQUEE,
  1255. GUMBO_TAG_OBJECT, GUMBO_TAG_MI, GUMBO_TAG_MO, GUMBO_TAG_MN,
  1256. GUMBO_TAG_MS, GUMBO_TAG_MTEXT, GUMBO_TAG_ANNOTATION_XML,
  1257. GUMBO_TAG_FOREIGNOBJECT, GUMBO_TAG_DESC, GUMBO_TAG_TITLE,
  1258. GUMBO_TAG_LAST)) {
  1259. return false;
  1260. }
  1261. }
  1262. assert(false);
  1263. return false;
  1264. }
  1265. // Like has_an_element_in_scope, but restricts the expected tag to a range of
  1266. // possible tag names instead of just a single one.
  1267. static bool has_an_element_in_scope_with_tagname(GumboParser* parser, ...) {
  1268. GumboVector tags;
  1269. // 6 = arbitrary initial size for vector, chosen because the major use-case
  1270. // for this method is heading tags, of which there are 6.
  1271. gumbo_vector_init(parser, 6, &tags);
  1272. va_list args;
  1273. va_start(args, parser);
  1274. for (GumboTag tag = va_arg(args, GumboTag); tag != GUMBO_TAG_LAST;
  1275. tag = va_arg(args, GumboTag)) {
  1276. gumbo_vector_add(parser, (void*) tag, &tags);
  1277. }
  1278. bool found = has_an_element_in_specific_scope(
  1279. parser, &tags, false, GUMBO_TAG_APPLET, GUMBO_TAG_CAPTION, GUMBO_TAG_HTML,
  1280. GUMBO_TAG_TABLE, GUMBO_TAG_TD, GUMBO_TAG_TH, GUMBO_TAG_MARQUEE,
  1281. GUMBO_TAG_OBJECT, GUMBO_TAG_MI, GUMBO_TAG_MO, GUMBO_TAG_MN, GUMBO_TAG_MS,
  1282. GUMBO_TAG_MTEXT, GUMBO_TAG_ANNOTATION_XML, GUMBO_TAG_FOREIGNOBJECT,
  1283. GUMBO_TAG_DESC, GUMBO_TAG_TITLE, GUMBO_TAG_LAST);
  1284. gumbo_vector_destroy(parser, &tags);
  1285. va_end(args);
  1286. return found;
  1287. }
  1288. // http://www.whatwg.org/specs/web-apps/current-work/multipage/parsing.html#has-an-element-in-list-item-scope
  1289. static bool has_an_element_in_list_scope(GumboParser* parser, GumboTag tag) {
  1290. DECLARE_ONE_ELEMENT_GUMBO_VECTOR(tags, tag);
  1291. return has_an_element_in_specific_scope(
  1292. parser, &tags, false, GUMBO_TAG_APPLET, GUMBO_TAG_CAPTION, GUMBO_TAG_HTML,
  1293. GUMBO_TAG_TABLE, GUMBO_TAG_TD, GUMBO_TAG_TH, GUMBO_TAG_MARQUEE,
  1294. GUMBO_TAG_OBJECT, GUMBO_TAG_MI, GUMBO_TAG_MO, GUMBO_TAG_MN, GUMBO_TAG_MS,
  1295. GUMBO_TAG_MTEXT, GUMBO_TAG_ANNOTATION_XML, GUMBO_TAG_FOREIGNOBJECT,
  1296. GUMBO_TAG_DESC, GUMBO_TAG_TITLE, GUMBO_TAG_OL, GUMBO_TAG_UL,
  1297. GUMBO_TAG_LAST);
  1298. }
  1299. // http://www.whatwg.org/specs/web-apps/current-work/multipage/parsing.html#has-an-element-in-button-scope
  1300. static bool has_an_element_in_button_scope(GumboParser* parser, GumboTag tag) {
  1301. DECLARE_ONE_ELEMENT_GUMBO_VECTOR(tags, tag);
  1302. return has_an_element_in_specific_scope(
  1303. parser, &tags, false, GUMBO_TAG_APPLET, GUMBO_TAG_CAPTION, GUMBO_TAG_HTML,
  1304. GUMBO_TAG_TABLE, GUMBO_TAG_TD, GUMBO_TAG_TH, GUMBO_TAG_MARQUEE,
  1305. GUMBO_TAG_OBJECT, GUMBO_TAG_MI, GUMBO_TAG_MO, GUMBO_TAG_MN, GUMBO_TAG_MS,
  1306. GUMBO_TAG_MTEXT, GUMBO_TAG_ANNOTATION_XML, GUMBO_TAG_FOREIGNOBJECT,
  1307. GUMBO_TAG_DESC, GUMBO_TAG_TITLE, GUMBO_TAG_BUTTON, GUMBO_TAG_LAST);
  1308. }
  1309. // http://www.whatwg.org/specs/web-apps/current-work/multipage/parsing.html#has-an-element-in-table-scope
  1310. static bool has_an_element_in_table_scope(GumboParser* parser, GumboTag tag) {
  1311. DECLARE_ONE_ELEMENT_GUMBO_VECTOR(tags, tag);
  1312. return has_an_element_in_specific_scope(
  1313. parser, &tags, false, GUMBO_TAG_HTML, GUMBO_TAG_TABLE, GUMBO_TAG_LAST);
  1314. }
  1315. // http://www.whatwg.org/specs/web-apps/current-work/multipage/parsing.html#has-an-element-in-select-scope
  1316. static bool has_an_element_in_select_scope(GumboParser* parser, GumboTag tag) {
  1317. DECLARE_ONE_ELEMENT_GUMBO_VECTOR(tags, tag);
  1318. return has_an_element_in_specific_scope(
  1319. parser, &tags, true, GUMBO_TAG_OPTGROUP, GUMBO_TAG_OPTION,
  1320. GUMBO_TAG_LAST);
  1321. }
  1322. // http://www.whatwg.org/specs/web-apps/current-work/complete/tokenization.html#generate-implied-end-tags
  1323. // "exception" is the "element to exclude from the process" listed in the spec.
  1324. // Pass GUMBO_TAG_LAST to not exclude any of them.
  1325. static void generate_implied_end_tags(GumboParser* parser, GumboTag exception) {
  1326. for (;
  1327. node_tag_in(get_current_node(parser), GUMBO_TAG_DD, GUMBO_TAG_DT,
  1328. GUMBO_TAG_LI, GUMBO_TAG_OPTION, GUMBO_TAG_OPTGROUP,
  1329. GUMBO_TAG_P, GUMBO_TAG_RP, GUMBO_TAG_RT, GUMBO_TAG_LAST) &&
  1330. !node_tag_is(get_current_node(parser), exception);
  1331. pop_current_node(parser));
  1332. }
  1333. // This factors out the clauses relating to "act as if an end tag token with tag
  1334. // name "table" had been seen. Returns true if there's a table element in table
  1335. // scope which was successfully closed, false if not and the token should be
  1336. // ignored. Does not add parse errors; callers should handle that.
  1337. static bool close_table(GumboParser* parser) {
  1338. if (!has_an_element_in_table_scope(parser, GUMBO_TAG_TABLE)) {
  1339. return false;
  1340. }
  1341. GumboNode* node = pop_current_node(parser);
  1342. while (!node_tag_is(node, GUMBO_TAG_TABLE)) {
  1343. node = pop_current_node(parser);
  1344. }
  1345. reset_insertion_mode_appropriately(parser);
  1346. return true;
  1347. }
  1348. // This factors out the clauses relating to "act as if an end tag token with tag
  1349. // name `cell_tag` had been seen".
  1350. static bool close_table_cell(GumboParser* parser, const GumboToken* token,
  1351. GumboTag cell_tag) {
  1352. bool result = true;
  1353. generate_implied_end_tags(parser, GUMBO_TAG_LAST);
  1354. const GumboNode* node = get_current_node(parser);
  1355. if (!node_tag_is(node, cell_tag)) {
  1356. add_parse_error(parser, token);
  1357. result = false;
  1358. }
  1359. do {
  1360. node = pop_current_node(parser);
  1361. } while (!node_tag_is(node, cell_tag));
  1362. clear_active_formatting_elements(parser);
  1363. set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_ROW);
  1364. return result;
  1365. }
  1366. // http://www.whatwg.org/specs/web-apps/current-work/complete/tokenization.html#close-the-cell
  1367. // This holds the logic to determine whether we should close a <td> or a <th>.
  1368. static bool close_current_cell(GumboParser* parser, const GumboToken* token) {
  1369. if (has_an_element_in_table_scope(parser, GUMBO_TAG_TD)) {
  1370. assert(!has_an_element_in_table_scope(parser, GUMBO_TAG_TH));
  1371. return close_table_cell(parser, token, GUMBO_TAG_TD);
  1372. } else {
  1373. assert(has_an_element_in_table_scope(parser, GUMBO_TAG_TH));
  1374. return close_table_cell(parser, token, GUMBO_TAG_TH);
  1375. }
  1376. }
  1377. // This factors out the "act as if an end tag of tag name 'select' had been
  1378. // seen" clause of the spec, since it's referenced in several places. It pops
  1379. // all nodes from the stack until the current <select> has been closed, then
  1380. // resets the insertion mode appropriately.
  1381. static void close_current_select(GumboParser* parser) {
  1382. GumboNode* node = pop_current_node(parser);
  1383. while (!node_tag_is(node, GUMBO_TAG_SELECT)) {
  1384. node = pop_current_node(parser);
  1385. }
  1386. reset_insertion_mode_appropriately(parser);
  1387. }
  1388. // The list of nodes in the "special" category:
  1389. // http://www.whatwg.org/specs/web-apps/current-work/complete/parsing.html#special
  1390. static bool is_special_node(const GumboNode* node) {
  1391. assert(node->type == GUMBO_NODE_ELEMENT);
  1392. switch (node->v.element.tag_namespace) {
  1393. case GUMBO_NAMESPACE_HTML:
  1394. return node_tag_in(node,
  1395. GUMBO_TAG_ADDRESS, GUMBO_TAG_APPLET, GUMBO_TAG_AREA,
  1396. GUMBO_TAG_ARTICLE, GUMBO_TAG_ASIDE, GUMBO_TAG_BASE,
  1397. GUMBO_TAG_BASEFONT, GUMBO_TAG_BGSOUND, GUMBO_TAG_BLOCKQUOTE,
  1398. GUMBO_TAG_BODY, GUMBO_TAG_BR, GUMBO_TAG_BUTTON, GUMBO_TAG_CAPTION,
  1399. GUMBO_TAG_CENTER, GUMBO_TAG_COL, GUMBO_TAG_COLGROUP,
  1400. GUMBO_TAG_MENUITEM, GUMBO_TAG_DD, GUMBO_TAG_DETAILS, GUMBO_TAG_DIR,
  1401. GUMBO_TAG_DIV, GUMBO_TAG_DL, GUMBO_TAG_DT, GUMBO_TAG_EMBED,
  1402. GUMBO_TAG_FIELDSET, GUMBO_TAG_FIGCAPTION, GUMBO_TAG_FIGURE,
  1403. GUMBO_TAG_FOOTER, GUMBO_TAG_FORM, GUMBO_TAG_FRAME,
  1404. GUMBO_TAG_FRAMESET, GUMBO_TAG_H1, GUMBO_TAG_H2, GUMBO_TAG_H3,
  1405. GUMBO_TAG_H4, GUMBO_TAG_H5, GUMBO_TAG_H6, GUMBO_TAG_HEAD,
  1406. GUMBO_TAG_HEADER, GUMBO_TAG_HGROUP, GUMBO_TAG_HR, GUMBO_TAG_HTML,
  1407. GUMBO_TAG_IFRAME, GUMBO_TAG_IMG, GUMBO_TAG_INPUT, GUMBO_TAG_ISINDEX,
  1408. GUMBO_TAG_LI, GUMBO_TAG_LINK, GUMBO_TAG_LISTING, GUMBO_TAG_MARQUEE,
  1409. GUMBO_TAG_MENU, GUMBO_TAG_META, GUMBO_TAG_NAV, GUMBO_TAG_NOEMBED,
  1410. GUMBO_TAG_NOFRAMES, GUMBO_TAG_NOSCRIPT, GUMBO_TAG_OBJECT,
  1411. GUMBO_TAG_OL, GUMBO_TAG_P, GUMBO_TAG_PARAM, GUMBO_TAG_PLAINTEXT,
  1412. GUMBO_TAG_PRE, GUMBO_TAG_SCRIPT, GUMBO_TAG_SECTION, GUMBO_TAG_SELECT,
  1413. GUMBO_TAG_STYLE, GUMBO_TAG_SUMMARY, GUMBO_TAG_TABLE, GUMBO_TAG_TBODY,
  1414. GUMBO_TAG_TD, GUMBO_TAG_TEXTAREA, GUMBO_TAG_TFOOT, GUMBO_TAG_TH,
  1415. GUMBO_TAG_THEAD, GUMBO_TAG_TITLE, GUMBO_TAG_TR, GUMBO_TAG_UL,
  1416. GUMBO_TAG_WBR, GUMBO_TAG_XMP, GUMBO_TAG_LAST);
  1417. case GUMBO_NAMESPACE_MATHML:
  1418. return node_tag_in(node,
  1419. GUMBO_TAG_MI, GUMBO_TAG_MO, GUMBO_TAG_MN, GUMBO_TAG_MS,
  1420. GUMBO_TAG_MTEXT, GUMBO_TAG_ANNOTATION_XML, GUMBO_TAG_LAST);
  1421. case GUMBO_NAMESPACE_SVG:
  1422. return node_tag_in(node,
  1423. GUMBO_TAG_FOREIGNOBJECT, GUMBO_TAG_DESC, GUMBO_TAG_LAST);
  1424. }
  1425. abort();
  1426. return false; // Pacify compiler.
  1427. }
  1428. // Implicitly closes currently open tags until it reaches an element with the
  1429. // specified tag name. If the elements closed are in the set handled by
  1430. // generate_implied_end_tags, this is normal operation and this function returns
  1431. // true. Otherwise, a parse error is recorded and this function returns false.
  1432. static bool implicitly_close_tags(
  1433. GumboParser* parser, GumboToken* token, GumboTag target) {
  1434. bool result = true;
  1435. generate_implied_end_tags(parser, target);
  1436. if (!node_tag_is(get_current_node(parser), target)) {
  1437. add_parse_error(parser, token);
  1438. while (!node_tag_is(get_current_node(parser), target)) {
  1439. pop_current_node(parser);
  1440. }
  1441. result = false;
  1442. }
  1443. assert(node_tag_is(get_current_node(parser), target));
  1444. pop_current_node(parser);
  1445. return result;
  1446. }
  1447. // If the stack of open elements has a <p> tag in button scope, this acts as if
  1448. // a </p> tag was encountered, implicitly closing tags. Returns false if a
  1449. // parse error occurs. This is a convenience function because this particular
  1450. // clause appears several times in the spec.
  1451. static bool maybe_implicitly_close_p_tag(GumboParser* parser, GumboToken* token) {
  1452. if (has_an_element_in_button_scope(parser, GUMBO_TAG_P)) {
  1453. return implicitly_close_tags(parser, token, GUMBO_TAG_P);
  1454. }
  1455. return true;
  1456. }
  1457. // Convenience function to encapsulate the logic for closing <li> or <dd>/<dt>
  1458. // tags. Pass true to is_li for handling <li> tags, false for <dd> and <dt>.
  1459. static void maybe_implicitly_close_list_tag(
  1460. GumboParser* parser, GumboToken* token, bool is_li) {
  1461. GumboParserState* state = parser->_parser_state;
  1462. state->_frameset_ok = false;
  1463. for (int i = state->_open_elements.length; --i >= 0; ) {
  1464. const GumboNode* node = state->_open_elements.data[i];
  1465. bool is_list_tag = is_li ?
  1466. node_tag_is(node, GUMBO_TAG_LI) :
  1467. node_tag_in(node, GUMBO_TAG_DD, GUMBO_TAG_DT, GUMBO_TAG_LAST);
  1468. if (is_list_tag) {
  1469. implicitly_close_tags(parser, token, node->v.element.tag);
  1470. return;
  1471. }
  1472. if (is_special_node(node) &&
  1473. !node_tag_in(node, GUMBO_TAG_ADDRESS, GUMBO_TAG_DIV, GUMBO_TAG_P,
  1474. GUMBO_TAG_LAST)) {
  1475. return;
  1476. }
  1477. }
  1478. }
  1479. static void merge_attributes(
  1480. GumboParser* parser, GumboToken* token, GumboNode* node) {
  1481. assert(token->type == GUMBO_TOKEN_START_TAG);
  1482. assert(node->type == GUMBO_NODE_ELEMENT);
  1483. const GumboVector* token_attr = &token->v.start_tag.attributes;
  1484. GumboVector* node_attr = &node->v.element.attributes;
  1485. for (int i = 0; i < token_attr->length; ++i) {
  1486. GumboAttribute* attr = token_attr->data[i];
  1487. if (!gumbo_get_attribute(node_attr, attr->name)) {
  1488. // Ownership of the attribute is transferred by this gumbo_vector_add,
  1489. // so it has to be nulled out of the original token so it doesn't get
  1490. // double-deleted.
  1491. gumbo_vector_add(parser, attr, node_attr);
  1492. token_attr->data[i] = NULL;
  1493. }
  1494. }
  1495. // When attributes are merged, it means the token has been ignored and merged
  1496. // with another token, so we need to free its memory. The attributes that are
  1497. // transferred need to be nulled-out in the vector above so that they aren't
  1498. // double-deleted.
  1499. gumbo_token_destroy(parser, token);
  1500. #ifndef NDEBUG
  1501. // Mark this sentinel so the assertion in the main loop knows it's been
  1502. // destroyed.
  1503. token->v.start_tag.attributes = kGumboEmptyVector;
  1504. #endif
  1505. }
  1506. const char* gumbo_normalize_svg_tagname(const GumboStringPiece* tag) {
  1507. for (int i = 0;
  1508. i < sizeof(kSvgTagReplacements) / sizeof(ReplacementEntry); ++i) {
  1509. const ReplacementEntry* entry = &kSvgTagReplacements[i];
  1510. if (gumbo_string_equals_ignore_case(tag, &entry->from)) {
  1511. return entry->to.data;
  1512. }
  1513. }
  1514. return NULL;
  1515. }
  1516. // http://www.whatwg.org/specs/web-apps/current-work/multipage/tree-construction.html#adjust-foreign-attributes
  1517. // This destructively modifies any matching attributes on the token and sets the
  1518. // namespace appropriately.
  1519. static void adjust_foreign_attributes(GumboParser* parser, GumboToken* token) {
  1520. assert(token->type == GUMBO_TOKEN_START_TAG);
  1521. const GumboVector* attributes = &token->v.start_tag.attributes;
  1522. for (int i = 0;
  1523. i < sizeof(kForeignAttributeReplacements) /
  1524. sizeof(NamespacedAttributeReplacement); ++i) {
  1525. const NamespacedAttributeReplacement* entry =
  1526. &kForeignAttributeReplacements[i];
  1527. GumboAttribute* attr = gumbo_get_attribute(attributes, entry->from);
  1528. if (!attr) {
  1529. continue;
  1530. }
  1531. gumbo_parser_deallocate(parser, (void*) attr->name);
  1532. attr->attr_namespace = entry->attr_namespace;
  1533. attr->name = gumbo_copy_stringz(parser, entry->local_name);
  1534. }
  1535. }
  1536. // http://www.whatwg.org/specs/web-apps/current-work/complete/tokenization.html#adjust-svg-attributes
  1537. // This destructively modifies any matching attributes on the token.
  1538. static void adjust_svg_attributes(GumboParser* parser, GumboToken* token) {
  1539. assert(token->type == GUMBO_TOKEN_START_TAG);
  1540. const GumboVector* attributes = &token->v.start_tag.attributes;
  1541. for (int i = 0;
  1542. i < sizeof(kSvgAttributeReplacements) / sizeof(ReplacementEntry); ++i) {
  1543. const ReplacementEntry* entry = &kSvgAttributeReplacements[i];
  1544. GumboAttribute* attr = gumbo_get_attribute(attributes, entry->from.data);
  1545. if (!attr) {
  1546. continue;
  1547. }
  1548. gumbo_parser_deallocate(parser, (void*) attr->name);
  1549. attr->name = gumbo_copy_stringz(parser, entry->to.data);
  1550. }
  1551. }
  1552. // http://www.whatwg.org/specs/web-apps/current-work/complete/tokenization.html#adjust-mathml-attributes
  1553. // Note that this may destructively modify the token with the new attribute
  1554. // value.
  1555. static void adjust_mathml_attributes(GumboParser* parser, GumboToken* token) {
  1556. assert(token->type == GUMBO_TOKEN_START_TAG);
  1557. GumboAttribute* attr = gumbo_get_attribute(
  1558. &token->v.start_tag.attributes, "definitionurl");
  1559. if (!attr) {
  1560. return;
  1561. }
  1562. gumbo_parser_deallocate(parser, (void*) attr->name);
  1563. attr->name = gumbo_copy_stringz(parser, "definitionURL");
  1564. }
  1565. static bool doctype_matches(
  1566. const GumboTokenDocType* doctype,
  1567. const GumboStringPiece* public_id,
  1568. const GumboStringPiece* system_id,
  1569. bool allow_missing_system_id) {
  1570. return !strcmp(doctype->public_identifier, public_id->data) &&
  1571. (allow_missing_system_id || doctype->has_system_identifier) &&
  1572. !strcmp(doctype->system_identifier, system_id->data);
  1573. }
  1574. static bool maybe_add_doctype_error(
  1575. GumboParser* parser, const GumboToken* token) {
  1576. const GumboTokenDocType* doctype = &token->v.doc_type;
  1577. bool html_doctype = !strcmp(doctype->name, kDoctypeHtml.data);
  1578. if ((!html_doctype ||
  1579. doctype->has_public_identifier ||
  1580. (doctype->has_system_identifier && !strcmp(
  1581. doctype->system_identifier, kSystemIdLegacyCompat.data))) &&
  1582. !(html_doctype && (
  1583. doctype_matches(doctype, &kPublicIdHtml4_0,
  1584. &kSystemIdRecHtml4_0, true) ||
  1585. doctype_matches(doctype, &kPublicIdHtml4_01, &kSystemIdHtml4, true) ||
  1586. doctype_matches(doctype, &kPublicIdXhtml1_0,
  1587. &kSystemIdXhtmlStrict1_1, false) ||
  1588. doctype_matches(doctype, &kPublicIdXhtml1_1,
  1589. &kSystemIdXhtml1_1, false)))) {
  1590. add_parse_error(parser, token);
  1591. return false;
  1592. }
  1593. return true;
  1594. }
  1595. static void remove_from_parent(GumboParser* parser, GumboNode* node) {
  1596. if (!node->parent) {
  1597. // The node may not have a parent if, for example, it is a newly-cloned copy
  1598. // of an active formatting element. DOM manipulations continue with the
  1599. // orphaned fragment of the DOM tree until it's appended/foster-parented to
  1600. // the common ancestor at the end of the adoption agency algorithm.
  1601. return;
  1602. }
  1603. assert(node->parent->type == GUMBO_NODE_ELEMENT);
  1604. GumboVector* children = &node->parent->v.element.children;
  1605. int index = gumbo_vector_index_of(children, node);
  1606. assert(index != -1);
  1607. gumbo_vector_remove_at(parser, index, children);
  1608. node->parent = NULL;
  1609. node->index_within_parent = -1;
  1610. for (int i = index; i < children->length; ++i) {
  1611. GumboNode* child = children->data[i];
  1612. child->index_within_parent = i;
  1613. }
  1614. }
  1615. // http://www.whatwg.org/specs/web-apps/current-work/multipage/the-end.html#an-introduction-to-error-handling-and-strange-cases-in-the-parser
  1616. // Also described in the "in body" handling for end formatting tags.
  1617. static bool adoption_agency_algorithm(
  1618. GumboParser* parser, GumboToken* token, GumboTag closing_tag) {
  1619. GumboParserState* state = parser->_parser_state;
  1620. gumbo_debug("Entering adoption agency algorithm.\n");
  1621. // Steps 1-3 & 16:
  1622. for (int i = 0; i < 8; ++i) {
  1623. // Step 4.
  1624. GumboNode* formatting_node = NULL;
  1625. int formatting_node_in_open_elements = -1;
  1626. for (int j = state->_active_formatting_elements.length; --j >= 0; ) {
  1627. GumboNode* current_node = state->_active_formatting_elements.data[j];
  1628. if (current_node == &kActiveFormattingScopeMarker) {
  1629. gumbo_debug("Broke on scope marker; aborting.\n");
  1630. // Last scope marker; abort the algorithm.
  1631. return false;
  1632. }
  1633. if (node_tag_is(current_node, closing_tag)) {
  1634. // Found it.
  1635. formatting_node = current_node;
  1636. formatting_node_in_open_elements = gumbo_vector_index_of(
  1637. &state->_open_elements, formatting_node);
  1638. gumbo_debug("Formatting element of tag %s at %d.\n",
  1639. gumbo_normalized_tagname(closing_tag),
  1640. formatting_node_in_open_elements);
  1641. break;
  1642. }
  1643. }
  1644. if (!formatting_node) {
  1645. // No matching tag; not a parse error outright, but fall through to the
  1646. // "any other end tag" clause (which may potentially add a parse error,
  1647. // but not always).
  1648. gumbo_debug("No active formatting elements; aborting.\n");
  1649. return false;
  1650. }
  1651. if (formatting_node_in_open_elements == -1) {
  1652. gumbo_debug("Formatting node not on stack of open elements.\n");
  1653. gumbo_vector_remove(parser, formatting_node,
  1654. &state->_active_formatting_elements);
  1655. return false;
  1656. }
  1657. if (!has_an_element_in_scope(parser, formatting_node->v.element.tag)) {
  1658. add_parse_error(parser, token);
  1659. gumbo_debug("Element not in scope.\n");
  1660. return false;
  1661. }
  1662. if (formatting_node != get_current_node(parser)) {
  1663. add_parse_error(parser, token); // But continue onwards.
  1664. }
  1665. assert(formatting_node);
  1666. assert(!node_tag_is(formatting_node, GUMBO_TAG_HTML));
  1667. assert(!node_tag_is(formatting_node, GUMBO_TAG_BODY));
  1668. // Step 5 & 6.
  1669. GumboNode* furthest_block = NULL;
  1670. for (int j = formatting_node_in_open_elements;
  1671. j < state->_open_elements.length; ++j) {
  1672. assert(j > 0);
  1673. GumboNode* current = state->_open_elements.data[j];
  1674. if (is_special_node(current)) {
  1675. // Step 5.
  1676. furthest_block = current;
  1677. break;
  1678. }
  1679. }
  1680. if (!furthest_block) {
  1681. // Step 6.
  1682. while (get_current_node(parser) != formatting_node) {
  1683. pop_current_node(parser);
  1684. }
  1685. // And the formatting element itself.
  1686. pop_current_node(parser);
  1687. gumbo_vector_remove(parser, formatting_node,
  1688. &state->_active_formatting_elements);
  1689. return false;
  1690. }
  1691. assert(!node_tag_is(furthest_block, GUMBO_TAG_HTML));
  1692. assert(furthest_block);
  1693. // Step 7.
  1694. // Elements may be moved and reparented by this algorithm, so
  1695. // common_ancestor is not necessarily the same as formatting_node->parent.
  1696. GumboNode* common_ancestor =
  1697. state->_open_elements.data[gumbo_vector_index_of(
  1698. &state->_open_elements, formatting_node) - 1];
  1699. gumbo_debug("Common ancestor tag = %s, furthest block tag = %s.\n",
  1700. gumbo_normalized_tagname(common_ancestor->v.element.tag),
  1701. gumbo_normalized_tagname(furthest_block->v.element.tag));
  1702. // Step 8.
  1703. int bookmark = gumbo_vector_index_of(
  1704. &state->_active_formatting_elements, formatting_node);;
  1705. // Step 9.
  1706. GumboNode* node = furthest_block;
  1707. GumboNode* last_node = furthest_block;
  1708. // Must be stored explicitly, in case node is removed from the stack of open
  1709. // elements, to handle step 9.4.
  1710. int saved_node_index = gumbo_vector_index_of(&state->_open_elements, node);
  1711. assert(saved_node_index > 0);
  1712. // Step 9.1-9.3 & 9.11.
  1713. for (int j = 0; j < 3; ++j) {
  1714. // Step 9.4.
  1715. int node_index = gumbo_vector_index_of(&state->_open_elements, node);
  1716. gumbo_debug(
  1717. "Current index: %d, last index: %d.\n", node_index, saved_node_index);
  1718. if (node_index == -1) {
  1719. node_index = saved_node_index;
  1720. }
  1721. saved_node_index = --node_index;
  1722. assert(node_index > 0);
  1723. assert(node_index < state->_open_elements.capacity);
  1724. node = state->_open_elements.data[node_index];
  1725. assert(node->parent);
  1726. // Step 9.5.
  1727. if (gumbo_vector_index_of(
  1728. &state->_active_formatting_elements, node) == -1) {
  1729. gumbo_vector_remove_at(parser, node_index, &state->_open_elements);
  1730. continue;
  1731. } else if (node == formatting_node) {
  1732. // Step 9.6.
  1733. break;
  1734. }
  1735. // Step 9.7.
  1736. int formatting_index = gumbo_vector_index_of(
  1737. &state->_active_formatting_elements, node);
  1738. node = clone_node(parser, node, GUMBO_INSERTION_ADOPTION_AGENCY_CLONED);
  1739. state->_active_formatting_elements.data[formatting_index] = node;
  1740. state->_open_elements.data[node_index] = node;
  1741. // Step 9.8.
  1742. if (last_node == furthest_block) {
  1743. bookmark = formatting_index + 1;
  1744. assert(bookmark <= state->_active_formatting_elements.length);
  1745. }
  1746. // Step 9.9.
  1747. last_node->parse_flags |= GUMBO_INSERTION_ADOPTION_AGENCY_MOVED;
  1748. remove_from_parent(parser, last_node);
  1749. append_node(parser, node, last_node);
  1750. // Step 9.10.
  1751. last_node = node;
  1752. }
  1753. // Step 10.
  1754. gumbo_debug("Removing %s node from parent ",
  1755. gumbo_normalized_tagname(last_node->v.element.tag));
  1756. remove_from_parent(parser, last_node);
  1757. last_node->parse_flags |= GUMBO_INSERTION_ADOPTION_AGENCY_MOVED;
  1758. if (node_tag_in(common_ancestor, GUMBO_TAG_TABLE, GUMBO_TAG_TBODY,
  1759. GUMBO_TAG_TFOOT, GUMBO_TAG_THEAD, GUMBO_TAG_TR,
  1760. GUMBO_TAG_LAST)) {
  1761. gumbo_debug("and foster-parenting it.\n");
  1762. foster_parent_element(parser, last_node);
  1763. } else {
  1764. gumbo_debug("and inserting it into %s.\n",
  1765. gumbo_normalized_tagname(common_ancestor->v.element.tag));
  1766. append_node(parser, common_ancestor, last_node);
  1767. }
  1768. // Step 11.
  1769. GumboNode* new_formatting_node = clone_node(
  1770. parser, formatting_node, GUMBO_INSERTION_ADOPTION_AGENCY_CLONED);
  1771. formatting_node->parse_flags |= GUMBO_INSERTION_IMPLICIT_END_TAG;
  1772. // Step 12. Instead of appending nodes one-by-one, we swap the children
  1773. // vector of furthest_block with the empty children of new_formatting_node,
  1774. // reducing memory traffic and allocations. We still have to reset their
  1775. // parent pointers, though.
  1776. GumboVector temp = new_formatting_node->v.element.children;
  1777. new_formatting_node->v.element.children =
  1778. furthest_block->v.element.children;
  1779. furthest_block->v.element.children = temp;
  1780. temp = new_formatting_node->v.element.children;
  1781. for (int i = 0; i < temp.length; ++i) {
  1782. GumboNode* child = temp.data[i];
  1783. child->parent = new_formatting_node;
  1784. }
  1785. // Step 13.
  1786. append_node(parser, furthest_block, new_formatting_node);
  1787. // Step 14.
  1788. // If the formatting node was before the bookmark, it may shift over all
  1789. // indices after it, so we need to explicitly find the index and possibly
  1790. // adjust the bookmark.
  1791. int formatting_node_index = gumbo_vector_index_of(
  1792. &state->_active_formatting_elements, formatting_node);
  1793. assert(formatting_node_index != -1);
  1794. if (formatting_node_index < bookmark) {
  1795. --bookmark;
  1796. }
  1797. gumbo_vector_remove_at(
  1798. parser, formatting_node_index, &state->_active_formatting_elements);
  1799. assert(bookmark >= 0);
  1800. assert(bookmark <= state->_active_formatting_elements.length);
  1801. gumbo_vector_insert_at(parser, new_formatting_node, bookmark,
  1802. &state->_active_formatting_elements);
  1803. // Step 15.
  1804. gumbo_vector_remove(
  1805. parser, formatting_node, &state->_open_elements);
  1806. int insert_at = gumbo_vector_index_of(
  1807. &state->_open_elements, furthest_block) + 1;
  1808. assert(insert_at >= 0);
  1809. assert(insert_at <= state->_open_elements.length);
  1810. gumbo_vector_insert_at(
  1811. parser, new_formatting_node, insert_at, &state->_open_elements);
  1812. }
  1813. return true;
  1814. }
  1815. // This is here to clean up memory when the spec says "Ignore current token."
  1816. static void ignore_token(GumboParser* parser) {
  1817. GumboToken* token = parser->_parser_state->_current_token;
  1818. // Ownership of the token's internal buffers are normally transferred to the
  1819. // element, but if no element is emitted (as happens in non-verbatim-mode
  1820. // when a token is ignored), we need to free it here to prevent a memory
  1821. // leak.
  1822. gumbo_token_destroy(parser, token);
  1823. #ifndef NDEBUG
  1824. if (token->type == GUMBO_TOKEN_START_TAG) {
  1825. // Mark this sentinel so the assertion in the main loop knows it's been
  1826. // destroyed.
  1827. token->v.start_tag.attributes = kGumboEmptyVector;
  1828. }
  1829. #endif
  1830. }
  1831. // http://www.whatwg.org/specs/web-apps/current-work/complete/the-end.html
  1832. static void finish_parsing(GumboParser* parser) {
  1833. maybe_flush_text_node_buffer(parser);
  1834. GumboParserState* state = parser->_parser_state;
  1835. for (GumboNode* node = pop_current_node(parser); node;
  1836. node = pop_current_node(parser)) {
  1837. if ((node_tag_is(node, GUMBO_TAG_BODY) && state->_closed_body_tag) ||
  1838. (node_tag_is(node, GUMBO_TAG_HTML) && state->_closed_html_tag)) {
  1839. continue;
  1840. }
  1841. node->parse_flags |= GUMBO_INSERTION_IMPLICIT_END_TAG;
  1842. }
  1843. while (pop_current_node(parser)); // Pop them all.
  1844. }
  1845. static bool handle_initial(GumboParser* parser, GumboToken* token) {
  1846. GumboDocument* document = &get_document_node(parser)->v.document;
  1847. if (token->type == GUMBO_TOKEN_WHITESPACE) {
  1848. ignore_token(parser);
  1849. return true;
  1850. } else if (token->type == GUMBO_TOKEN_COMMENT) {
  1851. append_comment_node(parser, get_document_node(parser), token);
  1852. return true;
  1853. } else if (token->type == GUMBO_TOKEN_DOCTYPE) {
  1854. document->has_doctype = true;
  1855. document->name = token->v.doc_type.name;
  1856. document->public_identifier = token->v.doc_type.public_identifier;
  1857. document->system_identifier = token->v.doc_type.system_identifier;
  1858. document->doc_type_quirks_mode = compute_quirks_mode(&token->v.doc_type);
  1859. set_insertion_mode(parser, GUMBO_INSERTION_MODE_BEFORE_HTML);
  1860. return maybe_add_doctype_error(parser, token);
  1861. }
  1862. add_parse_error(parser, token);
  1863. document->doc_type_quirks_mode = GUMBO_DOCTYPE_QUIRKS;
  1864. set_insertion_mode(parser, GUMBO_INSERTION_MODE_BEFORE_HTML);
  1865. parser->_parser_state->_reprocess_current_token = true;
  1866. return true;
  1867. }
  1868. // http://www.whatwg.org/specs/web-apps/current-work/complete/tokenization.html#the-before-html-insertion-mode
  1869. static bool handle_before_html(GumboParser* parser, GumboToken* token) {
  1870. if (token->type == GUMBO_TOKEN_DOCTYPE) {
  1871. add_parse_error(parser, token);
  1872. ignore_token(parser);
  1873. return false;
  1874. } else if (token->type == GUMBO_TOKEN_COMMENT) {
  1875. append_comment_node(parser, get_document_node(parser), token);
  1876. return true;
  1877. } else if (token->type == GUMBO_TOKEN_WHITESPACE) {
  1878. ignore_token(parser);
  1879. return true;
  1880. } else if (tag_is(token, kStartTag, GUMBO_TAG_HTML)) {
  1881. GumboNode* html_node = insert_element_from_token(parser, token);
  1882. parser->_output->root = html_node;
  1883. set_insertion_mode(parser, GUMBO_INSERTION_MODE_BEFORE_HEAD);
  1884. return true;
  1885. } else if (token->type == GUMBO_TOKEN_END_TAG && !tag_in(
  1886. token, false, GUMBO_TAG_HEAD, GUMBO_TAG_BODY, GUMBO_TAG_HTML,
  1887. GUMBO_TAG_BR, GUMBO_TAG_LAST)) {
  1888. add_parse_error(parser, token);
  1889. ignore_token(parser);
  1890. return false;
  1891. } else {
  1892. GumboNode* html_node = insert_element_of_tag_type(
  1893. parser, GUMBO_TAG_HTML, GUMBO_INSERTION_IMPLIED);
  1894. assert(html_node);
  1895. parser->_output->root = html_node;
  1896. set_insertion_mode(parser, GUMBO_INSERTION_MODE_BEFORE_HEAD);
  1897. parser->_parser_state->_reprocess_current_token = true;
  1898. return true;
  1899. }
  1900. }
  1901. // http://www.whatwg.org/specs/web-apps/current-work/complete/tokenization.html#the-before-head-insertion-mode
  1902. static bool handle_before_head(GumboParser* parser, GumboToken* token) {
  1903. if (token->type == GUMBO_TOKEN_DOCTYPE) {
  1904. add_parse_error(parser, token);
  1905. ignore_token(parser);
  1906. return false;
  1907. } else if (token->type == GUMBO_TOKEN_COMMENT) {
  1908. append_comment_node(parser, get_current_node(parser), token);
  1909. return true;
  1910. } else if (token->type == GUMBO_TOKEN_WHITESPACE) {
  1911. ignore_token(parser);
  1912. return true;
  1913. } else if (tag_is(token, kStartTag, GUMBO_TAG_HEAD)) {
  1914. GumboNode* node = insert_element_from_token(parser, token);
  1915. set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_HEAD);
  1916. parser->_parser_state->_head_element = node;
  1917. return true;
  1918. } else if (token->type == GUMBO_TOKEN_END_TAG && !tag_in(
  1919. token, false, GUMBO_TAG_HEAD, GUMBO_TAG_BODY, GUMBO_TAG_HTML,
  1920. GUMBO_TAG_BR, GUMBO_TAG_LAST)) {
  1921. add_parse_error(parser, token);
  1922. ignore_token(parser);
  1923. return false;
  1924. } else {
  1925. GumboNode* node = insert_element_of_tag_type(
  1926. parser, GUMBO_TAG_HEAD, GUMBO_INSERTION_IMPLIED);
  1927. set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_HEAD);
  1928. parser->_parser_state->_head_element = node;
  1929. parser->_parser_state->_reprocess_current_token = true;
  1930. return true;
  1931. }
  1932. }
  1933. // Forward declarations because of mutual dependencies.
  1934. static bool handle_token(GumboParser* parser, GumboToken* token);
  1935. static bool handle_in_body(GumboParser* parser, GumboToken* token);
  1936. // http://www.whatwg.org/specs/web-apps/current-work/complete/tokenization.html#parsing-main-inhead
  1937. static bool handle_in_head(GumboParser* parser, GumboToken* token) {
  1938. if (token->type == GUMBO_TOKEN_WHITESPACE) {
  1939. insert_text_token(parser, token);
  1940. return true;
  1941. } else if (token->type == GUMBO_TOKEN_DOCTYPE) {
  1942. add_parse_error(parser, token);
  1943. ignore_token(parser);
  1944. return false;
  1945. } else if (token->type == GUMBO_TOKEN_COMMENT) {
  1946. append_comment_node(parser, get_current_node(parser), token);
  1947. return true;
  1948. } else if (tag_is(token, kStartTag, GUMBO_TAG_HTML)) {
  1949. return handle_in_body(parser, token);
  1950. } else if (tag_in(token, kStartTag, GUMBO_TAG_BASE, GUMBO_TAG_BASEFONT,
  1951. GUMBO_TAG_BGSOUND, GUMBO_TAG_MENUITEM, GUMBO_TAG_LINK,
  1952. GUMBO_TAG_LAST)) {
  1953. insert_element_from_token(parser, token);
  1954. pop_current_node(parser);
  1955. acknowledge_self_closing_tag(parser);
  1956. return true;
  1957. } else if (tag_is(token, kStartTag, GUMBO_TAG_META)) {
  1958. insert_element_from_token(parser, token);
  1959. pop_current_node(parser);
  1960. acknowledge_self_closing_tag(parser);
  1961. // NOTE(jdtang): Gumbo handles only UTF-8, so the encoding clause of the
  1962. // spec doesn't apply. If clients want to handle meta-tag re-encoding, they
  1963. // should specifically look for that string in the document and re-encode it
  1964. // before passing to Gumbo.
  1965. return true;
  1966. } else if (tag_is(token, kStartTag, GUMBO_TAG_TITLE)) {
  1967. run_generic_parsing_algorithm(parser, token, GUMBO_LEX_RCDATA);
  1968. return true;
  1969. } else if (tag_in(token, kStartTag, GUMBO_TAG_NOFRAMES, GUMBO_TAG_STYLE,
  1970. GUMBO_TAG_LAST)) {
  1971. run_generic_parsing_algorithm(parser, token, GUMBO_LEX_RAWTEXT);
  1972. return true;
  1973. } else if (tag_is(token, kStartTag, GUMBO_TAG_NOSCRIPT)) {
  1974. insert_element_from_token(parser, token);
  1975. set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_HEAD_NOSCRIPT);
  1976. return true;
  1977. } else if (tag_is(token, kStartTag, GUMBO_TAG_SCRIPT)) {
  1978. run_generic_parsing_algorithm(parser, token, GUMBO_LEX_SCRIPT);
  1979. return true;
  1980. } else if (tag_is(token, kEndTag, GUMBO_TAG_HEAD)) {
  1981. GumboNode* head = pop_current_node(parser);
  1982. AVOID_UNUSED_VARIABLE_WARNING(head);
  1983. assert(node_tag_is(head, GUMBO_TAG_HEAD));
  1984. set_insertion_mode(parser, GUMBO_INSERTION_MODE_AFTER_HEAD);
  1985. return true;
  1986. } else if (tag_is(token, kStartTag, GUMBO_TAG_HEAD)) {
  1987. add_parse_error(parser, token);
  1988. ignore_token(parser);
  1989. return false;
  1990. } else if (tag_is(token, kStartTag, GUMBO_TAG_HEAD) ||
  1991. (token->type == GUMBO_TOKEN_END_TAG &&
  1992. !tag_in(token, kEndTag, GUMBO_TAG_BODY, GUMBO_TAG_HTML,
  1993. GUMBO_TAG_BR, GUMBO_TAG_LAST))) {
  1994. add_parse_error(parser, token);
  1995. return false;
  1996. } else {
  1997. const GumboNode* node = pop_current_node(parser);
  1998. assert(node_tag_is(node, GUMBO_TAG_HEAD));
  1999. AVOID_UNUSED_VARIABLE_WARNING(node);
  2000. set_insertion_mode(parser, GUMBO_INSERTION_MODE_AFTER_HEAD);
  2001. parser->_parser_state->_reprocess_current_token = true;
  2002. return true;
  2003. }
  2004. return true;
  2005. }
  2006. // http://www.whatwg.org/specs/web-apps/current-work/complete/tokenization.html#parsing-main-inheadnoscript
  2007. static bool handle_in_head_noscript(GumboParser* parser, GumboToken* token) {
  2008. if (token->type == GUMBO_TOKEN_DOCTYPE) {
  2009. add_parse_error(parser, token);
  2010. return false;
  2011. } else if (tag_is(token, kStartTag, GUMBO_TAG_HTML)) {
  2012. return handle_in_body(parser, token);
  2013. } else if (tag_is(token, kEndTag, GUMBO_TAG_NOSCRIPT)) {
  2014. const GumboNode* node = pop_current_node(parser);
  2015. assert(node_tag_is(node, GUMBO_TAG_NOSCRIPT));
  2016. AVOID_UNUSED_VARIABLE_WARNING(node);
  2017. set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_HEAD);
  2018. return true;
  2019. } else if (token->type == GUMBO_TOKEN_WHITESPACE ||
  2020. token->type == GUMBO_TOKEN_COMMENT ||
  2021. tag_in(token, kStartTag, GUMBO_TAG_BASEFONT, GUMBO_TAG_BGSOUND,
  2022. GUMBO_TAG_LINK, GUMBO_TAG_META, GUMBO_TAG_NOFRAMES,
  2023. GUMBO_TAG_STYLE, GUMBO_TAG_LAST)) {
  2024. return handle_in_head(parser, token);
  2025. } else if (tag_in(token, kStartTag, GUMBO_TAG_HEAD, GUMBO_TAG_NOSCRIPT,
  2026. GUMBO_TAG_LAST) ||
  2027. (token->type == GUMBO_TOKEN_END_TAG &&
  2028. !tag_is(token, kEndTag, GUMBO_TAG_BR))) {
  2029. add_parse_error(parser, token);
  2030. ignore_token(parser);
  2031. return false;
  2032. } else {
  2033. add_parse_error(parser, token);
  2034. const GumboNode* node = pop_current_node(parser);
  2035. assert(node_tag_is(node, GUMBO_TAG_NOSCRIPT));
  2036. AVOID_UNUSED_VARIABLE_WARNING(node);
  2037. set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_HEAD);
  2038. parser->_parser_state->_reprocess_current_token = true;
  2039. return false;
  2040. }
  2041. }
  2042. // http://www.whatwg.org/specs/web-apps/current-work/complete/tokenization.html#the-after-head-insertion-mode
  2043. static bool handle_after_head(GumboParser* parser, GumboToken* token) {
  2044. GumboParserState* state = parser->_parser_state;
  2045. if (token->type == GUMBO_TOKEN_WHITESPACE) {
  2046. insert_text_token(parser, token);
  2047. return true;
  2048. } else if (token->type == GUMBO_TOKEN_DOCTYPE) {
  2049. add_parse_error(parser, token);
  2050. ignore_token(parser);
  2051. return false;
  2052. } else if (token->type == GUMBO_TOKEN_COMMENT) {
  2053. append_comment_node(parser, get_current_node(parser), token);
  2054. return true;
  2055. } else if (tag_is(token, kStartTag, GUMBO_TAG_HTML)) {
  2056. return handle_in_body(parser, token);
  2057. } else if (tag_is(token, kStartTag, GUMBO_TAG_BODY)) {
  2058. insert_element_from_token(parser, token);
  2059. state->_frameset_ok = false;
  2060. set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_BODY);
  2061. return true;
  2062. } else if (tag_is(token, kStartTag, GUMBO_TAG_FRAMESET)) {
  2063. insert_element_from_token(parser, token);
  2064. set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_FRAMESET);
  2065. return true;
  2066. } else if (tag_in(token, kStartTag, GUMBO_TAG_BASE, GUMBO_TAG_BASEFONT,
  2067. GUMBO_TAG_BGSOUND, GUMBO_TAG_LINK, GUMBO_TAG_META,
  2068. GUMBO_TAG_NOFRAMES, GUMBO_TAG_SCRIPT, GUMBO_TAG_STYLE,
  2069. GUMBO_TAG_TITLE, GUMBO_TAG_LAST)) {
  2070. add_parse_error(parser, token);
  2071. assert(state->_head_element != NULL);
  2072. // This must be flushed before we push the head element on, as there may be
  2073. // pending character tokens that should be attached to the root.
  2074. maybe_flush_text_node_buffer(parser);
  2075. gumbo_vector_add(parser, state->_head_element, &state->_open_elements);
  2076. bool result = handle_in_head(parser, token);
  2077. gumbo_vector_remove(parser, state->_head_element, &state->_open_elements);
  2078. return result;
  2079. } else if (tag_is(token, kStartTag, GUMBO_TAG_HEAD) ||
  2080. (token->type == GUMBO_TOKEN_END_TAG &&
  2081. !tag_in(token, kEndTag, GUMBO_TAG_BODY, GUMBO_TAG_HTML,
  2082. GUMBO_TAG_BR, GUMBO_TAG_LAST))) {
  2083. add_parse_error(parser, token);
  2084. ignore_token(parser);
  2085. return false;
  2086. } else {
  2087. insert_element_of_tag_type(parser, GUMBO_TAG_BODY, GUMBO_INSERTION_IMPLIED);
  2088. set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_BODY);
  2089. state->_reprocess_current_token = true;
  2090. return true;
  2091. }
  2092. }
  2093. static void destroy_node(GumboParser* parser, GumboNode* node) {
  2094. switch (node->type) {
  2095. case GUMBO_NODE_DOCUMENT:
  2096. {
  2097. GumboDocument* doc = &node->v.document;
  2098. for (int i = 0; i < doc->children.length; ++i) {
  2099. destroy_node(parser, doc->children.data[i]);
  2100. }
  2101. gumbo_parser_deallocate(parser, (void*) doc->children.data);
  2102. gumbo_parser_deallocate(parser, (void*) doc->name);
  2103. gumbo_parser_deallocate(parser, (void*) doc->public_identifier);
  2104. gumbo_parser_deallocate(parser, (void*) doc->system_identifier);
  2105. }
  2106. break;
  2107. case GUMBO_NODE_ELEMENT:
  2108. for (int i = 0; i < node->v.element.attributes.length; ++i) {
  2109. gumbo_destroy_attribute(parser, node->v.element.attributes.data[i]);
  2110. }
  2111. gumbo_parser_deallocate(parser, node->v.element.attributes.data);
  2112. for (int i = 0; i < node->v.element.children.length; ++i) {
  2113. destroy_node(parser, node->v.element.children.data[i]);
  2114. }
  2115. gumbo_parser_deallocate(parser, node->v.element.children.data);
  2116. break;
  2117. case GUMBO_NODE_TEXT:
  2118. case GUMBO_NODE_CDATA:
  2119. case GUMBO_NODE_COMMENT:
  2120. case GUMBO_NODE_WHITESPACE:
  2121. gumbo_parser_deallocate(parser, (void*) node->v.text.text);
  2122. break;
  2123. }
  2124. gumbo_parser_deallocate(parser, node);
  2125. }
  2126. // http://www.whatwg.org/specs/web-apps/current-work/complete/tokenization.html#parsing-main-inbody
  2127. static bool handle_in_body(GumboParser* parser, GumboToken* token) {
  2128. GumboParserState* state = parser->_parser_state;
  2129. assert(state->_open_elements.length > 0);
  2130. if (token->type == GUMBO_TOKEN_NULL) {
  2131. add_parse_error(parser, token);
  2132. ignore_token(parser);
  2133. return false;
  2134. } else if (token->type == GUMBO_TOKEN_WHITESPACE) {
  2135. reconstruct_active_formatting_elements(parser);
  2136. insert_text_token(parser, token);
  2137. return true;
  2138. } else if (token->type == GUMBO_TOKEN_CHARACTER) {
  2139. reconstruct_active_formatting_elements(parser);
  2140. insert_text_token(parser, token);
  2141. set_frameset_not_ok(parser);
  2142. return true;
  2143. } else if (token->type == GUMBO_TOKEN_COMMENT) {
  2144. append_comment_node(parser, get_current_node(parser), token);
  2145. return true;
  2146. } else if (token->type == GUMBO_TOKEN_DOCTYPE) {
  2147. add_parse_error(parser, token);
  2148. ignore_token(parser);
  2149. return false;
  2150. } else if (tag_is(token, kStartTag, GUMBO_TAG_HTML)) {
  2151. assert(parser->_output->root != NULL);
  2152. assert(parser->_output->root->type == GUMBO_NODE_ELEMENT);
  2153. add_parse_error(parser, token);
  2154. merge_attributes(parser, token, parser->_output->root);
  2155. return false;
  2156. } else if (tag_in(token, kStartTag, GUMBO_TAG_BASE, GUMBO_TAG_BASEFONT,
  2157. GUMBO_TAG_BGSOUND, GUMBO_TAG_MENUITEM, GUMBO_TAG_LINK,
  2158. GUMBO_TAG_META, GUMBO_TAG_NOFRAMES, GUMBO_TAG_SCRIPT,
  2159. GUMBO_TAG_STYLE, GUMBO_TAG_TITLE, GUMBO_TAG_LAST)) {
  2160. return handle_in_head(parser, token);
  2161. } else if (tag_is(token, kStartTag, GUMBO_TAG_BODY)) {
  2162. add_parse_error(parser, token);
  2163. if (state->_open_elements.length < 2 ||
  2164. !node_tag_is(state->_open_elements.data[1], GUMBO_TAG_BODY)) {
  2165. ignore_token(parser);
  2166. return false;
  2167. }
  2168. state->_frameset_ok = false;
  2169. merge_attributes(parser, token, state->_open_elements.data[1]);
  2170. return false;
  2171. } else if (tag_is(token, kStartTag, GUMBO_TAG_FRAMESET)) {
  2172. add_parse_error(parser, token);
  2173. if (state->_open_elements.length < 2 ||
  2174. !node_tag_is(state->_open_elements.data[1], GUMBO_TAG_BODY) ||
  2175. !state->_frameset_ok) {
  2176. ignore_token(parser);
  2177. return false;
  2178. }
  2179. // Save the body node for later removal.
  2180. GumboNode* body_node = state->_open_elements.data[1];
  2181. // Pop all nodes except root HTML element.
  2182. GumboNode* node;
  2183. do {
  2184. node = pop_current_node(parser);
  2185. } while (node != state->_open_elements.data[1]);
  2186. // Removing & destroying the body node is going to kill any nodes that have
  2187. // been added to the list of active formatting elements, and so we should
  2188. // clear it to prevent a use-after-free if the list of active formatting
  2189. // elements is reconstructed afterwards. This may happen if whitespace
  2190. // follows the </frameset>.
  2191. clear_active_formatting_elements(parser);
  2192. // Remove the body node. We may want to factor this out into a generic
  2193. // helper, but right now this is the only code that needs to do this.
  2194. GumboVector* children = &parser->_output->root->v.element.children;
  2195. for (int i = 0; i < children->length; ++i) {
  2196. if (children->data[i] == body_node) {
  2197. gumbo_vector_remove_at(parser, i, children);
  2198. break;
  2199. }
  2200. }
  2201. destroy_node(parser, body_node);
  2202. // Insert the <frameset>, and switch the insertion mode.
  2203. insert_element_from_token(parser, token);
  2204. set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_FRAMESET);
  2205. return true;
  2206. } else if (token->type == GUMBO_TOKEN_EOF) {
  2207. for (int i = 0; i < state->_open_elements.length; ++i) {
  2208. if (!node_tag_in(state->_open_elements.data[i], GUMBO_TAG_DD,
  2209. GUMBO_TAG_DT, GUMBO_TAG_LI, GUMBO_TAG_P, GUMBO_TAG_TBODY,
  2210. GUMBO_TAG_TD, GUMBO_TAG_TFOOT, GUMBO_TAG_TH,
  2211. GUMBO_TAG_THEAD, GUMBO_TAG_TR, GUMBO_TAG_BODY,
  2212. GUMBO_TAG_HTML, GUMBO_TAG_LAST)) {
  2213. add_parse_error(parser, token);
  2214. return false;
  2215. }
  2216. }
  2217. return true;
  2218. } else if (tag_in(token, kEndTag, GUMBO_TAG_BODY, GUMBO_TAG_HTML,
  2219. GUMBO_TAG_LAST)) {
  2220. if (!has_an_element_in_scope(parser, GUMBO_TAG_BODY)) {
  2221. add_parse_error(parser, token);
  2222. ignore_token(parser);
  2223. return false;
  2224. }
  2225. bool success = true;
  2226. for (int i = 0; i < state->_open_elements.length; ++i) {
  2227. if (!node_tag_in(state->_open_elements.data[i], GUMBO_TAG_DD,
  2228. GUMBO_TAG_DT, GUMBO_TAG_LI, GUMBO_TAG_OPTGROUP,
  2229. GUMBO_TAG_OPTION, GUMBO_TAG_P, GUMBO_TAG_RP,
  2230. GUMBO_TAG_RT, GUMBO_TAG_TBODY, GUMBO_TAG_TD,
  2231. GUMBO_TAG_TFOOT, GUMBO_TAG_TH, GUMBO_TAG_THEAD,
  2232. GUMBO_TAG_TR, GUMBO_TAG_BODY, GUMBO_TAG_HTML,
  2233. GUMBO_TAG_LAST)) {
  2234. add_parse_error(parser, token);
  2235. success = false;
  2236. break;
  2237. }
  2238. }
  2239. set_insertion_mode(parser, GUMBO_INSERTION_MODE_AFTER_BODY);
  2240. if (tag_is(token, kEndTag, GUMBO_TAG_HTML)) {
  2241. parser->_parser_state->_reprocess_current_token = true;
  2242. } else {
  2243. GumboNode* body = state->_open_elements.data[1];
  2244. assert(node_tag_is(body, GUMBO_TAG_BODY));
  2245. record_end_of_element(state->_current_token, &body->v.element);
  2246. }
  2247. return success;
  2248. } else if (tag_in(token, kStartTag, GUMBO_TAG_ADDRESS, GUMBO_TAG_ARTICLE,
  2249. GUMBO_TAG_ASIDE, GUMBO_TAG_BLOCKQUOTE, GUMBO_TAG_CENTER,
  2250. GUMBO_TAG_DETAILS, GUMBO_TAG_DIR, GUMBO_TAG_DIV,
  2251. GUMBO_TAG_DL, GUMBO_TAG_FIELDSET, GUMBO_TAG_FIGCAPTION,
  2252. GUMBO_TAG_FIGURE, GUMBO_TAG_FOOTER, GUMBO_TAG_HEADER,
  2253. GUMBO_TAG_HGROUP, GUMBO_TAG_MENU, GUMBO_TAG_NAV,
  2254. GUMBO_TAG_OL, GUMBO_TAG_P, GUMBO_TAG_SECTION,
  2255. GUMBO_TAG_SUMMARY, GUMBO_TAG_UL, GUMBO_TAG_LAST)) {
  2256. bool result = maybe_implicitly_close_p_tag(parser, token);
  2257. insert_element_from_token(parser, token);
  2258. return result;
  2259. } else if (tag_in(token, kStartTag, GUMBO_TAG_H1, GUMBO_TAG_H2, GUMBO_TAG_H3,
  2260. GUMBO_TAG_H4, GUMBO_TAG_H5, GUMBO_TAG_H6, GUMBO_TAG_LAST)) {
  2261. bool result = maybe_implicitly_close_p_tag(parser, token);
  2262. if (node_tag_in(get_current_node(parser), GUMBO_TAG_H1, GUMBO_TAG_H2,
  2263. GUMBO_TAG_H3, GUMBO_TAG_H4, GUMBO_TAG_H5, GUMBO_TAG_H6,
  2264. GUMBO_TAG_LAST)) {
  2265. add_parse_error(parser, token);
  2266. pop_current_node(parser);
  2267. result = false;
  2268. }
  2269. insert_element_from_token(parser, token);
  2270. return result;
  2271. } else if (tag_in(token, kStartTag, GUMBO_TAG_PRE, GUMBO_TAG_LISTING,
  2272. GUMBO_TAG_LAST)) {
  2273. bool result = maybe_implicitly_close_p_tag(parser, token);
  2274. insert_element_from_token(parser, token);
  2275. state->_ignore_next_linefeed = true;
  2276. state->_frameset_ok = false;
  2277. return result;
  2278. } else if (tag_is(token, kStartTag, GUMBO_TAG_FORM)) {
  2279. if (state->_form_element != NULL) {
  2280. gumbo_debug("Ignoring nested form.\n");
  2281. add_parse_error(parser, token);
  2282. ignore_token(parser);
  2283. return false;
  2284. }
  2285. bool result = maybe_implicitly_close_p_tag(parser, token);
  2286. state->_form_element =
  2287. insert_element_from_token(parser, token);
  2288. return result;
  2289. } else if (tag_is(token, kStartTag, GUMBO_TAG_LI)) {
  2290. maybe_implicitly_close_list_tag(parser, token, true);
  2291. bool result = maybe_implicitly_close_p_tag(parser, token);
  2292. insert_element_from_token(parser, token);
  2293. return result;
  2294. } else if (tag_in(token, kStartTag, GUMBO_TAG_DD, GUMBO_TAG_DT,
  2295. GUMBO_TAG_LAST)) {
  2296. maybe_implicitly_close_list_tag(parser, token, false);
  2297. bool result = maybe_implicitly_close_p_tag(parser, token);
  2298. insert_element_from_token(parser, token);
  2299. return result;
  2300. } else if (tag_is(token, kStartTag, GUMBO_TAG_PLAINTEXT)) {
  2301. bool result = maybe_implicitly_close_p_tag(parser, token);
  2302. insert_element_from_token(parser, token);
  2303. gumbo_tokenizer_set_state(parser, GUMBO_LEX_PLAINTEXT);
  2304. return result;
  2305. } else if (tag_is(token, kStartTag, GUMBO_TAG_BUTTON)) {
  2306. if (has_an_element_in_scope(parser, GUMBO_TAG_BUTTON)) {
  2307. add_parse_error(parser, token);
  2308. implicitly_close_tags(parser, token, GUMBO_TAG_BUTTON);
  2309. state->_reprocess_current_token = true;
  2310. return false;
  2311. }
  2312. reconstruct_active_formatting_elements(parser);
  2313. insert_element_from_token(parser, token);
  2314. state->_frameset_ok = false;
  2315. return true;
  2316. } else if (tag_in(token, kEndTag, GUMBO_TAG_ADDRESS, GUMBO_TAG_ARTICLE,
  2317. GUMBO_TAG_ASIDE, GUMBO_TAG_BLOCKQUOTE, GUMBO_TAG_BUTTON,
  2318. GUMBO_TAG_CENTER, GUMBO_TAG_DETAILS, GUMBO_TAG_DIR,
  2319. GUMBO_TAG_DIV, GUMBO_TAG_DL, GUMBO_TAG_FIELDSET,
  2320. GUMBO_TAG_FIGCAPTION, GUMBO_TAG_FIGURE, GUMBO_TAG_FOOTER,
  2321. GUMBO_TAG_HEADER, GUMBO_TAG_HGROUP, GUMBO_TAG_LISTING,
  2322. GUMBO_TAG_MENU, GUMBO_TAG_NAV, GUMBO_TAG_OL, GUMBO_TAG_PRE,
  2323. GUMBO_TAG_SECTION, GUMBO_TAG_SUMMARY, GUMBO_TAG_UL,
  2324. GUMBO_TAG_LAST)) {
  2325. GumboTag tag = token->v.end_tag;
  2326. if (!has_an_element_in_scope(parser, tag)) {
  2327. add_parse_error(parser, token);
  2328. ignore_token(parser);
  2329. return false;
  2330. }
  2331. implicitly_close_tags(parser, token, token->v.end_tag);
  2332. return true;
  2333. } else if (tag_is(token, kEndTag, GUMBO_TAG_FORM)) {
  2334. bool result = true;
  2335. const GumboNode* node = state->_form_element;
  2336. assert(!node || node->type == GUMBO_NODE_ELEMENT);
  2337. state->_form_element = NULL;
  2338. if (!node || !has_node_in_scope(parser, node)) {
  2339. gumbo_debug("Closing an unopened form.\n");
  2340. add_parse_error(parser, token);
  2341. ignore_token(parser);
  2342. return false;
  2343. }
  2344. // This differs from implicitly_close_tags because we remove *only* the
  2345. // <form> element; other nodes are left in scope.
  2346. generate_implied_end_tags(parser, GUMBO_TAG_LAST);
  2347. if (get_current_node(parser) != node) {
  2348. add_parse_error(parser, token);
  2349. result = false;
  2350. }
  2351. GumboVector* open_elements = &state->_open_elements;
  2352. int index = open_elements->length - 1;
  2353. for (; index >= 0 && open_elements->data[index] != node; --index);
  2354. assert(index >= 0);
  2355. gumbo_vector_remove_at(parser, index, open_elements);
  2356. return result;
  2357. } else if (tag_is(token, kEndTag, GUMBO_TAG_P)) {
  2358. if (!has_an_element_in_button_scope(parser, GUMBO_TAG_P)) {
  2359. add_parse_error(parser, token);
  2360. reconstruct_active_formatting_elements(parser);
  2361. insert_element_of_tag_type(
  2362. parser, GUMBO_TAG_P, GUMBO_INSERTION_CONVERTED_FROM_END_TAG);
  2363. state->_reprocess_current_token = true;
  2364. return false;
  2365. }
  2366. return implicitly_close_tags(parser, token, GUMBO_TAG_P);
  2367. } else if (tag_is(token, kEndTag, GUMBO_TAG_LI)) {
  2368. if (!has_an_element_in_list_scope(parser, GUMBO_TAG_LI)) {
  2369. add_parse_error(parser, token);
  2370. ignore_token(parser);
  2371. return false;
  2372. }
  2373. return implicitly_close_tags(parser, token, GUMBO_TAG_LI);
  2374. } else if (tag_in(token, kEndTag, GUMBO_TAG_DD, GUMBO_TAG_DT,
  2375. GUMBO_TAG_LAST)) {
  2376. assert(token->type == GUMBO_TOKEN_END_TAG);
  2377. GumboTag token_tag = token->v.end_tag;
  2378. if (!has_an_element_in_scope(parser, token_tag)) {
  2379. add_parse_error(parser, token);
  2380. ignore_token(parser);
  2381. return false;
  2382. }
  2383. return implicitly_close_tags(parser, token, token_tag);
  2384. } else if (tag_in(token, kEndTag, GUMBO_TAG_H1, GUMBO_TAG_H2, GUMBO_TAG_H3,
  2385. GUMBO_TAG_H4, GUMBO_TAG_H5, GUMBO_TAG_H6, GUMBO_TAG_LAST)) {
  2386. if (!has_an_element_in_scope_with_tagname(
  2387. parser, GUMBO_TAG_H1, GUMBO_TAG_H2, GUMBO_TAG_H3, GUMBO_TAG_H4,
  2388. GUMBO_TAG_H5, GUMBO_TAG_H6, GUMBO_TAG_LAST)) {
  2389. // No heading open; ignore the token entirely.
  2390. add_parse_error(parser, token);
  2391. ignore_token(parser);
  2392. return false;
  2393. } else {
  2394. generate_implied_end_tags(parser, GUMBO_TAG_LAST);
  2395. const GumboNode* current_node = get_current_node(parser);
  2396. bool success = node_tag_is(current_node, token->v.end_tag);
  2397. if (!success) {
  2398. // There're children of the heading currently open; close them below and
  2399. // record a parse error.
  2400. // TODO(jdtang): Add a way to distinguish this error case from the one
  2401. // above.
  2402. add_parse_error(parser, token);
  2403. }
  2404. do {
  2405. current_node = pop_current_node(parser);
  2406. } while (!node_tag_in(current_node, GUMBO_TAG_H1, GUMBO_TAG_H2,
  2407. GUMBO_TAG_H3, GUMBO_TAG_H4, GUMBO_TAG_H5,
  2408. GUMBO_TAG_H6, GUMBO_TAG_LAST));
  2409. return success;
  2410. }
  2411. } else if (tag_is(token, kStartTag, GUMBO_TAG_A)) {
  2412. bool success = true;
  2413. int last_a;
  2414. int has_matching_a = find_last_anchor_index(parser, &last_a);
  2415. if (has_matching_a) {
  2416. assert(has_matching_a == 1);
  2417. add_parse_error(parser, token);
  2418. adoption_agency_algorithm(parser, token, GUMBO_TAG_A);
  2419. // The adoption agency algorithm usually removes all instances of <a>
  2420. // from the list of active formatting elements, but in case it doesn't,
  2421. // we're supposed to do this. (The conditions where it might not are
  2422. // listed in the spec.)
  2423. if (find_last_anchor_index(parser, &last_a)) {
  2424. void* last_element = gumbo_vector_remove_at(
  2425. parser, last_a, &state->_active_formatting_elements);
  2426. gumbo_vector_remove(
  2427. parser, last_element, &state->_open_elements);
  2428. }
  2429. success = false;
  2430. }
  2431. reconstruct_active_formatting_elements(parser);
  2432. add_formatting_element(parser, insert_element_from_token(parser, token));
  2433. return success;
  2434. } else if (tag_in(token, kStartTag, GUMBO_TAG_B, GUMBO_TAG_BIG,
  2435. GUMBO_TAG_CODE, GUMBO_TAG_EM, GUMBO_TAG_FONT, GUMBO_TAG_I,
  2436. GUMBO_TAG_S, GUMBO_TAG_SMALL, GUMBO_TAG_STRIKE,
  2437. GUMBO_TAG_STRONG, GUMBO_TAG_TT, GUMBO_TAG_U,
  2438. GUMBO_TAG_LAST)) {
  2439. reconstruct_active_formatting_elements(parser);
  2440. add_formatting_element(parser, insert_element_from_token(parser, token));
  2441. return true;
  2442. } else if (tag_is(token, kStartTag, GUMBO_TAG_NOBR)) {
  2443. bool result = true;
  2444. reconstruct_active_formatting_elements(parser);
  2445. if (has_an_element_in_scope(parser, GUMBO_TAG_NOBR)) {
  2446. result = false;
  2447. add_parse_error(parser, token);
  2448. adoption_agency_algorithm(parser, token, GUMBO_TAG_NOBR);
  2449. reconstruct_active_formatting_elements(parser);
  2450. }
  2451. insert_element_from_token(parser, token);
  2452. add_formatting_element(parser, get_current_node(parser));
  2453. return result;
  2454. } else if (tag_in(token, kEndTag, GUMBO_TAG_A, GUMBO_TAG_B, GUMBO_TAG_BIG,
  2455. GUMBO_TAG_CODE, GUMBO_TAG_EM, GUMBO_TAG_FONT, GUMBO_TAG_I,
  2456. GUMBO_TAG_NOBR, GUMBO_TAG_S, GUMBO_TAG_SMALL,
  2457. GUMBO_TAG_STRIKE, GUMBO_TAG_STRONG, GUMBO_TAG_TT,
  2458. GUMBO_TAG_U, GUMBO_TAG_LAST)) {
  2459. return adoption_agency_algorithm(parser, token, token->v.end_tag);
  2460. } else if (tag_in(token, kStartTag, GUMBO_TAG_APPLET, GUMBO_TAG_MARQUEE,
  2461. GUMBO_TAG_OBJECT, GUMBO_TAG_LAST)) {
  2462. reconstruct_active_formatting_elements(parser);
  2463. insert_element_from_token(parser, token);
  2464. add_formatting_element(parser, &kActiveFormattingScopeMarker);
  2465. set_frameset_not_ok(parser);
  2466. return true;
  2467. } else if (tag_in(token, kEndTag, GUMBO_TAG_APPLET, GUMBO_TAG_MARQUEE,
  2468. GUMBO_TAG_OBJECT, GUMBO_TAG_LAST)) {
  2469. GumboTag token_tag = token->v.end_tag;
  2470. if (!has_an_element_in_table_scope(parser, token_tag)) {
  2471. add_parse_error(parser, token);
  2472. ignore_token(parser);
  2473. return false;
  2474. }
  2475. implicitly_close_tags(parser, token, token_tag);
  2476. clear_active_formatting_elements(parser);
  2477. return true;
  2478. } else if (tag_is(token, kStartTag, GUMBO_TAG_TABLE)) {
  2479. if (get_document_node(parser)->v.document.doc_type_quirks_mode !=
  2480. GUMBO_DOCTYPE_QUIRKS) {
  2481. maybe_implicitly_close_p_tag(parser, token);
  2482. }
  2483. insert_element_from_token(parser, token);
  2484. set_frameset_not_ok(parser);
  2485. set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_TABLE);
  2486. return true;
  2487. } else if (tag_in(token, kStartTag, GUMBO_TAG_AREA, GUMBO_TAG_BR,
  2488. GUMBO_TAG_EMBED, GUMBO_TAG_IMG, GUMBO_TAG_IMAGE,
  2489. GUMBO_TAG_KEYGEN, GUMBO_TAG_WBR, GUMBO_TAG_LAST)) {
  2490. bool success = true;
  2491. if (tag_is(token, kStartTag, GUMBO_TAG_IMAGE)) {
  2492. success = false;
  2493. add_parse_error(parser, token);
  2494. token->v.start_tag.tag = GUMBO_TAG_IMG;
  2495. }
  2496. reconstruct_active_formatting_elements(parser);
  2497. GumboNode* node = insert_element_from_token(parser, token);
  2498. if (tag_is(token, kStartTag, GUMBO_TAG_IMAGE)) {
  2499. success = false;
  2500. add_parse_error(parser, token);
  2501. node->v.element.tag = GUMBO_TAG_IMG;
  2502. node->parse_flags |= GUMBO_INSERTION_FROM_IMAGE;
  2503. }
  2504. pop_current_node(parser);
  2505. acknowledge_self_closing_tag(parser);
  2506. set_frameset_not_ok(parser);
  2507. return success;
  2508. } else if (tag_is(token, kStartTag, GUMBO_TAG_INPUT)) {
  2509. if (!attribute_matches(&token->v.start_tag.attributes, "type", "hidden")) {
  2510. // Must be before the element is inserted, as that takes ownership of the
  2511. // token's attribute vector.
  2512. set_frameset_not_ok(parser);
  2513. }
  2514. reconstruct_active_formatting_elements(parser);
  2515. insert_element_from_token(parser, token);
  2516. pop_current_node(parser);
  2517. acknowledge_self_closing_tag(parser);
  2518. return true;
  2519. } else if (tag_in(token, kStartTag, GUMBO_TAG_PARAM, GUMBO_TAG_SOURCE,
  2520. GUMBO_TAG_TRACK, GUMBO_TAG_LAST)) {
  2521. insert_element_from_token(parser, token);
  2522. pop_current_node(parser);
  2523. acknowledge_self_closing_tag(parser);
  2524. return true;
  2525. } else if (tag_is(token, kStartTag, GUMBO_TAG_HR)) {
  2526. bool result = maybe_implicitly_close_p_tag(parser, token);
  2527. insert_element_from_token(parser, token);
  2528. pop_current_node(parser);
  2529. acknowledge_self_closing_tag(parser);
  2530. set_frameset_not_ok(parser);
  2531. return result;
  2532. } else if (tag_is(token, kStartTag, GUMBO_TAG_ISINDEX)) {
  2533. add_parse_error(parser, token);
  2534. if (parser->_parser_state->_form_element != NULL) {
  2535. ignore_token(parser);
  2536. return false;
  2537. }
  2538. acknowledge_self_closing_tag(parser);
  2539. maybe_implicitly_close_p_tag(parser, token);
  2540. set_frameset_not_ok(parser);
  2541. GumboVector* token_attrs = &token->v.start_tag.attributes;
  2542. GumboAttribute* prompt_attr = gumbo_get_attribute(token_attrs, "prompt");
  2543. GumboAttribute* action_attr = gumbo_get_attribute(token_attrs, "action");
  2544. GumboAttribute* name_attr = gumbo_get_attribute(token_attrs, "isindex");
  2545. GumboNode* form = insert_element_of_tag_type(
  2546. parser, GUMBO_TAG_FORM, GUMBO_INSERTION_FROM_ISINDEX);
  2547. if (action_attr) {
  2548. gumbo_vector_add(parser, action_attr, &form->v.element.attributes);
  2549. }
  2550. insert_element_of_tag_type(parser, GUMBO_TAG_HR,
  2551. GUMBO_INSERTION_FROM_ISINDEX);
  2552. pop_current_node(parser); // <hr>
  2553. insert_element_of_tag_type(parser, GUMBO_TAG_LABEL,
  2554. GUMBO_INSERTION_FROM_ISINDEX);
  2555. TextNodeBufferState* text_state = &parser->_parser_state->_text_node;
  2556. text_state->_start_original_text = token->original_text.data;
  2557. text_state->_start_position = token->position;
  2558. text_state->_type = GUMBO_NODE_TEXT;
  2559. if (prompt_attr) {
  2560. int prompt_attr_length = strlen(prompt_attr->value);
  2561. gumbo_string_buffer_destroy(parser, &text_state->_buffer);
  2562. text_state->_buffer.data = gumbo_copy_stringz(parser, prompt_attr->value);
  2563. text_state->_buffer.length = prompt_attr_length;
  2564. text_state->_buffer.capacity = prompt_attr_length + 1;
  2565. gumbo_destroy_attribute(parser, prompt_attr);
  2566. } else {
  2567. GumboStringPiece prompt_text = GUMBO_STRING(
  2568. "This is a searchable index. Enter search keywords: ");
  2569. gumbo_string_buffer_append_string(
  2570. parser, &prompt_text, &text_state->_buffer);
  2571. }
  2572. GumboNode* input = insert_element_of_tag_type(
  2573. parser, GUMBO_TAG_INPUT, GUMBO_INSERTION_FROM_ISINDEX);
  2574. for (int i = 0; i < token_attrs->length; ++i) {
  2575. GumboAttribute* attr = token_attrs->data[i];
  2576. if (attr != prompt_attr && attr != action_attr && attr != name_attr) {
  2577. gumbo_vector_add(parser, attr, &input->v.element.attributes);
  2578. }
  2579. token_attrs->data[i] = NULL;
  2580. }
  2581. // All attributes have been successfully transferred and nulled out at this
  2582. // point, so the call to ignore_token will free the memory for it without
  2583. // touching the attributes.
  2584. ignore_token(parser);
  2585. GumboAttribute* name =
  2586. gumbo_parser_allocate(parser, sizeof(GumboAttribute));
  2587. GumboStringPiece name_str = GUMBO_STRING("name");
  2588. GumboStringPiece isindex_str = GUMBO_STRING("isindex");
  2589. name->attr_namespace = GUMBO_ATTR_NAMESPACE_NONE;
  2590. name->name = gumbo_copy_stringz(parser, "name");
  2591. name->value = gumbo_copy_stringz(parser, "isindex");
  2592. name->original_name = name_str;
  2593. name->original_value = isindex_str;
  2594. name->name_start = kGumboEmptySourcePosition;
  2595. name->name_end = kGumboEmptySourcePosition;
  2596. name->value_start = kGumboEmptySourcePosition;
  2597. name->value_end = kGumboEmptySourcePosition;
  2598. gumbo_vector_add(parser, name, &input->v.element.attributes);
  2599. pop_current_node(parser); // <input>
  2600. pop_current_node(parser); // <label>
  2601. insert_element_of_tag_type(
  2602. parser, GUMBO_TAG_HR, GUMBO_INSERTION_FROM_ISINDEX);
  2603. pop_current_node(parser); // <hr>
  2604. pop_current_node(parser); // <form>
  2605. return false;
  2606. } else if (tag_is(token, kStartTag, GUMBO_TAG_TEXTAREA)) {
  2607. run_generic_parsing_algorithm(parser, token, GUMBO_LEX_RCDATA);
  2608. parser->_parser_state->_ignore_next_linefeed = true;
  2609. set_frameset_not_ok(parser);
  2610. return true;
  2611. } else if (tag_is(token, kStartTag, GUMBO_TAG_XMP)) {
  2612. bool result = maybe_implicitly_close_p_tag(parser, token);
  2613. reconstruct_active_formatting_elements(parser);
  2614. set_frameset_not_ok(parser);
  2615. run_generic_parsing_algorithm(parser, token, GUMBO_LEX_RAWTEXT);
  2616. return result;
  2617. } else if (tag_is(token, kStartTag, GUMBO_TAG_IFRAME)) {
  2618. set_frameset_not_ok(parser);
  2619. run_generic_parsing_algorithm(parser, token, GUMBO_LEX_RAWTEXT);
  2620. return true;
  2621. } else if (tag_is(token, kStartTag, GUMBO_TAG_NOEMBED)) {
  2622. run_generic_parsing_algorithm(parser, token, GUMBO_LEX_RAWTEXT);
  2623. return true;
  2624. } else if (tag_is(token, kStartTag, GUMBO_TAG_SELECT)) {
  2625. reconstruct_active_formatting_elements(parser);
  2626. insert_element_from_token(parser, token);
  2627. set_frameset_not_ok(parser);
  2628. GumboInsertionMode state = parser->_parser_state->_insertion_mode;
  2629. if (state == GUMBO_INSERTION_MODE_IN_TABLE ||
  2630. state == GUMBO_INSERTION_MODE_IN_CAPTION ||
  2631. state == GUMBO_INSERTION_MODE_IN_TABLE_BODY ||
  2632. state == GUMBO_INSERTION_MODE_IN_ROW ||
  2633. state == GUMBO_INSERTION_MODE_IN_CELL) {
  2634. set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_SELECT_IN_TABLE);
  2635. } else {
  2636. set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_SELECT);
  2637. }
  2638. return true;
  2639. } else if (tag_in(token, kStartTag, GUMBO_TAG_OPTION, GUMBO_TAG_OPTGROUP,
  2640. GUMBO_TAG_LAST)) {
  2641. if (node_tag_is(get_current_node(parser), GUMBO_TAG_OPTION)) {
  2642. pop_current_node(parser);
  2643. }
  2644. reconstruct_active_formatting_elements(parser);
  2645. insert_element_from_token(parser, token);
  2646. return true;
  2647. } else if (tag_in(token, kStartTag, GUMBO_TAG_RP, GUMBO_TAG_RT,
  2648. GUMBO_TAG_LAST)) {
  2649. bool success = true;
  2650. if (has_an_element_in_scope(parser, GUMBO_TAG_RUBY)) {
  2651. generate_implied_end_tags(parser, GUMBO_TAG_LAST);
  2652. }
  2653. if (!node_tag_is(get_current_node(parser), GUMBO_TAG_RUBY)) {
  2654. add_parse_error(parser, token);
  2655. success = false;
  2656. }
  2657. insert_element_from_token(parser, token);
  2658. return success;
  2659. } else if (tag_is(token, kEndTag, GUMBO_TAG_BR)) {
  2660. add_parse_error(parser, token);
  2661. reconstruct_active_formatting_elements(parser);
  2662. insert_element_of_tag_type(
  2663. parser, GUMBO_TAG_BR, GUMBO_INSERTION_CONVERTED_FROM_END_TAG);
  2664. pop_current_node(parser);
  2665. return false;
  2666. } else if (tag_is(token, kStartTag, GUMBO_TAG_MATH)) {
  2667. reconstruct_active_formatting_elements(parser);
  2668. adjust_mathml_attributes(parser, token);
  2669. adjust_foreign_attributes(parser, token);
  2670. insert_foreign_element(parser, token, GUMBO_NAMESPACE_MATHML);
  2671. if (token->v.start_tag.is_self_closing) {
  2672. pop_current_node(parser);
  2673. acknowledge_self_closing_tag(parser);
  2674. }
  2675. return true;
  2676. } else if (tag_is(token, kStartTag, GUMBO_TAG_SVG)) {
  2677. reconstruct_active_formatting_elements(parser);
  2678. adjust_svg_attributes(parser, token);
  2679. adjust_foreign_attributes(parser, token);
  2680. insert_foreign_element(parser, token, GUMBO_NAMESPACE_SVG);
  2681. if (token->v.start_tag.is_self_closing) {
  2682. pop_current_node(parser);
  2683. acknowledge_self_closing_tag(parser);
  2684. }
  2685. return true;
  2686. } else if (tag_in(token, kStartTag, GUMBO_TAG_CAPTION, GUMBO_TAG_COL,
  2687. GUMBO_TAG_COLGROUP, GUMBO_TAG_FRAME, GUMBO_TAG_HEAD,
  2688. GUMBO_TAG_TBODY, GUMBO_TAG_TD, GUMBO_TAG_TFOOT,
  2689. GUMBO_TAG_TH, GUMBO_TAG_THEAD, GUMBO_TAG_TR,
  2690. GUMBO_TAG_LAST)) {
  2691. add_parse_error(parser, token);
  2692. ignore_token(parser);
  2693. return false;
  2694. } else if (token->type == GUMBO_TOKEN_START_TAG) {
  2695. reconstruct_active_formatting_elements(parser);
  2696. insert_element_from_token(parser, token);
  2697. return true;
  2698. } else {
  2699. assert(token->type == GUMBO_TOKEN_END_TAG);
  2700. GumboTag end_tag = token->v.end_tag;
  2701. assert(state->_open_elements.length > 0);
  2702. assert(node_tag_is(state->_open_elements.data[0], GUMBO_TAG_HTML));
  2703. // Walk up the stack of open elements until we find one that either:
  2704. // a) Matches the tag name we saw
  2705. // b) Is in the "special" category.
  2706. // If we see a), implicitly close everything up to and including it. If we
  2707. // see b), then record a parse error, don't close anything (except the
  2708. // implied end tags) and ignore the end tag token.
  2709. for (int i = state->_open_elements.length; --i >= 0; ) {
  2710. const GumboNode* node = state->_open_elements.data[i];
  2711. if (node->v.element.tag_namespace == GUMBO_NAMESPACE_HTML &&
  2712. node_tag_is(node, end_tag)) {
  2713. generate_implied_end_tags(parser, end_tag);
  2714. // TODO(jdtang): Do I need to add a parse error here? The condition in
  2715. // the spec seems like it's the inverse of the loop condition above, and
  2716. // so would never fire.
  2717. while (node != pop_current_node(parser)); // Pop everything.
  2718. return true;
  2719. } else if (is_special_node(node)) {
  2720. add_parse_error(parser, token);
  2721. ignore_token(parser);
  2722. return false;
  2723. }
  2724. }
  2725. // <html> is in the special category, so we should never get here.
  2726. assert(0);
  2727. return false;
  2728. }
  2729. }
  2730. // http://www.whatwg.org/specs/web-apps/current-work/complete/tokenization.html#parsing-main-incdata
  2731. static bool handle_text(GumboParser* parser, GumboToken* token) {
  2732. if (token->type == GUMBO_TOKEN_CHARACTER || token->type == GUMBO_TOKEN_WHITESPACE) {
  2733. insert_text_token(parser, token);
  2734. } else {
  2735. // We provide only bare-bones script handling that doesn't involve any of
  2736. // the parser-pause/already-started/script-nesting flags or re-entrant
  2737. // invocations of the tokenizer. Because the intended usage of this library
  2738. // is mostly for templating, refactoring, and static-analysis libraries, we
  2739. // provide the script body as a text-node child of the <script> element.
  2740. // This behavior doesn't support document.write of partial HTML elements,
  2741. // but should be adequate for almost all other scripting support.
  2742. if (token->type == GUMBO_TOKEN_EOF) {
  2743. add_parse_error(parser, token);
  2744. parser->_parser_state->_reprocess_current_token = true;
  2745. }
  2746. pop_current_node(parser);
  2747. set_insertion_mode(parser, parser->_parser_state->_original_insertion_mode);
  2748. }
  2749. return true;
  2750. }
  2751. // http://www.whatwg.org/specs/web-apps/current-work/complete/tokenization.html#parsing-main-intable
  2752. static bool handle_in_table(GumboParser* parser, GumboToken* token) {
  2753. GumboParserState* state = parser->_parser_state;
  2754. if (token->type == GUMBO_TOKEN_CHARACTER ||
  2755. token->type == GUMBO_TOKEN_WHITESPACE) {
  2756. // The "pending table character tokens" list described in the spec is
  2757. // nothing more than the TextNodeBufferState. We accumulate text tokens as
  2758. // normal, except that when we go to flush them in the handle_in_table_text,
  2759. // we set _foster_parent_insertions if there're non-whitespace characters in
  2760. // the buffer.
  2761. assert(state->_text_node._buffer.length == 0);
  2762. state->_original_insertion_mode = state->_insertion_mode;
  2763. state->_reprocess_current_token = true;
  2764. set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_TABLE_TEXT);
  2765. return true;
  2766. } else if (token->type == GUMBO_TOKEN_DOCTYPE) {
  2767. add_parse_error(parser, token);
  2768. ignore_token(parser);
  2769. return false;
  2770. } else if (token->type == GUMBO_TOKEN_COMMENT) {
  2771. append_comment_node(parser, get_current_node(parser), token);
  2772. return true;
  2773. } else if (tag_is(token, kStartTag, GUMBO_TAG_CAPTION)) {
  2774. clear_stack_to_table_context(parser);
  2775. add_formatting_element(parser, &kActiveFormattingScopeMarker);
  2776. insert_element_from_token(parser, token);
  2777. set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_CAPTION);
  2778. return true;
  2779. } else if (tag_is(token, kStartTag, GUMBO_TAG_COLGROUP)) {
  2780. clear_stack_to_table_context(parser);
  2781. insert_element_from_token(parser, token);
  2782. set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_COLUMN_GROUP);
  2783. return true;
  2784. } else if (tag_is(token, kStartTag, GUMBO_TAG_COL)) {
  2785. clear_stack_to_table_context(parser);
  2786. insert_element_of_tag_type(
  2787. parser, GUMBO_TAG_COLGROUP, GUMBO_INSERTION_IMPLIED);
  2788. parser->_parser_state->_reprocess_current_token = true;
  2789. set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_COLUMN_GROUP);
  2790. return true;
  2791. } else if (tag_in(token, kStartTag, GUMBO_TAG_TBODY, GUMBO_TAG_TFOOT,
  2792. GUMBO_TAG_THEAD, GUMBO_TAG_TD, GUMBO_TAG_TH, GUMBO_TAG_TR,
  2793. GUMBO_TAG_LAST)) {
  2794. clear_stack_to_table_context(parser);
  2795. set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_TABLE_BODY);
  2796. if (tag_in(token, kStartTag, GUMBO_TAG_TD, GUMBO_TAG_TH, GUMBO_TAG_TR,
  2797. GUMBO_TAG_LAST)) {
  2798. insert_element_of_tag_type(
  2799. parser, GUMBO_TAG_TBODY, GUMBO_INSERTION_IMPLIED);
  2800. state->_reprocess_current_token = true;
  2801. } else {
  2802. insert_element_from_token(parser, token);
  2803. }
  2804. return true;
  2805. } else if (tag_is(token, kStartTag, GUMBO_TAG_TABLE)) {
  2806. add_parse_error(parser, token);
  2807. if (close_table(parser)) {
  2808. parser->_parser_state->_reprocess_current_token = true;
  2809. } else {
  2810. ignore_token(parser);
  2811. }
  2812. return false;
  2813. } else if (tag_is(token, kEndTag, GUMBO_TAG_TABLE)) {
  2814. if (!close_table(parser)) {
  2815. add_parse_error(parser, token);
  2816. return false;
  2817. }
  2818. return true;
  2819. } else if (tag_in(token, kEndTag, GUMBO_TAG_BODY, GUMBO_TAG_CAPTION,
  2820. GUMBO_TAG_COL, GUMBO_TAG_COLGROUP, GUMBO_TAG_HTML,
  2821. GUMBO_TAG_TBODY, GUMBO_TAG_TD, GUMBO_TAG_TFOOT,
  2822. GUMBO_TAG_TH, GUMBO_TAG_THEAD, GUMBO_TAG_TR,
  2823. GUMBO_TAG_LAST)) {
  2824. add_parse_error(parser, token);
  2825. ignore_token(parser);
  2826. return false;
  2827. } else if (tag_in(token, kStartTag, GUMBO_TAG_STYLE, GUMBO_TAG_SCRIPT,
  2828. GUMBO_TAG_LAST)) {
  2829. return handle_in_head(parser, token);
  2830. } else if (tag_is(token, kStartTag, GUMBO_TAG_INPUT) &&
  2831. attribute_matches(&token->v.start_tag.attributes,
  2832. "type", "hidden")) {
  2833. add_parse_error(parser, token);
  2834. insert_element_from_token(parser, token);
  2835. pop_current_node(parser);
  2836. return false;
  2837. } else if (tag_is(token, kStartTag, GUMBO_TAG_FORM)) {
  2838. add_parse_error(parser, token);
  2839. if (state->_form_element) {
  2840. ignore_token(parser);
  2841. return false;
  2842. }
  2843. state->_form_element = insert_element_from_token(parser, token);
  2844. pop_current_node(parser);
  2845. return false;
  2846. } else if (token->type == GUMBO_TOKEN_EOF) {
  2847. if (!node_tag_is(get_current_node(parser), GUMBO_TAG_HTML)) {
  2848. add_parse_error(parser, token);
  2849. return false;
  2850. }
  2851. return true;
  2852. } else {
  2853. add_parse_error(parser, token);
  2854. state->_foster_parent_insertions = true;
  2855. bool result = handle_in_body(parser, token);
  2856. state->_foster_parent_insertions = false;
  2857. return result;
  2858. }
  2859. }
  2860. // http://www.whatwg.org/specs/web-apps/current-work/complete/tokenization.html#parsing-main-intabletext
  2861. static bool handle_in_table_text(GumboParser* parser, GumboToken* token) {
  2862. if (token->type == GUMBO_TOKEN_NULL) {
  2863. add_parse_error(parser, token);
  2864. ignore_token(parser);
  2865. return false;
  2866. } else if (token->type == GUMBO_TOKEN_CHARACTER ||
  2867. token->type == GUMBO_TOKEN_WHITESPACE) {
  2868. insert_text_token(parser, token);
  2869. return true;
  2870. } else {
  2871. GumboParserState* state = parser->_parser_state;
  2872. GumboStringBuffer* buffer = &state->_text_node._buffer;
  2873. // Can't use strspn for this because GumboStringBuffers are not
  2874. // null-terminated.
  2875. // Note that TextNodeBuffer may contain UTF-8 characters, but the presence
  2876. // of any one byte that is not whitespace means we flip the flag, so this
  2877. // loop is still valid.
  2878. for (int i = 0; i < buffer->length; ++i) {
  2879. if (!isspace(buffer->data[i]) || buffer->data[i] == '\v') {
  2880. state->_foster_parent_insertions = true;
  2881. reconstruct_active_formatting_elements(parser);
  2882. break;
  2883. }
  2884. }
  2885. maybe_flush_text_node_buffer(parser);
  2886. state->_foster_parent_insertions = false;
  2887. state->_reprocess_current_token = true;
  2888. state->_insertion_mode = state->_original_insertion_mode;
  2889. return true;
  2890. }
  2891. }
  2892. // http://www.whatwg.org/specs/web-apps/current-work/complete/tokenization.html#parsing-main-incaption
  2893. static bool handle_in_caption(GumboParser* parser, GumboToken* token) {
  2894. if (tag_in(token, kStartTag, GUMBO_TAG_CAPTION, GUMBO_TAG_COL,
  2895. GUMBO_TAG_COLGROUP, GUMBO_TAG_TBODY, GUMBO_TAG_TD,
  2896. GUMBO_TAG_TFOOT, GUMBO_TAG_TH, GUMBO_TAG_THEAD,
  2897. GUMBO_TAG_TR, GUMBO_TAG_LAST) ||
  2898. tag_in(token, kEndTag, GUMBO_TAG_CAPTION, GUMBO_TAG_TABLE,
  2899. GUMBO_TAG_LAST)) {
  2900. if (!has_an_element_in_table_scope(parser, GUMBO_TAG_CAPTION)) {
  2901. add_parse_error(parser, token);
  2902. ignore_token(parser);
  2903. return false;
  2904. }
  2905. if (!tag_is(token, kEndTag, GUMBO_TAG_CAPTION)) {
  2906. add_parse_error(parser, token);
  2907. parser->_parser_state->_reprocess_current_token = true;
  2908. }
  2909. generate_implied_end_tags(parser, GUMBO_TAG_LAST);
  2910. bool result = true;
  2911. if (!node_tag_is(get_current_node(parser), GUMBO_TAG_CAPTION)) {
  2912. add_parse_error(parser, token);
  2913. while (!node_tag_is(get_current_node(parser), GUMBO_TAG_CAPTION)) {
  2914. pop_current_node(parser);
  2915. }
  2916. result = false;
  2917. }
  2918. pop_current_node(parser); // The <caption> itself.
  2919. clear_active_formatting_elements(parser);
  2920. set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_TABLE);
  2921. return result;
  2922. } else if (tag_in(token, kEndTag, GUMBO_TAG_BODY, GUMBO_TAG_COL,
  2923. GUMBO_TAG_COLGROUP, GUMBO_TAG_HTML, GUMBO_TAG_TBODY,
  2924. GUMBO_TAG_TD, GUMBO_TAG_TFOOT, GUMBO_TAG_TH,
  2925. GUMBO_TAG_THEAD, GUMBO_TAG_TR, GUMBO_TAG_LAST)) {
  2926. add_parse_error(parser, token);
  2927. ignore_token(parser);
  2928. return false;
  2929. } else {
  2930. return handle_in_body(parser, token);
  2931. }
  2932. }
  2933. // http://www.whatwg.org/specs/web-apps/current-work/complete/tokenization.html#parsing-main-incolgroup
  2934. static bool handle_in_column_group(GumboParser* parser, GumboToken* token) {
  2935. if (token->type == GUMBO_TOKEN_WHITESPACE) {
  2936. insert_text_token(parser, token);
  2937. return true;
  2938. } else if (token->type == GUMBO_TOKEN_DOCTYPE) {
  2939. add_parse_error(parser, token);
  2940. ignore_token(parser);
  2941. return false;
  2942. } else if (token->type == GUMBO_TOKEN_COMMENT) {
  2943. append_comment_node(parser, get_current_node(parser), token);
  2944. return true;
  2945. } else if (tag_is(token, kStartTag, GUMBO_TAG_HTML)) {
  2946. return handle_in_body(parser, token);
  2947. } else if (tag_is(token, kStartTag, GUMBO_TAG_COL)) {
  2948. insert_element_from_token(parser, token);
  2949. pop_current_node(parser);
  2950. acknowledge_self_closing_tag(parser);
  2951. return true;
  2952. } else if (tag_is(token, kEndTag, GUMBO_TAG_COL)) {
  2953. add_parse_error(parser, token);
  2954. ignore_token(parser);
  2955. return false;
  2956. } else if (token->type == GUMBO_TOKEN_EOF &&
  2957. get_current_node(parser) == parser->_output->root) {
  2958. return true;
  2959. } else {
  2960. if (get_current_node(parser) == parser->_output->root) {
  2961. add_parse_error(parser, token);
  2962. return false;
  2963. }
  2964. assert(node_tag_is(get_current_node(parser), GUMBO_TAG_COLGROUP));
  2965. pop_current_node(parser);
  2966. set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_TABLE);
  2967. if (!tag_is(token, kEndTag, GUMBO_TAG_COLGROUP)) {
  2968. parser->_parser_state->_reprocess_current_token = true;
  2969. }
  2970. return true;
  2971. }
  2972. }
  2973. // http://www.whatwg.org/specs/web-apps/current-work/complete/tokenization.html#parsing-main-intbody
  2974. static bool handle_in_table_body(GumboParser* parser, GumboToken* token) {
  2975. if (tag_is(token, kStartTag, GUMBO_TAG_TR)) {
  2976. clear_stack_to_table_body_context(parser);
  2977. insert_element_from_token(parser, token);
  2978. set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_ROW);
  2979. return true;
  2980. } else if (tag_in(token, kStartTag, GUMBO_TAG_TD, GUMBO_TAG_TH,
  2981. GUMBO_TAG_LAST)) {
  2982. add_parse_error(parser, token);
  2983. clear_stack_to_table_body_context(parser);
  2984. insert_element_of_tag_type(parser, GUMBO_TAG_TR, GUMBO_INSERTION_IMPLIED);
  2985. parser->_parser_state->_reprocess_current_token = true;
  2986. set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_ROW);
  2987. return false;
  2988. } else if (tag_in(token, kEndTag, GUMBO_TAG_TBODY, GUMBO_TAG_TFOOT,
  2989. GUMBO_TAG_THEAD, GUMBO_TAG_LAST)) {
  2990. if (!has_an_element_in_table_scope(parser, token->v.end_tag)) {
  2991. add_parse_error(parser, token);
  2992. ignore_token(parser);
  2993. return false;
  2994. }
  2995. clear_stack_to_table_body_context(parser);
  2996. pop_current_node(parser);
  2997. set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_TABLE);
  2998. return true;
  2999. } else if (tag_in(token, kStartTag, GUMBO_TAG_CAPTION, GUMBO_TAG_COL,
  3000. GUMBO_TAG_COLGROUP, GUMBO_TAG_TBODY, GUMBO_TAG_TFOOT,
  3001. GUMBO_TAG_THEAD, GUMBO_TAG_LAST) ||
  3002. tag_is(token, kEndTag, GUMBO_TAG_TABLE)) {
  3003. if (!(has_an_element_in_table_scope(parser, GUMBO_TAG_TBODY) ||
  3004. has_an_element_in_table_scope(parser, GUMBO_TAG_THEAD) ||
  3005. has_an_element_in_table_scope(parser, GUMBO_TAG_TFOOT))) {
  3006. add_parse_error(parser, token);
  3007. ignore_token(parser);
  3008. return false;
  3009. }
  3010. clear_stack_to_table_body_context(parser);
  3011. pop_current_node(parser);
  3012. set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_TABLE);
  3013. parser->_parser_state->_reprocess_current_token = true;
  3014. return true;
  3015. } else if (tag_in(token, kEndTag, GUMBO_TAG_BODY, GUMBO_TAG_CAPTION,
  3016. GUMBO_TAG_COL, GUMBO_TAG_TR, GUMBO_TAG_COLGROUP,
  3017. GUMBO_TAG_HTML, GUMBO_TAG_TD, GUMBO_TAG_TH, GUMBO_TAG_LAST))
  3018. {
  3019. add_parse_error(parser, token);
  3020. ignore_token(parser);
  3021. return false;
  3022. } else {
  3023. return handle_in_table(parser, token);
  3024. }
  3025. }
  3026. // http://www.whatwg.org/specs/web-apps/current-work/complete/tokenization.html#parsing-main-intr
  3027. static bool handle_in_row(GumboParser* parser, GumboToken* token) {
  3028. if (tag_in(token, kStartTag, GUMBO_TAG_TH, GUMBO_TAG_TD, GUMBO_TAG_LAST)) {
  3029. clear_stack_to_table_row_context(parser);
  3030. insert_element_from_token(parser, token);
  3031. set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_CELL);
  3032. add_formatting_element(parser, &kActiveFormattingScopeMarker);
  3033. return true;
  3034. } else if (tag_in(token, kStartTag, GUMBO_TAG_CAPTION, GUMBO_TAG_COLGROUP,
  3035. GUMBO_TAG_TBODY, GUMBO_TAG_TFOOT, GUMBO_TAG_THEAD,
  3036. GUMBO_TAG_TR, GUMBO_TAG_LAST) ||
  3037. tag_in(token, kEndTag, GUMBO_TAG_TR, GUMBO_TAG_TABLE,
  3038. GUMBO_TAG_TBODY, GUMBO_TAG_TFOOT, GUMBO_TAG_THEAD,
  3039. GUMBO_TAG_LAST)) {
  3040. // This case covers 4 clauses of the spec, each of which say "Otherwise, act
  3041. // as if an end tag with the tag name "tr" had been seen." The differences
  3042. // are in error handling and whether the current token is reprocessed.
  3043. GumboTag desired_tag =
  3044. tag_in(token, kEndTag, GUMBO_TAG_TBODY, GUMBO_TAG_TFOOT,
  3045. GUMBO_TAG_THEAD, GUMBO_TAG_LAST)
  3046. ? token->v.end_tag : GUMBO_TAG_TR;
  3047. if (!has_an_element_in_table_scope(parser, desired_tag)) {
  3048. gumbo_debug("Bailing because there is no tag %s in table scope.\nOpen elements:",
  3049. gumbo_normalized_tagname(desired_tag));
  3050. for (int i = 0; i < parser->_parser_state->_open_elements.length; ++i) {
  3051. const GumboNode* node = parser->_parser_state->_open_elements.data[i];
  3052. gumbo_debug("%s\n", gumbo_normalized_tagname(node->v.element.tag));
  3053. }
  3054. add_parse_error(parser, token);
  3055. ignore_token(parser);
  3056. return false;
  3057. }
  3058. clear_stack_to_table_row_context(parser);
  3059. GumboNode* last_element = pop_current_node(parser);
  3060. assert(node_tag_is(last_element, GUMBO_TAG_TR));
  3061. AVOID_UNUSED_VARIABLE_WARNING(last_element);
  3062. set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_TABLE_BODY);
  3063. if (!tag_is(token, kEndTag, GUMBO_TAG_TR)) {
  3064. parser->_parser_state->_reprocess_current_token = true;
  3065. }
  3066. return true;
  3067. } else if (tag_in(token, kEndTag, GUMBO_TAG_BODY, GUMBO_TAG_CAPTION,
  3068. GUMBO_TAG_COL, GUMBO_TAG_COLGROUP, GUMBO_TAG_HTML,
  3069. GUMBO_TAG_TD, GUMBO_TAG_TH, GUMBO_TAG_LAST)) {
  3070. add_parse_error(parser, token);
  3071. ignore_token(parser);
  3072. return false;
  3073. } else {
  3074. return handle_in_table(parser, token);
  3075. }
  3076. }
  3077. // http://www.whatwg.org/specs/web-apps/current-work/complete/tokenization.html#parsing-main-intd
  3078. static bool handle_in_cell(GumboParser* parser, GumboToken* token) {
  3079. if (tag_in(token, kEndTag, GUMBO_TAG_TD, GUMBO_TAG_TH, GUMBO_TAG_LAST)) {
  3080. GumboTag token_tag = token->v.end_tag;
  3081. if (!has_an_element_in_table_scope(parser, token_tag)) {
  3082. add_parse_error(parser, token);
  3083. return false;
  3084. }
  3085. return close_table_cell(parser, token, token_tag);
  3086. } else if (tag_in(token, kStartTag, GUMBO_TAG_CAPTION, GUMBO_TAG_COL,
  3087. GUMBO_TAG_COLGROUP, GUMBO_TAG_TBODY, GUMBO_TAG_TD,
  3088. GUMBO_TAG_TFOOT, GUMBO_TAG_TH, GUMBO_TAG_THEAD,
  3089. GUMBO_TAG_TR, GUMBO_TAG_LAST)) {
  3090. gumbo_debug("Handling <td> in cell.\n");
  3091. if (!has_an_element_in_table_scope(parser, GUMBO_TAG_TH) &&
  3092. !has_an_element_in_table_scope(parser, GUMBO_TAG_TD)) {
  3093. gumbo_debug("Bailing out because there's no <td> or <th> in scope.\n");
  3094. add_parse_error(parser, token);
  3095. ignore_token(parser);
  3096. return false;
  3097. }
  3098. parser->_parser_state->_reprocess_current_token = true;
  3099. return close_current_cell(parser, token);
  3100. } else if (tag_in(token, kEndTag, GUMBO_TAG_BODY, GUMBO_TAG_CAPTION,
  3101. GUMBO_TAG_COL, GUMBO_TAG_COLGROUP, GUMBO_TAG_HTML,
  3102. GUMBO_TAG_LAST)) {
  3103. add_parse_error(parser, token);
  3104. ignore_token(parser);
  3105. return false;
  3106. } else if (tag_in(token, kEndTag, GUMBO_TAG_TABLE, GUMBO_TAG_TBODY,
  3107. GUMBO_TAG_TFOOT, GUMBO_TAG_THEAD, GUMBO_TAG_TR,
  3108. GUMBO_TAG_LAST)) {
  3109. if (!has_an_element_in_table_scope(parser, token->v.end_tag)) {
  3110. add_parse_error(parser, token);
  3111. ignore_token(parser);
  3112. return false;
  3113. }
  3114. parser->_parser_state->_reprocess_current_token = true;
  3115. return close_current_cell(parser, token);
  3116. } else {
  3117. return handle_in_body(parser, token);
  3118. }
  3119. }
  3120. // http://www.whatwg.org/specs/web-apps/current-work/complete/tokenization.html#parsing-main-inselect
  3121. static bool handle_in_select(GumboParser* parser, GumboToken* token) {
  3122. if (token->type == GUMBO_TOKEN_NULL) {
  3123. add_parse_error(parser, token);
  3124. ignore_token(parser);
  3125. return false;
  3126. } else if (token->type == GUMBO_TOKEN_CHARACTER ||
  3127. token->type == GUMBO_TOKEN_WHITESPACE) {
  3128. insert_text_token(parser, token);
  3129. return true;
  3130. } else if (token->type == GUMBO_TOKEN_DOCTYPE) {
  3131. add_parse_error(parser, token);
  3132. ignore_token(parser);
  3133. return false;
  3134. } else if (token->type == GUMBO_TOKEN_COMMENT) {
  3135. append_comment_node(parser, get_current_node(parser), token);
  3136. return true;
  3137. } else if (tag_is(token, kStartTag, GUMBO_TAG_HTML)) {
  3138. return handle_in_body(parser, token);
  3139. } else if (tag_is(token, kStartTag, GUMBO_TAG_OPTION)) {
  3140. if (node_tag_is(get_current_node(parser), GUMBO_TAG_OPTION)) {
  3141. pop_current_node(parser);
  3142. }
  3143. insert_element_from_token(parser, token);
  3144. return true;
  3145. } else if (tag_is(token, kStartTag, GUMBO_TAG_OPTGROUP)) {
  3146. if (node_tag_is(get_current_node(parser), GUMBO_TAG_OPTION)) {
  3147. pop_current_node(parser);
  3148. }
  3149. if (node_tag_is(get_current_node(parser), GUMBO_TAG_OPTGROUP)) {
  3150. pop_current_node(parser);
  3151. }
  3152. insert_element_from_token(parser, token);
  3153. return true;
  3154. } else if (tag_is(token, kEndTag, GUMBO_TAG_OPTGROUP)) {
  3155. GumboVector* open_elements = &parser->_parser_state->_open_elements;
  3156. if (node_tag_is(get_current_node(parser), GUMBO_TAG_OPTION) &&
  3157. node_tag_is(open_elements->data[open_elements->length - 2],
  3158. GUMBO_TAG_OPTGROUP)) {
  3159. pop_current_node(parser);
  3160. }
  3161. if (node_tag_is(get_current_node(parser), GUMBO_TAG_OPTGROUP)) {
  3162. pop_current_node(parser);
  3163. return true;
  3164. } else {
  3165. add_parse_error(parser, token);
  3166. ignore_token(parser);
  3167. return false;
  3168. }
  3169. } else if (tag_is(token, kEndTag, GUMBO_TAG_OPTION)) {
  3170. if (node_tag_is(get_current_node(parser), GUMBO_TAG_OPTION)) {
  3171. pop_current_node(parser);
  3172. return true;
  3173. } else {
  3174. add_parse_error(parser, token);
  3175. ignore_token(parser);
  3176. return false;
  3177. }
  3178. } else if (tag_is(token, kEndTag, GUMBO_TAG_SELECT)) {
  3179. if (!has_an_element_in_select_scope(parser, GUMBO_TAG_SELECT)) {
  3180. add_parse_error(parser, token);
  3181. ignore_token(parser);
  3182. return false;
  3183. }
  3184. close_current_select(parser);
  3185. return true;
  3186. } else if (tag_is(token, kStartTag, GUMBO_TAG_SELECT)) {
  3187. add_parse_error(parser, token);
  3188. ignore_token(parser);
  3189. close_current_select(parser);
  3190. return false;
  3191. } else if (tag_in(token, kStartTag, GUMBO_TAG_INPUT, GUMBO_TAG_KEYGEN,
  3192. GUMBO_TAG_TEXTAREA, GUMBO_TAG_LAST)) {
  3193. add_parse_error(parser, token);
  3194. if (!has_an_element_in_select_scope(parser, GUMBO_TAG_SELECT)) {
  3195. ignore_token(parser);
  3196. } else {
  3197. close_current_select(parser);
  3198. parser->_parser_state->_reprocess_current_token = true;
  3199. }
  3200. return false;
  3201. } else if (tag_is(token, kStartTag, GUMBO_TAG_SCRIPT)) {
  3202. return handle_in_head(parser, token);
  3203. } else if (token->type == GUMBO_TOKEN_EOF) {
  3204. if (get_current_node(parser) != parser->_output->root) {
  3205. add_parse_error(parser, token);
  3206. return false;
  3207. }
  3208. return true;
  3209. } else {
  3210. add_parse_error(parser, token);
  3211. ignore_token(parser);
  3212. return false;
  3213. }
  3214. }
  3215. // http://www.whatwg.org/specs/web-apps/current-work/complete/tokenization.html#parsing-main-inselectintable
  3216. static bool handle_in_select_in_table(GumboParser* parser, GumboToken* token) {
  3217. if (tag_in(token, kStartTag, GUMBO_TAG_CAPTION, GUMBO_TAG_TABLE,
  3218. GUMBO_TAG_TBODY, GUMBO_TAG_TFOOT, GUMBO_TAG_THEAD, GUMBO_TAG_TR,
  3219. GUMBO_TAG_TD, GUMBO_TAG_TH, GUMBO_TAG_LAST)) {
  3220. add_parse_error(parser, token);
  3221. close_current_select(parser);
  3222. parser->_parser_state->_reprocess_current_token = true;
  3223. return false;
  3224. } else if (tag_in(token, kEndTag, GUMBO_TAG_CAPTION, GUMBO_TAG_TABLE,
  3225. GUMBO_TAG_TBODY, GUMBO_TAG_TFOOT, GUMBO_TAG_THEAD,
  3226. GUMBO_TAG_TR, GUMBO_TAG_TD, GUMBO_TAG_TH, GUMBO_TAG_LAST)) {
  3227. add_parse_error(parser, token);
  3228. if (has_an_element_in_table_scope(parser, token->v.end_tag)) {
  3229. close_current_select(parser);
  3230. reset_insertion_mode_appropriately(parser);
  3231. parser->_parser_state->_reprocess_current_token = true;
  3232. } else {
  3233. ignore_token(parser);
  3234. }
  3235. return false;
  3236. } else {
  3237. return handle_in_select(parser, token);
  3238. }
  3239. }
  3240. // http://www.whatwg.org/specs/web-apps/current-work/multipage/tree-construction.html#parsing-main-intemplate
  3241. static bool handle_in_template(GumboParser* parser, GumboToken* token) {
  3242. // TODO(jdtang): Implement this.
  3243. return true;
  3244. }
  3245. // http://www.whatwg.org/specs/web-apps/current-work/complete/tokenization.html#parsing-main-afterbody
  3246. static bool handle_after_body(GumboParser* parser, GumboToken* token) {
  3247. if (token->type == GUMBO_TOKEN_WHITESPACE ||
  3248. tag_is(token, kStartTag, GUMBO_TAG_HTML)) {
  3249. return handle_in_body(parser, token);
  3250. } else if (token->type == GUMBO_TOKEN_COMMENT) {
  3251. GumboNode* html_node = parser->_output->root;
  3252. assert(html_node != NULL);
  3253. append_comment_node(parser, html_node, token);
  3254. return true;
  3255. } else if (token->type == GUMBO_TOKEN_DOCTYPE) {
  3256. add_parse_error(parser, token);
  3257. ignore_token(parser);
  3258. return false;
  3259. } else if (tag_is(token, kEndTag, GUMBO_TAG_HTML)) {
  3260. // TODO(jdtang): Handle fragment parsing algorithm case.
  3261. set_insertion_mode(parser, GUMBO_INSERTION_MODE_AFTER_AFTER_BODY);
  3262. GumboNode* html = parser->_parser_state->_open_elements.data[0];
  3263. assert(node_tag_is(html, GUMBO_TAG_HTML));
  3264. record_end_of_element(
  3265. parser->_parser_state->_current_token, &html->v.element);
  3266. return true;
  3267. } else if (token->type == GUMBO_TOKEN_EOF) {
  3268. return true;
  3269. } else {
  3270. add_parse_error(parser, token);
  3271. set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_BODY);
  3272. parser->_parser_state->_reprocess_current_token = true;
  3273. return false;
  3274. }
  3275. }
  3276. // http://www.whatwg.org/specs/web-apps/current-work/complete/tokenization.html#parsing-main-inframeset
  3277. static bool handle_in_frameset(GumboParser* parser, GumboToken* token) {
  3278. if (token->type == GUMBO_TOKEN_WHITESPACE) {
  3279. insert_text_token(parser, token);
  3280. return true;
  3281. } else if (token->type == GUMBO_TOKEN_COMMENT) {
  3282. append_comment_node(parser, get_current_node(parser), token);
  3283. return true;
  3284. } else if (token->type == GUMBO_TOKEN_DOCTYPE) {
  3285. add_parse_error(parser, token);
  3286. ignore_token(parser);
  3287. return false;
  3288. } else if (tag_is(token, kStartTag, GUMBO_TAG_HTML)) {
  3289. return handle_in_body(parser, token);
  3290. } else if (tag_is(token, kStartTag, GUMBO_TAG_FRAMESET)) {
  3291. insert_element_from_token(parser, token);
  3292. return true;
  3293. } else if (tag_is(token, kEndTag, GUMBO_TAG_FRAMESET)) {
  3294. if (node_tag_is(get_current_node(parser), GUMBO_TAG_HTML)) {
  3295. add_parse_error(parser, token);
  3296. ignore_token(parser);
  3297. return false;
  3298. }
  3299. pop_current_node(parser);
  3300. // TODO(jdtang): Add a condition to ignore this for the fragment parsing
  3301. // algorithm.
  3302. if (!node_tag_is(get_current_node(parser), GUMBO_TAG_FRAMESET)) {
  3303. set_insertion_mode(parser, GUMBO_INSERTION_MODE_AFTER_FRAMESET);
  3304. }
  3305. return true;
  3306. } else if (tag_is(token, kStartTag, GUMBO_TAG_FRAME)) {
  3307. insert_element_from_token(parser, token);
  3308. pop_current_node(parser);
  3309. acknowledge_self_closing_tag(parser);
  3310. return true;
  3311. } else if (tag_is(token, kStartTag, GUMBO_TAG_NOFRAMES)) {
  3312. return handle_in_head(parser, token);
  3313. } else if (token->type == GUMBO_TOKEN_EOF) {
  3314. if (!node_tag_is(get_current_node(parser), GUMBO_TAG_HTML)) {
  3315. add_parse_error(parser, token);
  3316. return false;
  3317. }
  3318. return true;
  3319. } else {
  3320. add_parse_error(parser, token);
  3321. ignore_token(parser);
  3322. return false;
  3323. }
  3324. }
  3325. // http://www.whatwg.org/specs/web-apps/current-work/complete/tokenization.html#parsing-main-afterframeset
  3326. static bool handle_after_frameset(GumboParser* parser, GumboToken* token) {
  3327. if (token->type == GUMBO_TOKEN_WHITESPACE) {
  3328. insert_text_token(parser, token);
  3329. return true;
  3330. } else if (token->type == GUMBO_TOKEN_COMMENT) {
  3331. append_comment_node(parser, get_current_node(parser), token);
  3332. return true;
  3333. } else if (token->type == GUMBO_TOKEN_DOCTYPE) {
  3334. add_parse_error(parser, token);
  3335. ignore_token(parser);
  3336. return false;
  3337. } else if (tag_is(token, kStartTag, GUMBO_TAG_HTML)) {
  3338. return handle_in_body(parser, token);
  3339. } else if (tag_is(token, kEndTag, GUMBO_TAG_HTML)) {
  3340. GumboNode* html = parser->_parser_state->_open_elements.data[0];
  3341. assert(node_tag_is(html, GUMBO_TAG_HTML));
  3342. record_end_of_element(
  3343. parser->_parser_state->_current_token, &html->v.element);
  3344. set_insertion_mode(parser, GUMBO_INSERTION_MODE_AFTER_AFTER_FRAMESET);
  3345. return true;
  3346. } else if (tag_is(token, kStartTag, GUMBO_TAG_NOFRAMES)) {
  3347. return handle_in_head(parser, token);
  3348. } else if (token->type == GUMBO_TOKEN_EOF) {
  3349. return true;
  3350. } else {
  3351. add_parse_error(parser, token);
  3352. ignore_token(parser);
  3353. return false;
  3354. }
  3355. }
  3356. // http://www.whatwg.org/specs/web-apps/current-work/complete/tokenization.html#the-after-after-body-insertion-mode
  3357. static bool handle_after_after_body(GumboParser* parser, GumboToken* token) {
  3358. if (token->type == GUMBO_TOKEN_COMMENT) {
  3359. append_comment_node(parser, get_document_node(parser), token);
  3360. return true;
  3361. } else if (token->type == GUMBO_TOKEN_DOCTYPE ||
  3362. token->type == GUMBO_TOKEN_WHITESPACE ||
  3363. tag_is(token, kStartTag, GUMBO_TAG_HTML)) {
  3364. return handle_in_body(parser, token);
  3365. } else if (token->type == GUMBO_TOKEN_EOF) {
  3366. return true;
  3367. } else {
  3368. add_parse_error(parser, token);
  3369. set_insertion_mode(parser, GUMBO_INSERTION_MODE_IN_BODY);
  3370. parser->_parser_state->_reprocess_current_token = true;
  3371. return false;
  3372. }
  3373. }
  3374. // http://www.whatwg.org/specs/web-apps/current-work/complete/tokenization.html#the-after-after-frameset-insertion-mode
  3375. static bool handle_after_after_frameset(
  3376. GumboParser* parser, GumboToken* token) {
  3377. if (token->type == GUMBO_TOKEN_COMMENT) {
  3378. append_comment_node(parser, get_document_node(parser), token);
  3379. return true;
  3380. } else if (token->type == GUMBO_TOKEN_DOCTYPE ||
  3381. token->type == GUMBO_TOKEN_WHITESPACE ||
  3382. tag_is(token, kStartTag, GUMBO_TAG_HTML)) {
  3383. return handle_in_body(parser, token);
  3384. } else if (token->type == GUMBO_TOKEN_EOF) {
  3385. return true;
  3386. } else if (tag_is(token, kStartTag, GUMBO_TAG_NOFRAMES)) {
  3387. return handle_in_head(parser, token);
  3388. } else {
  3389. add_parse_error(parser, token);
  3390. ignore_token(parser);
  3391. return false;
  3392. }
  3393. }
  3394. // Function pointers for each insertion mode. Keep in sync with
  3395. // insertion_mode.h.
  3396. typedef bool (*TokenHandler)(GumboParser* parser, GumboToken* token);
  3397. static const TokenHandler kTokenHandlers[] = {
  3398. handle_initial,
  3399. handle_before_html,
  3400. handle_before_head,
  3401. handle_in_head,
  3402. handle_in_head_noscript,
  3403. handle_after_head,
  3404. handle_in_body,
  3405. handle_text,
  3406. handle_in_table,
  3407. handle_in_table_text,
  3408. handle_in_caption,
  3409. handle_in_column_group,
  3410. handle_in_table_body,
  3411. handle_in_row,
  3412. handle_in_cell,
  3413. handle_in_select,
  3414. handle_in_select_in_table,
  3415. handle_in_template,
  3416. handle_after_body,
  3417. handle_in_frameset,
  3418. handle_after_frameset,
  3419. handle_after_after_body,
  3420. handle_after_after_frameset
  3421. };
  3422. static bool handle_html_content(GumboParser* parser, GumboToken* token) {
  3423. return kTokenHandlers[(unsigned int) parser->_parser_state->_insertion_mode](
  3424. parser, token);
  3425. }
  3426. // http://www.whatwg.org/specs/web-apps/current-work/complete/tokenization.html#parsing-main-inforeign
  3427. static bool handle_in_foreign_content(GumboParser* parser, GumboToken* token) {
  3428. switch (token->type) {
  3429. case GUMBO_TOKEN_NULL:
  3430. add_parse_error(parser, token);
  3431. token->type = GUMBO_TOKEN_CHARACTER;
  3432. token->v.character = kUtf8ReplacementChar;
  3433. insert_text_token(parser, token);
  3434. return false;
  3435. case GUMBO_TOKEN_WHITESPACE:
  3436. insert_text_token(parser, token);
  3437. return true;
  3438. case GUMBO_TOKEN_CHARACTER:
  3439. insert_text_token(parser, token);
  3440. set_frameset_not_ok(parser);
  3441. return true;
  3442. case GUMBO_TOKEN_COMMENT:
  3443. append_comment_node(parser, get_current_node(parser), token);
  3444. return true;
  3445. case GUMBO_TOKEN_DOCTYPE:
  3446. add_parse_error(parser, token);
  3447. ignore_token(parser);
  3448. return false;
  3449. default:
  3450. // Fall through to the if-statements below.
  3451. break;
  3452. }
  3453. // Order matters for these clauses.
  3454. if (tag_in(token, kStartTag, GUMBO_TAG_B, GUMBO_TAG_BIG,
  3455. GUMBO_TAG_BLOCKQUOTE, GUMBO_TAG_BODY, GUMBO_TAG_BR,
  3456. GUMBO_TAG_CENTER, GUMBO_TAG_CODE, GUMBO_TAG_DD, GUMBO_TAG_DIV,
  3457. GUMBO_TAG_DL, GUMBO_TAG_DT, GUMBO_TAG_EM, GUMBO_TAG_EMBED,
  3458. GUMBO_TAG_H1, GUMBO_TAG_H2, GUMBO_TAG_H3, GUMBO_TAG_H4,
  3459. GUMBO_TAG_H5, GUMBO_TAG_H6, GUMBO_TAG_HEAD, GUMBO_TAG_HR,
  3460. GUMBO_TAG_I, GUMBO_TAG_IMG, GUMBO_TAG_LI, GUMBO_TAG_LISTING,
  3461. GUMBO_TAG_MENU, GUMBO_TAG_META, GUMBO_TAG_NOBR, GUMBO_TAG_OL,
  3462. GUMBO_TAG_P, GUMBO_TAG_PRE, GUMBO_TAG_RUBY, GUMBO_TAG_S,
  3463. GUMBO_TAG_SMALL, GUMBO_TAG_SPAN, GUMBO_TAG_STRONG,
  3464. GUMBO_TAG_STRIKE, GUMBO_TAG_SUB, GUMBO_TAG_SUP,
  3465. GUMBO_TAG_TABLE, GUMBO_TAG_TT, GUMBO_TAG_U, GUMBO_TAG_UL,
  3466. GUMBO_TAG_VAR, GUMBO_TAG_LAST) ||
  3467. (tag_is(token, kStartTag, GUMBO_TAG_FONT) && (
  3468. token_has_attribute(token, "color") ||
  3469. token_has_attribute(token, "face") ||
  3470. token_has_attribute(token, "size")))) {
  3471. add_parse_error(parser, token);
  3472. do {
  3473. pop_current_node(parser);
  3474. } while(!(is_mathml_integration_point(get_current_node(parser)) ||
  3475. is_html_integration_point(get_current_node(parser)) ||
  3476. get_current_node(parser)->v.element.tag_namespace ==
  3477. GUMBO_NAMESPACE_HTML));
  3478. parser->_parser_state->_reprocess_current_token = true;
  3479. return false;
  3480. } else if (token->type == GUMBO_TOKEN_START_TAG) {
  3481. const GumboNamespaceEnum current_namespace =
  3482. get_current_node(parser)->v.element.tag_namespace;
  3483. if (current_namespace == GUMBO_NAMESPACE_MATHML) {
  3484. adjust_mathml_attributes(parser, token);
  3485. }
  3486. if (current_namespace == GUMBO_NAMESPACE_SVG) {
  3487. // Tag adjustment is left to the gumbo_normalize_svg_tagname helper
  3488. // function.
  3489. adjust_svg_attributes(parser, token);
  3490. }
  3491. adjust_foreign_attributes(parser, token);
  3492. insert_foreign_element(parser, token, current_namespace);
  3493. if (token->v.start_tag.is_self_closing) {
  3494. pop_current_node(parser);
  3495. acknowledge_self_closing_tag(parser);
  3496. }
  3497. return true;
  3498. // </script> tags are handled like any other end tag, putting the script's
  3499. // text into a text node child and closing the current node.
  3500. } else {
  3501. assert(token->type == GUMBO_TOKEN_END_TAG);
  3502. GumboNode* node = get_current_node(parser);
  3503. assert(node != NULL);
  3504. GumboStringPiece token_tagname = token->original_text;
  3505. GumboStringPiece node_tagname = node->v.element.original_tag;
  3506. gumbo_tag_from_original_text(&token_tagname);
  3507. gumbo_tag_from_original_text(&node_tagname);
  3508. bool is_success = true;
  3509. if (!gumbo_string_equals_ignore_case(&node_tagname, &token_tagname)) {
  3510. add_parse_error(parser, token);
  3511. is_success = false;
  3512. }
  3513. int i = parser->_parser_state->_open_elements.length;
  3514. for( --i; i > 0; ) {
  3515. // Here we move up the stack until we find an HTML element (in which
  3516. // case we do nothing) or we find the element that we're about to
  3517. // close (in which case we pop everything we've seen until that
  3518. // point.)
  3519. gumbo_debug("Foreign %.*s node at %d.\n", node_tagname.length,
  3520. node_tagname.data, i);
  3521. if (gumbo_string_equals_ignore_case(&node_tagname, &token_tagname)) {
  3522. gumbo_debug("Matches.\n");
  3523. while (pop_current_node(parser) != node) {
  3524. // Pop all the nodes below the current one. Node is guaranteed to
  3525. // be an element on the stack of open elements (set below), so
  3526. // this loop is guaranteed to terminate.
  3527. }
  3528. return is_success;
  3529. }
  3530. --i;
  3531. node = parser->_parser_state->_open_elements.data[i];
  3532. if (node->v.element.tag_namespace == GUMBO_NAMESPACE_HTML) {
  3533. // Must break before gumbo_tag_from_original_text to avoid passing
  3534. // parser-inserted nodes through.
  3535. break;
  3536. }
  3537. node_tagname = node->v.element.original_tag;
  3538. gumbo_tag_from_original_text(&node_tagname);
  3539. }
  3540. assert(node->v.element.tag_namespace == GUMBO_NAMESPACE_HTML);
  3541. // We can't call handle_token directly because the current node is still in
  3542. // the SVG namespace, so it would re-enter this and result in infinite
  3543. // recursion.
  3544. return handle_html_content(parser, token) && is_success;
  3545. }
  3546. }
  3547. // http://www.whatwg.org/specs/web-apps/current-work/multipage/tree-construction.html#tree-construction
  3548. static bool handle_token(GumboParser* parser, GumboToken* token) {
  3549. if (parser->_parser_state->_ignore_next_linefeed &&
  3550. token->type == GUMBO_TOKEN_WHITESPACE && token->v.character == '\n') {
  3551. parser->_parser_state->_ignore_next_linefeed = false;
  3552. ignore_token(parser);
  3553. return true;
  3554. }
  3555. // This needs to be reset both here and in the conditional above to catch both
  3556. // the case where the next token is not whitespace (so we don't ignore
  3557. // whitespace in the middle of <pre> tags) and where there are multiple
  3558. // whitespace tokens (so we don't ignore the second one).
  3559. parser->_parser_state->_ignore_next_linefeed = false;
  3560. if (tag_is(token, kEndTag, GUMBO_TAG_BODY)) {
  3561. parser->_parser_state->_closed_body_tag = true;
  3562. }
  3563. if (tag_is(token, kEndTag, GUMBO_TAG_HTML)) {
  3564. parser->_parser_state->_closed_html_tag = true;
  3565. }
  3566. const GumboNode* current_node = get_current_node(parser);
  3567. assert(!current_node || current_node->type == GUMBO_NODE_ELEMENT);
  3568. if (current_node) {
  3569. gumbo_debug("Current node: <%s>.\n",
  3570. gumbo_normalized_tagname(current_node->v.element.tag));
  3571. }
  3572. if (!current_node ||
  3573. current_node->v.element.tag_namespace == GUMBO_NAMESPACE_HTML ||
  3574. (is_mathml_integration_point(current_node) &&
  3575. (token->type == GUMBO_TOKEN_CHARACTER ||
  3576. token->type == GUMBO_TOKEN_WHITESPACE ||
  3577. token->type == GUMBO_TOKEN_NULL ||
  3578. (token->type == GUMBO_TOKEN_START_TAG &&
  3579. !tag_in(token, kStartTag, GUMBO_TAG_MGLYPH, GUMBO_TAG_MALIGNMARK,
  3580. GUMBO_TAG_LAST)))) ||
  3581. (current_node->v.element.tag_namespace == GUMBO_NAMESPACE_MATHML &&
  3582. node_tag_is(current_node, GUMBO_TAG_ANNOTATION_XML) &&
  3583. tag_is(token, kStartTag, GUMBO_TAG_SVG)) ||
  3584. (is_html_integration_point(current_node) && (
  3585. token->type == GUMBO_TOKEN_START_TAG ||
  3586. token->type == GUMBO_TOKEN_CHARACTER ||
  3587. token->type == GUMBO_TOKEN_NULL ||
  3588. token->type == GUMBO_TOKEN_WHITESPACE)) ||
  3589. token->type == GUMBO_TOKEN_EOF) {
  3590. return handle_html_content(parser, token);
  3591. } else {
  3592. return handle_in_foreign_content(parser, token);
  3593. }
  3594. }
  3595. GumboOutput* gumbo_parse(const char* buffer) {
  3596. return gumbo_parse_with_options(
  3597. &kGumboDefaultOptions, buffer, strlen(buffer));
  3598. }
  3599. GumboOutput* gumbo_parse_with_options(
  3600. const GumboOptions* options, const char* buffer, size_t length) {
  3601. GumboParser parser;
  3602. parser._options = options;
  3603. output_init(&parser);
  3604. gumbo_tokenizer_state_init(&parser, buffer, length);
  3605. parser_state_init(&parser);
  3606. GumboParserState* state = parser._parser_state;
  3607. gumbo_debug("Parsing %.*s.\n", length, buffer);
  3608. // Sanity check so that infinite loops die with an assertion failure instead
  3609. // of hanging the process before we ever get an error.
  3610. int loop_count = 0;
  3611. GumboToken token;
  3612. bool has_error = false;
  3613. do {
  3614. if (state->_reprocess_current_token) {
  3615. state->_reprocess_current_token = false;
  3616. } else {
  3617. GumboNode* current_node = get_current_node(&parser);
  3618. gumbo_tokenizer_set_is_current_node_foreign(
  3619. &parser, current_node &&
  3620. current_node->v.element.tag_namespace != GUMBO_NAMESPACE_HTML);
  3621. has_error = !gumbo_lex(&parser, &token) || has_error;
  3622. }
  3623. const char* token_type = "text";
  3624. switch (token.type) {
  3625. case GUMBO_TOKEN_DOCTYPE:
  3626. token_type = "doctype";
  3627. break;
  3628. case GUMBO_TOKEN_START_TAG:
  3629. token_type = gumbo_normalized_tagname(token.v.start_tag.tag);
  3630. break;
  3631. case GUMBO_TOKEN_END_TAG:
  3632. token_type = gumbo_normalized_tagname(token.v.end_tag);
  3633. break;
  3634. case GUMBO_TOKEN_COMMENT:
  3635. token_type = "comment";
  3636. break;
  3637. default:
  3638. break;
  3639. }
  3640. gumbo_debug("Handling %s token @%d:%d in state %d.\n",
  3641. (char*) token_type, token.position.line, token.position.column,
  3642. state->_insertion_mode);
  3643. state->_current_token = &token;
  3644. state->_self_closing_flag_acknowledged =
  3645. !(token.type == GUMBO_TOKEN_START_TAG &&
  3646. token.v.start_tag.is_self_closing);
  3647. has_error = !handle_token(&parser, &token) || has_error;
  3648. // Check for memory leaks when ownership is transferred from start tag
  3649. // tokens to nodes.
  3650. assert(state->_reprocess_current_token ||
  3651. token.type != GUMBO_TOKEN_START_TAG ||
  3652. token.v.start_tag.attributes.data == NULL);
  3653. if (!state->_self_closing_flag_acknowledged) {
  3654. GumboError* error = add_parse_error(&parser, &token);
  3655. if (error) {
  3656. error->type = GUMBO_ERR_UNACKNOWLEDGED_SELF_CLOSING_TAG;
  3657. }
  3658. }
  3659. ++loop_count;
  3660. assert(loop_count < 1000000000);
  3661. } while ((token.type != GUMBO_TOKEN_EOF || state->_reprocess_current_token) &&
  3662. !(options->stop_on_first_error && has_error));
  3663. finish_parsing(&parser);
  3664. // For API uniformity reasons, if the doctype still has nulls, convert them to
  3665. // empty strings.
  3666. GumboDocument* doc_type = &parser._output->document->v.document;
  3667. if (doc_type->name == NULL) {
  3668. doc_type->name = gumbo_copy_stringz(&parser, "");
  3669. }
  3670. if (doc_type->public_identifier == NULL) {
  3671. doc_type->public_identifier = gumbo_copy_stringz(&parser, "");
  3672. }
  3673. if (doc_type->system_identifier == NULL) {
  3674. doc_type->system_identifier = gumbo_copy_stringz(&parser, "");
  3675. }
  3676. parser_state_destroy(&parser);
  3677. gumbo_tokenizer_state_destroy(&parser);
  3678. return parser._output;
  3679. }
  3680. void gumbo_destroy_node(GumboOptions* options, GumboNode* node) {
  3681. // Need a dummy GumboParser because the allocator comes along with the
  3682. // options object.
  3683. GumboParser parser;
  3684. parser._options = options;
  3685. destroy_node(&parser, node);
  3686. }
  3687. void gumbo_destroy_output(const GumboOptions* options, GumboOutput* output) {
  3688. // Need a dummy GumboParser because the allocator comes along with the
  3689. // options object.
  3690. GumboParser parser;
  3691. parser._options = options;
  3692. destroy_node(&parser, output->document);
  3693. for (int i = 0; i < output->errors.length; ++i) {
  3694. gumbo_error_destroy(&parser, output->errors.data[i]);
  3695. }
  3696. gumbo_vector_destroy(&parser, &output->errors);
  3697. gumbo_parser_deallocate(&parser, output);
  3698. }