1
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2025-08-02 12:21:09 +02:00

Minor test-case refactoring.

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1100 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang
2007-05-27 23:12:17 +00:00
parent f758f7c534
commit ee61ffc0d9
5 changed files with 81 additions and 52 deletions

View File

@@ -42,13 +42,15 @@ class HTMLPurifier_Strategy_FixNesting extends HTMLPurifier_Strategy
$definition = $config->getHTMLDefinition(); $definition = $config->getHTMLDefinition();
// insert implicit "parent" node, will be removed at end. // insert implicit "parent" node, will be removed at end.
// ! we might want to move this to configuration
// DEFINITION CALL // DEFINITION CALL
$parent_name = $definition->info_parent; $parent_name = $definition->info_parent;
array_unshift($tokens, new HTMLPurifier_Token_Start($parent_name)); array_unshift($tokens, new HTMLPurifier_Token_Start($parent_name));
$tokens[] = new HTMLPurifier_Token_End($parent_name); $tokens[] = new HTMLPurifier_Token_End($parent_name);
// setup the context variables // setup the context variable 'IsInline', for chameleon processing
// is 'false' when we are not inline, 'true' when it must always
// be inline, and an integer when it is inline for a certain
// branch of the document tree
$is_inline = $definition->info_parent_def->descendants_are_inline; $is_inline = $definition->info_parent_def->descendants_are_inline;
$context->register('IsInline', $is_inline); $context->register('IsInline', $is_inline);
@@ -60,8 +62,9 @@ class HTMLPurifier_Strategy_FixNesting extends HTMLPurifier_Strategy
$stack = array(); $stack = array();
// stack that contains all elements that are excluded // stack that contains all elements that are excluded
// same structure as $stack, but it is only populated when an element // it is organized by parent elements, similar to $stack,
// with exclusions is processed, i.e. there won't be empty exclusions. // but it is only populated when an element with exclusions is
// processed, i.e. there won't be empty exclusions.
$exclude_stack = array(); $exclude_stack = array();
//####################################################################// //####################################################################//
@@ -110,7 +113,10 @@ class HTMLPurifier_Strategy_FixNesting extends HTMLPurifier_Strategy
$parent_def = $definition->info[$parent_name]; $parent_def = $definition->info[$parent_name];
} }
} else { } else {
// unknown info, it won't be used anyway // processing as if the parent were the "root" node
// unknown info, it won't be used anyway, in the future,
// we may want to enforce one element only (this is
// necessary for HTML Purifier to clean entire documents
$parent_index = $parent_name = $parent_def = null; $parent_index = $parent_name = $parent_def = null;
} }
@@ -207,6 +213,12 @@ class HTMLPurifier_Strategy_FixNesting extends HTMLPurifier_Strategy
// current node is now the next possible start node // current node is now the next possible start node
// unless it turns out that we need to do a double-check // unless it turns out that we need to do a double-check
// this is a rought heuristic that covers 100% of HTML's
// cases and 99% of all other cases. A child definition
// that would be tricked by this would be something like:
// ( | a b c) where it's all or nothing. Fortunantely,
// our current implementation claims that that case would
// not allow empty, even if it did
if (!$parent_def->child->allow_empty) { if (!$parent_def->child->allow_empty) {
// we need to do a double-check // we need to do a double-check
$i = $parent_index; $i = $parent_index;

View File

@@ -62,6 +62,8 @@ class HTMLPurifier_Strategy_MakeWellFormed extends HTMLPurifier_Strategy
$parent_name = $parent->name; $parent_name = $parent->name;
$parent_info = $definition->info[$parent_name]; $parent_info = $definition->info[$parent_name];
// we need to replace this with a more general
// algorithm
if (isset($parent_info->auto_close[$token->name])) { if (isset($parent_info->auto_close[$token->name])) {
$result[] = new HTMLPurifier_Token_End($parent_name); $result[] = new HTMLPurifier_Token_End($parent_name);
$result[] = $token; $result[] = $token;

View File

@@ -11,15 +11,12 @@ class HTMLPurifier_Strategy_FixNestingTest extends HTMLPurifier_StrategyHarness
$this->obj = new HTMLPurifier_Strategy_FixNesting(); $this->obj = new HTMLPurifier_Strategy_FixNesting();
} }
function test() { function testBlockAndInlineIntegration() {
$this->config = array('HTML.Doctype' => 'XHTML 1.0 Strict');
// legal inline // legal inline
$this->assertResult('<b>Bold text</b>'); $this->assertResult('<b>Bold text</b>');
// legal inline and block // legal inline and block (default parent element is FLOW)
// as the parent element is considered FLOW
$this->assertResult('<a href="about:blank">Blank</a><div>Block</div>'); $this->assertResult('<a href="about:blank">Blank</a><div>Block</div>');
// illegal block in inline // illegal block in inline
@@ -35,6 +32,10 @@ class HTMLPurifier_Strategy_FixNestingTest extends HTMLPurifier_StrategyHarness
array('Core.EscapeInvalidChildren' => true) array('Core.EscapeInvalidChildren' => true)
); );
}
function testNodeRemovalIntegration() {
// test of empty set that's required, resulting in removal of node // test of empty set that's required, resulting in removal of node
$this->assertResult('<ul></ul>', ''); $this->assertResult('<ul></ul>', '');
@@ -44,27 +45,17 @@ class HTMLPurifier_Strategy_FixNestingTest extends HTMLPurifier_StrategyHarness
'<ul><li>Legal item</li></ul>' '<ul><li>Legal item</li></ul>'
); );
}
function testTableIntegration() {
// test custom table definition // test custom table definition
$this->assertResult( $this->assertResult(
'<table><tr><td>Cell 1</td></tr></table>',
'<table><tr><td>Cell 1</td></tr></table>' '<table><tr><td>Cell 1</td></tr></table>'
); );
$this->assertResult('<table></table>', ''); $this->assertResult('<table></table>', '');
}
// breaks without the redundant checking code
$this->assertResult('<table><tr></tr></table>', ''); function testChameleonIntegration() {
// special case, prevents scrolling one back to find parent
$this->assertResult('<table><tr></tr><tr></tr></table>', '');
// cascading rollbacks
$this->assertResult(
'<table><tbody><tr></tr><tr></tr></tbody><tr></tr><tr></tr></table>',
''
);
// rollbacks twice
$this->assertResult('<table></table><table></table>', '');
// block in inline ins not allowed // block in inline ins not allowed
$this->assertResult( $this->assertResult(
@@ -84,12 +75,6 @@ class HTMLPurifier_Strategy_FixNestingTest extends HTMLPurifier_StrategyHarness
'<h1><ins>Not allowed!</ins></h1>' '<h1><ins>Not allowed!</ins></h1>'
); );
// test exclusions
$this->assertResult(
'<a><span><a>Not allowed</a></span></a>',
'<a><span></span></a>'
);
// stacked ins/del // stacked ins/del
$this->assertResult( $this->assertResult(
'<h1><ins><del><div>Not allowed!</div></del></ins></h1>', '<h1><ins><del><div>Not allowed!</div></del></ins></h1>',
@@ -99,6 +84,17 @@ class HTMLPurifier_Strategy_FixNestingTest extends HTMLPurifier_StrategyHarness
'<div><ins><del><div>Allowed!</div></del></ins></div>' '<div><ins><del><div>Allowed!</div></del></ins></div>'
); );
}
function testExclusionsIntegration() {
// test exclusions
$this->assertResult(
'<a><span><a>Not allowed</a></span></a>',
'<a><span></span></a>'
);
}
function testCustomParentIntegration() {
// test inline parent // test inline parent
$this->assertResult( $this->assertResult(
'<b>Bold</b>', true, array('HTML.Parent' => 'span') '<b>Bold</b>', true, array('HTML.Parent' => 'span')
@@ -107,13 +103,31 @@ class HTMLPurifier_Strategy_FixNestingTest extends HTMLPurifier_StrategyHarness
'<div>Reject</div>', 'Reject', array('HTML.Parent' => 'span') '<div>Reject</div>', 'Reject', array('HTML.Parent' => 'span')
); );
// test fallback to div
$this->expectError('Cannot use unrecognized element as parent.'); $this->expectError('Cannot use unrecognized element as parent.');
$this->assertResult( $this->assertResult(
'<div>Accept</div>', true, array('HTML.Parent' => 'fling') '<div>Accept</div>', true, array('HTML.Parent' => 'obviously-impossible')
); );
} }
function testDoubleCheckIntegration() {
// breaks without the redundant checking code
$this->assertResult('<table><tr></tr></table>', '');
// special case, prevents scrolling one back to find parent
$this->assertResult('<table><tr></tr><tr></tr></table>', '');
// cascading rollbacks
$this->assertResult(
'<table><tbody><tr></tr><tr></tr></tbody><tr></tr><tr></tr></table>',
''
);
// rollbacks twice
$this->assertResult('<table></table><table></table>', '');
}
} }
?> ?>

