0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2025-01-03 05:11:52 +00:00

Increase test coverage, and modify handleEnd behavior to only see correct tokens.

Previously, handleEnd was called for any end tag, except ones that were obviously
spurious because there were no parent tags. Now, it is only called for end tags
that are "approved." If an injector operates on the end tag, we automatically
punt. There may be some optimizations that could be made to this procedure,
but for now it's much more consistent.

Signed-off-by: Edward Z. Yang <edwardzyang@thewritingpot.com>
This commit is contained in:
Edward Z. Yang 2008-10-01 15:40:31 -04:00
parent 1555cb617f
commit 70515dd48f
8 changed files with 166 additions and 10 deletions

View File

@ -274,20 +274,23 @@ class HTMLPurifier_Strategy_MakeWellFormed extends HTMLPurifier_Strategy
$reprocess = true;
continue;
}
foreach ($this->injectors as $i => $injector) {
if (isset($token->skip[$i])) continue;
if ($token->rewind !== null && $token->rewind !== $i) continue;
$injector->handleEnd($token);
$this->processToken($token, $i);
$reprocess = true;
break;
}
if ($reprocess) continue;
// first, check for the simplest case: everything closes neatly
// first, check for the simplest case: everything closes neatly.
// Eventually, everything passes through here; if there are problems
// we modify the input stream accordingly and then punt, so that
// the tokens get processed again.
$current_parent = array_pop($this->stack);
if ($current_parent->name == $token->name) {
$token->start = $current_parent;
foreach ($this->injectors as $i => $injector) {
if (isset($token->skip[$i])) continue;
if ($token->rewind !== null && $token->rewind !== $i) continue;
$injector->handleEnd($token);
$this->processToken($token, $i);
$this->stack[] = $current_parent;
$reprocess = true;
break;
}
continue;
}
@ -343,6 +346,7 @@ class HTMLPurifier_Strategy_MakeWellFormed extends HTMLPurifier_Strategy
$this->insertBefore($new_token);
}
$reprocess = true;
continue;
}
$context->destroy('CurrentNesting');

View File

@ -0,0 +1,16 @@
<?php
class HTMLPurifier_Strategy_MakeWellFormed_EndInsertInjector extends HTMLPurifier_Injector
{
public $name = 'EndInsertInjector';
public $needed = array('span');
public function handleEnd(&$token) {
if ($token->name == 'div') return;
$token = array(
new HTMLPurifier_Token_Start('b'),
new HTMLPurifier_Token_Text('Comment'),
new HTMLPurifier_Token_End('b'),
$token
);
}
}

View File

@ -0,0 +1,37 @@
<?php
class HTMLPurifier_Strategy_MakeWellFormed_EndInsertInjectorTest extends HTMLPurifier_StrategyHarness
{
function setUp() {
parent::setUp();
$this->obj = new HTMLPurifier_Strategy_MakeWellFormed();
$this->config->set('AutoFormat', 'Custom', array(
new HTMLPurifier_Strategy_MakeWellFormed_EndInsertInjector()
));
}
function testEmpty() {
$this->assertResult('');
}
function testNormal() {
$this->assertResult('<i>Foo</i>', '<i>Foo<b>Comment</b></i>');
}
function testEndOfDocumentProcessing() {
$this->assertResult('<i>Foo', '<i>Foo<b>Comment</b></i>');
}
function testDoubleEndOfDocumentProcessing() {
$this->assertResult('<i><i>Foo', '<i><i>Foo<b>Comment</b></i><b>Comment</b></i>');
}
function testEndOfNodeProcessing() {
$this->assertResult('<div><i>Foo</div>', '<div><i>Foo<b>Comment</b></i></div>');
}
function testEmptyToStartEndProcessing() {
$this->assertResult('<i />', '<i><b>Comment</b></i>');
}
function testSpuriousEndTag() {
$this->assertResult('</i>', '');
}
function testLessButStillSpuriousEndTag() {
$this->assertResult('<div></i></div>', '<div></div>');
}
}

View File

