8000 Fix foster parenting bug, and current node bug. Improve debugging fac… · Nimbleworks/html5lib-php@6b6ea0b · GitHub
[go: up one dir, main page]

Skip to content

Commit 6b6ea0b

Browse files
author
Edward Z. Yang ext:(%22)
committed
Fix foster parenting bug, and current node bug. Improve debugging facilities.
1 parent 120e332 commit 6b6ea0b

File tree

1 file changed

+30
-17
lines changed

1 file changed

+30
-17
lines changed

library/HTML5/TreeConstructer.php

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,10 @@ public function emitToken($token, $mode = null) {
140140
if ($backtrace[1]['class'] !== 'HTML5_TreeConstructer') echo "--\n";
141141
echo $this->strConst($mode) . "\n ";
142142
token_dump($token);
143+
$this->printStack();
144+
$this->printActiveFormattingElements();
143145
if ($this->foster_parent) echo " -> this is a foster parent mode\n";
144-
*/
146+
*/
145147

146148
if ($this->ignore_lf_token) $this->ignore_lf_token--;
147149
$this->ignored = false;
@@ -1933,18 +1935,10 @@ public function emitToken($token, $mode = null) {
19331935
/* Parse error. Process the token as if the insertion mode was "in
19341936
body", with the following exception: */
19351937

1936-
/* If the current node is a table, tbody, tfoot, thead, or tr
1937-
element, then, whenever a node would be inserted into the current
1938-
node, it must instead be inserted into the foster parent element. */
1939-
if(in_array(end($this->stack)->tagName,
1940-
array('table', 'tbody', 'tfoot', 'thead', 'tr'))) {
1941-
$old = $this->foster_parent;
1942-
$this->foster_parent = true;
1943-
$this->processWithRulesFor($token, self::IN_BODY);
1944-
$this->foster_parent = $old;
1945-
} else {
1946-
$this->processWithRulesFor($token, self::IN_BODY);
1947-
}
1938+
$old = $this->foster_parent;
1939+
$this->foster_parent = true;
1940+
$this->processWithRulesFor($token, self::IN_BODY);
1941+
$this->foster_parent = $old;
19481942
}
19491943
break;
19501944

@@ -2788,7 +2782,12 @@ private function insertComment($data) {
27882782
}
27892783

27902784
private function appendToRealParent($node) {
2791-
if(!$this->foster_parent) {
2785+
// this is only for the foster_parent case
2786+
/* If the current node is a table, tbody, tfoot, thead, or tr
2787+
element, then, whenever a node would be inserted into the current
2788+
node, it must instead be inserted into the foster parent element. */
2789+
if(!$this->foster_parent || !in_array(end($this->stack)->tagName,
2790+
array('table', 'tbody', 'tfoot', 'thead', 'tr'))) {
27922791
$this->appendChild(end($this->stack), $node);
27932792
} else {
27942793
$this->fosterParent($node);
@@ -2910,7 +2909,7 @@ private function reconstructActiveFormattingElements() {
29102909

29112910
/* 9. Append clone to the current node and push it onto the stack
29122911
of open elements so that it is the new current node. */
2913-
end($this->stack)->appendChild($clone);
2912+
$this->appendToRealParent($clone);
29142913
$this->stack[] = $clone;
29152914

29162915
/* 10. Replace the entry for entry in the list with an entry for
@@ -3209,10 +3208,24 @@ public function fosterParent($node) {
32093208
* For debugging, prints the stack
32103209
*/
32113210
private function printStack() {
3212-
echo " Stack:\n";
3211+
$names = array();
32133212
foreach ($this->stack as $i => $element) {
3214-
echo " " . ($i+1) . ". " . $element->tagName . "\n";
3213+
$names[] = $element->tagName;
3214+
}
3215+
echo " -> stack [" . implode(', ', $names) . "]\n";
3216+
}
3217+
3218+
/**
3219+
* For debugging, prints active formatting elements
3220+
*/
3221+
private function printActiveFormattingElements() {
3222+
if (!$this->a_formatting) return;
3223+
$names = array();
3224+
foreach ($this->a_formatting as $node) {
3225+
if ($node === self::MARKER) $names[] = 'MARKER';
3226+
else $names[] = $node->tagName;
32153227
}
3228+
echo " -> active formatting [" . implode(', ', $names) . "]\n";
32163229
}
32173230

32183231
public function currentTableIsTainted() {

0 commit comments

Comments
 (0)
0