Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

PH5P.php 158KB

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