PH5P.php 178 KB

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