@ -0,0 +1,27 @@
<?php
class HTMLPurifier_Strategy_MakeWellFormed_EndRewindInjector extends HTMLPurifier_Injector
{
public $name = 'EndRewindInjector';
public $needed = array('span');
public function handleElement(&$token) {
if (isset($token->_InjectorTest_EndRewindInjector_delete)) {
$token = false;
}
}
public function handleText(&$token) {
$token = false;
}
public function handleEnd(&$token) {
$i = null;
if (
$this->backward($i, $prev) &&
$prev instanceof HTMLPurifier_Token_Start &&
$prev->name == 'span'
) {
$token = false;
$prev->_InjectorTest_EndRewindInjector_delete = true;
$this->rewind($i);
}
}
}

View File

@ -0,0 +1,32 @@
<?php
class HTMLPurifier_Strategy_MakeWellFormed_EndRewindInjectorTest extends HTMLPurifier_StrategyHarness
{
function setUp() {
parent::setUp();
$this->obj = new HTMLPurifier_Strategy_MakeWellFormed();
$this->config->set('AutoFormat', 'Custom', array(
new HTMLPurifier_Strategy_MakeWellFormed_EndRewindInjector()
));
}
function testBasic() {
$this->assertResult('');
}
function testFunction() {
$this->assertResult('<span>asdf</span>','');
}
function testFailedFunction() {
$this->assertResult('<span>asd<b>asdf</b>asdf</span>','<span><b></b></span>');
}
function testPadded() {
$this->assertResult('<b></b><span>asdf</span><b></b>','<b></b><b></b>');
}
function testDoubled() {
$this->config->set('AutoFormat', 'Custom', array(
new HTMLPurifier_Strategy_MakeWellFormed_EndRewindInjector(),
new HTMLPurifier_Strategy_MakeWellFormed_EndRewindInjector(),
));
$this->assertResult('<b></b><span>asdf</span>', '<b></b>');
}
}

View File

@ -0,0 +1,10 @@
<?php
class HTMLPurifier_Strategy_MakeWellFormed_SkipInjector extends HTMLPurifier_Injector
{
public $name = 'EndRewindInjector';
public $needed = array('span');
public function handleElement(&$token) {
$token = array(clone $token, clone $token);
}
}

View File

@ -0,0 +1,26 @@
<?php
class HTMLPurifier_Strategy_MakeWellFormed_SkipInjectorTest extends HTMLPurifier_StrategyHarness
{
function setUp() {
parent::setUp();
$this->obj = new HTMLPurifier_Strategy_MakeWellFormed();
$this->config->set('AutoFormat', 'Custom', array(
new HTMLPurifier_Strategy_MakeWellFormed_SkipInjector()
));
}
function testEmpty() {
$this->assertResult('');
}
function testMultiply() {
$this->assertResult('<br />', '<br /><br />');
}
function testMultiplyMultiply() {
$this->config->set('AutoFormat', 'Custom', array(
new HTMLPurifier_Strategy_MakeWellFormed_SkipInjector(),
new HTMLPurifier_Strategy_MakeWellFormed_SkipInjector()
));
$this->assertResult('<br />', '<br /><br /><br /><br />');
}
}

View File

@ -16,9 +16,13 @@ class HTMLPurifier_Strategy_MakeWellFormed_InjectorTest extends HTMLPurifier_Str
$mock = new HTMLPurifier_InjectorMock();
$b = new HTMLPurifier_Token_End('b');
$b->skip = array(0 => true);
$b->start = new HTMLPurifier_Token_Start('b');
$b->start->skip = array(0 => true, 1 => true);
$mock->expectAt(0, 'handleEnd', array($b));
$i = new HTMLPurifier_Token_End('i');
$i->start = new HTMLPurifier_Token_Start('i');
$i->skip = array(0 => true);
$i->start->skip = array(0 => true, 1 => true);
$mock->expectAt(1, 'handleEnd', array($i));
$mock->expectCallCount('handleEnd', 2);
$mock->setReturnValue('getRewind', false);