View File

@@ -11,13 +11,12 @@ class HTMLPurifier_Strategy_MakeWellFormedTest extends HTMLPurifier_StrategyHarn
$this->obj = new HTMLPurifier_Strategy_MakeWellFormed(); $this->obj = new HTMLPurifier_Strategy_MakeWellFormed();
} }
function test() { function testNormalIntegration() {
$this->config = array('HTML.Doctype' => 'XHTML 1.0 Strict');
$this->assertResult(''); $this->assertResult('');
$this->assertResult('This is <b>bold text</b>.'); $this->assertResult('This is <b>bold text</b>.');
}
function testUnclosedTagIntegration() {
$this->assertResult( $this->assertResult(
'<b>Unclosed tag, gasp!', '<b>Unclosed tag, gasp!',
'<b>Unclosed tag, gasp!</b>' '<b>Unclosed tag, gasp!</b>'
@@ -32,7 +31,9 @@ class HTMLPurifier_Strategy_MakeWellFormedTest extends HTMLPurifier_StrategyHarn
'Unused end tags... recycle!</b>', 'Unused end tags... recycle!</b>',
'Unused end tags... recycle!' 'Unused end tags... recycle!'
); );
}
function testEmptyTagDetectionIntegration() {
$this->assertResult( $this->assertResult(
'<br style="clear:both;">', '<br style="clear:both;">',
'<br style="clear:both;" />' '<br style="clear:both;" />'
@@ -42,8 +43,10 @@ class HTMLPurifier_Strategy_MakeWellFormedTest extends HTMLPurifier_StrategyHarn
'<div style="clear:both;" />', '<div style="clear:both;" />',
'<div style="clear:both;"></div>' '<div style="clear:both;"></div>'
); );
}
// test automatic paragraph closing
function testAutoClose() {
// paragraph
$this->assertResult( $this->assertResult(
'<p>Paragraph 1<p>Paragraph 2', '<p>Paragraph 1<p>Paragraph 2',
@@ -55,12 +58,20 @@ class HTMLPurifier_Strategy_MakeWellFormedTest extends HTMLPurifier_StrategyHarn
'<div><p>Paragraphs</p><p>In</p><p>A</p><p>Div</p></div>' '<div><p>Paragraphs</p><p>In</p><p>A</p><p>Div</p></div>'
); );
// automatic list closing // list
$this->assertResult( $this->assertResult(
'<ol><li>Item 1<li>Item 2</ol>', '<ol><li>Item 1<li>Item 2</ol>',
'<ol><li>Item 1</li><li>Item 2</li></ol>' '<ol><li>Item 1</li><li>Item 2</li></ol>'
); );
// colgroup
$this->assertResult(
'<table><colgroup><col /><tr></tr></table>',
'<table><colgroup><col /></colgroup><tr></tr></table>'
);
} }
} }

View File

@@ -97,16 +97,6 @@ class HTMLPurifier_Test extends UnitTestCase
} }
function test_table() {
$this->purifier = new HTMLPurifier();
$this->assertPurification(
'<TABLE><COLGROUP><COL span=3 width=64 /><TBODY><TR><TD>1</TD><TD>2</TD><TD>3</TD></TR></TBODY></TABLE>',
'<table><colgroup><col span="3" width="64" /></colgroup><tbody><tr><td>1</td><td>2</td><td>3</td></tr></tbody></table>'
);
}
} }
?> ?>