1
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2025-07-31 19:30:21 +02:00

[2.1.0] Fix bug in auto-paragraphing: empty tags should be treated like start tags too.

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1269 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang
2007-06-29 00:24:59 +00:00
parent 02051e465c
commit 2e7e411491
4 changed files with 38 additions and 31 deletions

View File

@@ -103,9 +103,9 @@ class HTMLPurifier_Injector
function handleText(&$token) {} function handleText(&$token) {}
/** /**
* Handler that is called when a start token is processed * Handler that is called when a start or empty token is processed
*/ */
function handleStart(&$token) {} function handleElement(&$token) {}
} }

View File

@@ -87,7 +87,7 @@ class HTMLPurifier_Injector_AutoParagraph extends HTMLPurifier_Injector
} }
function handleStart(&$token) { function handleElement(&$token) {
// check if we're inside a tag already // check if we're inside a tag already
if (!empty($this->currentNesting)) { if (!empty($this->currentNesting)) {
if ($this->allowsElement('p')) { if ($this->allowsElement('p')) {
@@ -113,8 +113,8 @@ class HTMLPurifier_Injector_AutoParagraph extends HTMLPurifier_Injector
$ok = false; $ok = false;
// maintain a mini-nesting counter, this lets us bail out // maintain a mini-nesting counter, this lets us bail out
// early if possible // early if possible
$j = 2; // current nesting, is two due to parent and this start $j = 1; // current nesting, one is due to parent (we recalculate current token)
for ($i = $this->inputIndex + 1; isset($this->inputTokens[$i]); $i++) { for ($i = $this->inputIndex; isset($this->inputTokens[$i]); $i++) {
if ($this->inputTokens[$i]->type == 'start') $j++; if ($this->inputTokens[$i]->type == 'start') $j++;
if ($this->inputTokens[$i]->type == 'end') $j--; if ($this->inputTokens[$i]->type == 'end') $j--;
if ($this->inputTokens[$i]->type == 'text') { if ($this->inputTokens[$i]->type == 'text') {

View File

@@ -127,26 +127,24 @@ class HTMLPurifier_Strategy_MakeWellFormed extends HTMLPurifier_Strategy
$info = $definition->info[$token->name]->child; $info = $definition->info[$token->name]->child;
// quick checks: // quick tag checks: anything that's *not* an end tag
// test if it claims to be a start tag but is empty $ok = false;
if ($info->type == 'empty' && $token->type == 'start') { if ($info->type == 'empty' && $token->type == 'start') {
$result[] = new HTMLPurifier_Token_Empty($token->name, $token->attr); // test if it claims to be a start tag but is empty
continue; $token = new HTMLPurifier_Token_Empty($token->name, $token->attr);
} $ok = true;
// test if it claims to be empty but really is a start tag } elseif ($info->type != 'empty' && $token->type == 'empty' ) {
if ($info->type != 'empty' && $token->type == 'empty' ) { // claims to be empty but really is a start tag
$result[] = new HTMLPurifier_Token_Start($token->name, $token->attr); $token = array(
$result[] = new HTMLPurifier_Token_End($token->name); new HTMLPurifier_Token_Start($token->name, $token->attr),
continue; new HTMLPurifier_Token_End($token->name)
} );
// automatically insert empty tags $ok = true;
if ($token->type == 'empty') { } elseif ($token->type == 'empty') {
$result[] = $token; // real empty token
continue; $ok = true;
} } elseif ($token->type == 'start') {
// start tag
// start tags have precedence, so they get passed through...
if ($token->type == 'start') {
// ...unless they also have to close their parent // ...unless they also have to close their parent
if (!empty($this->currentNesting)) { if (!empty($this->currentNesting)) {
@@ -168,16 +166,18 @@ class HTMLPurifier_Strategy_MakeWellFormed extends HTMLPurifier_Strategy
$this->currentNesting[] = $parent; // undo the pop $this->currentNesting[] = $parent; // undo the pop
} }
$ok = true;
// injector handler code; duplicated for performance reasons }
// injector handler code; duplicated for performance reasons
if ($ok) {
foreach ($this->injectors as $i => $x) { foreach ($this->injectors as $i => $x) {
if (!$x->skip) $x->handleStart($token); if (!$x->skip) $x->handleElement($token);
if (is_array($token)) { if (is_array($token)) {
$this->currentInjector = $i; $this->currentInjector = $i;
break; break;
} }
} }
$this->processToken($token, $config, $context); $this->processToken($token, $config, $context);
continue; continue;
} }
@@ -285,9 +285,11 @@ class HTMLPurifier_Strategy_MakeWellFormed extends HTMLPurifier_Strategy
array_splice($this->inputTokens, $this->inputIndex--, 1, $token); array_splice($this->inputTokens, $this->inputIndex--, 1, $token);
// adjust the injector skips based on the array substitution // adjust the injector skips based on the array substitution
$offset = count($token) + 1; if ($this->injectors) {
for ($i = 0; $i <= $this->currentInjector; $i++) { $offset = count($token) + 1;
$this->injectors[$i]->skip += $offset; for ($i = 0; $i <= $this->currentInjector; $i++) {
$this->injectors[$i]->skip += $offset;
}
} }
} elseif ($token) { } elseif ($token) {
// regular case // regular case

View File

@@ -237,6 +237,11 @@ Par1
'<p>Par1</p><p><b>Par2</b></p>' '<p>Par1</p><p><b>Par2</b></p>'
); );
$this->assertResult(
'<img /> Foo',
'<p><img /> Foo</p>'
);
} }
function testInlineRootNode() { function testInlineRootNode() {