31
31
// XERROR - with regards to parse errors
32
32
// XSCRIPT - with regards to scripting mode
33
33
// XENCODING - with regards to encoding (for reparsing tests)
34
+ // XDOM - DOM specific code (tagName is explicitly not marked).
35
+ // this is not (yet) in helper functions.
34
36
35
37
class HTML5_TreeBuilder {
36
38
public $ stack =array ();
@@ -205,6 +207,7 @@ public function emitToken($token, $mode = null) {
205
207
* doctype attribute of the Document object. */
206
208
if (!isset ($ token ['public ' ]))$ token ['public ' ] =null ;
207
209
if (!isset ($ token ['system ' ]))$ token ['system ' ] =null ;
210
+ // XDOM
208
211
// Yes this is hacky. I'm kind of annoyed that I can't appendChild
209
212
// a doctype to DOMDocument. Maybe I haven't chanted the right
210
213
// syllables.
@@ -367,6 +370,7 @@ public function emitToken($token, $mode = null) {
367
370
}elseif ($ token ['type ' ] === HTML5_Tokenizer::COMMENT ) {
368
371
/* Append a Comment node to the Document object with the data
369
372
attribute set to the data given in the comment token. */
373
+ // XDOM
370
374
$ comment =$ this ->dom ->createComment ($ token ['data ' ]);
371
375
$ this ->dom ->appendChild ($ comment );
372
376
@@ -382,6 +386,7 @@ public function emitToken($token, $mode = null) {
382
386
/* Create an element for the token in the HTML namespace. Append it
383
387
* to the Document object. Put this element in the stack of open
384
388
* elements. */
389
+ // XDOM
385
390
$ html =$ this ->insertElement ($ token ,false );
386
391
$ this ->dom ->appendChild ($ html );
387
392
$ this ->stack [] =$ html ;
@@ -391,6 +396,7 @@ public function emitToken($token, $mode = null) {
391
396
}else {
392
397
/* Create an html element. Append it to the Document object. Put
393
398
* this element in the stack of open elements. */
399
+ // XDOM
394
400
$ html =$ this ->dom ->createElementNS (self ::NS_HTML ,'html ' );
395
401
$ this ->dom ->appendChild ($ html );
396
402
$ this ->stack [] =$ html ;
@@ -1748,6 +1754,7 @@ public function emitToken($token, $mode = null) {
1748
1754
* elements with an entry for the new element, and
1749
1755
* let node be the new element. */
1750
1756
// we don't know what the token is anymore
1757
+ // XDOM
1751
1758
$ clone =$ node ->cloneNode ();
1752
1759
$ a_pos =array_search ($ node ,$ this ->a_formatting ,true );
1753
1760
$ s_pos =array_search ($ node ,$ this ->stack ,true );
@@ -1757,10 +1764,12 @@ public function emitToken($token, $mode = null) {
1757
1764
1758
1765
/* 6.6 Insert last node into node, first removing
1759
1766
it from its previous parent node if any. */
1767
+ // XDOM
1760
1768
if ($ last_node ->parentNode !==null ) {
1761
1769
$ last_node ->parentNode ->removeChild ($ last_node );
1762
1770
}
1763
1771
1772
+ // XDOM
1764
1773
$ node ->appendChild ($ last_node );
1765
1774
1766
1775
/* 6.7 Let last node be node. */
@@ -1774,6 +1783,7 @@ public function emitToken($token, $mode = null) {
1774
1783
* whatever last node ended up being in the previous
1775
1784
* step, first removing it from its previous parent
1776
1785
* node if any. */
1786
+ // XDOM
1777
1787
if ($ last_node ->parentNode ) {// common step
1778
1788
$ last_node ->parentNode ->removeChild ($ last_node );
1779
1789
}
@@ -1784,23 +1794,27 @@ public function emitToken($token, $mode = null) {
1784
1794
* first removing it from its previous parent node if
1785
1795
* any. */
1786
1796
}else {
1797
+ // XDOM
1787
1798
$ common_ancestor ->appendChild ($ last_node );
1788
1799
}
1789
1800
1790
1801
/* 8. Create an element for the token for which the
1791
1802
* formatting element was created. */
1803
+ // XDOM
1792
1804
$ clone =$ formatting_element ->cloneNode ();
1793
1805
1794
1806
/* 9. Take all of the child nodes of the furthest
1795
1807
block and append them to the element created in the
1796
1808
last step. */
1809
+ // XDOM
1797
1810
while ($ furthest_block ->hasChildNodes ()) {
1798
1811
$ child =$ furthest_block ->firstChild ;
1799
1812
$ furthest_block ->removeChild ($ child );
1800
1813
$ clone ->appendChild ($ child );
1801
1814
}
1802
1815
1803
1816
/* 10. Append that clone to the furthest block. */
1817
+ // XDOM
1804
1818
$ furthest_block ->appendChild ($ clone );
1805
1819
1806
1820
/* 11. Remove the formatting element from the list
@@ -2753,6 +2767,7 @@ public function emitToken($token, $mode = null) {
2753
2767
// XERROR: parse error
2754
2768
}elseif ($ token ['type ' ] === HTML5_Tokenizer::ENDTAG &&
2755
2769
$ token ['name ' ] ==='script ' &&end ($ this ->stack )->tagName ==='script ' &&
2770
+ // XDOM
2756
2771
end ($ this ->stack )->namespaceURI ===self ::NS_SVG ) {
2757
2772
array_pop ($ this ->stack );
2758
2773
// a bunch of script running mumbo jumbo
@@ -2761,20 +2776,23 @@ public function emitToken($token, $mode = null) {
2761
2776
((
2762
2777
$ token ['name ' ] !=='mglyph ' &&
2763
2778
$ token ['name ' ] !=='malignmark ' &&
2779
+ // XDOM
2764
2780
end ($ this ->stack )->namespaceURI ===self ::NS_MATHML &&
2765
2781
in_array (end ($ this ->stack )->tagName ,array ('mi ' ,'mo ' ,'mn ' ,'ms ' ,'mtext ' ))
2766
2782
) ||
2767
2783
(
2768
2784
$ token ['name ' ] ==='svg ' &&
2785
+ // XDOM
2769
2786
end ($ this ->stack )->namespaceURI ===self ::NS_MATHML &&
2770
2787
end ($ this ->stack )->tagName ==='annotation-xml '
2771
2788
) ||
2772
2789
(
2790
+ // XDOM
2773
2791
end ($ this ->stack )->namespaceURI ===self ::NS_SVG &&
2774
2792
in_array (end ($ this ->stack )->tagName ,array ('foreignObject ' ,'desc ' ,'title ' ))
2775
2793
) ||
2776
2794
(
2777
- // XSKETCHY
2795
+ // XSKETCHY && XDOM
2778
2796
end ($ this ->stack )->namespaceURI ===self ::NS_HTML
2779
2797
))
2780
2798
) ||$ token ['type ' ] === HTML5_Tokenizer::ENDTAG
@@ -2788,6 +2806,7 @@ public function emitToken($token, $mode = null) {
2788
2806
$ found =false ;
2789
2807
// this basically duplicates elementInScope()
2790
2808
for ($ i =count ($ this ->stack ) -1 ;$ i >=0 ;$ i --) {
2809
+ // XDOM
2791
2810
$ node =$ this ->stack [$ i ];
2792
2811
if ($ node ->namespaceURI !==self ::NS_HTML ) {
2793
2812
$ found =true ;
@@ -2815,6 +2834,7 @@ public function emitToken($token, $mode = null) {
2815
2834
// XERROR: parse error
2816
2835
do {
2817
2836
$ node =array_pop ($ this ->stack );
2837
+ // XDOM
2818
2838
}while ($ node ->namespaceURI !==self ::NS_HTML );
2819
2839
$ this ->stack [] =$ node ;
2820
2840
$ this ->mode =$ this ->secondary_mode ;
@@ -2858,6 +2878,7 @@ public function emitToken($token, $mode = null) {
2858
2878
'radialgradient ' =>'radialGradient ' ,
2859
2879
'textpath ' =>'textPath ' ,
2860
2880
);
2881
+ // XDOM
2861
2882
$ current =end ($ this ->stack );
2862
2883
if ($ current ->namespaceURI ===self ::NS_MATHML ) {
2863
2884
$ token =$ this ->adjustMathMLAttributes ($ token );
@@ -2894,6 +2915,7 @@ public function emitToken($token, $mode = null) {
2894
2915
/* Append a Comment node to the first element in the stack of open
2895
2916
elements (the html element), with the data attribute set to the
2896
2917
data given in the comment token. */
2918
+ // XDOM
2897
2919
$ comment =$ this ->dom ->createComment ($ token ['data ' ]);
2898
2920
$ this ->stack [0 ]->appendChild ($ comment );
2899
2921
@@ -3044,6 +3066,7 @@ public function emitToken($token, $mode = null) {
3044
3066
if ($ token ['type ' ] === HTML5_Tokenizer::COMMENT ) {
3045
3067
/* Append a Comment node to the Document object with the data
3046
3068
attribute set to the data given in the comment token. */
3069
+ // XDOM
3047
3070
$ comment =$ this ->dom ->createComment ($ token ['data ' ]);
3048
3071
$ this ->dom ->appendChild ($ comment );
3049
3072
@@ -3067,6 +3090,7 @@ public function emitToken($token, $mode = null) {
3067
3090
if ($ token ['type ' ] === HTML5_Tokenizer::COMMENT ) {
3068
3091
/* Append a Comment node to the Document object with the data
3069
3092
attribute set to the data given in the comment token. */
3093
+ // XDOM
3070
3094
$ comment =$ this ->dom ->createComment ($ token ['data ' ]);
3071
3095
$ this ->dom ->appendChild ($ comment );
3072
3096