0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2024-11-09 23:28:42 +00:00

[1.1.1] As far as possible, preserve whitespace is table internals.

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@445 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang 2006-09-24 02:08:18 +00:00
parent 6c04bbdac1
commit 1ad55e0ed5
2 changed files with 30 additions and 9 deletions

View File

@ -327,6 +327,8 @@ class HTMLPurifier_ChildDef_Table extends HTMLPurifier_ChildDef
$is_collecting = false; // are we globbing together tokens to package $is_collecting = false; // are we globbing together tokens to package
// into one of the collectors? // into one of the collectors?
$collection = array(); // collected nodes $collection = array(); // collected nodes
$tag_index = 0; // the first node might be whitespace,
// so this tells us where the start tag is
foreach ($tokens_of_children as $token) { foreach ($tokens_of_children as $token) {
$is_child = ($nesting == 0); $is_child = ($nesting == 0);
@ -344,7 +346,7 @@ class HTMLPurifier_ChildDef_Table extends HTMLPurifier_ChildDef
if ($is_child) { if ($is_child) {
// okay, let's stash the tokens away // okay, let's stash the tokens away
// first token tells us the type of the collection // first token tells us the type of the collection
switch ($collection[0]->name) { switch ($collection[$tag_index]->name) {
case 'tr': case 'tr':
case 'tbody': case 'tbody':
$content[] = $collection; $content[] = $collection;
@ -356,13 +358,13 @@ class HTMLPurifier_ChildDef_Table extends HTMLPurifier_ChildDef
case 'thead': case 'thead':
case 'tfoot': case 'tfoot':
// access the appropriate variable, $thead or $tfoot // access the appropriate variable, $thead or $tfoot
$var = $collection[0]->name; $var = $collection[$tag_index]->name;
if ($$var === false) { if ($$var === false) {
$$var = $collection; $$var = $collection;
} else { } else {
// transmutate the first and less entries into // transmutate the first and less entries into
// tbody tags, and then put into content // tbody tags, and then put into content
$collection[0]->name = 'tbody'; $collection[$tag_index]->name = 'tbody';
$collection[count($collection)-1]->name = 'tbody'; $collection[count($collection)-1]->name = 'tbody';
$content[] = $collection; $content[] = $collection;
} }
@ -373,6 +375,7 @@ class HTMLPurifier_ChildDef_Table extends HTMLPurifier_ChildDef
} }
$collection = array(); $collection = array();
$is_collecting = false; $is_collecting = false;
$tag_index = 0;
} else { } else {
// add the node to the collection // add the node to the collection
$collection[] = $token; $collection[] = $token;
@ -387,7 +390,9 @@ class HTMLPurifier_ChildDef_Table extends HTMLPurifier_ChildDef
if ($token->name == 'col') { if ($token->name == 'col') {
// the only empty tag in the possie, we can handle it // the only empty tag in the possie, we can handle it
// immediately // immediately
$cols[] = array($token); $cols[] = array_merge($collection, array($token));
$collection = array();
$tag_index = 0;
continue; continue;
} }
switch($token->name) { switch($token->name) {
@ -401,7 +406,10 @@ class HTMLPurifier_ChildDef_Table extends HTMLPurifier_ChildDef
$collection[] = $token; $collection[] = $token;
continue; continue;
default: default:
// unrecognized, drop silently if ($token->type == 'text' && $token->is_whitespace) {
$collection[] = $token;
$tag_index++;
}
continue; continue;
} }
} }
@ -415,6 +423,10 @@ class HTMLPurifier_ChildDef_Table extends HTMLPurifier_ChildDef
if ($thead !== false) $ret = array_merge($ret, $thead); if ($thead !== false) $ret = array_merge($ret, $thead);
if ($tfoot !== false) $ret = array_merge($ret, $tfoot); if ($tfoot !== false) $ret = array_merge($ret, $tfoot);
foreach ($content as $token_array) $ret = array_merge($ret, $token_array); foreach ($content as $token_array) $ret = array_merge($ret, $token_array);
if (!empty($collection) && $is_collecting == false){
// grab the trailing space
$ret = array_merge($ret, $collection);
}
array_pop($tokens_of_children); // remove phantom token array_pop($tokens_of_children); // remove phantom token
@ -423,4 +435,4 @@ class HTMLPurifier_ChildDef_Table extends HTMLPurifier_ChildDef
} }
} }
?> ?>

View File

@ -1,7 +1,7 @@
<?php <?php
require_once 'HTMLPurifier/ChildDef.php'; require_once 'HTMLPurifier/ChildDef.php';
require_once 'HTMLPurifier/Lexer.php'; require_once 'HTMLPurifier/Lexer/DirectLex.php';
require_once 'HTMLPurifier/Generator.php'; require_once 'HTMLPurifier/Generator.php';
class HTMLPurifier_ChildDefTest extends UnitTestCase class HTMLPurifier_ChildDefTest extends UnitTestCase
@ -12,7 +12,8 @@ class HTMLPurifier_ChildDefTest extends UnitTestCase
var $gen; var $gen;
function HTMLPurifier_ChildDefTest() { function HTMLPurifier_ChildDefTest() {
$this->lex = HTMLPurifier_Lexer::create(); // it is vital that the tags be treated as literally as possible
$this->lex = new HTMLPurifier_Lexer_DirectLex();
$this->gen = new HTMLPurifier_Generator(); $this->gen = new HTMLPurifier_Generator();
parent::UnitTestCase(); parent::UnitTestCase();
} }
@ -98,6 +99,14 @@ class HTMLPurifier_ChildDefTest extends UnitTestCase
$inputs[6] = 'foo'; $inputs[6] = 'foo';
$expect[6] = false; $expect[6] = false;
// whitespace sticks to the previous element, last whitespace is
// stationary
$inputs[7] = "\n <tr />\n <tr />\n ";
$expect[7] = true;
$inputs[8] = "\n\t<tbody />\n\t\t<tfoot />\n\t\t\t";
$expect[8] = "\n\t\t<tfoot />\n\t<tbody />\n\t\t\t";
$this->assertSeries($inputs, $expect, $config); $this->assertSeries($inputs, $expect, $config);
} }
@ -209,4 +218,4 @@ class HTMLPurifier_ChildDefTest extends UnitTestCase
} }
?> ?>