Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788
  1. <?php
  2. /**
  3. * Experimental HTML5-based parser using Jeroen van der Meer's PH5P library.
  4. * Occupies space in the HTML5 pseudo-namespace, which may cause conflicts.
  5. *
  6. * @note
  7. * Recent changes to PHP's DOM extension have resulted in some fatal
  8. * error conditions with the original version of PH5P. Pending changes,
  9. * this lexer will punt to DirectLex if DOM throws an exception.
  10. */
  11. class HTMLPurifier_Lexer_PH5P extends HTMLPurifier_Lexer_DOMLex
  12. {
  13. /**
  14. * @param string $html
  15. * @param HTMLPurifier_Config $config
  16. * @param HTMLPurifier_Context $context
  17. * @return HTMLPurifier_Token[]
  18. */
  19. public function tokenizeHTML($html, $config, $context)
  20. {
  21. $new_html = $this->normalize($html, $config, $context);
  22. $new_html = $this->wrapHTML($new_html, $config, $context);
  23. try {
  24. $parser = new HTML5($new_html);
  25. $doc = $parser->save();
  26. } catch (DOMException $e) {
  27. // Uh oh, it failed. Punt to DirectLex.
  28. $lexer = new HTMLPurifier_Lexer_DirectLex();
  29. $context->register('PH5PError', $e); // save the error, so we can detect it
  30. return $lexer->tokenizeHTML($html, $config, $context); // use original HTML
  31. }
  32. $tokens = array();
  33. $this->tokenizeDOM(
  34. $doc->getElementsByTagName('html')->item(0)-> // <html>
  35. getElementsByTagName('body')->item(0)-> // <body>
  36. getElementsByTagName('div')->item(0) // <div>
  37. ,
  38. $tokens
  39. );
  40. return $tokens;
  41. }
  42. }
  43. /*
  44. Copyright 2007 Jeroen van der Meer <http://jero.net/>
  45. Permission is hereby granted, free of charge, to any person obtaining a
  46. copy of this software and associated documentation files (the
  47. "Software"), to deal in the Software without restriction, including
  48. without limitation the rights to use, copy, modify, merge, publish,
  49. distribute, sublicense, and/or sell copies of the Software, and to
  50. permit persons to whom the Software is furnished to do so, subject to
  51. the following conditions:
  52. The above copyright notice and this permission notice shall be included
  53. in all copies or substantial portions of the Software.
  54. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  55. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  56. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  57. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  58. CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  59. TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  60. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  61. */
  62. class HTML5
  63. {
  64. private $data;
  65. private $char;
  66. private $EOF;
  67. private $state;
  68. private $tree;
  69. private $token;
  70. private $content_model;
  71. private $escape = false;
  72. private $entities = array(
  73. 'AElig;',
  74. 'AElig',
  75. 'AMP;',
  76. 'AMP',
  77. 'Aacute;',
  78. 'Aacute',
  79. 'Acirc;',
  80. 'Acirc',
  81. 'Agrave;',
  82. 'Agrave',
  83. 'Alpha;',
  84. 'Aring;',
  85. 'Aring',
  86. 'Atilde;',
  87. 'Atilde',
  88. 'Auml;',
  89. 'Auml',
  90. 'Beta;',
  91. 'COPY;',
  92. 'COPY',
  93. 'Ccedil;',
  94. 'Ccedil',
  95. 'Chi;',
  96. 'Dagger;',
  97. 'Delta;',
  98. 'ETH;',
  99. 'ETH',
  100. 'Eacute;',
  101. 'Eacute',
  102. 'Ecirc;',
  103. 'Ecirc',
  104. 'Egrave;',
  105. 'Egrave',
  106. 'Epsilon;',
  107. 'Eta;',
  108. 'Euml;',
  109. 'Euml',
  110. 'GT;',
  111. 'GT',
  112. 'Gamma;',
  113. 'Iacute;',
  114. 'Iacute',
  115. 'Icirc;',
  116. 'Icirc',
  117. 'Igrave;',
  118. 'Igrave',
  119. 'Iota;',
  120. 'Iuml;',
  121. 'Iuml',
  122. 'Kappa;',
  123. 'LT;',
  124. 'LT',
  125. 'Lambda;',
  126. 'Mu;',
  127. 'Ntilde;',
  128. 'Ntilde',
  129. 'Nu;',
  130. 'OElig;',
  131. 'Oacute;',
  132. 'Oacute',
  133. 'Ocirc;',
  134. 'Ocirc',
  135. 'Ograve;',
  136. 'Ograve',
  137. 'Omega;',
  138. 'Omicron;',
  139. 'Oslash;',
  140. 'Oslash',
  141. 'Otilde;',
  142. 'Otilde',
  143. 'Ouml;',
  144. 'Ouml',
  145. 'Phi;',
  146. 'Pi;',
  147. 'Prime;',
  148. 'Psi;',
  149. 'QUOT;',
  150. 'QUOT',
  151. 'REG;',
  152. 'REG',
  153. 'Rho;',
  154. 'Scaron;',
  155. 'Sigma;',
  156. 'THORN;',
  157. 'THORN',
  158. 'TRADE;',
  159. 'Tau;',
  160. 'Theta;',
  161. 'Uacute;',
  162. 'Uacute',
  163. 'Ucirc;',
  164. 'Ucirc',
  165. 'Ugrave;',
  166. 'Ugrave',
  167. 'Upsilon;',
  168. 'Uuml;',
  169. 'Uuml',
  170. 'Xi;',
  171. 'Yacute;',
  172. 'Yacute',
  173. 'Yuml;',
  174. 'Zeta;',
  175. 'aacute;',
  176. 'aacute',
  177. 'acirc;',
  178. 'acirc',
  179. 'acute;',
  180. 'acute',
  181. 'aelig;',
  182. 'aelig',
  183. 'agrave;',
  184. 'agrave',
  185. 'alefsym;',
  186. 'alpha;',
  187. 'amp;',
  188. 'amp',
  189. 'and;',
  190. 'ang;',
  191. 'apos;',
  192. 'aring;',
  193. 'aring',
  194. 'asymp;',
  195. 'atilde;',
  196. 'atilde',
  197. 'auml;',
  198. 'auml',
  199. 'bdquo;',
  200. 'beta;',
  201. 'brvbar;',
  202. 'brvbar',
  203. 'bull;',
  204. 'cap;',
  205. 'ccedil;',
  206. 'ccedil',
  207. 'cedil;',
  208. 'cedil',
  209. 'cent;',
  210. 'cent',
  211. 'chi;',
  212. 'circ;',
  213. 'clubs;',
  214. 'cong;',
  215. 'copy;',
  216. 'copy',
  217. 'crarr;',
  218. 'cup;',
  219. 'curren;',
  220. 'curren',
  221. 'dArr;',
  222. 'dagger;',
  223. 'darr;',
  224. 'deg;',
  225. 'deg',
  226. 'delta;',
  227. 'diams;',
  228. 'divide;',
  229. 'divide',
  230. 'eacute;',
  231. 'eacute',
  232. 'ecirc;',
  233. 'ecirc',
  234. 'egrave;',
  235. 'egrave',
  236. 'empty;',
  237. 'emsp;',
  238. 'ensp;',
  239. 'epsilon;',
  240. 'equiv;',
  241. 'eta;',
  242. 'eth;',
  243. 'eth',
  244. 'euml;',
  245. 'euml',
  246. 'euro;',
  247. 'exist;',
  248. 'fnof;',
  249. 'forall;',
  250. 'frac12;',
  251. 'frac12',
  252. 'frac14;',
  253. 'frac14',
  254. 'frac34;',
  255. 'frac34',
  256. 'frasl;',
  257. 'gamma;',
  258. 'ge;',
  259. 'gt;',
  260. 'gt',
  261. 'hArr;',
  262. 'harr;',
  263. 'hearts;',
  264. 'hellip;',
  265. 'iacute;',
  266. 'iacute',
  267. 'icirc;',
  268. 'icirc',
  269. 'iexcl;',
  270. 'iexcl',
  271. 'igrave;',
  272. 'igrave',
  273. 'image;',
  274. 'infin;',
  275. 'int;',
  276. 'iota;',
  277. 'iquest;',
  278. 'iquest',
  279. 'isin;',
  280. 'iuml;',
  281. 'iuml',
  282. 'kappa;',
  283. 'lArr;',
  284. 'lambda;',
  285. 'lang;',
  286. 'laquo;',
  287. 'laquo',
  288. 'larr;',
  289. 'lceil;',
  290. 'ldquo;',
  291. 'le;',
  292. 'lfloor;',
  293. 'lowast;',
  294. 'loz;',
  295. 'lrm;',
  296. 'lsaquo;',
  297. 'lsquo;',
  298. 'lt;',
  299. 'lt',
  300. 'macr;',
  301. 'macr',
  302. 'mdash;',
  303. 'micro;',
  304. 'micro',
  305. 'middot;',
  306. 'middot',
  307. 'minus;',
  308. 'mu;',
  309. 'nabla;',
  310. 'nbsp;',
  311. 'nbsp',
  312. 'ndash;',
  313. 'ne;',
  314. 'ni;',
  315. 'not;',
  316. 'not',
  317. 'notin;',
  318. 'nsub;',
  319. 'ntilde;',
  320. 'ntilde',
  321. 'nu;',
  322. 'oacute;',
  323. 'oacute',
  324. 'ocirc;',
  325. 'ocirc',
  326. 'oelig;',
  327. 'ograve;',
  328. 'ograve',
  329. 'oline;',
  330. 'omega;',
  331. 'omicron;',
  332. 'oplus;',
  333. 'or;',
  334. 'ordf;',
  335. 'ordf',
  336. 'ordm;',
  337. 'ordm',
  338. 'oslash;',
  339. 'oslash',
  340. 'otilde;',
  341. 'otilde',
  342. 'otimes;',
  343. 'ouml;',
  344. 'ouml',
  345. 'para;',
  346. 'para',
  347. 'part;',
  348. 'permil;',
  349. 'perp;',
  350. 'phi;',
  351. 'pi;',
  352. 'piv;',
  353. 'plusmn;',
  354. 'plusmn',
  355. 'pound;',
  356. 'pound',
  357. 'prime;',
  358. 'prod;',
  359. 'prop;',
  360. 'psi;',
  361. 'quot;',
  362. 'quot',
  363. 'rArr;',
  364. 'radic;',
  365. 'rang;',
  366. 'raquo;',
  367. 'raquo',
  368. 'rarr;',
  369. 'rceil;',
  370. 'rdquo;',
  371. 'real;',
  372. 'reg;',
  373. 'reg',
  374. 'rfloor;',
  375. 'rho;',
  376. 'rlm;',
  377. 'rsaquo;',
  378. 'rsquo;',
  379. 'sbquo;',
  380. 'scaron;',
  381. 'sdot;',
  382. 'sect;',
  383. 'sect',
  384. 'shy;',
  385. 'shy',
  386. 'sigma;',
  387. 'sigmaf;',
  388. 'sim;',
  389. 'spades;',
  390. 'sub;',
  391. 'sube;',
  392. 'sum;',
  393. 'sup1;',
  394. 'sup1',
  395. 'sup2;',
  396. 'sup2',
  397. 'sup3;',
  398. 'sup3',
  399. 'sup;',
  400. 'supe;',
  401. 'szlig;',
  402. 'szlig',
  403. 'tau;',
  404. 'there4;',
  405. 'theta;',
  406. 'thetasym;',
  407. 'thinsp;',
  408. 'thorn;',
  409. 'thorn',
  410. 'tilde;',
  411. 'times;',
  412. 'times',
  413. 'trade;',
  414. 'uArr;',
  415. 'uacute;',
  416. 'uacute',
  417. 'uarr;',
  418. 'ucirc;',
  419. 'ucirc',
  420. 'ugrave;',
  421. 'ugrave',
  422. 'uml;',
  423. 'uml',
  424. 'upsih;',
  425. 'upsilon;',
  426. 'uuml;',
  427. 'uuml',
  428. 'weierp;',
  429. 'xi;',
  430. 'yacute;',
  431. 'yacute',
  432. 'yen;',
  433. 'yen',
  434. 'yuml;',
  435. 'yuml',
  436. 'zeta;',
  437. 'zwj;',
  438. 'zwnj;'
  439. );
  440. const PCDATA = 0;
  441. const RCDATA = 1;
  442. const CDATA = 2;
  443. const PLAINTEXT = 3;
  444. const DOCTYPE = 0;
  445. const STARTTAG = 1;
  446. const ENDTAG = 2;
  447. const COMMENT = 3;
  448. const CHARACTR = 4;
  449. const EOF = 5;
  450. public function __construct($data)
  451. {
  452. $this->data = $data;
  453. $this->char = -1;
  454. $this->EOF = strlen($data);
  455. $this->tree = new HTML5TreeConstructer;
  456. $this->content_model = self::PCDATA;
  457. $this->state = 'data';
  458. while ($this->state !== null) {
  459. $this->{$this->state . 'State'}();
  460. }
  461. }
  462. public function save()
  463. {
  464. return $this->tree->save();
  465. }
  466. private function char()
  467. {
  468. return ($this->char < $this->EOF)
  469. ? $this->data[$this->char]
  470. : false;
  471. }
  472. private function character($s, $l = 0)
  473. {
  474. if ($s + $l < $this->EOF) {
  475. if ($l === 0) {
  476. return $this->data[$s];
  477. } else {
  478. return substr($this->data, $s, $l);
  479. }
  480. }
  481. }
  482. private function characters($char_class, $start)
  483. {
  484. return preg_replace('#^([' . $char_class . ']+).*#s', '\\1', substr($this->data, $start));
  485. }
  486. private function dataState()
  487. {
  488. // Consume the next input character
  489. $this->char++;
  490. $char = $this->char();
  491. if ($char === '&' && ($this->content_model === self::PCDATA || $this->content_model === self::RCDATA)) {
  492. /* U+0026 AMPERSAND (&)
  493. When the content model flag is set to one of the PCDATA or RCDATA
  494. states: switch to the entity data state. Otherwise: treat it as per
  495. the "anything else" entry below. */
  496. $this->state = 'entityData';
  497. } elseif ($char === '-') {
  498. /* If the content model flag is set to either the RCDATA state or
  499. the CDATA state, and the escape flag is false, and there are at
  500. least three characters before this one in the input stream, and the
  501. last four characters in the input stream, including this one, are
  502. U+003C LESS-THAN SIGN, U+0021 EXCLAMATION MARK, U+002D HYPHEN-MINUS,
  503. and U+002D HYPHEN-MINUS ("<!--"), then set the escape flag to true. */
  504. if (($this->content_model === self::RCDATA || $this->content_model ===
  505. self::CDATA) && $this->escape === false &&
  506. $this->char >= 3 && $this->character($this->char - 4, 4) === '<!--'
  507. ) {
  508. $this->escape = true;
  509. }
  510. /* In any case, emit the input character as a character token. Stay
  511. in the data state. */
  512. $this->emitToken(
  513. array(
  514. 'type' => self::CHARACTR,
  515. 'data' => $char
  516. )
  517. );
  518. /* U+003C LESS-THAN SIGN (<) */
  519. } elseif ($char === '<' && ($this->content_model === self::PCDATA ||
  520. (($this->content_model === self::RCDATA ||
  521. $this->content_model === self::CDATA) && $this->escape === false))
  522. ) {
  523. /* When the content model flag is set to the PCDATA state: switch
  524. to the tag open state.
  525. When the content model flag is set to either the RCDATA state or
  526. the CDATA state and the escape flag is false: switch to the tag
  527. open state.
  528. Otherwise: treat it as per the "anything else" entry below. */
  529. $this->state = 'tagOpen';
  530. /* U+003E GREATER-THAN SIGN (>) */
  531. } elseif ($char === '>') {
  532. /* If the content model flag is set to either the RCDATA state or
  533. the CDATA state, and the escape flag is true, and the last three
  534. characters in the input stream including this one are U+002D
  535. HYPHEN-MINUS, U+002D HYPHEN-MINUS, U+003E GREATER-THAN SIGN ("-->"),
  536. set the escape flag to false. */
  537. if (($this->content_model === self::RCDATA ||
  538. $this->content_model === self::CDATA) && $this->escape === true &&
  539. $this->character($this->char, 3) === '-->'
  540. ) {
  541. $this->escape = false;
  542. }
  543. /* In any case, emit the input character as a character token.
  544. Stay in the data state. */
  545. $this->emitToken(
  546. array(
  547. 'type' => self::CHARACTR,
  548. 'data' => $char
  549. )
  550. );
  551. } elseif ($this->char === $this->EOF) {
  552. /* EOF
  553. Emit an end-of-file token. */
  554. $this->EOF();
  555. } elseif ($this->content_model === self::PLAINTEXT) {
  556. /* When the content model flag is set to the PLAINTEXT state
  557. THIS DIFFERS GREATLY FROM THE SPEC: Get the remaining characters of
  558. the text and emit it as a character token. */
  559. $this->emitToken(
  560. array(
  561. 'type' => self::CHARACTR,
  562. 'data' => substr($this->data, $this->char)
  563. )
  564. );
  565. $this->EOF();
  566. } else {
  567. /* Anything else
  568. THIS DIFFERS GREATLY FROM THE SPEC: Get as many character that
  569. otherwise would also be treated as a character token and emit it
  570. as a single character token. Stay in the data state. */
  571. $len = strcspn($this->data, '<&', $this->char);
  572. $char = substr($this->data, $this->char, $len);
  573. $this->char += $len - 1;
  574. $this->emitToken(
  575. array(
  576. 'type' => self::CHARACTR,
  577. 'data' => $char
  578. )
  579. );
  580. $this->state = 'data';
  581. }
  582. }
  583. private function entityDataState()
  584. {
  585. // Attempt to consume an entity.
  586. $entity = $this->entity();
  587. // If nothing is returned, emit a U+0026 AMPERSAND character token.
  588. // Otherwise, emit the character token that was returned.
  589. $char = (!$entity) ? '&' : $entity;
  590. $this->emitToken(
  591. array(
  592. 'type' => self::CHARACTR,
  593. 'data' => $char
  594. )
  595. );
  596. // Finally, switch to the data state.
  597. $this->state = 'data';
  598. }
  599. private function tagOpenState()
  600. {
  601. switch ($this->content_model) {
  602. case self::RCDATA:
  603. case self::CDATA:
  604. /* If the next input character is a U+002F SOLIDUS (/) character,
  605. consume it and switch to the close tag open state. If the next
  606. input character is not a U+002F SOLIDUS (/) character, emit a
  607. U+003C LESS-THAN SIGN character token and switch to the data
  608. state to process the next input character. */
  609. if ($this->character($this->char + 1) === '/') {
  610. $this->char++;
  611. $this->state = 'closeTagOpen';
  612. } else {
  613. $this->emitToken(
  614. array(
  615. 'type' => self::CHARACTR,
  616. 'data' => '<'
  617. )
  618. );
  619. $this->state = 'data';
  620. }
  621. break;
  622. case self::PCDATA:
  623. // If the content model flag is set to the PCDATA state
  624. // Consume the next input character:
  625. $this->char++;
  626. $char = $this->char();
  627. if ($char === '!') {
  628. /* U+0021 EXCLAMATION MARK (!)
  629. Switch to the markup declaration open state. */
  630. $this->state = 'markupDeclarationOpen';
  631. } elseif ($char === '/') {
  632. /* U+002F SOLIDUS (/)
  633. Switch to the close tag open state. */
  634. $this->state = 'closeTagOpen';
  635. } elseif (preg_match('/^[A-Za-z]$/', $char)) {
  636. /* U+0041 LATIN LETTER A through to U+005A LATIN LETTER Z
  637. Create a new start tag token, set its tag name to the lowercase
  638. version of the input character (add 0x0020 to the character's code
  639. point), then switch to the tag name state. (Don't emit the token
  640. yet; further details will be filled in before it is emitted.) */
  641. $this->token = array(
  642. 'name' => strtolower($char),
  643. 'type' => self::STARTTAG,
  644. 'attr' => array()
  645. );
  646. $this->state = 'tagName';
  647. } elseif ($char === '>') {
  648. /* U+003E GREATER-THAN SIGN (>)
  649. Parse error. Emit a U+003C LESS-THAN SIGN character token and a
  650. U+003E GREATER-THAN SIGN character token. Switch to the data state. */
  651. $this->emitToken(
  652. array(
  653. 'type' => self::CHARACTR,
  654. 'data' => '<>'
  655. )
  656. );
  657. $this->state = 'data';
  658. } elseif ($char === '?') {
  659. /* U+003F QUESTION MARK (?)
  660. Parse error. Switch to the bogus comment state. */
  661. $this->state = 'bogusComment';
  662. } else {
  663. /* Anything else
  664. Parse error. Emit a U+003C LESS-THAN SIGN character token and
  665. reconsume the current input character in the data state. */
  666. $this->emitToken(
  667. array(
  668. 'type' => self::CHARACTR,
  669. 'data' => '<'
  670. )
  671. );
  672. $this->char--;
  673. $this->state = 'data';
  674. }
  675. break;
  676. }
  677. }
  678. private function closeTagOpenState()
  679. {
  680. $next_node = strtolower($this->characters('A-Za-z', $this->char + 1));
  681. $the_same = count($this->tree->stack) > 0 && $next_node === end($this->tree->stack)->nodeName;
  682. if (($this->content_model === self::RCDATA || $this->content_model === self::CDATA) &&
  683. (!$the_same || ($the_same && (!preg_match(
  684. '/[\t\n\x0b\x0c >\/]/',
  685. $this->character($this->char + 1 + strlen($next_node))
  686. ) || $this->EOF === $this->char)))
  687. ) {
  688. /* If the content model flag is set to the RCDATA or CDATA states then
  689. examine the next few characters. If they do not match the tag name of
  690. the last start tag token emitted (case insensitively), or if they do but
  691. they are not immediately followed by one of the following characters:
  692. * U+0009 CHARACTER TABULATION
  693. * U+000A LINE FEED (LF)
  694. * U+000B LINE TABULATION
  695. * U+000C FORM FEED (FF)
  696. * U+0020 SPACE
  697. * U+003E GREATER-THAN SIGN (>)
  698. * U+002F SOLIDUS (/)
  699. * EOF
  700. ...then there is a parse error. Emit a U+003C LESS-THAN SIGN character
  701. token, a U+002F SOLIDUS character token, and switch to the data state
  702. to process the next input character. */
  703. $this->emitToken(
  704. array(
  705. 'type' => self::CHARACTR,
  706. 'data' => '</'
  707. )
  708. );
  709. $this->state = 'data';
  710. } else {
  711. /* Otherwise, if the content model flag is set to the PCDATA state,
  712. or if the next few characters do match that tag name, consume the
  713. next input character: */
  714. $this->char++;
  715. $char = $this->char();
  716. if (preg_match('/^[A-Za-z]$/', $char)) {
  717. /* U+0041 LATIN LETTER A through to U+005A LATIN LETTER Z
  718. Create a new end tag token, set its tag name to the lowercase version
  719. of the input character (add 0x0020 to the character's code point), then
  720. switch to the tag name state. (Don't emit the token yet; further details
  721. will be filled in before it is emitted.) */
  722. $this->token = array(
  723. 'name' => strtolower($char),
  724. 'type' => self::ENDTAG
  725. );
  726. $this->state = 'tagName';
  727. } elseif ($char === '>') {
  728. /* U+003E GREATER-THAN SIGN (>)
  729. Parse error. Switch to the data state. */
  730. $this->state = 'data';
  731. } elseif ($this->char === $this->EOF) {
  732. /* EOF
  733. Parse error. Emit a U+003C LESS-THAN SIGN character token and a U+002F
  734. SOLIDUS character token. Reconsume the EOF character in the data state. */
  735. $this->emitToken(
  736. array(
  737. 'type' => self::CHARACTR,
  738. 'data' => '</'
  739. )
  740. );
  741. $this->char--;
  742. $this->state = 'data';
  743. } else {
  744. /* Parse error. Switch to the bogus comment state. */
  745. $this->state = 'bogusComment';
  746. }
  747. }
  748. }
  749. private function tagNameState()
  750. {
  751. // Consume the next input character:
  752. $this->char++;
  753. $char = $this->character($this->char);
  754. if (preg_match('/^[\t\n\x0b\x0c ]$/', $char)) {
  755. /* U+0009 CHARACTER TABULATION
  756. U+000A LINE FEED (LF)
  757. U+000B LINE TABULATION
  758. U+000C FORM FEED (FF)
  759. U+0020 SPACE
  760. Switch to the before attribute name state. */
  761. $this->state = 'beforeAttributeName';
  762. } elseif ($char === '>') {
  763. /* U+003E GREATER-THAN SIGN (>)
  764. Emit the current tag token. Switch to the data state. */
  765. $this->emitToken($this->token);
  766. $this->state = 'data';
  767. } elseif ($this->char === $this->EOF) {
  768. /* EOF
  769. Parse error. Emit the current tag token. Reconsume the EOF
  770. character in the data state. */
  771. $this->emitToken($this->token);
  772. $this->char--;
  773. $this->state = 'data';
  774. } elseif ($char === '/') {
  775. /* U+002F SOLIDUS (/)
  776. Parse error unless this is a permitted slash. Switch to the before
  777. attribute name state. */
  778. $this->state = 'beforeAttributeName';
  779. } else {
  780. /* Anything else
  781. Append the current input character to the current tag token's tag name.
  782. Stay in the tag name state. */
  783. $this->token['name'] .= strtolower($char);
  784. $this->state = 'tagName';
  785. }
  786. }
  787. private function beforeAttributeNameState()
  788. {
  789. // Consume the next input character:
  790. $this->char++;
  791. $char = $this->character($this->char);
  792. if (preg_match('/^[\t\n\x0b\x0c ]$/', $char)) {
  793. /* U+0009 CHARACTER TABULATION
  794. U+000A LINE FEED (LF)
  795. U+000B LINE TABULATION
  796. U+000C FORM FEED (FF)
  797. U+0020 SPACE
  798. Stay in the before attribute name state. */
  799. $this->state = 'beforeAttributeName';
  800. } elseif ($char === '>') {
  801. /* U+003E GREATER-THAN SIGN (>)
  802. Emit the current tag token. Switch to the data state. */
  803. $this->emitToken($this->token);
  804. $this->state = 'data';
  805. } elseif ($char === '/') {
  806. /* U+002F SOLIDUS (/)
  807. Parse error unless this is a permitted slash. Stay in the before
  808. attribute name state. */
  809. $this->state = 'beforeAttributeName';
  810. } elseif ($this->char === $this->EOF) {
  811. /* EOF
  812. Parse error. Emit the current tag token. Reconsume the EOF
  813. character in the data state. */
  814. $this->emitToken($this->token);
  815. $this->char--;
  816. $this->state = 'data';
  817. } else {
  818. /* Anything else
  819. Start a new attribute in the current tag token. Set that attribute's
  820. name to the current input character, and its value to the empty string.
  821. Switch to the attribute name state. */
  822. $this->token['attr'][] = array(
  823. 'name' => strtolower($char),
  824. 'value' => null
  825. );
  826. $this->state = 'attributeName';
  827. }
  828. }
  829. private function attributeNameState()
  830. {
  831. // Consume the next input character:
  832. $this->char++;
  833. $char = $this->character($this->char);
  834. if (preg_match('/^[\t\n\x0b\x0c ]$/', $char)) {
  835. /* U+0009 CHARACTER TABULATION
  836. U+000A LINE FEED (LF)
  837. U+000B LINE TABULATION
  838. U+000C FORM FEED (FF)
  839. U+0020 SPACE
  840. Stay in the before attribute name state. */
  841. $this->state = 'afterAttributeName';
  842. } elseif ($char === '=') {
  843. /* U+003D EQUALS SIGN (=)
  844. Switch to the before attribute value state. */
  845. $this->state = 'beforeAttributeValue';
  846. } elseif ($char === '>') {
  847. /* U+003E GREATER-THAN SIGN (>)
  848. Emit the current tag token. Switch to the data state. */
  849. $this->emitToken($this->token);
  850. $this->state = 'data';
  851. } elseif ($char === '/' && $this->character($this->char + 1) !== '>') {
  852. /* U+002F SOLIDUS (/)
  853. Parse error unless this is a permitted slash. Switch to the before
  854. attribute name state. */
  855. $this->state = 'beforeAttributeName';
  856. } elseif ($this->char === $this->EOF) {
  857. /* EOF
  858. Parse error. Emit the current tag token. Reconsume the EOF
  859. character in the data state. */
  860. $this->emitToken($this->token);
  861. $this->char--;
  862. $this->state = 'data';
  863. } else {
  864. /* Anything else
  865. Append the current input character to the current attribute's name.
  866. Stay in the attribute name state. */
  867. $last = count($this->token['attr']) - 1;
  868. $this->token['attr'][$last]['name'] .= strtolower($char);
  869. $this->state = 'attributeName';
  870. }
  871. }
  872. private function afterAttributeNameState()
  873. {
  874. // Consume the next input character:
  875. $this->char++;
  876. $char = $this->character($this->char);
  877. if (preg_match('/^[\t\n\x0b\x0c ]$/', $char)) {
  878. /* U+0009 CHARACTER TABULATION
  879. U+000A LINE FEED (LF)
  880. U+000B LINE TABULATION
  881. U+000C FORM FEED (FF)
  882. U+0020 SPACE
  883. Stay in the after attribute name state. */
  884. $this->state = 'afterAttributeName';
  885. } elseif ($char === '=') {
  886. /* U+003D EQUALS SIGN (=)
  887. Switch to the before attribute value state. */
  888. $this->state = 'beforeAttributeValue';
  889. } elseif ($char === '>') {
  890. /* U+003E GREATER-THAN SIGN (>)
  891. Emit the current tag token. Switch to the data state. */
  892. $this->emitToken($this->token);
  893. $this->state = 'data';
  894. } elseif ($char === '/' && $this->character($this->char + 1) !== '>') {
  895. /* U+002F SOLIDUS (/)
  896. Parse error unless this is a permitted slash. Switch to the
  897. before attribute name state. */
  898. $this->state = 'beforeAttributeName';
  899. } elseif ($this->char === $this->EOF) {
  900. /* EOF
  901. Parse error. Emit the current tag token. Reconsume the EOF
  902. character in the data state. */
  903. $this->emitToken($this->token);
  904. $this->char--;
  905. $this->state = 'data';
  906. } else {
  907. /* Anything else
  908. Start a new attribute in the current tag token. Set that attribute's
  909. name to the current input character, and its value to the empty string.
  910. Switch to the attribute name state. */
  911. $this->token['attr'][] = array(
  912. 'name' => strtolower($char),
  913. 'value' => null
  914. );
  915. $this->state = 'attributeName';
  916. }
  917. }
  918. private function beforeAttributeValueState()
  919. {
  920. // Consume the next input character:
  921. $this->char++;
  922. $char = $this->character($this->char);
  923. if (preg_match('/^[\t\n\x0b\x0c ]$/', $char)) {
  924. /* U+0009 CHARACTER TABULATION
  925. U+000A LINE FEED (LF)
  926. U+000B LINE TABULATION
  927. U+000C FORM FEED (FF)
  928. U+0020 SPACE
  929. Stay in the before attribute value state. */
  930. $this->state = 'beforeAttributeValue';
  931. } elseif ($char === '"') {
  932. /* U+0022 QUOTATION MARK (")
  933. Switch to the attribute value (double-quoted) state. */
  934. $this->state = 'attributeValueDoubleQuoted';
  935. } elseif ($char === '&') {
  936. /* U+0026 AMPERSAND (&)
  937. Switch to the attribute value (unquoted) state and reconsume
  938. this input character. */
  939. $this->char--;
  940. $this->state = 'attributeValueUnquoted';
  941. } elseif ($char === '\'') {
  942. /* U+0027 APOSTROPHE (')
  943. Switch to the attribute value (single-quoted) state. */
  944. $this->state = 'attributeValueSingleQuoted';
  945. } elseif ($char === '>') {
  946. /* U+003E GREATER-THAN SIGN (>)
  947. Emit the current tag token. Switch to the data state. */
  948. $this->emitToken($this->token);
  949. $this->state = 'data';
  950. } else {
  951. /* Anything else
  952. Append the current input character to the current attribute's value.
  953. Switch to the attribute value (unquoted) state. */
  954. $last = count($this->token['attr']) - 1;
  955. $this->token['attr'][$last]['value'] .= $char;
  956. $this->state = 'attributeValueUnquoted';
  957. }
  958. }
  959. private function attributeValueDoubleQuotedState()
  960. {
  961. // Consume the next input character:
  962. $this->char++;
  963. $char = $this->character($this->char);
  964. if ($char === '"') {
  965. /* U+0022 QUOTATION MARK (")
  966. Switch to the before attribute name state. */
  967. $this->state = 'beforeAttributeName';
  968. } elseif ($char === '&') {
  969. /* U+0026 AMPERSAND (&)
  970. Switch to the entity in attribute value state. */
  971. $this->entityInAttributeValueState('double');
  972. } elseif ($this->char === $this->EOF) {
  973. /* EOF
  974. Parse error. Emit the current tag token. Reconsume the character
  975. in the data state. */
  976. $this->emitToken($this->token);
  977. $this->char--;
  978. $this->state = 'data';
  979. } else {
  980. /* Anything else
  981. Append the current input character to the current attribute's value.
  982. Stay in the attribute value (double-quoted) state. */
  983. $last = count($this->token['attr']) - 1;
  984. $this->token['attr'][$last]['value'] .= $char;
  985. $this->state = 'attributeValueDoubleQuoted';
  986. }
  987. }
  988. private function attributeValueSingleQuotedState()
  989. {
  990. // Consume the next input character:
  991. $this->char++;
  992. $char = $this->character($this->char);
  993. if ($char === '\'') {
  994. /* U+0022 QUOTATION MARK (')
  995. Switch to the before attribute name state. */
  996. $this->state = 'beforeAttributeName';
  997. } elseif ($char === '&') {
  998. /* U+0026 AMPERSAND (&)
  999. Switch to the entity in attribute value state. */
  1000. $this->entityInAttributeValueState('single');
  1001. } elseif ($this->char === $this->EOF) {
  1002. /* EOF
  1003. Parse error. Emit the current tag token. Reconsume the character
  1004. in the data state. */
  1005. $this->emitToken($this->token);
  1006. $this->char--;
  1007. $this->state = 'data';
  1008. } else {
  1009. /* Anything else
  1010. Append the current input character to the current attribute's value.
  1011. Stay in the attribute value (single-quoted) state. */
  1012. $last = count($this->token['attr']) - 1;
  1013. $this->token['attr'][$last]['value'] .= $char;
  1014. $this->state = 'attributeValueSingleQuoted';
  1015. }
  1016. }
  1017. private function attributeValueUnquotedState()
  1018. {
  1019. // Consume the next input character:
  1020. $this->char++;
  1021. $char = $this->character($this->char);
  1022. if (preg_match('/^[\t\n\x0b\x0c ]$/', $char)) {
  1023. /* U+0009 CHARACTER TABULATION
  1024. U+000A LINE FEED (LF)
  1025. U+000B LINE TABULATION
  1026. U+000C FORM FEED (FF)
  1027. U+0020 SPACE
  1028. Switch to the before attribute name state. */
  1029. $this->state = 'beforeAttributeName';
  1030. } elseif ($char === '&') {
  1031. /* U+0026 AMPERSAND (&)
  1032. Switch to the entity in attribute value state. */
  1033. $this->entityInAttributeValueState();
  1034. } elseif ($char === '>') {
  1035. /* U+003E GREATER-THAN SIGN (>)
  1036. Emit the current tag token. Switch to the data state. */
  1037. $this->emitToken($this->token);
  1038. $this->state = 'data';
  1039. } else {
  1040. /* Anything else
  1041. Append the current input character to the current attribute's value.
  1042. Stay in the attribute value (unquoted) state. */
  1043. $last = count($this->token['attr']) - 1;
  1044. $this->token['attr'][$last]['value'] .= $char;
  1045. $this->state = 'attributeValueUnquoted';
  1046. }
  1047. }
  1048. private function entityInAttributeValueState()
  1049. {
  1050. // Attempt to consume an entity.
  1051. $entity = $this->entity();
  1052. // If nothing is returned, append a U+0026 AMPERSAND character to the
  1053. // current attribute's value. Otherwise, emit the character token that
  1054. // was returned.
  1055. $char = (!$entity)
  1056. ? '&'
  1057. : $entity;
  1058. $last = count($this->token['attr']) - 1;
  1059. $this->token['attr'][$last]['value'] .= $char;
  1060. }
  1061. private function bogusCommentState()
  1062. {
  1063. /* Consume every character up to the first U+003E GREATER-THAN SIGN
  1064. character (>) or the end of the file (EOF), whichever comes first. Emit
  1065. a comment token whose data is the concatenation of all the characters
  1066. starting from and including the character that caused the state machine
  1067. to switch into the bogus comment state, up to and including the last
  1068. consumed character before the U+003E character, if any, or up to the
  1069. end of the file otherwise. (If the comment was started by the end of
  1070. the file (EOF), the token is empty.) */
  1071. $data = $this->characters('^>', $this->char);
  1072. $this->emitToken(
  1073. array(
  1074. 'data' => $data,
  1075. 'type' => self::COMMENT
  1076. )
  1077. );
  1078. $this->char += strlen($data);
  1079. /* Switch to the data state. */
  1080. $this->state = 'data';
  1081. /* If the end of the file was reached, reconsume the EOF character. */
  1082. if ($this->char === $this->EOF) {
  1083. $this->char = $this->EOF - 1;
  1084. }
  1085. }
  1086. private function markupDeclarationOpenState()
  1087. {
  1088. /* If the next two characters are both U+002D HYPHEN-MINUS (-)
  1089. characters, consume those two characters, create a comment token whose
  1090. data is the empty string, and switch to the comment state. */
  1091. if ($this->character($this->char + 1, 2) === '--') {
  1092. $this->char += 2;
  1093. $this->state = 'comment';
  1094. $this->token = array(
  1095. 'data' => null,
  1096. 'type' => self::COMMENT
  1097. );
  1098. /* Otherwise if the next seven chacacters are a case-insensitive match
  1099. for the word "DOCTYPE", then consume those characters and switch to the
  1100. DOCTYPE state. */
  1101. } elseif (strtolower($this->character($this->char + 1, 7)) === 'doctype') {
  1102. $this->char += 7;
  1103. $this->state = 'doctype';
  1104. /* Otherwise, is is a parse error. Switch to the bogus comment state.
  1105. The next character that is consumed, if any, is the first character
  1106. that will be in the comment. */
  1107. } else {
  1108. $this->char++;
  1109. $this->state = 'bogusComment';
  1110. }
  1111. }
  1112. private function commentState()
  1113. {
  1114. /* Consume the next input character: */
  1115. $this->char++;
  1116. $char = $this->char();
  1117. /* U+002D HYPHEN-MINUS (-) */
  1118. if ($char === '-') {
  1119. /* Switch to the comment dash state */
  1120. $this->state = 'commentDash';
  1121. /* EOF */
  1122. } elseif ($this->char === $this->EOF) {
  1123. /* Parse error. Emit the comment token. Reconsume the EOF character
  1124. in the data state. */
  1125. $this->emitToken($this->token);
  1126. $this->char--;
  1127. $this->state = 'data';
  1128. /* Anything else */
  1129. } else {
  1130. /* Append the input character to the comment token's data. Stay in
  1131. the comment state. */
  1132. $this->token['data'] .= $char;
  1133. }
  1134. }
  1135. private function commentDashState()
  1136. {
  1137. /* Consume the next input character: */
  1138. $this->char++;
  1139. $char = $this->char();
  1140. /* U+002D HYPHEN-MINUS (-) */
  1141. if ($char === '-') {
  1142. /* Switch to the comment end state */
  1143. $this->state = 'commentEnd';
  1144. /* EOF */
  1145. } elseif ($this->char === $this->EOF) {
  1146. /* Parse error. Emit the comment token. Reconsume the EOF character
  1147. in the data state. */
  1148. $this->emitToken($this->token);
  1149. $this->char--;
  1150. $this->state = 'data';
  1151. /* Anything else */
  1152. } else {
  1153. /* Append a U+002D HYPHEN-MINUS (-) character and the input
  1154. character to the comment token's data. Switch to the comment state. */
  1155. $this->token['data'] .= '-' . $char;
  1156. $this->state = 'comment';
  1157. }
  1158. }
  1159. private function commentEndState()
  1160. {
  1161. /* Consume the next input character: */
  1162. $this->char++;
  1163. $char = $this->char();
  1164. if ($char === '>') {
  1165. $this->emitToken($this->token);
  1166. $this->state = 'data';
  1167. } elseif ($char === '-') {
  1168. $this->token['data'] .= '-';
  1169. } elseif ($this->char === $this->EOF) {
  1170. $this->emitToken($this->token);
  1171. $this->char--;
  1172. $this->state = 'data';
  1173. } else {
  1174. $this->token['data'] .= '--' . $char;
  1175. $this->state = 'comment';
  1176. }
  1177. }
  1178. private function doctypeState()
  1179. {
  1180. /* Consume the next input character: */
  1181. $this->char++;
  1182. $char = $this->char();
  1183. if (preg_match('/^[\t\n\x0b\x0c ]$/', $char)) {
  1184. $this->state = 'beforeDoctypeName';
  1185. } else {
  1186. $this->char--;
  1187. $this->state = 'beforeDoctypeName';
  1188. }
  1189. }
  1190. private function beforeDoctypeNameState()
  1191. {
  1192. /* Consume the next input character: */
  1193. $this->char++;
  1194. $char = $this->char();
  1195. if (preg_match('/^[\t\n\x0b\x0c ]$/', $char)) {
  1196. // Stay in the before DOCTYPE name state.
  1197. } elseif (preg_match('/^[a-z]$/', $char)) {
  1198. $this->token = array(
  1199. 'name' => strtoupper($char),
  1200. 'type' => self::DOCTYPE,
  1201. 'error' => true
  1202. );
  1203. $this->state = 'doctypeName';
  1204. } elseif ($char === '>') {
  1205. $this->emitToken(
  1206. array(
  1207. 'name' => null,
  1208. 'type' => self::DOCTYPE,
  1209. 'error' => true
  1210. )
  1211. );
  1212. $this->state = 'data';
  1213. } elseif ($this->char === $this->EOF) {
  1214. $this->emitToken(
  1215. array(
  1216. 'name' => null,
  1217. 'type' => self::DOCTYPE,
  1218. 'error' => true
  1219. )
  1220. );
  1221. $this->char--;
  1222. $this->state = 'data';
  1223. } else {
  1224. $this->token = array(
  1225. 'name' => $char,
  1226. 'type' => self::DOCTYPE,
  1227. 'error' => true
  1228. );
  1229. $this->state = 'doctypeName';
  1230. }
  1231. }
  1232. private function doctypeNameState()
  1233. {
  1234. /* Consume the next input character: */
  1235. $this->char++;
  1236. $char = $this->char();
  1237. if (preg_match('/^[\t\n\x0b\x0c ]$/', $char)) {
  1238. $this->state = 'AfterDoctypeName';
  1239. } elseif ($char === '>') {
  1240. $this->emitToken($this->token);
  1241. $this->state = 'data';
  1242. } elseif (preg_match('/^[a-z]$/', $char)) {
  1243. $this->token['name'] .= strtoupper($char);
  1244. } elseif ($this->char === $this->EOF) {
  1245. $this->emitToken($this->token);
  1246. $this->char--;
  1247. $this->state = 'data';
  1248. } else {
  1249. $this->token['name'] .= $char;
  1250. }
  1251. $this->token['error'] = ($this->token['name'] === 'HTML')
  1252. ? false
  1253. : true;
  1254. }
  1255. private function afterDoctypeNameState()
  1256. {
  1257. /* Consume the next input character: */
  1258. $this->char++;
  1259. $char = $this->char();
  1260. if (preg_match('/^[\t\n\x0b\x0c ]$/', $char)) {
  1261. // Stay in the DOCTYPE name state.
  1262. } elseif ($char === '>') {
  1263. $this->emitToken($this->token);
  1264. $this->state = 'data';
  1265. } elseif ($this->char === $this->EOF) {
  1266. $this->emitToken($this->token);
  1267. $this->char--;
  1268. $this->state = 'data';
  1269. } else {
  1270. $this->token['error'] = true;
  1271. $this->state = 'bogusDoctype';
  1272. }
  1273. }
  1274. private function bogusDoctypeState()
  1275. {
  1276. /* Consume the next input character: */
  1277. $this->char++;
  1278. $char = $this->char();
  1279. if ($char === '>') {
  1280. $this->emitToken($this->token);
  1281. $this->state = 'data';
  1282. } elseif ($this->char === $this->EOF) {
  1283. $this->emitToken($this->token);
  1284. $this->char--;
  1285. $this->state = 'data';
  1286. } else {
  1287. // Stay in the bogus DOCTYPE state.
  1288. }
  1289. }
  1290. private function entity()
  1291. {
  1292. $start = $this->char;
  1293. // This section defines how to consume an entity. This definition is
  1294. // used when parsing entities in text and in attributes.
  1295. // The behaviour depends on the identity of the next character (the
  1296. // one immediately after the U+0026 AMPERSAND character):
  1297. switch ($this->character($this->char + 1)) {
  1298. // U+0023 NUMBER SIGN (#)
  1299. case '#':
  1300. // The behaviour further depends on the character after the
  1301. // U+0023 NUMBER SIGN:
  1302. switch ($this->character($this->char + 1)) {
  1303. // U+0078 LATIN SMALL LETTER X
  1304. // U+0058 LATIN CAPITAL LETTER X
  1305. case 'x':
  1306. case 'X':
  1307. // Follow the steps below, but using the range of
  1308. // characters U+0030 DIGIT ZERO through to U+0039 DIGIT
  1309. // NINE, U+0061 LATIN SMALL LETTER A through to U+0066
  1310. // LATIN SMALL LETTER F, and U+0041 LATIN CAPITAL LETTER
  1311. // A, through to U+0046 LATIN CAPITAL LETTER F (in other
  1312. // words, 0-9, A-F, a-f).
  1313. $char = 1;
  1314. $char_class = '0-9A-Fa-f';
  1315. break;
  1316. // Anything else
  1317. default:
  1318. // Follow the steps below, but using the range of
  1319. // characters U+0030 DIGIT ZERO through to U+0039 DIGIT
  1320. // NINE (i.e. just 0-9).
  1321. $char = 0;
  1322. $char_class = '0-9';
  1323. break;
  1324. }
  1325. // Consume as many characters as match the range of characters
  1326. // given above.
  1327. $this->char++;
  1328. $e_name = $this->characters($char_class, $this->char + $char + 1);
  1329. $entity = $this->character($start, $this->char);
  1330. $cond = strlen($e_name) > 0;
  1331. // The rest of the parsing happens bellow.
  1332. break;
  1333. // Anything else
  1334. default:
  1335. // Consume the maximum number of characters possible, with the
  1336. // consumed characters case-sensitively matching one of the
  1337. // identifiers in the first column of the entities table.
  1338. $e_name = $this->characters('0-9A-Za-z;', $this->char + 1);
  1339. $len = strlen($e_name);
  1340. for ($c = 1; $c <= $len; $c++) {
  1341. $id = substr($e_name, 0, $c);
  1342. $this->char++;
  1343. if (in_array($id, $this->entities)) {
  1344. if ($e_name[$c - 1] !== ';') {
  1345. if ($c < $len && $e_name[$c] == ';') {
  1346. $this->char++; // consume extra semicolon
  1347. }
  1348. }
  1349. $entity = $id;
  1350. break;
  1351. }
  1352. }
  1353. $cond = isset($entity);
  1354. // The rest of the parsing happens bellow.
  1355. break;
  1356. }
  1357. if (!$cond) {
  1358. // If no match can be made, then this is a parse error. No
  1359. // characters are consumed, and nothing is returned.
  1360. $this->char = $start;
  1361. return false;
  1362. }
  1363. // Return a character token for the character corresponding to the
  1364. // entity name (as given by the second column of the entities table).
  1365. return html_entity_decode('&' . $entity . ';', ENT_QUOTES, 'UTF-8');
  1366. }
  1367. private function emitToken($token)
  1368. {
  1369. $emit = $this->tree->emitToken($token);
  1370. if (is_int($emit)) {
  1371. $this->content_model = $emit;
  1372. } elseif ($token['type'] === self::ENDTAG) {
  1373. $this->content_model = self::PCDATA;
  1374. }
  1375. }
  1376. private function EOF()
  1377. {
  1378. $this->state = null;
  1379. $this->tree->emitToken(
  1380. array(
  1381. 'type' => self::EOF
  1382. )
  1383. );
  1384. }
  1385. }
  1386. class HTML5TreeConstructer
  1387. {
  1388. public $stack = array();
  1389. private $phase;
  1390. private $mode;
  1391. private $dom;
  1392. private $foster_parent = null;
  1393. private $a_formatting = array();
  1394. private $head_pointer = null;
  1395. private $form_pointer = null;
  1396. private $scoping = array('button', 'caption', 'html', 'marquee', 'object', 'table', 'td', 'th');
  1397. private $formatting = array(
  1398. 'a',
  1399. 'b',
  1400. 'big',
  1401. 'em',
  1402. 'font',
  1403. 'i',
  1404. 'nobr',
  1405. 's',
  1406. 'small',
  1407. 'strike',
  1408. 'strong',
  1409. 'tt',
  1410. 'u'
  1411. );
  1412. private $special = array(
  1413. 'address',
  1414. 'area',
  1415. 'base',
  1416. 'basefont',
  1417. 'bgsound',
  1418. 'blockquote',
  1419. 'body',
  1420. 'br',
  1421. 'center',
  1422. 'col',
  1423. 'colgroup',
  1424. 'dd',
  1425. 'dir',
  1426. 'div',
  1427. 'dl',
  1428. 'dt',
  1429. 'embed',
  1430. 'fieldset',
  1431. 'form',
  1432. 'frame',
  1433. 'frameset',
  1434. 'h1',
  1435. 'h2',
  1436. 'h3',
  1437. 'h4',
  1438. 'h5',
  1439. 'h6',
  1440. 'head',
  1441. 'hr',
  1442. 'iframe',
  1443. 'image',
  1444. 'img',
  1445. 'input',
  1446. 'isindex',
  1447. 'li',
  1448. 'link',
  1449. 'listing',
  1450. 'menu',
  1451. 'meta',
  1452. 'noembed',
  1453. 'noframes',
  1454. 'noscript',
  1455. 'ol',
  1456. 'optgroup',
  1457. 'option',
  1458. 'p',
  1459. 'param',
  1460. 'plaintext',
  1461. 'pre',
  1462. 'script',
  1463. 'select',
  1464. 'spacer',
  1465. 'style',
  1466. 'tbody',
  1467. 'textarea',
  1468. 'tfoot',
  1469. 'thead',
  1470. 'title',
  1471. 'tr',
  1472. 'ul',
  1473. 'wbr'
  1474. );
  1475. // The different phases.
  1476. const INIT_PHASE = 0;
  1477. const ROOT_PHASE = 1;
  1478. const MAIN_PHASE = 2;
  1479. const END_PHASE = 3;
  1480. // The different insertion modes for the main phase.
  1481. const BEFOR_HEAD = 0;
  1482. const IN_HEAD = 1;
  1483. const AFTER_HEAD = 2;
  1484. const IN_BODY = 3;
  1485. const IN_TABLE = 4;
  1486. const IN_CAPTION = 5;
  1487. const IN_CGROUP = 6;
  1488. const IN_TBODY = 7;
  1489. const IN_ROW = 8;
  1490. const IN_CELL = 9;
  1491. const IN_SELECT = 10;
  1492. const AFTER_BODY = 11;
  1493. const IN_FRAME = 12;
  1494. const AFTR_FRAME = 13;
  1495. // The different types of elements.
  1496. const SPECIAL = 0;
  1497. const SCOPING = 1;
  1498. const FORMATTING = 2;
  1499. const PHRASING = 3;
  1500. const MARKER = 0;
  1501. public function __construct()
  1502. {
  1503. $this->phase = self::INIT_PHASE;
  1504. $this->mode = self::BEFOR_HEAD;
  1505. $this->dom = new DOMDocument;
  1506. $this->dom->encoding = 'UTF-8';
  1507. $this->dom->preserveWhiteSpace = true;
  1508. $this->dom->substituteEntities = true;
  1509. $this->dom->strictErrorChecking = false;
  1510. }
  1511. // Process tag tokens
  1512. public function emitToken($token)
  1513. {
  1514. switch ($this->phase) {
  1515. case self::INIT_PHASE:
  1516. return $this->initPhase($token);
  1517. break;
  1518. case self::ROOT_PHASE:
  1519. return $this->rootElementPhase($token);
  1520. break;
  1521. case self::MAIN_PHASE:
  1522. return $this->mainPhase($token);
  1523. break;
  1524. case self::END_PHASE :
  1525. return $this->trailingEndPhase($token);
  1526. break;
  1527. }
  1528. }
  1529. private function initPhase($token)
  1530. {
  1531. /* Initially, the tree construction stage must handle each token
  1532. emitted from the tokenisation stage as follows: */
  1533. /* A DOCTYPE token that is marked as being in error
  1534. A comment token
  1535. A start tag token
  1536. An end tag token
  1537. A character token that is not one of one of U+0009 CHARACTER TABULATION,
  1538. U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF),
  1539. or U+0020 SPACE
  1540. An end-of-file token */
  1541. if ((isset($token['error']) && $token['error']) ||
  1542. $token['type'] === HTML5::COMMENT ||
  1543. $token['type'] === HTML5::STARTTAG ||
  1544. $token['type'] === HTML5::ENDTAG ||
  1545. $token['type'] === HTML5::EOF ||
  1546. ($token['type'] === HTML5::CHARACTR && isset($token['data']) &&
  1547. !preg_match('/^[\t\n\x0b\x0c ]+$/', $token['data']))
  1548. ) {
  1549. /* This specification does not define how to handle this case. In
  1550. particular, user agents may ignore the entirety of this specification
  1551. altogether for such documents, and instead invoke special parse modes
  1552. with a greater emphasis on backwards compatibility. */
  1553. $this->phase = self::ROOT_PHASE;
  1554. return $this->rootElementPhase($token);
  1555. /* A DOCTYPE token marked as being correct */
  1556. } elseif (isset($token['error']) && !$token['error']) {
  1557. /* Append a DocumentType node to the Document node, with the name
  1558. attribute set to the name given in the DOCTYPE token (which will be
  1559. "HTML"), and the other attributes specific to DocumentType objects
  1560. set to null, empty lists, or the empty string as appropriate. */
  1561. $doctype = new DOMDocumentType(null, null, 'HTML');
  1562. /* Then, switch to the root element phase of the tree construction
  1563. stage. */
  1564. $this->phase = self::ROOT_PHASE;
  1565. /* A character token that is one of one of U+0009 CHARACTER TABULATION,
  1566. U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF),
  1567. or U+0020 SPACE */
  1568. } elseif (isset($token['data']) && preg_match(
  1569. '/^[\t\n\x0b\x0c ]+$/',
  1570. $token['data']
  1571. )
  1572. ) {
  1573. /* Append that character to the Document node. */
  1574. $text = $this->dom->createTextNode($token['data']);
  1575. $this->dom->appendChild($text);
  1576. }
  1577. }
  1578. private function rootElementPhase($token)
  1579. {
  1580. /* After the initial phase, as each token is emitted from the tokenisation
  1581. stage, it must be processed as described in this section. */
  1582. /* A DOCTYPE token */
  1583. if ($token['type'] === HTML5::DOCTYPE) {
  1584. // Parse error. Ignore the token.
  1585. /* A comment token */
  1586. } elseif ($token['type'] === HTML5::COMMENT) {
  1587. /* Append a Comment node to the Document object with the data
  1588. attribute set to the data given in the comment token. */
  1589. $comment = $this->dom->createComment($token['data']);
  1590. $this->dom->appendChild($comment);
  1591. /* A character token that is one of one of U+0009 CHARACTER TABULATION,
  1592. U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF),
  1593. or U+0020 SPACE */
  1594. } elseif ($token['type'] === HTML5::CHARACTR &&
  1595. preg_match('/^[\t\n\x0b\x0c ]+$/', $token['data'])
  1596. ) {
  1597. /* Append that character to the Document node. */
  1598. $text = $this->dom->createTextNode($token['data']);
  1599. $this->dom->appendChild($text);
  1600. /* A character token that is not one of U+0009 CHARACTER TABULATION,
  1601. U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED
  1602. (FF), or U+0020 SPACE
  1603. A start tag token
  1604. An end tag token
  1605. An end-of-file token */
  1606. } elseif (($token['type'] === HTML5::CHARACTR &&
  1607. !preg_match('/^[\t\n\x0b\x0c ]+$/', $token['data'])) ||
  1608. $token['type'] === HTML5::STARTTAG ||
  1609. $token['type'] === HTML5::ENDTAG ||
  1610. $token['type'] === HTML5::EOF
  1611. ) {
  1612. /* Create an HTMLElement node with the tag name html, in the HTML
  1613. namespace. Append it to the Document object. Switch to the main
  1614. phase and reprocess the current token. */
  1615. $html = $this->dom->createElement('html');
  1616. $this->dom->appendChild($html);
  1617. $this->stack[] = $html;
  1618. $this->phase = self::MAIN_PHASE;
  1619. return $this->mainPhase($token);
  1620. }
  1621. }
  1622. private function mainPhase($token)
  1623. {
  1624. /* Tokens in the main phase must be handled as follows: */
  1625. /* A DOCTYPE token */
  1626. if ($token['type'] === HTML5::DOCTYPE) {
  1627. // Parse error. Ignore the token.
  1628. /* A start tag token with the tag name "html" */
  1629. } elseif ($token['type'] === HTML5::STARTTAG && $token['name'] === 'html') {
  1630. /* If this start tag token was not the first start tag token, then
  1631. it is a parse error. */
  1632. /* For each attribute on the token, check to see if the attribute
  1633. is already present on the top element of the stack of open elements.
  1634. If it is not, add the attribute and its corresponding value to that
  1635. element. */
  1636. foreach ($token['attr'] as $attr) {
  1637. if (!$this->stack[0]->hasAttribute($attr['name'])) {
  1638. $this->stack[0]->setAttribute($attr['name'], $attr['value']);
  1639. }
  1640. }
  1641. /* An end-of-file token */
  1642. } elseif ($token['type'] === HTML5::EOF) {
  1643. /* Generate implied end tags. */
  1644. $this->generateImpliedEndTags();
  1645. /* Anything else. */
  1646. } else {
  1647. /* Depends on the insertion mode: */
  1648. switch ($this->mode) {
  1649. case self::BEFOR_HEAD:
  1650. return $this->beforeHead($token);
  1651. break;
  1652. case self::IN_HEAD:
  1653. return $this->inHead($token);
  1654. break;
  1655. case self::AFTER_HEAD:
  1656. return $this->afterHead($token);
  1657. break;
  1658. case self::IN_BODY:
  1659. return $this->inBody($token);
  1660. break;
  1661. case self::IN_TABLE:
  1662. return $this->inTable($token);
  1663. break;
  1664. case self::IN_CAPTION:
  1665. return $this->inCaption($token);
  1666. break;
  1667. case self::IN_CGROUP:
  1668. return $this->inColumnGroup($token);
  1669. break;
  1670. case self::IN_TBODY:
  1671. return $this->inTableBody($token);
  1672. break;
  1673. case self::IN_ROW:
  1674. return $this->inRow($token);
  1675. break;
  1676. case self::IN_CELL:
  1677. return $this->inCell($token);
  1678. break;
  1679. case self::IN_SELECT:
  1680. return $this->inSelect($token);
  1681. break;
  1682. case self::AFTER_BODY:
  1683. return $this->afterBody($token);
  1684. break;
  1685. case self::IN_FRAME:
  1686. return $this->inFrameset($token);
  1687. break;
  1688. case self::AFTR_FRAME:
  1689. return $this->afterFrameset($token);
  1690. break;
  1691. case self::END_PHASE:
  1692. return $this->trailingEndPhase($token);
  1693. break;
  1694. }
  1695. }
  1696. }
  1697. private function beforeHead($token)
  1698. {
  1699. /* Handle the token as follows: */
  1700. /* A character token that is one of one of U+0009 CHARACTER TABULATION,
  1701. U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF),
  1702. or U+0020 SPACE */
  1703. if ($token['type'] === HTML5::CHARACTR &&
  1704. preg_match('/^[\t\n\x0b\x0c ]+$/', $token['data'])
  1705. ) {
  1706. /* Append the character to the current node. */
  1707. $this->insertText($token['data']);
  1708. /* A comment token */
  1709. } elseif ($token['type'] === HTML5::COMMENT) {
  1710. /* Append a Comment node to the current node with the data attribute
  1711. set to the data given in the comment token. */
  1712. $this->insertComment($token['data']);
  1713. /* A start tag token with the tag name "head" */
  1714. } elseif ($token['type'] === HTML5::STARTTAG && $token['name'] === 'head') {
  1715. /* Create an element for the token, append the new element to the
  1716. current node and push it onto the stack of open elements. */
  1717. $element = $this->insertElement($token);
  1718. /* Set the head element pointer to this new element node. */
  1719. $this->head_pointer = $element;
  1720. /* Change the insertion mode to "in head". */
  1721. $this->mode = self::IN_HEAD;
  1722. /* A start tag token whose tag name is one of: "base", "link", "meta",
  1723. "script", "style", "title". Or an end tag with the tag name "html".
  1724. Or a character token that is not one of U+0009 CHARACTER TABULATION,
  1725. U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF),
  1726. or U+0020 SPACE. Or any other start tag token */
  1727. } elseif ($token['type'] === HTML5::STARTTAG ||
  1728. ($token['type'] === HTML5::ENDTAG && $token['name'] === 'html') ||
  1729. ($token['type'] === HTML5::CHARACTR && !preg_match(
  1730. '/^[\t\n\x0b\x0c ]$/',
  1731. $token['data']
  1732. ))
  1733. ) {
  1734. /* Act as if a start tag token with the tag name "head" and no
  1735. attributes had been seen, then reprocess the current token. */
  1736. $this->beforeHead(
  1737. array(
  1738. 'name' => 'head',
  1739. 'type' => HTML5::STARTTAG,
  1740. 'attr' => array()
  1741. )
  1742. );
  1743. return $this->inHead($token);
  1744. /* Any other end tag */
  1745. } elseif ($token['type'] === HTML5::ENDTAG) {
  1746. /* Parse error. Ignore the token. */
  1747. }
  1748. }
  1749. private function inHead($token)
  1750. {
  1751. /* Handle the token as follows: */
  1752. /* A character token that is one of one of U+0009 CHARACTER TABULATION,
  1753. U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF),
  1754. or U+0020 SPACE.
  1755. THIS DIFFERS FROM THE SPEC: If the current node is either a title, style
  1756. or script element, append the character to the current node regardless
  1757. of its content. */
  1758. if (($token['type'] === HTML5::CHARACTR &&
  1759. preg_match('/^[\t\n\x0b\x0c ]+$/', $token['data'])) || (
  1760. $token['type'] === HTML5::CHARACTR && in_array(
  1761. end($this->stack)->nodeName,
  1762. array('title', 'style', 'script')
  1763. ))
  1764. ) {
  1765. /* Append the character to the current node. */
  1766. $this->insertText($token['data']);
  1767. /* A comment token */
  1768. } elseif ($token['type'] === HTML5::COMMENT) {
  1769. /* Append a Comment node to the current node with the data attribute
  1770. set to the data given in the comment token. */
  1771. $this->insertComment($token['data']);
  1772. } elseif ($token['type'] === HTML5::ENDTAG &&
  1773. in_array($token['name'], array('title', 'style', 'script'))
  1774. ) {
  1775. array_pop($this->stack);
  1776. return HTML5::PCDATA;
  1777. /* A start tag with the tag name "title" */
  1778. } elseif ($token['type'] === HTML5::STARTTAG && $token['name'] === 'title') {
  1779. /* Create an element for the token and append the new element to the
  1780. node pointed to by the head element pointer, or, if that is null
  1781. (innerHTML case), to the current node. */
  1782. if ($this->head_pointer !== null) {
  1783. $element = $this->insertElement($token, false);
  1784. $this->head_pointer->appendChild($element);
  1785. } else {
  1786. $element = $this->insertElement($token);
  1787. }
  1788. /* Switch the tokeniser's content model flag to the RCDATA state. */
  1789. return HTML5::RCDATA;
  1790. /* A start tag with the tag name "style" */
  1791. } elseif ($token['type'] === HTML5::STARTTAG && $token['name'] === 'style') {
  1792. /* Create an element for the token and append the new element to the
  1793. node pointed to by the head element pointer, or, if that is null
  1794. (innerHTML case), to the current node. */
  1795. if ($this->head_pointer !== null) {
  1796. $element = $this->insertElement($token, false);
  1797. $this->head_pointer->appendChild($element);
  1798. } else {
  1799. $this->insertElement($token);
  1800. }
  1801. /* Switch the tokeniser's content model flag to the CDATA state. */
  1802. return HTML5::CDATA;
  1803. /* A start tag with the tag name "script" */
  1804. } elseif ($token['type'] === HTML5::STARTTAG && $token['name'] === 'script') {
  1805. /* Create an element for the token. */
  1806. $element = $this->insertElement($token, false);
  1807. $this->head_pointer->appendChild($element);
  1808. /* Switch the tokeniser's content model flag to the CDATA state. */
  1809. return HTML5::CDATA;
  1810. /* A start tag with the tag name "base", "link", or "meta" */
  1811. } elseif ($token['type'] === HTML5::STARTTAG && in_array(
  1812. $token['name'],
  1813. array('base', 'link', 'meta')
  1814. )
  1815. ) {
  1816. /* Create an element for the token and append the new element to the
  1817. node pointed to by the head element pointer, or, if that is null
  1818. (innerHTML case), to the current node. */
  1819. if ($this->head_pointer !== null) {
  1820. $element = $this->insertElement($token, false);
  1821. $this->head_pointer->appendChild($element);
  1822. array_pop($this->stack);
  1823. } else {
  1824. $this->insertElement($token);
  1825. }
  1826. /* An end tag with the tag name "head" */
  1827. } elseif ($token['type'] === HTML5::ENDTAG && $token['name'] === 'head') {
  1828. /* If the current node is a head element, pop the current node off
  1829. the stack of open elements. */
  1830. if ($this->head_pointer->isSameNode(end($this->stack))) {
  1831. array_pop($this->stack);
  1832. /* Otherwise, this is a parse error. */
  1833. } else {
  1834. // k
  1835. }
  1836. /* Change the insertion mode to "after head". */
  1837. $this->mode = self::AFTER_HEAD;
  1838. /* A start tag with the tag name "head" or an end tag except "html". */
  1839. } elseif (($token['type'] === HTML5::STARTTAG && $token['name'] === 'head') ||
  1840. ($token['type'] === HTML5::ENDTAG && $token['name'] !== 'html')
  1841. ) {
  1842. // Parse error. Ignore the token.
  1843. /* Anything else */
  1844. } else {
  1845. /* If the current node is a head element, act as if an end tag
  1846. token with the tag name "head" had been seen. */
  1847. if ($this->head_pointer->isSameNode(end($this->stack))) {
  1848. $this->inHead(
  1849. array(
  1850. 'name' => 'head',
  1851. 'type' => HTML5::ENDTAG
  1852. )
  1853. );
  1854. /* Otherwise, change the insertion mode to "after head". */
  1855. } else {
  1856. $this->mode = self::AFTER_HEAD;
  1857. }
  1858. /* Then, reprocess the current token. */
  1859. return $this->afterHead($token);
  1860. }
  1861. }
  1862. private function afterHead($token)
  1863. {
  1864. /* Handle the token as follows: */
  1865. /* A character token that is one of one of U+0009 CHARACTER TABULATION,
  1866. U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF),
  1867. or U+0020 SPACE */
  1868. if ($token['type'] === HTML5::CHARACTR &&
  1869. preg_match('/^[\t\n\x0b\x0c ]+$/', $token['data'])
  1870. ) {
  1871. /* Append the character to the current node. */
  1872. $this->insertText($token['data']);
  1873. /* A comment token */
  1874. } elseif ($token['type'] === HTML5::COMMENT) {
  1875. /* Append a Comment node to the current node with the data attribute
  1876. set to the data given in the comment token. */
  1877. $this->insertComment($token['data']);
  1878. /* A start tag token with the tag name "body" */
  1879. } elseif ($token['type'] === HTML5::STARTTAG && $token['name'] === 'body') {
  1880. /* Insert a body element for the token. */
  1881. $this->insertElement($token);
  1882. /* Change the insertion mode to "in body". */
  1883. $this->mode = self::IN_BODY;
  1884. /* A start tag token with the tag name "frameset" */
  1885. } elseif ($token['type'] === HTML5::STARTTAG && $token['name'] === 'frameset') {
  1886. /* Insert a frameset element for the token. */
  1887. $this->insertElement($token);
  1888. /* Change the insertion mode to "in frameset". */
  1889. $this->mode = self::IN_FRAME;
  1890. /* A start tag token whose tag name is one of: "base", "link", "meta",
  1891. "script", "style", "title" */
  1892. } elseif ($token['type'] === HTML5::STARTTAG && in_array(
  1893. $token['name'],
  1894. array('base', 'link', 'meta', 'script', 'style', 'title')
  1895. )
  1896. ) {
  1897. /* Parse error. Switch the insertion mode back to "in head" and
  1898. reprocess the token. */
  1899. $this->mode = self::IN_HEAD;
  1900. return $this->inHead($token);
  1901. /* Anything else */
  1902. } else {
  1903. /* Act as if a start tag token with the tag name "body" and no
  1904. attributes had been seen, and then reprocess the current token. */
  1905. $this->afterHead(
  1906. array(
  1907. 'name' => 'body',
  1908. 'type' => HTML5::STARTTAG,
  1909. 'attr' => array()
  1910. )
  1911. );
  1912. return $this->inBody($token);
  1913. }
  1914. }
  1915. private function inBody($token)
  1916. {
  1917. /* Handle the token as follows: */
  1918. switch ($token['type']) {
  1919. /* A character token */
  1920. case HTML5::CHARACTR:
  1921. /* Reconstruct the active formatting elements, if any. */
  1922. $this->reconstructActiveFormattingElements();
  1923. /* Append the token's character to the current node. */
  1924. $this->insertText($token['data']);
  1925. break;
  1926. /* A comment token */
  1927. case HTML5::COMMENT:
  1928. /* Append a Comment node to the current node with the data
  1929. attribute set to the data given in the comment token. */
  1930. $this->insertComment($token['data']);
  1931. break;
  1932. case HTML5::STARTTAG:
  1933. switch ($token['name']) {
  1934. /* A start tag token whose tag name is one of: "script",
  1935. "style" */
  1936. case 'script':
  1937. case 'style':
  1938. /* Process the token as if the insertion mode had been "in
  1939. head". */
  1940. return $this->inHead($token);
  1941. break;
  1942. /* A start tag token whose tag name is one of: "base", "link",
  1943. "meta", "title" */
  1944. case 'base':
  1945. case 'link':
  1946. case 'meta':
  1947. case 'title':
  1948. /* Parse error. Process the token as if the insertion mode
  1949. had been "in head". */
  1950. return $this->inHead($token);
  1951. break;
  1952. /* A start tag token with the tag name "body" */
  1953. case 'body':
  1954. /* Parse error. If the second element on the stack of open
  1955. elements is not a body element, or, if the stack of open
  1956. elements has only one node on it, then ignore the token.
  1957. (innerHTML case) */
  1958. if (count($this->stack) === 1 || $this->stack[1]->nodeName !== 'body') {
  1959. // Ignore
  1960. /* Otherwise, for each attribute on the token, check to see
  1961. if the attribute is already present on the body element (the
  1962. second element) on the stack of open elements. If it is not,
  1963. add the attribute and its corresponding value to that
  1964. element. */
  1965. } else {
  1966. foreach ($token['attr'] as $attr) {
  1967. if (!$this->stack[1]->hasAttribute($attr['name'])) {
  1968. $this->stack[1]->setAttribute($attr['name'], $attr['value']);
  1969. }
  1970. }
  1971. }
  1972. break;
  1973. /* A start tag whose tag name is one of: "address",
  1974. "blockquote", "center", "dir", "div", "dl", "fieldset",
  1975. "listing", "menu", "ol", "p", "ul" */
  1976. case 'address':
  1977. case 'blockquote':
  1978. case 'center':
  1979. case 'dir':
  1980. case 'div':
  1981. case 'dl':
  1982. case 'fieldset':
  1983. case 'listing':
  1984. case 'menu':
  1985. case 'ol':
  1986. case 'p':
  1987. case 'ul':
  1988. /* If the stack of open elements has a p element in scope,
  1989. then act as if an end tag with the tag name p had been
  1990. seen. */
  1991. if ($this->elementInScope('p')) {
  1992. $this->emitToken(
  1993. array(
  1994. 'name' => 'p',
  1995. 'type' => HTML5::ENDTAG
  1996. )
  1997. );
  1998. }
  1999. /* Insert an HTML element for the token. */
  2000. $this->insertElement($token);
  2001. break;
  2002. /* A start tag whose tag name is "form" */
  2003. case 'form':
  2004. /* If the form element pointer is not null, ignore the
  2005. token with a parse error. */
  2006. if ($this->form_pointer !== null) {
  2007. // Ignore.
  2008. /* Otherwise: */
  2009. } else {
  2010. /* If the stack of open elements has a p element in
  2011. scope, then act as if an end tag with the tag name p
  2012. had been seen. */
  2013. if ($this->elementInScope('p')) {
  2014. $this->emitToken(
  2015. array(
  2016. 'name' => 'p',
  2017. 'type' => HTML5::ENDTAG
  2018. )
  2019. );
  2020. }
  2021. /* Insert an HTML element for the token, and set the
  2022. form element pointer to point to the element created. */
  2023. $element = $this->insertElement($token);
  2024. $this->form_pointer = $element;
  2025. }
  2026. break;
  2027. /* A start tag whose tag name is "li", "dd" or "dt" */
  2028. case 'li':
  2029. case 'dd':
  2030. case 'dt':
  2031. /* If the stack of open elements has a p element in scope,
  2032. then act as if an end tag with the tag name p had been
  2033. seen. */
  2034. if ($this->elementInScope('p')) {
  2035. $this->emitToken(
  2036. array(
  2037. 'name' => 'p',
  2038. 'type' => HTML5::ENDTAG
  2039. )
  2040. );
  2041. }
  2042. $stack_length = count($this->stack) - 1;
  2043. for ($n = $stack_length; 0 <= $n; $n--) {
  2044. /* 1. Initialise node to be the current node (the
  2045. bottommost node of the stack). */
  2046. $stop = false;
  2047. $node = $this->stack[$n];
  2048. $cat = $this->getElementCategory($node->tagName);
  2049. /* 2. If node is an li, dd or dt element, then pop all
  2050. the nodes from the current node up to node, including
  2051. node, then stop this algorithm. */
  2052. if ($token['name'] === $node->tagName || ($token['name'] !== 'li'
  2053. && ($node->tagName === 'dd' || $node->tagName === 'dt'))
  2054. ) {
  2055. for ($x = $stack_length; $x >= $n; $x--) {
  2056. array_pop($this->stack);
  2057. }
  2058. break;
  2059. }
  2060. /* 3. If node is not in the formatting category, and is
  2061. not in the phrasing category, and is not an address or
  2062. div element, then stop this algorithm. */
  2063. if ($cat !== self::FORMATTING && $cat !== self::PHRASING &&
  2064. $node->tagName !== 'address' && $node->tagName !== 'div'
  2065. ) {
  2066. break;
  2067. }
  2068. }
  2069. /* Finally, insert an HTML element with the same tag
  2070. name as the token's. */
  2071. $this->insertElement($token);
  2072. break;
  2073. /* A start tag token whose tag name is "plaintext" */
  2074. case 'plaintext':
  2075. /* If the stack of open elements has a p element in scope,
  2076. then act as if an end tag with the tag name p had been
  2077. seen. */
  2078. if ($this->elementInScope('p')) {
  2079. $this->emitToken(
  2080. array(
  2081. 'name' => 'p',
  2082. 'type' => HTML5::ENDTAG
  2083. )
  2084. );
  2085. }
  2086. /* Insert an HTML element for the token. */
  2087. $this->insertElement($token);
  2088. return HTML5::PLAINTEXT;
  2089. break;
  2090. /* A start tag whose tag name is one of: "h1", "h2", "h3", "h4",
  2091. "h5", "h6" */
  2092. case 'h1':
  2093. case 'h2':
  2094. case 'h3':
  2095. case 'h4':
  2096. case 'h5':
  2097. case 'h6':
  2098. /* If the stack of open elements has a p element in scope,
  2099. then act as if an end tag with the tag name p had been seen. */
  2100. if ($this->elementInScope('p')) {
  2101. $this->emitToken(
  2102. array(
  2103. 'name' => 'p',
  2104. 'type' => HTML5::ENDTAG
  2105. )
  2106. );
  2107. }
  2108. /* If the stack of open elements has in scope an element whose
  2109. tag name is one of "h1", "h2", "h3", "h4", "h5", or "h6", then
  2110. this is a parse error; pop elements from the stack until an
  2111. element with one of those tag names has been popped from the
  2112. stack. */
  2113. while ($this->elementInScope(array('h1', 'h2', 'h3', 'h4', 'h5', 'h6'))) {
  2114. array_pop($this->stack);
  2115. }
  2116. /* Insert an HTML element for the token. */
  2117. $this->insertElement($token);
  2118. break;
  2119. /* A start tag whose tag name is "a" */
  2120. case 'a':
  2121. /* If the list of active formatting elements contains
  2122. an element whose tag name is "a" between the end of the
  2123. list and the last marker on the list (or the start of
  2124. the list if there is no marker on the list), then this
  2125. is a parse error; act as if an end tag with the tag name
  2126. "a" had been seen, then remove that element from the list
  2127. of active formatting elements and the stack of open
  2128. elements if the end tag didn't already remove it (it
  2129. might not have if the element is not in table scope). */
  2130. $leng = count($this->a_formatting);
  2131. for ($n = $leng - 1; $n >= 0; $n--) {
  2132. if ($this->a_formatting[$n] === self::MARKER) {
  2133. break;
  2134. } elseif ($this->a_formatting[$n]->nodeName === 'a') {
  2135. $this->emitToken(
  2136. array(
  2137. 'name' => 'a',
  2138. 'type' => HTML5::ENDTAG
  2139. )
  2140. );
  2141. break;
  2142. }
  2143. }
  2144. /* Reconstruct the active formatting elements, if any. */
  2145. $this->reconstructActiveFormattingElements();
  2146. /* Insert an HTML element for the token. */
  2147. $el = $this->insertElement($token);
  2148. /* Add that element to the list of active formatting
  2149. elements. */
  2150. $this->a_formatting[] = $el;
  2151. break;
  2152. /* A start tag whose tag name is one of: "b", "big", "em", "font",
  2153. "i", "nobr", "s", "small", "strike", "strong", "tt", "u" */
  2154. case 'b':
  2155. case 'big':
  2156. case 'em':
  2157. case 'font':
  2158. case 'i':
  2159. case 'nobr':
  2160. case 's':
  2161. case 'small':
  2162. case 'strike':
  2163. case 'strong':
  2164. case 'tt':
  2165. case 'u':
  2166. /* Reconstruct the active formatting elements, if any. */
  2167. $this->reconstructActiveFormattingElements();
  2168. /* Insert an HTML element for the token. */
  2169. $el = $this->insertElement($token);
  2170. /* Add that element to the list of active formatting
  2171. elements. */
  2172. $this->a_formatting[] = $el;
  2173. break;
  2174. /* A start tag token whose tag name is "button" */
  2175. case 'button':
  2176. /* If the stack of open elements has a button element in scope,
  2177. then this is a parse error; act as if an end tag with the tag
  2178. name "button" had been seen, then reprocess the token. (We don't
  2179. do that. Unnecessary.) */
  2180. if ($this->elementInScope('button')) {
  2181. $this->inBody(
  2182. array(
  2183. 'name' => 'button',
  2184. 'type' => HTML5::ENDTAG
  2185. )
  2186. );
  2187. }
  2188. /* Reconstruct the active formatting elements, if any. */
  2189. $this->reconstructActiveFormattingElements();
  2190. /* Insert an HTML element for the token. */
  2191. $this->insertElement($token);
  2192. /* Insert a marker at the end of the list of active
  2193. formatting elements. */
  2194. $this->a_formatting[] = self::MARKER;
  2195. break;
  2196. /* A start tag token whose tag name is one of: "marquee", "object" */
  2197. case 'marquee':
  2198. case 'object':
  2199. /* Reconstruct the active formatting elements, if any. */
  2200. $this->reconstructActiveFormattingElements();
  2201. /* Insert an HTML element for the token. */
  2202. $this->insertElement($token);
  2203. /* Insert a marker at the end of the list of active
  2204. formatting elements. */
  2205. $this->a_formatting[] = self::MARKER;
  2206. break;
  2207. /* A start tag token whose tag name is "xmp" */
  2208. case 'xmp':
  2209. /* Reconstruct the active formatting elements, if any. */
  2210. $this->reconstructActiveFormattingElements();
  2211. /* Insert an HTML element for the token. */
  2212. $this->insertElement($token);
  2213. /* Switch the content model flag to the CDATA state. */
  2214. return HTML5::CDATA;
  2215. break;
  2216. /* A start tag whose tag name is "table" */
  2217. case 'table':
  2218. /* If the stack of open elements has a p element in scope,
  2219. then act as if an end tag with the tag name p had been seen. */
  2220. if ($this->elementInScope('p')) {
  2221. $this->emitToken(
  2222. array(
  2223. 'name' => 'p',
  2224. 'type' => HTML5::ENDTAG
  2225. )
  2226. );
  2227. }
  2228. /* Insert an HTML element for the token. */
  2229. $this->insertElement($token);
  2230. /* Change the insertion mode to "in table". */
  2231. $this->mode = self::IN_TABLE;
  2232. break;
  2233. /* A start tag whose tag name is one of: "area", "basefont",
  2234. "bgsound", "br", "embed", "img", "param", "spacer", "wbr" */
  2235. case 'area':
  2236. case 'basefont':
  2237. case 'bgsound':
  2238. case 'br':
  2239. case 'embed':
  2240. case 'img':
  2241. case 'param':
  2242. case 'spacer':
  2243. case 'wbr':
  2244. /* Reconstruct the active formatting elements, if any. */
  2245. $this->reconstructActiveFormattingElements();
  2246. /* Insert an HTML element for the token. */
  2247. $this->insertElement($token);
  2248. /* Immediately pop the current node off the stack of open elements. */
  2249. array_pop($this->stack);
  2250. break;
  2251. /* A start tag whose tag name is "hr" */
  2252. case 'hr':
  2253. /* If the stack of open elements has a p element in scope,
  2254. then act as if an end tag with the tag name p had been seen. */
  2255. if ($this->elementInScope('p')) {
  2256. $this->emitToken(
  2257. array(
  2258. 'name' => 'p',
  2259. 'type' => HTML5::ENDTAG
  2260. )
  2261. );
  2262. }
  2263. /* Insert an HTML element for the token. */
  2264. $this->insertElement($token);
  2265. /* Immediately pop the current node off the stack of open elements. */
  2266. array_pop($this->stack);
  2267. break;
  2268. /* A start tag whose tag name is "image" */
  2269. case 'image':
  2270. /* Parse error. Change the token's tag name to "img" and
  2271. reprocess it. (Don't ask.) */
  2272. $token['name'] = 'img';
  2273. return $this->inBody($token);
  2274. break;
  2275. /* A start tag whose tag name is "input" */
  2276. case 'input':
  2277. /* Reconstruct the active formatting elements, if any. */
  2278. $this->reconstructActiveFormattingElements();
  2279. /* Insert an input element for the token. */
  2280. $element = $this->insertElement($token, false);
  2281. /* If the form element pointer is not null, then associate the
  2282. input element with the form element pointed to by the form
  2283. element pointer. */
  2284. $this->form_pointer !== null
  2285. ? $this->form_pointer->appendChild($element)
  2286. : end($this->stack)->appendChild($element);
  2287. /* Pop that input element off the stack of open elements. */
  2288. array_pop($this->stack);
  2289. break;
  2290. /* A start tag whose tag name is "isindex" */
  2291. case 'isindex':
  2292. /* Parse error. */
  2293. // w/e
  2294. /* If the form element pointer is not null,
  2295. then ignore the token. */
  2296. if ($this->form_pointer === null) {
  2297. /* Act as if a start tag token with the tag name "form" had
  2298. been seen. */
  2299. $this->inBody(
  2300. array(
  2301. 'name' => 'body',
  2302. 'type' => HTML5::STARTTAG,
  2303. 'attr' => array()
  2304. )
  2305. );
  2306. /* Act as if a start tag token with the tag name "hr" had
  2307. been seen. */
  2308. $this->inBody(
  2309. array(
  2310. 'name' => 'hr',
  2311. 'type' => HTML5::STARTTAG,
  2312. 'attr' => array()
  2313. )
  2314. );
  2315. /* Act as if a start tag token with the tag name "p" had
  2316. been seen. */
  2317. $this->inBody(
  2318. array(
  2319. 'name' => 'p',
  2320. 'type' => HTML5::STARTTAG,
  2321. 'attr' => array()
  2322. )
  2323. );
  2324. /* Act as if a start tag token with the tag name "label"
  2325. had been seen. */
  2326. $this->inBody(
  2327. array(
  2328. 'name' => 'label',
  2329. 'type' => HTML5::STARTTAG,
  2330. 'attr' => array()
  2331. )
  2332. );
  2333. /* Act as if a stream of character tokens had been seen. */
  2334. $this->insertText(
  2335. 'This is a searchable index. ' .
  2336. 'Insert your search keywords here: '
  2337. );
  2338. /* Act as if a start tag token with the tag name "input"
  2339. had been seen, with all the attributes from the "isindex"
  2340. token, except with the "name" attribute set to the value
  2341. "isindex" (ignoring any explicit "name" attribute). */
  2342. $attr = $token['attr'];
  2343. $attr[] = array('name' => 'name', 'value' => 'isindex');
  2344. $this->inBody(
  2345. array(
  2346. 'name' => 'input',
  2347. 'type' => HTML5::STARTTAG,
  2348. 'attr' => $attr
  2349. )
  2350. );
  2351. /* Act as if a stream of character tokens had been seen
  2352. (see below for what they should say). */
  2353. $this->insertText(
  2354. 'This is a searchable index. ' .
  2355. 'Insert your search keywords here: '
  2356. );
  2357. /* Act as if an end tag token with the tag name "label"
  2358. had been seen. */
  2359. $this->inBody(
  2360. array(
  2361. 'name' => 'label',
  2362. 'type' => HTML5::ENDTAG
  2363. )
  2364. );
  2365. /* Act as if an end tag token with the tag name "p" had
  2366. been seen. */
  2367. $this->inBody(
  2368. array(
  2369. 'name' => 'p',
  2370. 'type' => HTML5::ENDTAG
  2371. )
  2372. );
  2373. /* Act as if a start tag token with the tag name "hr" had
  2374. been seen. */
  2375. $this->inBody(
  2376. array(
  2377. 'name' => 'hr',
  2378. 'type' => HTML5::ENDTAG
  2379. )
  2380. );
  2381. /* Act as if an end tag token with the tag name "form" had
  2382. been seen. */
  2383. $this->inBody(
  2384. array(
  2385. 'name' => 'form',
  2386. 'type' => HTML5::ENDTAG
  2387. )
  2388. );
  2389. }
  2390. break;
  2391. /* A start tag whose tag name is "textarea" */
  2392. case 'textarea':
  2393. $this->insertElement($token);
  2394. /* Switch the tokeniser's content model flag to the
  2395. RCDATA state. */
  2396. return HTML5::RCDATA;
  2397. break;
  2398. /* A start tag whose tag name is one of: "iframe", "noembed",
  2399. "noframes" */
  2400. case 'iframe':
  2401. case 'noembed':
  2402. case 'noframes':
  2403. $this->insertElement($token);
  2404. /* Switch the tokeniser's content model flag to the CDATA state. */
  2405. return HTML5::CDATA;
  2406. break;
  2407. /* A start tag whose tag name is "select" */
  2408. case 'select':
  2409. /* Reconstruct the active formatting elements, if any. */
  2410. $this->reconstructActiveFormattingElements();
  2411. /* Insert an HTML element for the token. */
  2412. $this->insertElement($token);
  2413. /* Change the insertion mode to "in select". */
  2414. $this->mode = self::IN_SELECT;
  2415. break;
  2416. /* A start or end tag whose tag name is one of: "caption", "col",
  2417. "colgroup", "frame", "frameset", "head", "option", "optgroup",
  2418. "tbody", "td", "tfoot", "th", "thead", "tr". */
  2419. case 'caption':
  2420. case 'col':
  2421. case 'colgroup':
  2422. case 'frame':
  2423. case 'frameset':
  2424. case 'head':
  2425. case 'option':
  2426. case 'optgroup':
  2427. case 'tbody':
  2428. case 'td':
  2429. case 'tfoot':
  2430. case 'th':
  2431. case 'thead':
  2432. case 'tr':
  2433. // Parse error. Ignore the token.
  2434. break;
  2435. /* A start or end tag whose tag name is one of: "event-source",
  2436. "section", "nav", "article", "aside", "header", "footer",
  2437. "datagrid", "command" */
  2438. case 'event-source':
  2439. case 'section':
  2440. case 'nav':
  2441. case 'article':
  2442. case 'aside':
  2443. case 'header':
  2444. case 'footer':
  2445. case 'datagrid':
  2446. case 'command':
  2447. // Work in progress!
  2448. break;
  2449. /* A start tag token not covered by the previous entries */
  2450. default:
  2451. /* Reconstruct the active formatting elements, if any. */
  2452. $this->reconstructActiveFormattingElements();
  2453. $this->insertElement($token, true, true);
  2454. break;
  2455. }
  2456. break;
  2457. case HTML5::ENDTAG:
  2458. switch ($token['name']) {
  2459. /* An end tag with the tag name "body" */
  2460. case 'body':
  2461. /* If the second element in the stack of open elements is
  2462. not a body element, this is a parse error. Ignore the token.
  2463. (innerHTML case) */
  2464. if (count($this->stack) < 2 || $this->stack[1]->nodeName !== 'body') {
  2465. // Ignore.
  2466. /* If the current node is not the body element, then this
  2467. is a parse error. */
  2468. } elseif (end($this->stack)->nodeName !== 'body') {
  2469. // Parse error.
  2470. }
  2471. /* Change the insertion mode to "after body". */
  2472. $this->mode = self::AFTER_BODY;
  2473. break;
  2474. /* An end tag with the tag name "html" */
  2475. case 'html':
  2476. /* Act as if an end tag with tag name "body" had been seen,
  2477. then, if that token wasn't ignored, reprocess the current
  2478. token. */
  2479. $this->inBody(
  2480. array(
  2481. 'name' => 'body',
  2482. 'type' => HTML5::ENDTAG
  2483. )
  2484. );
  2485. return $this->afterBody($token);
  2486. break;
  2487. /* An end tag whose tag name is one of: "address", "blockquote",
  2488. "center", "dir", "div", "dl", "fieldset", "listing", "menu",
  2489. "ol", "pre", "ul" */
  2490. case 'address':
  2491. case 'blockquote':
  2492. case 'center':
  2493. case 'dir':
  2494. case 'div':
  2495. case 'dl':
  2496. case 'fieldset':
  2497. case 'listing':
  2498. case 'menu':
  2499. case 'ol':
  2500. case 'pre':
  2501. case 'ul':
  2502. /* If the stack of open elements has an element in scope
  2503. with the same tag name as that of the token, then generate
  2504. implied end tags. */
  2505. if ($this->elementInScope($token['name'])) {
  2506. $this->generateImpliedEndTags();
  2507. /* Now, if the current node is not an element with
  2508. the same tag name as that of the token, then this
  2509. is a parse error. */
  2510. // w/e
  2511. /* If the stack of open elements has an element in
  2512. scope with the same tag name as that of the token,
  2513. then pop elements from this stack until an element
  2514. with that tag name has been popped from the stack. */
  2515. for ($n = count($this->stack) - 1; $n >= 0; $n--) {
  2516. if ($this->stack[$n]->nodeName === $token['name']) {
  2517. $n = -1;
  2518. }
  2519. array_pop($this->stack);
  2520. }
  2521. }
  2522. break;
  2523. /* An end tag whose tag name is "form" */
  2524. case 'form':
  2525. /* If the stack of open elements has an element in scope
  2526. with the same tag name as that of the token, then generate
  2527. implied end tags. */
  2528. if ($this->elementInScope($token['name'])) {
  2529. $this->generateImpliedEndTags();
  2530. }
  2531. if (end($this->stack)->nodeName !== $token['name']) {
  2532. /* Now, if the current node is not an element with the
  2533. same tag name as that of the token, then this is a parse
  2534. error. */
  2535. // w/e
  2536. } else {
  2537. /* Otherwise, if the current node is an element with
  2538. the same tag name as that of the token pop that element
  2539. from the stack. */
  2540. array_pop($this->stack);
  2541. }
  2542. /* In any case, set the form element pointer to null. */
  2543. $this->form_pointer = null;
  2544. break;
  2545. /* An end tag whose tag name is "p" */
  2546. case 'p':
  2547. /* If the stack of open elements has a p element in scope,
  2548. then generate implied end tags, except for p elements. */
  2549. if ($this->elementInScope('p')) {
  2550. $this->generateImpliedEndTags(array('p'));
  2551. /* If the current node is not a p element, then this is
  2552. a parse error. */
  2553. // k
  2554. /* If the stack of open elements has a p element in
  2555. scope, then pop elements from this stack until the stack
  2556. no longer has a p element in scope. */
  2557. for ($n = count($this->stack) - 1; $n >= 0; $n--) {
  2558. if ($this->elementInScope('p')) {
  2559. array_pop($this->stack);
  2560. } else {
  2561. break;
  2562. }
  2563. }
  2564. }
  2565. break;
  2566. /* An end tag whose tag name is "dd", "dt", or "li" */
  2567. case 'dd':
  2568. case 'dt':
  2569. case 'li':
  2570. /* If the stack of open elements has an element in scope
  2571. whose tag name matches the tag name of the token, then
  2572. generate implied end tags, except for elements with the
  2573. same tag name as the token. */
  2574. if ($this->elementInScope($token['name'])) {
  2575. $this->generateImpliedEndTags(array($token['name']));
  2576. /* If the current node is not an element with the same
  2577. tag name as the token, then this is a parse error. */
  2578. // w/e
  2579. /* If the stack of open elements has an element in scope
  2580. whose tag name matches the tag name of the token, then
  2581. pop elements from this stack until an element with that
  2582. tag name has been popped from the stack. */
  2583. for ($n = count($this->stack) - 1; $n >= 0; $n--) {
  2584. if ($this->stack[$n]->nodeName === $token['name']) {
  2585. $n = -1;
  2586. }
  2587. array_pop($this->stack);
  2588. }
  2589. }
  2590. break;
  2591. /* An end tag whose tag name is one of: "h1", "h2", "h3", "h4",
  2592. "h5", "h6" */
  2593. case 'h1':
  2594. case 'h2':
  2595. case 'h3':
  2596. case 'h4':
  2597. case 'h5':
  2598. case 'h6':
  2599. $elements = array('h1', 'h2', 'h3', 'h4', 'h5', 'h6');
  2600. /* If the stack of open elements has in scope an element whose
  2601. tag name is one of "h1", "h2", "h3", "h4", "h5", or "h6", then
  2602. generate implied end tags. */
  2603. if ($this->elementInScope($elements)) {
  2604. $this->generateImpliedEndTags();
  2605. /* Now, if the current node is not an element with the same
  2606. tag name as that of the token, then this is a parse error. */
  2607. // w/e
  2608. /* If the stack of open elements has in scope an element
  2609. whose tag name is one of "h1", "h2", "h3", "h4", "h5", or
  2610. "h6", then pop elements from the stack until an element
  2611. with one of those tag names has been popped from the stack. */
  2612. while ($this->elementInScope($elements)) {
  2613. array_pop($this->stack);
  2614. }
  2615. }
  2616. break;
  2617. /* An end tag whose tag name is one of: "a", "b", "big", "em",
  2618. "font", "i", "nobr", "s", "small", "strike", "strong", "tt", "u" */
  2619. case 'a':
  2620. case 'b':
  2621. case 'big':
  2622. case 'em':
  2623. case 'font':
  2624. case 'i':
  2625. case 'nobr':
  2626. case 's':
  2627. case 'small':
  2628. case 'strike':
  2629. case 'strong':
  2630. case 'tt':
  2631. case 'u':
  2632. /* 1. Let the formatting element be the last element in
  2633. the list of active formatting elements that:
  2634. * is between the end of the list and the last scope
  2635. marker in the list, if any, or the start of the list
  2636. otherwise, and
  2637. * has the same tag name as the token.
  2638. */
  2639. while (true) {
  2640. for ($a = count($this->a_formatting) - 1; $a >= 0; $a--) {
  2641. if ($this->a_formatting[$a] === self::MARKER) {
  2642. break;
  2643. } elseif ($this->a_formatting[$a]->tagName === $token['name']) {
  2644. $formatting_element = $this->a_formatting[$a];
  2645. $in_stack = in_array($formatting_element, $this->stack, true);
  2646. $fe_af_pos = $a;
  2647. break;
  2648. }
  2649. }
  2650. /* If there is no such node, or, if that node is
  2651. also in the stack of open elements but the element
  2652. is not in scope, then this is a parse error. Abort
  2653. these steps. The token is ignored. */
  2654. if (!isset($formatting_element) || ($in_stack &&
  2655. !$this->elementInScope($token['name']))
  2656. ) {
  2657. break;
  2658. /* Otherwise, if there is such a node, but that node
  2659. is not in the stack of open elements, then this is a
  2660. parse error; remove the element from the list, and
  2661. abort these steps. */
  2662. } elseif (isset($formatting_element) && !$in_stack) {
  2663. unset($this->a_formatting[$fe_af_pos]);
  2664. $this->a_formatting = array_merge($this->a_formatting);
  2665. break;
  2666. }
  2667. /* 2. Let the furthest block be the topmost node in the
  2668. stack of open elements that is lower in the stack
  2669. than the formatting element, and is not an element in
  2670. the phrasing or formatting categories. There might
  2671. not be one. */
  2672. $fe_s_pos = array_search($formatting_element, $this->stack, true);
  2673. $length = count($this->stack);
  2674. for ($s = $fe_s_pos + 1; $s < $length; $s++) {
  2675. $category = $this->getElementCategory($this->stack[$s]->nodeName);
  2676. if ($category !== self::PHRASING && $category !== self::FORMATTING) {
  2677. $furthest_block = $this->stack[$s];
  2678. }
  2679. }
  2680. /* 3. If there is no furthest block, then the UA must
  2681. skip the subsequent steps and instead just pop all
  2682. the nodes from the bottom of the stack of open
  2683. elements, from the current node up to the formatting
  2684. element, and remove the formatting element from the
  2685. list of active formatting elements. */
  2686. if (!isset($furthest_block)) {
  2687. for ($n = $length - 1; $n >= $fe_s_pos; $n--) {
  2688. array_pop($this->stack);
  2689. }
  2690. unset($this->a_formatting[$fe_af_pos]);
  2691. $this->a_formatting = array_merge($this->a_formatting);
  2692. break;
  2693. }
  2694. /* 4. Let the common ancestor be the element
  2695. immediately above the formatting element in the stack
  2696. of open elements. */
  2697. $common_ancestor = $this->stack[$fe_s_pos - 1];
  2698. /* 5. If the furthest block has a parent node, then
  2699. remove the furthest block from its parent node. */
  2700. if ($furthest_block->parentNode !== null) {
  2701. $furthest_block->parentNode->removeChild($furthest_block);
  2702. }
  2703. /* 6. Let a bookmark note the position of the
  2704. formatting element in the list of active formatting
  2705. elements relative to the elements on either side
  2706. of it in the list. */
  2707. $bookmark = $fe_af_pos;
  2708. /* 7. Let node and last node be the furthest block.
  2709. Follow these steps: */
  2710. $node = $furthest_block;
  2711. $last_node = $furthest_block;
  2712. while (true) {
  2713. for ($n = array_search($node, $this->stack, true) - 1; $n >= 0; $n--) {
  2714. /* 7.1 Let node be the element immediately
  2715. prior to node in the stack of open elements. */
  2716. $node = $this->stack[$n];
  2717. /* 7.2 If node is not in the list of active
  2718. formatting elements, then remove node from
  2719. the stack of open elements and then go back
  2720. to step 1. */
  2721. if (!in_array($node, $this->a_formatting, true)) {
  2722. unset($this->stack[$n]);
  2723. $this->stack = array_merge($this->stack);
  2724. } else {
  2725. break;
  2726. }
  2727. }
  2728. /* 7.3 Otherwise, if node is the formatting
  2729. element, then go to the next step in the overall
  2730. algorithm. */
  2731. if ($node === $formatting_element) {
  2732. break;
  2733. /* 7.4 Otherwise, if last node is the furthest
  2734. block, then move the aforementioned bookmark to
  2735. be immediately after the node in the list of
  2736. active formatting elements. */
  2737. } elseif ($last_node === $furthest_block) {
  2738. $bookmark = array_search($node, $this->a_formatting, true) + 1;
  2739. }
  2740. /* 7.5 If node has any children, perform a
  2741. shallow clone of node, replace the entry for
  2742. node in the list of active formatting elements
  2743. with an entry for the clone, replace the entry
  2744. for node in the stack of open elements with an
  2745. entry for the clone, and let node be the clone. */
  2746. if ($node->hasChildNodes()) {
  2747. $clone = $node->cloneNode();
  2748. $s_pos = array_search($node, $this->stack, true);
  2749. $a_pos = array_search($node, $this->a_formatting, true);
  2750. $this->stack[$s_pos] = $clone;
  2751. $this->a_formatting[$a_pos] = $clone;
  2752. $node = $clone;
  2753. }
  2754. /* 7.6 Insert last node into node, first removing
  2755. it from its previous parent node if any. */
  2756. if ($last_node->parentNode !== null) {
  2757. $last_node->parentNode->removeChild($last_node);
  2758. }
  2759. $node->appendChild($last_node);
  2760. /* 7.7 Let last node be node. */
  2761. $last_node = $node;
  2762. }
  2763. /* 8. Insert whatever last node ended up being in
  2764. the previous step into the common ancestor node,
  2765. first removing it from its previous parent node if
  2766. any. */
  2767. if ($last_node->parentNode !== null) {
  2768. $last_node->parentNode->removeChild($last_node);
  2769. }
  2770. $common_ancestor->appendChild($last_node);
  2771. /* 9. Perform a shallow clone of the formatting
  2772. element. */
  2773. $clone = $formatting_element->cloneNode();
  2774. /* 10. Take all of the child nodes of the furthest
  2775. block and append them to the clone created in the
  2776. last step. */
  2777. while ($furthest_block->hasChildNodes()) {
  2778. $child = $furthest_block->firstChild;
  2779. $furthest_block->removeChild($child);
  2780. $clone->appendChild($child);
  2781. }
  2782. /* 11. Append that clone to the furthest block. */
  2783. $furthest_block->appendChild($clone);
  2784. /* 12. Remove the formatting element from the list
  2785. of active formatting elements, and insert the clone
  2786. into the list of active formatting elements at the
  2787. position of the aforementioned bookmark. */
  2788. $fe_af_pos = array_search($formatting_element, $this->a_formatting, true);
  2789. unset($this->a_formatting[$fe_af_pos]);
  2790. $this->a_formatting = array_merge($this->a_formatting);
  2791. $af_part1 = array_slice($this->a_formatting, 0, $bookmark - 1);
  2792. $af_part2 = array_slice($this->a_formatting, $bookmark, count($this->a_formatting));
  2793. $this->a_formatting = array_merge($af_part1, array($clone), $af_part2);
  2794. /* 13. Remove the formatting element from the stack
  2795. of open elements, and insert the clone into the stack
  2796. of open elements immediately after (i.e. in a more
  2797. deeply nested position than) the position of the
  2798. furthest block in that stack. */
  2799. $fe_s_pos = array_search($formatting_element, $this->stack, true);
  2800. $fb_s_pos = array_search($furthest_block, $this->stack, true);
  2801. unset($this->stack[$fe_s_pos]);
  2802. $s_part1 = array_slice($this->stack, 0, $fb_s_pos);
  2803. $s_part2 = array_slice($this->stack, $fb_s_pos + 1, count($this->stack));
  2804. $this->stack = array_merge($s_part1, array($clone), $s_part2);
  2805. /* 14. Jump back to step 1 in this series of steps. */
  2806. unset($formatting_element, $fe_af_pos, $fe_s_pos, $furthest_block);
  2807. }
  2808. break;
  2809. /* An end tag token whose tag name is one of: "button",
  2810. "marquee", "object" */
  2811. case 'button':
  2812. case 'marquee':
  2813. case 'object':
  2814. /* If the stack of open elements has an element in scope whose
  2815. tag name matches the tag name of the token, then generate implied
  2816. tags. */
  2817. if ($this->elementInScope($token['name'])) {
  2818. $this->generateImpliedEndTags();
  2819. /* Now, if the current node is not an element with the same
  2820. tag name as the token, then this is a parse error. */
  2821. // k
  2822. /* Now, if the stack of open elements has an element in scope
  2823. whose tag name matches the tag name of the token, then pop
  2824. elements from the stack until that element has been popped from
  2825. the stack, and clear the list of active formatting elements up
  2826. to the last marker. */
  2827. for ($n = count($this->stack) - 1; $n >= 0; $n--) {
  2828. if ($this->stack[$n]->nodeName === $token['name']) {
  2829. $n = -1;
  2830. }
  2831. array_pop($this->stack);
  2832. }
  2833. $marker = end(array_keys($this->a_formatting, self::MARKER, true));
  2834. for ($n = count($this->a_formatting) - 1; $n > $marker; $n--) {
  2835. array_pop($this->a_formatting);
  2836. }
  2837. }
  2838. break;
  2839. /* Or an end tag whose tag name is one of: "area", "basefont",
  2840. "bgsound", "br", "embed", "hr", "iframe", "image", "img",
  2841. "input", "isindex", "noembed", "noframes", "param", "select",
  2842. "spacer", "table", "textarea", "wbr" */
  2843. case 'area':
  2844. case 'basefont':
  2845. case 'bgsound':
  2846. case 'br':
  2847. case 'embed':
  2848. case 'hr':
  2849. case 'iframe':
  2850. case 'image':
  2851. case 'img':
  2852. case 'input':
  2853. case 'isindex':
  2854. case 'noembed':
  2855. case 'noframes':
  2856. case 'param':
  2857. case 'select':
  2858. case 'spacer':
  2859. case 'table':
  2860. case 'textarea':
  2861. case 'wbr':
  2862. // Parse error. Ignore the token.
  2863. break;
  2864. /* An end tag token not covered by the previous entries */
  2865. default:
  2866. for ($n = count($this->stack) - 1; $n >= 0; $n--) {
  2867. /* Initialise node to be the current node (the bottommost
  2868. node of the stack). */
  2869. $node = end($this->stack);
  2870. /* If node has the same tag name as the end tag token,
  2871. then: */
  2872. if ($token['name'] === $node->nodeName) {
  2873. /* Generate implied end tags. */
  2874. $this->generateImpliedEndTags();
  2875. /* If the tag name of the end tag token does not
  2876. match the tag name of the current node, this is a
  2877. parse error. */
  2878. // k
  2879. /* Pop all the nodes from the current node up to
  2880. node, including node, then stop this algorithm. */
  2881. for ($x = count($this->stack) - $n; $x >= $n; $x--) {
  2882. array_pop($this->stack);
  2883. }
  2884. } else {
  2885. $category = $this->getElementCategory($node);
  2886. if ($category !== self::SPECIAL && $category !== self::SCOPING) {
  2887. /* Otherwise, if node is in neither the formatting
  2888. category nor the phrasing category, then this is a
  2889. parse error. Stop this algorithm. The end tag token
  2890. is ignored. */
  2891. return false;
  2892. }
  2893. }
  2894. }
  2895. break;
  2896. }
  2897. break;
  2898. }
  2899. }
  2900. private function inTable($token)
  2901. {
  2902. $clear = array('html', 'table');
  2903. /* A character token that is one of one of U+0009 CHARACTER TABULATION,
  2904. U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF),
  2905. or U+0020 SPACE */
  2906. if ($token['type'] === HTML5::CHARACTR &&
  2907. preg_match('/^[\t\n\x0b\x0c ]+$/', $token['data'])
  2908. ) {
  2909. /* Append the character to the current node. */
  2910. $text = $this->dom->createTextNode($token['data']);
  2911. end($this->stack)->appendChild($text);
  2912. /* A comment token */
  2913. } elseif ($token['type'] === HTML5::COMMENT) {
  2914. /* Append a Comment node to the current node with the data
  2915. attribute set to the data given in the comment token. */
  2916. $comment = $this->dom->createComment($token['data']);
  2917. end($this->stack)->appendChild($comment);
  2918. /* A start tag whose tag name is "caption" */
  2919. } elseif ($token['type'] === HTML5::STARTTAG &&
  2920. $token['name'] === 'caption'
  2921. ) {
  2922. /* Clear the stack back to a table context. */
  2923. $this->clearStackToTableContext($clear);
  2924. /* Insert a marker at the end of the list of active
  2925. formatting elements. */
  2926. $this->a_formatting[] = self::MARKER;
  2927. /* Insert an HTML element for the token, then switch the
  2928. insertion mode to "in caption". */
  2929. $this->insertElement($token);
  2930. $this->mode = self::IN_CAPTION;
  2931. /* A start tag whose tag name is "colgroup" */
  2932. } elseif ($token['type'] === HTML5::STARTTAG &&
  2933. $token['name'] === 'colgroup'
  2934. ) {
  2935. /* Clear the stack back to a table context. */
  2936. $this->clearStackToTableContext($clear);
  2937. /* Insert an HTML element for the token, then switch the
  2938. insertion mode to "in column group". */
  2939. $this->insertElement($token);
  2940. $this->mode = self::IN_CGROUP;
  2941. /* A start tag whose tag name is "col" */
  2942. } elseif ($token['type'] === HTML5::STARTTAG &&
  2943. $token['name'] === 'col'
  2944. ) {
  2945. $this->inTable(
  2946. array(
  2947. 'name' => 'colgroup',
  2948. 'type' => HTML5::STARTTAG,
  2949. 'attr' => array()
  2950. )
  2951. );
  2952. $this->inColumnGroup($token);
  2953. /* A start tag whose tag name is one of: "tbody", "tfoot", "thead" */
  2954. } elseif ($token['type'] === HTML5::STARTTAG && in_array(
  2955. $token['name'],
  2956. array('tbody', 'tfoot', 'thead')
  2957. )
  2958. ) {
  2959. /* Clear the stack back to a table context. */
  2960. $this->clearStackToTableContext($clear);
  2961. /* Insert an HTML element for the token, then switch the insertion
  2962. mode to "in table body". */
  2963. $this->insertElement($token);
  2964. $this->mode = self::IN_TBODY;
  2965. /* A start tag whose tag name is one of: "td", "th", "tr" */
  2966. } elseif ($token['type'] === HTML5::STARTTAG &&
  2967. in_array($token['name'], array('td', 'th', 'tr'))
  2968. ) {
  2969. /* Act as if a start tag token with the tag name "tbody" had been
  2970. seen, then reprocess the current token. */
  2971. $this->inTable(
  2972. array(
  2973. 'name' => 'tbody',
  2974. 'type' => HTML5::STARTTAG,
  2975. 'attr' => array()
  2976. )
  2977. );
  2978. return $this->inTableBody($token);
  2979. /* A start tag whose tag name is "table" */
  2980. } elseif ($token['type'] === HTML5::STARTTAG &&
  2981. $token['name'] === 'table'
  2982. ) {
  2983. /* Parse error. Act as if an end tag token with the tag name "table"
  2984. had been seen, then, if that token wasn't ignored, reprocess the
  2985. current token. */
  2986. $this->inTable(
  2987. array(
  2988. 'name' => 'table',
  2989. 'type' => HTML5::ENDTAG
  2990. )
  2991. );
  2992. return $this->mainPhase($token);
  2993. /* An end tag whose tag name is "table" */
  2994. } elseif ($token['type'] === HTML5::ENDTAG &&
  2995. $token['name'] === 'table'
  2996. ) {
  2997. /* If the stack of open elements does not have an element in table
  2998. scope with the same tag name as the token, this is a parse error.
  2999. Ignore the token. (innerHTML case) */
  3000. if (!$this->elementInScope($token['name'], true)) {
  3001. return false;
  3002. /* Otherwise: */
  3003. } else {
  3004. /* Generate implied end tags. */
  3005. $this->generateImpliedEndTags();
  3006. /* Now, if the current node is not a table element, then this
  3007. is a parse error. */
  3008. // w/e
  3009. /* Pop elements from this stack until a table element has been
  3010. popped from the stack. */
  3011. while (true) {
  3012. $current = end($this->stack)->nodeName;
  3013. array_pop($this->stack);
  3014. if ($current === 'table') {
  3015. break;
  3016. }
  3017. }
  3018. /* Reset the insertion mode appropriately. */
  3019. $this->resetInsertionMode();
  3020. }
  3021. /* An end tag whose tag name is one of: "body", "caption", "col",
  3022. "colgroup", "html", "tbody", "td", "tfoot", "th", "thead", "tr" */
  3023. } elseif ($token['type'] === HTML5::ENDTAG && in_array(
  3024. $token['name'],
  3025. array(
  3026. 'body',
  3027. 'caption',
  3028. 'col',
  3029. 'colgroup',
  3030. 'html',
  3031. 'tbody',
  3032. 'td',
  3033. 'tfoot',
  3034. 'th',
  3035. 'thead',
  3036. 'tr'
  3037. )
  3038. )
  3039. ) {
  3040. // Parse error. Ignore the token.
  3041. /* Anything else */
  3042. } else {
  3043. /* Parse error. Process the token as if the insertion mode was "in
  3044. body", with the following exception: */
  3045. /* If the current node is a table, tbody, tfoot, thead, or tr
  3046. element, then, whenever a node would be inserted into the current
  3047. node, it must instead be inserted into the foster parent element. */
  3048. if (in_array(
  3049. end($this->stack)->nodeName,
  3050. array('table', 'tbody', 'tfoot', 'thead', 'tr')
  3051. )
  3052. ) {
  3053. /* The foster parent element is the parent element of the last
  3054. table element in the stack of open elements, if there is a
  3055. table element and it has such a parent element. If there is no
  3056. table element in the stack of open elements (innerHTML case),
  3057. then the foster parent element is the first element in the
  3058. stack of open elements (the html element). Otherwise, if there
  3059. is a table element in the stack of open elements, but the last
  3060. table element in the stack of open elements has no parent, or
  3061. its parent node is not an element, then the foster parent
  3062. element is the element before the last table element in the
  3063. stack of open elements. */
  3064. for ($n = count($this->stack) - 1; $n >= 0; $n--) {
  3065. if ($this->stack[$n]->nodeName === 'table') {
  3066. $table = $this->stack[$n];
  3067. break;
  3068. }
  3069. }
  3070. if (isset($table) && $table->parentNode !== null) {
  3071. $this->foster_parent = $table->parentNode;
  3072. } elseif (!isset($table)) {
  3073. $this->foster_parent = $this->stack[0];
  3074. } elseif (isset($table) && ($table->parentNode === null ||
  3075. $table->parentNode->nodeType !== XML_ELEMENT_NODE)
  3076. ) {
  3077. $this->foster_parent = $this->stack[$n - 1];
  3078. }
  3079. }
  3080. $this->inBody($token);
  3081. }
  3082. }
  3083. private function inCaption($token)
  3084. {
  3085. /* An end tag whose tag name is "caption" */
  3086. if ($token['type'] === HTML5::ENDTAG && $token['name'] === 'caption') {
  3087. /* If the stack of open elements does not have an element in table
  3088. scope with the same tag name as the token, this is a parse error.
  3089. Ignore the token. (innerHTML case) */
  3090. if (!$this->elementInScope($token['name'], true)) {
  3091. // Ignore
  3092. /* Otherwise: */
  3093. } else {
  3094. /* Generate implied end tags. */
  3095. $this->generateImpliedEndTags();
  3096. /* Now, if the current node is not a caption element, then this
  3097. is a parse error. */
  3098. // w/e
  3099. /* Pop elements from this stack until a caption element has
  3100. been popped from the stack. */
  3101. while (true) {
  3102. $node = end($this->stack)->nodeName;
  3103. array_pop($this->stack);
  3104. if ($node === 'caption') {
  3105. break;
  3106. }
  3107. }
  3108. /* Clear the list of active formatting elements up to the last
  3109. marker. */
  3110. $this->clearTheActiveFormattingElementsUpToTheLastMarker();
  3111. /* Switch the insertion mode to "in table". */
  3112. $this->mode = self::IN_TABLE;
  3113. }
  3114. /* A start tag whose tag name is one of: "caption", "col", "colgroup",
  3115. "tbody", "td", "tfoot", "th", "thead", "tr", or an end tag whose tag
  3116. name is "table" */
  3117. } elseif (($token['type'] === HTML5::STARTTAG && in_array(
  3118. $token['name'],
  3119. array(
  3120. 'caption',
  3121. 'col',
  3122. 'colgroup',
  3123. 'tbody',
  3124. 'td',
  3125. 'tfoot',
  3126. 'th',
  3127. 'thead',
  3128. 'tr'
  3129. )
  3130. )) || ($token['type'] === HTML5::ENDTAG &&
  3131. $token['name'] === 'table')
  3132. ) {
  3133. /* Parse error. Act as if an end tag with the tag name "caption"
  3134. had been seen, then, if that token wasn't ignored, reprocess the
  3135. current token. */
  3136. $this->inCaption(
  3137. array(
  3138. 'name' => 'caption',
  3139. 'type' => HTML5::ENDTAG
  3140. )
  3141. );
  3142. return $this->inTable($token);
  3143. /* An end tag whose tag name is one of: "body", "col", "colgroup",
  3144. "html", "tbody", "td", "tfoot", "th", "thead", "tr" */
  3145. } elseif ($token['type'] === HTML5::ENDTAG && in_array(
  3146. $token['name'],
  3147. array(
  3148. 'body',
  3149. 'col',
  3150. 'colgroup',
  3151. 'html',
  3152. 'tbody',
  3153. 'tfoot',
  3154. 'th',
  3155. 'thead',
  3156. 'tr'
  3157. )
  3158. )
  3159. ) {
  3160. // Parse error. Ignore the token.
  3161. /* Anything else */
  3162. } else {
  3163. /* Process the token as if the insertion mode was "in body". */
  3164. $this->inBody($token);
  3165. }
  3166. }
  3167. private function inColumnGroup($token)
  3168. {
  3169. /* A character token that is one of one of U+0009 CHARACTER TABULATION,
  3170. U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF),
  3171. or U+0020 SPACE */
  3172. if ($token['type'] === HTML5::CHARACTR &&
  3173. preg_match('/^[\t\n\x0b\x0c ]+$/', $token['data'])
  3174. ) {
  3175. /* Append the character to the current node. */
  3176. $text = $this->dom->createTextNode($token['data']);
  3177. end($this->stack)->appendChild($text);
  3178. /* A comment token */
  3179. } elseif ($token['type'] === HTML5::COMMENT) {
  3180. /* Append a Comment node to the current node with the data
  3181. attribute set to the data given in the comment token. */
  3182. $comment = $this->dom->createComment($token['data']);
  3183. end($this->stack)->appendChild($comment);
  3184. /* A start tag whose tag name is "col" */
  3185. } elseif ($token['type'] === HTML5::STARTTAG && $token['name'] === 'col') {
  3186. /* Insert a col element for the token. Immediately pop the current
  3187. node off the stack of open elements. */
  3188. $this->insertElement($token);
  3189. array_pop($this->stack);
  3190. /* An end tag whose tag name is "colgroup" */
  3191. } elseif ($token['type'] === HTML5::ENDTAG &&
  3192. $token['name'] === 'colgroup'
  3193. ) {
  3194. /* If the current node is the root html element, then this is a
  3195. parse error, ignore the token. (innerHTML case) */
  3196. if (end($this->stack)->nodeName === 'html') {
  3197. // Ignore
  3198. /* Otherwise, pop the current node (which will be a colgroup
  3199. element) from the stack of open elements. Switch the insertion
  3200. mode to "in table". */
  3201. } else {
  3202. array_pop($this->stack);
  3203. $this->mode = self::IN_TABLE;
  3204. }
  3205. /* An end tag whose tag name is "col" */
  3206. } elseif ($token['type'] === HTML5::ENDTAG && $token['name'] === 'col') {
  3207. /* Parse error. Ignore the token. */
  3208. /* Anything else */
  3209. } else {
  3210. /* Act as if an end tag with the tag name "colgroup" had been seen,
  3211. and then, if that token wasn't ignored, reprocess the current token. */
  3212. $this->inColumnGroup(
  3213. array(
  3214. 'name' => 'colgroup',
  3215. 'type' => HTML5::ENDTAG
  3216. )
  3217. );
  3218. return $this->inTable($token);
  3219. }
  3220. }
  3221. private function inTableBody($token)
  3222. {
  3223. $clear = array('tbody', 'tfoot', 'thead', 'html');
  3224. /* A start tag whose tag name is "tr" */
  3225. if ($token['type'] === HTML5::STARTTAG && $token['name'] === 'tr') {
  3226. /* Clear the stack back to a table body context. */
  3227. $this->clearStackToTableContext($clear);
  3228. /* Insert a tr element for the token, then switch the insertion
  3229. mode to "in row". */
  3230. $this->insertElement($token);
  3231. $this->mode = self::IN_ROW;
  3232. /* A start tag whose tag name is one of: "th", "td" */
  3233. } elseif ($token['type'] === HTML5::STARTTAG &&
  3234. ($token['name'] === 'th' || $token['name'] === 'td')
  3235. ) {
  3236. /* Parse error. Act as if a start tag with the tag name "tr" had
  3237. been seen, then reprocess the current token. */
  3238. $this->inTableBody(
  3239. array(
  3240. 'name' => 'tr',
  3241. 'type' => HTML5::STARTTAG,
  3242. 'attr' => array()
  3243. )
  3244. );
  3245. return $this->inRow($token);
  3246. /* An end tag whose tag name is one of: "tbody", "tfoot", "thead" */
  3247. } elseif ($token['type'] === HTML5::ENDTAG &&
  3248. in_array($token['name'], array('tbody', 'tfoot', 'thead'))
  3249. ) {
  3250. /* If the stack of open elements does not have an element in table
  3251. scope with the same tag name as the token, this is a parse error.
  3252. Ignore the token. */
  3253. if (!$this->elementInScope($token['name'], true)) {
  3254. // Ignore
  3255. /* Otherwise: */
  3256. } else {
  3257. /* Clear the stack back to a table body context. */
  3258. $this->clearStackToTableContext($clear);
  3259. /* Pop the current node from the stack of open elements. Switch
  3260. the insertion mode to "in table". */
  3261. array_pop($this->stack);
  3262. $this->mode = self::IN_TABLE;
  3263. }
  3264. /* A start tag whose tag name is one of: "caption", "col", "colgroup",
  3265. "tbody", "tfoot", "thead", or an end tag whose tag name is "table" */
  3266. } elseif (($token['type'] === HTML5::STARTTAG && in_array(
  3267. $token['name'],
  3268. array('caption', 'col', 'colgroup', 'tbody', 'tfoor', 'thead')
  3269. )) ||
  3270. ($token['type'] === HTML5::STARTTAG && $token['name'] === 'table')
  3271. ) {
  3272. /* If the stack of open elements does not have a tbody, thead, or
  3273. tfoot element in table scope, this is a parse error. Ignore the
  3274. token. (innerHTML case) */
  3275. if (!$this->elementInScope(array('tbody', 'thead', 'tfoot'), true)) {
  3276. // Ignore.
  3277. /* Otherwise: */
  3278. } else {
  3279. /* Clear the stack back to a table body context. */
  3280. $this->clearStackToTableContext($clear);
  3281. /* Act as if an end tag with the same tag name as the current
  3282. node ("tbody", "tfoot", or "thead") had been seen, then
  3283. reprocess the current token. */
  3284. $this->inTableBody(
  3285. array(
  3286. 'name' => end($this->stack)->nodeName,
  3287. 'type' => HTML5::ENDTAG
  3288. )
  3289. );
  3290. return $this->mainPhase($token);
  3291. }
  3292. /* An end tag whose tag name is one of: "body", "caption", "col",
  3293. "colgroup", "html", "td", "th", "tr" */
  3294. } elseif ($token['type'] === HTML5::ENDTAG && in_array(
  3295. $token['name'],
  3296. array('body', 'caption', 'col', 'colgroup', 'html', 'td', 'th', 'tr')
  3297. )
  3298. ) {
  3299. /* Parse error. Ignore the token. */
  3300. /* Anything else */
  3301. } else {
  3302. /* Process the token as if the insertion mode was "in table". */
  3303. $this->inTable($token);
  3304. }
  3305. }
  3306. private function inRow($token)
  3307. {
  3308. $clear = array('tr', 'html');
  3309. /* A start tag whose tag name is one of: "th", "td" */
  3310. if ($token['type'] === HTML5::STARTTAG &&
  3311. ($token['name'] === 'th' || $token['name'] === 'td')
  3312. ) {
  3313. /* Clear the stack back to a table row context. */
  3314. $this->clearStackToTableContext($clear);
  3315. /* Insert an HTML element for the token, then switch the insertion
  3316. mode to "in cell". */
  3317. $this->insertElement($token);
  3318. $this->mode = self::IN_CELL;
  3319. /* Insert a marker at the end of the list of active formatting
  3320. elements. */
  3321. $this->a_formatting[] = self::MARKER;
  3322. /* An end tag whose tag name is "tr" */
  3323. } elseif ($token['type'] === HTML5::ENDTAG && $token['name'] === 'tr') {
  3324. /* If the stack of open elements does not have an element in table
  3325. scope with the same tag name as the token, this is a parse error.
  3326. Ignore the token. (innerHTML case) */
  3327. if (!$this->elementInScope($token['name'], true)) {
  3328. // Ignore.
  3329. /* Otherwise: */
  3330. } else {
  3331. /* Clear the stack back to a table row context. */
  3332. $this->clearStackToTableContext($clear);
  3333. /* Pop the current node (which will be a tr element) from the
  3334. stack of open elements. Switch the insertion mode to "in table
  3335. body". */
  3336. array_pop($this->stack);
  3337. $this->mode = self::IN_TBODY;
  3338. }
  3339. /* A start tag whose tag name is one of: "caption", "col", "colgroup",
  3340. "tbody", "tfoot", "thead", "tr" or an end tag whose tag name is "table" */
  3341. } elseif ($token['type'] === HTML5::STARTTAG && in_array(
  3342. $token['name'],
  3343. array('caption', 'col', 'colgroup', 'tbody', 'tfoot', 'thead', 'tr')
  3344. )
  3345. ) {
  3346. /* Act as if an end tag with the tag name "tr" had been seen, then,
  3347. if that token wasn't ignored, reprocess the current token. */
  3348. $this->inRow(
  3349. array(
  3350. 'name' => 'tr',
  3351. 'type' => HTML5::ENDTAG
  3352. )
  3353. );
  3354. return $this->inCell($token);
  3355. /* An end tag whose tag name is one of: "tbody", "tfoot", "thead" */
  3356. } elseif ($token['type'] === HTML5::ENDTAG &&
  3357. in_array($token['name'], array('tbody', 'tfoot', 'thead'))
  3358. ) {
  3359. /* If the stack of open elements does not have an element in table
  3360. scope with the same tag name as the token, this is a parse error.
  3361. Ignore the token. */
  3362. if (!$this->elementInScope($token['name'], true)) {
  3363. // Ignore.
  3364. /* Otherwise: */
  3365. } else {
  3366. /* Otherwise, act as if an end tag with the tag name "tr" had
  3367. been seen, then reprocess the current token. */
  3368. $this->inRow(
  3369. array(
  3370. 'name' => 'tr',
  3371. 'type' => HTML5::ENDTAG
  3372. )
  3373. );
  3374. return $this->inCell($token);
  3375. }
  3376. /* An end tag whose tag name is one of: "body", "caption", "col",
  3377. "colgroup", "html", "td", "th" */
  3378. } elseif ($token['type'] === HTML5::ENDTAG && in_array(
  3379. $token['name'],
  3380. array('body', 'caption', 'col', 'colgroup', 'html', 'td', 'th', 'tr')
  3381. )
  3382. ) {
  3383. /* Parse error. Ignore the token. */
  3384. /* Anything else */
  3385. } else {
  3386. /* Process the token as if the insertion mode was "in table". */
  3387. $this->inTable($token);
  3388. }
  3389. }
  3390. private function inCell($token)
  3391. {
  3392. /* An end tag whose tag name is one of: "td", "th" */
  3393. if ($token['type'] === HTML5::ENDTAG &&
  3394. ($token['name'] === 'td' || $token['name'] === 'th')
  3395. ) {
  3396. /* If the stack of open elements does not have an element in table
  3397. scope with the same tag name as that of the token, then this is a
  3398. parse error and the token must be ignored. */
  3399. if (!$this->elementInScope($token['name'], true)) {
  3400. // Ignore.
  3401. /* Otherwise: */
  3402. } else {
  3403. /* Generate implied end tags, except for elements with the same
  3404. tag name as the token. */
  3405. $this->generateImpliedEndTags(array($token['name']));
  3406. /* Now, if the current node is not an element with the same tag
  3407. name as the token, then this is a parse error. */
  3408. // k
  3409. /* Pop elements from this stack until an element with the same
  3410. tag name as the token has been popped from the stack. */
  3411. while (true) {
  3412. $node = end($this->stack)->nodeName;
  3413. array_pop($this->stack);
  3414. if ($node === $token['name']) {
  3415. break;
  3416. }
  3417. }
  3418. /* Clear the list of active formatting elements up to the last
  3419. marker. */
  3420. $this->clearTheActiveFormattingElementsUpToTheLastMarker();
  3421. /* Switch the insertion mode to "in row". (The current node
  3422. will be a tr element at this point.) */
  3423. $this->mode = self::IN_ROW;
  3424. }
  3425. /* A start tag whose tag name is one of: "caption", "col", "colgroup",
  3426. "tbody", "td", "tfoot", "th", "thead", "tr" */
  3427. } elseif ($token['type'] === HTML5::STARTTAG && in_array(
  3428. $token['name'],
  3429. array(
  3430. 'caption',
  3431. 'col',
  3432. 'colgroup',
  3433. 'tbody',
  3434. 'td',
  3435. 'tfoot',
  3436. 'th',
  3437. 'thead',
  3438. 'tr'
  3439. )
  3440. )
  3441. ) {
  3442. /* If the stack of open elements does not have a td or th element
  3443. in table scope, then this is a parse error; ignore the token.
  3444. (innerHTML case) */
  3445. if (!$this->elementInScope(array('td', 'th'), true)) {
  3446. // Ignore.
  3447. /* Otherwise, close the cell (see below) and reprocess the current
  3448. token. */
  3449. } else {
  3450. $this->closeCell();
  3451. return $this->inRow($token);
  3452. }
  3453. /* A start tag whose tag name is one of: "caption", "col", "colgroup",
  3454. "tbody", "td", "tfoot", "th", "thead", "tr" */
  3455. } elseif ($token['type'] === HTML5::STARTTAG && in_array(
  3456. $token['name'],
  3457. array(
  3458. 'caption',
  3459. 'col',
  3460. 'colgroup',
  3461. 'tbody',
  3462. 'td',
  3463. 'tfoot',
  3464. 'th',
  3465. 'thead',
  3466. 'tr'
  3467. )
  3468. )
  3469. ) {
  3470. /* If the stack of open elements does not have a td or th element
  3471. in table scope, then this is a parse error; ignore the token.
  3472. (innerHTML case) */
  3473. if (!$this->elementInScope(array('td', 'th'), true)) {
  3474. // Ignore.
  3475. /* Otherwise, close the cell (see below) and reprocess the current
  3476. token. */
  3477. } else {
  3478. $this->closeCell();
  3479. return $this->inRow($token);
  3480. }
  3481. /* An end tag whose tag name is one of: "body", "caption", "col",
  3482. "colgroup", "html" */
  3483. } elseif ($token['type'] === HTML5::ENDTAG && in_array(
  3484. $token['name'],
  3485. array('body', 'caption', 'col', 'colgroup', 'html')
  3486. )
  3487. ) {
  3488. /* Parse error. Ignore the token. */
  3489. /* An end tag whose tag name is one of: "table", "tbody", "tfoot",
  3490. "thead", "tr" */
  3491. } elseif ($token['type'] === HTML5::ENDTAG && in_array(
  3492. $token['name'],
  3493. array('table', 'tbody', 'tfoot', 'thead', 'tr')
  3494. )
  3495. ) {
  3496. /* If the stack of open elements does not have an element in table
  3497. scope with the same tag name as that of the token (which can only
  3498. happen for "tbody", "tfoot" and "thead", or, in the innerHTML case),
  3499. then this is a parse error and the token must be ignored. */
  3500. if (!$this->elementInScope($token['name'], true)) {
  3501. // Ignore.
  3502. /* Otherwise, close the cell (see below) and reprocess the current
  3503. token. */
  3504. } else {
  3505. $this->closeCell();
  3506. return $this->inRow($token);
  3507. }
  3508. /* Anything else */
  3509. } else {
  3510. /* Process the token as if the insertion mode was "in body". */
  3511. $this->inBody($token);
  3512. }
  3513. }
  3514. private function inSelect($token)
  3515. {
  3516. /* Handle the token as follows: */
  3517. /* A character token */
  3518. if ($token['type'] === HTML5::CHARACTR) {
  3519. /* Append the token's character to the current node. */
  3520. $this->insertText($token['data']);
  3521. /* A comment token */
  3522. } elseif ($token['type'] === HTML5::COMMENT) {
  3523. /* Append a Comment node to the current node with the data
  3524. attribute set to the data given in the comment token. */
  3525. $this->insertComment($token['data']);
  3526. /* A start tag token whose tag name is "option" */
  3527. } elseif ($token['type'] === HTML5::STARTTAG &&
  3528. $token['name'] === 'option'
  3529. ) {
  3530. /* If the current node is an option element, act as if an end tag
  3531. with the tag name "option" had been seen. */
  3532. if (end($this->stack)->nodeName === 'option') {
  3533. $this->inSelect(
  3534. array(
  3535. 'name' => 'option',
  3536. 'type' => HTML5::ENDTAG
  3537. )
  3538. );
  3539. }
  3540. /* Insert an HTML element for the token. */
  3541. $this->insertElement($token);
  3542. /* A start tag token whose tag name is "optgroup" */
  3543. } elseif ($token['type'] === HTML5::STARTTAG &&
  3544. $token['name'] === 'optgroup'
  3545. ) {
  3546. /* If the current node is an option element, act as if an end tag
  3547. with the tag name "option" had been seen. */
  3548. if (end($this->stack)->nodeName === 'option') {
  3549. $this->inSelect(
  3550. array(
  3551. 'name' => 'option',
  3552. 'type' => HTML5::ENDTAG
  3553. )
  3554. );
  3555. }
  3556. /* If the current node is an optgroup element, act as if an end tag
  3557. with the tag name "optgroup" had been seen. */
  3558. if (end($this->stack)->nodeName === 'optgroup') {
  3559. $this->inSelect(
  3560. array(
  3561. 'name' => 'optgroup',
  3562. 'type' => HTML5::ENDTAG
  3563. )
  3564. );
  3565. }
  3566. /* Insert an HTML element for the token. */
  3567. $this->insertElement($token);
  3568. /* An end tag token whose tag name is "optgroup" */
  3569. } elseif ($token['type'] === HTML5::ENDTAG &&
  3570. $token['name'] === 'optgroup'
  3571. ) {
  3572. /* First, if the current node is an option element, and the node
  3573. immediately before it in the stack of open elements is an optgroup
  3574. element, then act as if an end tag with the tag name "option" had
  3575. been seen. */
  3576. $elements_in_stack = count($this->stack);
  3577. if ($this->stack[$elements_in_stack - 1]->nodeName === 'option' &&
  3578. $this->stack[$elements_in_stack - 2]->nodeName === 'optgroup'
  3579. ) {
  3580. $this->inSelect(
  3581. array(
  3582. 'name' => 'option',
  3583. 'type' => HTML5::ENDTAG
  3584. )
  3585. );
  3586. }
  3587. /* If the current node is an optgroup element, then pop that node
  3588. from the stack of open elements. Otherwise, this is a parse error,
  3589. ignore the token. */
  3590. if ($this->stack[$elements_in_stack - 1] === 'optgroup') {
  3591. array_pop($this->stack);
  3592. }
  3593. /* An end tag token whose tag name is "option" */
  3594. } elseif ($token['type'] === HTML5::ENDTAG &&
  3595. $token['name'] === 'option'
  3596. ) {
  3597. /* If the current node is an option element, then pop that node
  3598. from the stack of open elements. Otherwise, this is a parse error,
  3599. ignore the token. */
  3600. if (end($this->stack)->nodeName === 'option') {
  3601. array_pop($this->stack);
  3602. }
  3603. /* An end tag whose tag name is "select" */
  3604. } elseif ($token['type'] === HTML5::ENDTAG &&
  3605. $token['name'] === 'select'
  3606. ) {
  3607. /* If the stack of open elements does not have an element in table
  3608. scope with the same tag name as the token, this is a parse error.
  3609. Ignore the token. (innerHTML case) */
  3610. if (!$this->elementInScope($token['name'], true)) {
  3611. // w/e
  3612. /* Otherwise: */
  3613. } else {
  3614. /* Pop elements from the stack of open elements until a select
  3615. element has been popped from the stack. */
  3616. while (true) {
  3617. $current = end($this->stack)->nodeName;
  3618. array_pop($this->stack);
  3619. if ($current === 'select') {
  3620. break;
  3621. }
  3622. }
  3623. /* Reset the insertion mode appropriately. */
  3624. $this->resetInsertionMode();
  3625. }
  3626. /* A start tag whose tag name is "select" */
  3627. } elseif ($token['name'] === 'select' &&
  3628. $token['type'] === HTML5::STARTTAG
  3629. ) {
  3630. /* Parse error. Act as if the token had been an end tag with the
  3631. tag name "select" instead. */
  3632. $this->inSelect(
  3633. array(
  3634. 'name' => 'select',
  3635. 'type' => HTML5::ENDTAG
  3636. )
  3637. );
  3638. /* An end tag whose tag name is one of: "caption", "table", "tbody",
  3639. "tfoot", "thead", "tr", "td", "th" */
  3640. } elseif (in_array(
  3641. $token['name'],
  3642. array(
  3643. 'caption',
  3644. 'table',
  3645. 'tbody',
  3646. 'tfoot',
  3647. 'thead',
  3648. 'tr',
  3649. 'td',
  3650. 'th'
  3651. )
  3652. ) && $token['type'] === HTML5::ENDTAG
  3653. ) {
  3654. /* Parse error. */
  3655. // w/e
  3656. /* If the stack of open elements has an element in table scope with
  3657. the same tag name as that of the token, then act as if an end tag
  3658. with the tag name "select" had been seen, and reprocess the token.
  3659. Otherwise, ignore the token. */
  3660. if ($this->elementInScope($token['name'], true)) {
  3661. $this->inSelect(
  3662. array(
  3663. 'name' => 'select',
  3664. 'type' => HTML5::ENDTAG
  3665. )
  3666. );
  3667. $this->mainPhase($token);
  3668. }
  3669. /* Anything else */
  3670. } else {
  3671. /* Parse error. Ignore the token. */
  3672. }
  3673. }
  3674. private function afterBody($token)
  3675. {
  3676. /* Handle the token as follows: */
  3677. /* A character token that is one of one of U+0009 CHARACTER TABULATION,
  3678. U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF),
  3679. or U+0020 SPACE */
  3680. if ($token['type'] === HTML5::CHARACTR &&
  3681. preg_match('/^[\t\n\x0b\x0c ]+$/', $token['data'])
  3682. ) {
  3683. /* Process the token as it would be processed if the insertion mode
  3684. was "in body". */
  3685. $this->inBody($token);
  3686. /* A comment token */
  3687. } elseif ($token['type'] === HTML5::COMMENT) {
  3688. /* Append a Comment node to the first element in the stack of open
  3689. elements (the html element), with the data attribute set to the
  3690. data given in the comment token. */
  3691. $comment = $this->dom->createComment($token['data']);
  3692. $this->stack[0]->appendChild($comment);
  3693. /* An end tag with the tag name "html" */
  3694. } elseif ($token['type'] === HTML5::ENDTAG && $token['name'] === 'html') {
  3695. /* If the parser was originally created in order to handle the
  3696. setting of an element's innerHTML attribute, this is a parse error;
  3697. ignore the token. (The element will be an html element in this
  3698. case.) (innerHTML case) */
  3699. /* Otherwise, switch to the trailing end phase. */
  3700. $this->phase = self::END_PHASE;
  3701. /* Anything else */
  3702. } else {
  3703. /* Parse error. Set the insertion mode to "in body" and reprocess
  3704. the token. */
  3705. $this->mode = self::IN_BODY;
  3706. return $this->inBody($token);
  3707. }
  3708. }
  3709. private function inFrameset($token)
  3710. {
  3711. /* Handle the token as follows: */
  3712. /* A character token that is one of one of U+0009 CHARACTER TABULATION,
  3713. U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF),
  3714. U+000D CARRIAGE RETURN (CR), or U+0020 SPACE */
  3715. if ($token['type'] === HTML5::CHARACTR &&
  3716. preg_match('/^[\t\n\x0b\x0c ]+$/', $token['data'])
  3717. ) {
  3718. /* Append the character to the current node. */
  3719. $this->insertText($token['data']);
  3720. /* A comment token */
  3721. } elseif ($token['type'] === HTML5::COMMENT) {
  3722. /* Append a Comment node to the current node with the data
  3723. attribute set to the data given in the comment token. */
  3724. $this->insertComment($token['data']);
  3725. /* A start tag with the tag name "frameset" */
  3726. } elseif ($token['name'] === 'frameset' &&
  3727. $token['type'] === HTML5::STARTTAG
  3728. ) {
  3729. $this->insertElement($token);
  3730. /* An end tag with the tag name "frameset" */
  3731. } elseif ($token['name'] === 'frameset' &&
  3732. $token['type'] === HTML5::ENDTAG
  3733. ) {
  3734. /* If the current node is the root html element, then this is a
  3735. parse error; ignore the token. (innerHTML case) */
  3736. if (end($this->stack)->nodeName === 'html') {
  3737. // Ignore
  3738. } else {
  3739. /* Otherwise, pop the current node from the stack of open
  3740. elements. */
  3741. array_pop($this->stack);
  3742. /* If the parser was not originally created in order to handle
  3743. the setting of an element's innerHTML attribute (innerHTML case),
  3744. and the current node is no longer a frameset element, then change
  3745. the insertion mode to "after frameset". */
  3746. $this->mode = self::AFTR_FRAME;
  3747. }
  3748. /* A start tag with the tag name "frame" */
  3749. } elseif ($token['name'] === 'frame' &&
  3750. $token['type'] === HTML5::STARTTAG
  3751. ) {
  3752. /* Insert an HTML element for the token. */
  3753. $this->insertElement($token);
  3754. /* Immediately pop the current node off the stack of open elements. */
  3755. array_pop($this->stack);
  3756. /* A start tag with the tag name "noframes" */
  3757. } elseif ($token['name'] === 'noframes' &&
  3758. $token['type'] === HTML5::STARTTAG
  3759. ) {
  3760. /* Process the token as if the insertion mode had been "in body". */
  3761. $this->inBody($token);
  3762. /* Anything else */
  3763. } else {
  3764. /* Parse error. Ignore the token. */
  3765. }
  3766. }
  3767. private function afterFrameset($token)
  3768. {
  3769. /* Handle the token as follows: */
  3770. /* A character token that is one of one of U+0009 CHARACTER TABULATION,
  3771. U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF),
  3772. U+000D CARRIAGE RETURN (CR), or U+0020 SPACE */
  3773. if ($token['type'] === HTML5::CHARACTR &&
  3774. preg_match('/^[\t\n\x0b\x0c ]+$/', $token['data'])
  3775. ) {
  3776. /* Append the character to the current node. */
  3777. $this->insertText($token['data']);
  3778. /* A comment token */
  3779. } elseif ($token['type'] === HTML5::COMMENT) {
  3780. /* Append a Comment node to the current node with the data
  3781. attribute set to the data given in the comment token. */
  3782. $this->insertComment($token['data']);
  3783. /* An end tag with the tag name "html" */
  3784. } elseif ($token['name'] === 'html' &&
  3785. $token['type'] === HTML5::ENDTAG
  3786. ) {
  3787. /* Switch to the trailing end phase. */
  3788. $this->phase = self::END_PHASE;
  3789. /* A start tag with the tag name "noframes" */
  3790. } elseif ($token['name'] === 'noframes' &&
  3791. $token['type'] === HTML5::STARTTAG
  3792. ) {
  3793. /* Process the token as if the insertion mode had been "in body". */
  3794. $this->inBody($token);
  3795. /* Anything else */
  3796. } else {
  3797. /* Parse error. Ignore the token. */
  3798. }
  3799. }
  3800. private function trailingEndPhase($token)
  3801. {
  3802. /* After the main phase, as each token is emitted from the tokenisation
  3803. stage, it must be processed as described in this section. */
  3804. /* A DOCTYPE token */
  3805. if ($token['type'] === HTML5::DOCTYPE) {
  3806. // Parse error. Ignore the token.
  3807. /* A comment token */
  3808. } elseif ($token['type'] === HTML5::COMMENT) {
  3809. /* Append a Comment node to the Document object with the data
  3810. attribute set to the data given in the comment token. */
  3811. $comment = $this->dom->createComment($token['data']);
  3812. $this->dom->appendChild($comment);
  3813. /* A character token that is one of one of U+0009 CHARACTER TABULATION,
  3814. U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF),
  3815. or U+0020 SPACE */
  3816. } elseif ($token['type'] === HTML5::CHARACTR &&
  3817. preg_match('/^[\t\n\x0b\x0c ]+$/', $token['data'])
  3818. ) {
  3819. /* Process the token as it would be processed in the main phase. */
  3820. $this->mainPhase($token);
  3821. /* A character token that is not one of U+0009 CHARACTER TABULATION,
  3822. U+000A LINE FEED (LF), U+000B LINE TABULATION, U+000C FORM FEED (FF),
  3823. or U+0020 SPACE. Or a start tag token. Or an end tag token. */
  3824. } elseif (($token['type'] === HTML5::CHARACTR &&
  3825. preg_match('/^[\t\n\x0b\x0c ]+$/', $token['data'])) ||
  3826. $token['type'] === HTML5::STARTTAG || $token['type'] === HTML5::ENDTAG
  3827. ) {
  3828. /* Parse error. Switch back to the main phase and reprocess the
  3829. token. */
  3830. $this->phase = self::MAIN_PHASE;
  3831. return $this->mainPhase($token);
  3832. /* An end-of-file token */
  3833. } elseif ($token['type'] === HTML5::EOF) {
  3834. /* OMG DONE!! */
  3835. }
  3836. }
  3837. private function insertElement($token, $append = true, $check = false)
  3838. {
  3839. // Proprietary workaround for libxml2's limitations with tag names
  3840. if ($check) {
  3841. // Slightly modified HTML5 tag-name modification,
  3842. // removing anything that's not an ASCII letter, digit, or hyphen
  3843. $token['name'] = preg_replace('/[^a-z0-9-]/i', '', $token['name']);
  3844. // Remove leading hyphens and numbers
  3845. $token['name'] = ltrim($token['name'], '-0..9');
  3846. // In theory, this should ever be needed, but just in case
  3847. if ($token['name'] === '') {
  3848. $token['name'] = 'span';
  3849. } // arbitrary generic choice
  3850. }
  3851. $el = $this->dom->createElement($token['name']);
  3852. foreach ($token['attr'] as $attr) {
  3853. if (!$el->hasAttribute($attr['name'])) {
  3854. $el->setAttribute($attr['name'], $attr['value']);
  3855. }
  3856. }
  3857. $this->appendToRealParent($el);
  3858. $this->stack[] = $el;
  3859. return $el;
  3860. }
  3861. private function insertText($data)
  3862. {
  3863. $text = $this->dom->createTextNode($data);
  3864. $this->appendToRealParent($text);
  3865. }
  3866. private function insertComment($data)
  3867. {
  3868. $comment = $this->dom->createComment($data);
  3869. $this->appendToRealParent($comment);
  3870. }
  3871. private function appendToRealParent($node)
  3872. {
  3873. if ($this->foster_parent === null) {
  3874. end($this->stack)->appendChild($node);
  3875. } elseif ($this->foster_parent !== null) {
  3876. /* If the foster parent element is the parent element of the
  3877. last table element in the stack of open elements, then the new
  3878. node must be inserted immediately before the last table element
  3879. in the stack of open elements in the foster parent element;
  3880. otherwise, the new node must be appended to the foster parent
  3881. element. */
  3882. for ($n = count($this->stack) - 1; $n >= 0; $n--) {
  3883. if ($this->stack[$n]->nodeName === 'table' &&
  3884. $this->stack[$n]->parentNode !== null
  3885. ) {
  3886. $table = $this->stack[$n];
  3887. break;
  3888. }
  3889. }
  3890. if (isset($table) && $this->foster_parent->isSameNode($table->parentNode)) {
  3891. $this->foster_parent->insertBefore($node, $table);
  3892. } else {
  3893. $this->foster_parent->appendChild($node);
  3894. }
  3895. $this->foster_parent = null;
  3896. }
  3897. }
  3898. private function elementInScope($el, $table = false)
  3899. {
  3900. if (is_array($el)) {
  3901. foreach ($el as $element) {
  3902. if ($this->elementInScope($element, $table)) {
  3903. return true;
  3904. }
  3905. }
  3906. return false;
  3907. }
  3908. $leng = count($this->stack);
  3909. for ($n = 0; $n < $leng; $n++) {
  3910. /* 1. Initialise node to be the current node (the bottommost node of
  3911. the stack). */
  3912. $node = $this->stack[$leng - 1 - $n];
  3913. if ($node->tagName === $el) {
  3914. /* 2. If node is the target node, terminate in a match state. */
  3915. return true;
  3916. } elseif ($node->tagName === 'table') {
  3917. /* 3. Otherwise, if node is a table element, terminate in a failure
  3918. state. */
  3919. return false;
  3920. } elseif ($table === true && in_array(
  3921. $node->tagName,
  3922. array(
  3923. 'caption',
  3924. 'td',
  3925. 'th',
  3926. 'button',
  3927. 'marquee',
  3928. 'object'
  3929. )
  3930. )
  3931. ) {
  3932. /* 4. Otherwise, if the algorithm is the "has an element in scope"
  3933. variant (rather than the "has an element in table scope" variant),
  3934. and node is one of the following, terminate in a failure state. */
  3935. return false;
  3936. } elseif ($node === $node->ownerDocument->documentElement) {
  3937. /* 5. Otherwise, if node is an html element (root element), terminate
  3938. in a failure state. (This can only happen if the node is the topmost
  3939. node of the stack of open elements, and prevents the next step from
  3940. being invoked if there are no more elements in the stack.) */
  3941. return false;
  3942. }
  3943. /* Otherwise, set node to the previous entry in the stack of open
  3944. elements and return to step 2. (This will never fail, since the loop
  3945. will always terminate in the previous step if the top of the stack
  3946. is reached.) */
  3947. }
  3948. }
  3949. private function reconstructActiveFormattingElements()
  3950. {
  3951. /* 1. If there are no entries in the list of active formatting elements,
  3952. then there is nothing to reconstruct; stop this algorithm. */
  3953. $formatting_elements = count($this->a_formatting);
  3954. if ($formatting_elements === 0) {
  3955. return false;
  3956. }
  3957. /* 3. Let entry be the last (most recently added) element in the list
  3958. of active formatting elements. */
  3959. $entry = end($this->a_formatting);
  3960. /* 2. If the last (most recently added) entry in the list of active
  3961. formatting elements is a marker, or if it is an element that is in the
  3962. stack of open elements, then there is nothing to reconstruct; stop this
  3963. algorithm. */
  3964. if ($entry === self::MARKER || in_array($entry, $this->stack, true)) {
  3965. return false;
  3966. }
  3967. for ($a = $formatting_elements - 1; $a >= 0; true) {
  3968. /* 4. If there are no entries before entry in the list of active
  3969. formatting elements, then jump to step 8. */
  3970. if ($a === 0) {
  3971. $step_seven = false;
  3972. break;
  3973. }
  3974. /* 5. Let entry be the entry one earlier than entry in the list of
  3975. active formatting elements. */
  3976. $a--;
  3977. $entry = $this->a_formatting[$a];
  3978. /* 6. If entry is neither a marker nor an element that is also in
  3979. thetack of open elements, go to step 4. */
  3980. if ($entry === self::MARKER || in_array($entry, $this->stack, true)) {
  3981. break;
  3982. }
  3983. }
  3984. while (true) {
  3985. /* 7. Let entry be the element one later than entry in the list of
  3986. active formatting elements. */
  3987. if (isset($step_seven) && $step_seven === true) {
  3988. $a++;
  3989. $entry = $this->a_formatting[$a];
  3990. }
  3991. /* 8. Perform a shallow clone of the element entry to obtain clone. */
  3992. $clone = $entry->cloneNode();
  3993. /* 9. Append clone to the current node and push it onto the stack
  3994. of open elements so that it is the new current node. */
  3995. end($this->stack)->appendChild($clone);
  3996. $this->stack[] = $clone;
  3997. /* 10. Replace the entry for entry in the list with an entry for
  3998. clone. */
  3999. $this->a_formatting[$a] = $clone;
  4000. /* 11. If the entry for clone in the list of active formatting
  4001. elements is not the last entry in the list, return to step 7. */
  4002. if (end($this->a_formatting) !== $clone) {
  4003. $step_seven = true;
  4004. } else {
  4005. break;
  4006. }
  4007. }
  4008. }
  4009. private function clearTheActiveFormattingElementsUpToTheLastMarker()
  4010. {
  4011. /* When the steps below require the UA to clear the list of active
  4012. formatting elements up to the last marker, the UA must perform the
  4013. following steps: */
  4014. while (true) {
  4015. /* 1. Let entry be the last (most recently added) entry in the list
  4016. of active formatting elements. */
  4017. $entry = end($this->a_formatting);
  4018. /* 2. Remove entry from the list of active formatting elements. */
  4019. array_pop($this->a_formatting);
  4020. /* 3. If entry was a marker, then stop the algorithm at this point.
  4021. The list has been cleared up to the last marker. */
  4022. if ($entry === self::MARKER) {
  4023. break;
  4024. }
  4025. }
  4026. }
  4027. private function generateImpliedEndTags($exclude = array())
  4028. {
  4029. /* When the steps below require the UA to generate implied end tags,
  4030. then, if the current node is a dd element, a dt element, an li element,
  4031. a p element, a td element, a th element, or a tr element, the UA must
  4032. act as if an end tag with the respective tag name had been seen and
  4033. then generate implied end tags again. */
  4034. $node = end($this->stack);
  4035. $elements = array_diff(array('dd', 'dt', 'li', 'p', 'td', 'th', 'tr'), $exclude);
  4036. while (in_array(end($this->stack)->nodeName, $elements)) {
  4037. array_pop($this->stack);
  4038. }
  4039. }
  4040. private function getElementCategory($node)
  4041. {
  4042. $name = $node->tagName;
  4043. if (in_array($name, $this->special)) {
  4044. return self::SPECIAL;
  4045. } elseif (in_array($name, $this->scoping)) {
  4046. return self::SCOPING;
  4047. } elseif (in_array($name, $this->formatting)) {
  4048. return self::FORMATTING;
  4049. } else {
  4050. return self::PHRASING;
  4051. }
  4052. }
  4053. private function clearStackToTableContext($elements)
  4054. {
  4055. /* When the steps above require the UA to clear the stack back to a
  4056. table context, it means that the UA must, while the current node is not
  4057. a table element or an html element, pop elements from the stack of open
  4058. elements. If this causes any elements to be popped from the stack, then
  4059. this is a parse error. */
  4060. while (true) {
  4061. $node = end($this->stack)->nodeName;
  4062. if (in_array($node, $elements)) {
  4063. break;
  4064. } else {
  4065. array_pop($this->stack);
  4066. }
  4067. }
  4068. }
  4069. private function resetInsertionMode()
  4070. {
  4071. /* 1. Let last be false. */
  4072. $last = false;
  4073. $leng = count($this->stack);
  4074. for ($n = $leng - 1; $n >= 0; $n--) {
  4075. /* 2. Let node be the last node in the stack of open elements. */
  4076. $node = $this->stack[$n];
  4077. /* 3. If node is the first node in the stack of open elements, then
  4078. set last to true. If the element whose innerHTML attribute is being
  4079. set is neither a td element nor a th element, then set node to the
  4080. element whose innerHTML attribute is being set. (innerHTML case) */
  4081. if ($this->stack[0]->isSameNode($node)) {
  4082. $last = true;
  4083. }
  4084. /* 4. If node is a select element, then switch the insertion mode to
  4085. "in select" and abort these steps. (innerHTML case) */
  4086. if ($node->nodeName === 'select') {
  4087. $this->mode = self::IN_SELECT;
  4088. break;
  4089. /* 5. If node is a td or th element, then switch the insertion mode
  4090. to "in cell" and abort these steps. */
  4091. } elseif ($node->nodeName === 'td' || $node->nodeName === 'th') {
  4092. $this->mode = self::IN_CELL;
  4093. break;
  4094. /* 6. If node is a tr element, then switch the insertion mode to
  4095. "in row" and abort these steps. */
  4096. } elseif ($node->nodeName === 'tr') {
  4097. $this->mode = self::IN_ROW;
  4098. break;
  4099. /* 7. If node is a tbody, thead, or tfoot element, then switch the
  4100. insertion mode to "in table body" and abort these steps. */
  4101. } elseif (in_array($node->nodeName, array('tbody', 'thead', 'tfoot'))) {
  4102. $this->mode = self::IN_TBODY;
  4103. break;
  4104. /* 8. If node is a caption element, then switch the insertion mode
  4105. to "in caption" and abort these steps. */
  4106. } elseif ($node->nodeName === 'caption') {
  4107. $this->mode = self::IN_CAPTION;
  4108. break;
  4109. /* 9. If node is a colgroup element, then switch the insertion mode
  4110. to "in column group" and abort these steps. (innerHTML case) */
  4111. } elseif ($node->nodeName === 'colgroup') {
  4112. $this->mode = self::IN_CGROUP;
  4113. break;
  4114. /* 10. If node is a table element, then switch the insertion mode
  4115. to "in table" and abort these steps. */
  4116. } elseif ($node->nodeName === 'table') {
  4117. $this->mode = self::IN_TABLE;
  4118. break;
  4119. /* 11. If node is a head element, then switch the insertion mode
  4120. to "in body" ("in body"! not "in head"!) and abort these steps.
  4121. (innerHTML case) */
  4122. } elseif ($node->nodeName === 'head') {
  4123. $this->mode = self::IN_BODY;
  4124. break;
  4125. /* 12. If node is a body element, then switch the insertion mode to
  4126. "in body" and abort these steps. */
  4127. } elseif ($node->nodeName === 'body') {
  4128. $this->mode = self::IN_BODY;
  4129. break;
  4130. /* 13. If node is a frameset element, then switch the insertion
  4131. mode to "in frameset" and abort these steps. (innerHTML case) */
  4132. } elseif ($node->nodeName === 'frameset') {
  4133. $this->mode = self::IN_FRAME;
  4134. break;
  4135. /* 14. If node is an html element, then: if the head element
  4136. pointer is null, switch the insertion mode to "before head",
  4137. otherwise, switch the insertion mode to "after head". In either
  4138. case, abort these steps. (innerHTML case) */
  4139. } elseif ($node->nodeName === 'html') {
  4140. $this->mode = ($this->head_pointer === null)
  4141. ? self::BEFOR_HEAD
  4142. : self::AFTER_HEAD;
  4143. break;
  4144. /* 15. If last is true, then set the insertion mode to "in body"
  4145. and abort these steps. (innerHTML case) */
  4146. } elseif ($last) {
  4147. $this->mode = self::IN_BODY;
  4148. break;
  4149. }
  4150. }
  4151. }
  4152. private function closeCell()
  4153. {
  4154. /* If the stack of open elements has a td or th element in table scope,
  4155. then act as if an end tag token with that tag name had been seen. */
  4156. foreach (array('td', 'th') as $cell) {
  4157. if ($this->elementInScope($cell, true)) {
  4158. $this->inCell(
  4159. array(
  4160. 'name' => $cell,
  4161. 'type' => HTML5::ENDTAG
  4162. )
  4163. );
  4164. break;
  4165. }
  4166. }
  4167. }
  4168. public function save()
  4169. {
  4170. return $this->dom;
  4171. }
  4172. }