diff options
Diffstat (limited to 'library/ezyang/htmlpurifier/tests/HTMLPurifier/ChildDef')
7 files changed, 496 insertions, 0 deletions
diff --git a/library/ezyang/htmlpurifier/tests/HTMLPurifier/ChildDef/ChameleonTest.php b/library/ezyang/htmlpurifier/tests/HTMLPurifier/ChildDef/ChameleonTest.php new file mode 100644 index 0000000000..1a751395c0 --- /dev/null +++ b/library/ezyang/htmlpurifier/tests/HTMLPurifier/ChildDef/ChameleonTest.php @@ -0,0 +1,44 @@ +<?php + +class HTMLPurifier_ChildDef_ChameleonTest extends HTMLPurifier_ChildDefHarness +{ + + protected $isInline; + + public function setUp() + { + parent::setUp(); + $this->obj = new HTMLPurifier_ChildDef_Chameleon( + 'b | i', // allowed only when in inline context + 'b | i | div' // allowed only when in block context + ); + $this->context->register('IsInline', $this->isInline); + } + + public function testInlineAlwaysAllowed() + { + $this->isInline = true; + $this->assertResult( + '<b>Allowed.</b>' + ); + } + + public function testBlockNotAllowedInInline() + { + $this->isInline = true; + $this->assertResult( + '<div>Not allowed.</div>', '' + ); + } + + public function testBlockAllowedInNonInline() + { + $this->isInline = false; + $this->assertResult( + '<div>Allowed.</div>' + ); + } + +} + +// vim: et sw=4 sts=4 diff --git a/library/ezyang/htmlpurifier/tests/HTMLPurifier/ChildDef/CustomTest.php b/library/ezyang/htmlpurifier/tests/HTMLPurifier/ChildDef/CustomTest.php new file mode 100644 index 0000000000..0094323d57 --- /dev/null +++ b/library/ezyang/htmlpurifier/tests/HTMLPurifier/ChildDef/CustomTest.php @@ -0,0 +1,99 @@ +<?php + +class HTMLPurifier_ChildDef_CustomTest extends HTMLPurifier_ChildDefHarness +{ + + public function setUp() + { + parent::setUp(); + } + + public function test() + { + $this->obj = new HTMLPurifier_ChildDef_Custom('(a,b?,c*,d+,(a,b)*)'); + + $this->assertEqual($this->obj->elements, array('a' => true, + 'b' => true, 'c' => true, 'd' => true)); + + $this->assertResult('', false); + $this->assertResult('<a /><a />', false); + + $this->assertResult('<a /><b /><c /><d /><a /><b />'); + $this->assertResult('<a /><d>Dob</d><a /><b>foo</b>'. + '<a href="moo" /><b>foo</b>'); + + } + + public function testNesting() + { + $this->obj = new HTMLPurifier_ChildDef_Custom('(a,b,(c|d))+'); + $this->assertEqual($this->obj->elements, array('a' => true, + 'b' => true, 'c' => true, 'd' => true)); + $this->assertResult('', false); + $this->assertResult('<a /><b /><c /><a /><b /><d />'); + $this->assertResult('<a /><b /><c /><d />', false); + } + + public function testNestedEitherOr() + { + $this->obj = new HTMLPurifier_ChildDef_Custom('b,(a|(c|d))+'); + $this->assertEqual($this->obj->elements, array('a' => true, + 'b' => true, 'c' => true, 'd' => true)); + $this->assertResult('', false); + $this->assertResult('<b /><a /><c /><d />'); + $this->assertResult('<b /><d /><a /><a />'); + $this->assertResult('<b /><a />'); + $this->assertResult('<acd />', false); + } + + public function testNestedQuantifier() + { + $this->obj = new HTMLPurifier_ChildDef_Custom('(b,c+)*'); + $this->assertEqual($this->obj->elements, array('b' => true, 'c' => true)); + $this->assertResult(''); + $this->assertResult('<b /><c />'); + $this->assertResult('<b /><c /><c /><c />'); + $this->assertResult('<b /><c /><b /><c />'); + $this->assertResult('<b /><c /><b />', false); + } + + public function testEitherOr() + { + $this->obj = new HTMLPurifier_ChildDef_Custom('a|b'); + $this->assertEqual($this->obj->elements, array('a' => true, 'b' => true)); + $this->assertResult('', false); + $this->assertResult('<a />'); + $this->assertResult('<b />'); + $this->assertResult('<a /><b />', false); + + } + + public function testCommafication() + { + $this->obj = new HTMLPurifier_ChildDef_Custom('a,b'); + $this->assertEqual($this->obj->elements, array('a' => true, 'b' => true)); + $this->assertResult('<a /><b />'); + $this->assertResult('<ab />', false); + + } + + public function testPcdata() + { + $this->obj = new HTMLPurifier_ChildDef_Custom('#PCDATA,a'); + $this->assertEqual($this->obj->elements, array('#PCDATA' => true, 'a' => true)); + $this->assertResult('foo<a />'); + $this->assertResult('<a />', false); + } + + public function testWhitespace() + { + $this->obj = new HTMLPurifier_ChildDef_Custom('a'); + $this->assertEqual($this->obj->elements, array('a' => true)); + $this->assertResult('foo<a />', false); + $this->assertResult('<a />'); + $this->assertResult(' <a />'); + } + +} + +// vim: et sw=4 sts=4 diff --git a/library/ezyang/htmlpurifier/tests/HTMLPurifier/ChildDef/ListTest.php b/library/ezyang/htmlpurifier/tests/HTMLPurifier/ChildDef/ListTest.php new file mode 100644 index 0000000000..0e3d5c72fc --- /dev/null +++ b/library/ezyang/htmlpurifier/tests/HTMLPurifier/ChildDef/ListTest.php @@ -0,0 +1,54 @@ +<?php + +class HTMLPurifier_ChildDef_ListTest extends HTMLPurifier_ChildDefHarness +{ + + public function setUp() + { + parent::setUp(); + $this->obj = new HTMLPurifier_ChildDef_List(); + } + + public function testEmptyInput() + { + $this->assertResult('', false); + } + + public function testSingleLi() + { + $this->assertResult('<li />'); + } + + public function testSomeLi() + { + $this->assertResult('<li>asdf</li><li />'); + } + + public function testOlAtBeginning() + { + $this->assertResult('<ol />', '<li><ol /></li>'); + } + + public function testOlAtBeginningWithOtherJunk() + { + $this->assertResult('<ol /><li />', '<li><ol /></li><li />'); + } + + public function testOlInMiddle() + { + $this->assertResult('<li>Foo</li><ol><li>Bar</li></ol>', '<li>Foo<ol><li>Bar</li></ol></li>'); + } + + public function testMultipleOl() + { + $this->assertResult('<li /><ol /><ol />', '<li><ol /><ol /></li>'); + } + + public function testUlAtBeginning() + { + $this->assertResult('<ul />', '<li><ul /></li>'); + } + +} + +// vim: et sw=4 sts=4 diff --git a/library/ezyang/htmlpurifier/tests/HTMLPurifier/ChildDef/OptionalTest.php b/library/ezyang/htmlpurifier/tests/HTMLPurifier/ChildDef/OptionalTest.php new file mode 100644 index 0000000000..dd035e4c84 --- /dev/null +++ b/library/ezyang/htmlpurifier/tests/HTMLPurifier/ChildDef/OptionalTest.php @@ -0,0 +1,39 @@ +<?php + +class HTMLPurifier_ChildDef_OptionalTest extends HTMLPurifier_ChildDefHarness +{ + + public function setUp() + { + parent::setUp(); + $this->obj = new HTMLPurifier_ChildDef_Optional('b | i'); + } + + public function testBasicUsage() + { + $this->assertResult('<b>Bold text</b><img />', '<b>Bold text</b>'); + } + + public function testRemoveForbiddenText() + { + $this->assertResult('Not allowed text', ''); + } + + public function testEmpty() + { + $this->assertResult(''); + } + + public function testWhitespace() + { + $this->assertResult(' '); + } + + public function testMultipleWhitespace() + { + $this->assertResult(' '); + } + +} + +// vim: et sw=4 sts=4 diff --git a/library/ezyang/htmlpurifier/tests/HTMLPurifier/ChildDef/RequiredTest.php b/library/ezyang/htmlpurifier/tests/HTMLPurifier/ChildDef/RequiredTest.php new file mode 100644 index 0000000000..d4f7c98824 --- /dev/null +++ b/library/ezyang/htmlpurifier/tests/HTMLPurifier/ChildDef/RequiredTest.php @@ -0,0 +1,78 @@ +<?php + +class HTMLPurifier_ChildDef_RequiredTest extends HTMLPurifier_ChildDefHarness +{ + + public function testPrepareString() + { + $def = new HTMLPurifier_ChildDef_Required('foobar | bang |gizmo'); + $this->assertIdentical($def->elements, + array( + 'foobar' => true + ,'bang' => true + ,'gizmo' => true + )); + } + + public function testPrepareArray() + { + $def = new HTMLPurifier_ChildDef_Required(array('href', 'src')); + $this->assertIdentical($def->elements, + array( + 'href' => true + ,'src' => true + )); + } + + public function setUp() + { + parent::setUp(); + $this->obj = new HTMLPurifier_ChildDef_Required('dt | dd'); + } + + public function testEmptyInput() + { + $this->assertResult('', false); + } + + public function testRemoveIllegalTagsAndElements() + { + $this->assertResult( + '<dt>Term</dt>Text in an illegal location'. + '<dd>Definition</dd><b>Illegal tag</b>', + '<dt>Term</dt><dd>Definition</dd>'); + $this->assertResult('How do you do!', false); + } + + public function testIgnoreWhitespace() + { + // whitespace shouldn't trigger it + $this->assertResult("\n<dd>Definition</dd> "); + } + + public function testPreserveWhitespaceAfterRemoval() + { + $this->assertResult( + '<dd>Definition</dd> <b></b> ', + '<dd>Definition</dd> ' + ); + } + + public function testDeleteNodeIfOnlyWhitespace() + { + $this->assertResult("\t ", false); + } + + public function testPCDATAAllowed() + { + $this->obj = new HTMLPurifier_ChildDef_Required('#PCDATA | b'); + $this->assertResult('Out <b>Bold text</b><img />', 'Out <b>Bold text</b>'); + } + public function testPCDATAAllowedJump() + { + $this->obj = new HTMLPurifier_ChildDef_Required('#PCDATA | b'); + $this->assertResult('A <i>foo</i>', 'A foo'); + } +} + +// vim: et sw=4 sts=4 diff --git a/library/ezyang/htmlpurifier/tests/HTMLPurifier/ChildDef/StrictBlockquoteTest.php b/library/ezyang/htmlpurifier/tests/HTMLPurifier/ChildDef/StrictBlockquoteTest.php new file mode 100644 index 0000000000..82861ee7e6 --- /dev/null +++ b/library/ezyang/htmlpurifier/tests/HTMLPurifier/ChildDef/StrictBlockquoteTest.php @@ -0,0 +1,96 @@ +<?php + +class HTMLPurifier_ChildDef_StrictBlockquoteTest +extends HTMLPurifier_ChildDefHarness +{ + + public function setUp() + { + parent::setUp(); + $this->obj = new HTMLPurifier_ChildDef_StrictBlockquote('div | p'); + } + + public function testEmptyInput() + { + $this->assertResult(''); + } + + public function testPreserveValidP() + { + $this->assertResult('<p>Valid</p>'); + } + + public function testPreserveValidDiv() + { + $this->assertResult('<div>Still valid</div>'); + } + + public function testWrapTextWithP() + { + $this->assertResult('Needs wrap', '<p>Needs wrap</p>'); + } + + public function testNoWrapForWhitespaceOrValidElements() + { + $this->assertResult('<p>Do not wrap</p> <p>Whitespace</p>'); + } + + public function testWrapTextNextToValidElements() + { + $this->assertResult( + 'Wrap'. '<p>Do not wrap</p>', + '<p>Wrap</p><p>Do not wrap</p>' + ); + } + + public function testWrapInlineElements() + { + $this->assertResult( + '<p>Do not</p>'.'<b>Wrap</b>', + '<p>Do not</p><p><b>Wrap</b></p>' + ); + } + + public function testWrapAndRemoveInvalidTags() + { + $this->assertResult( + '<li>Not allowed</li>Paragraph.<p>Hmm.</p>', + '<p>Not allowedParagraph.</p><p>Hmm.</p>' + ); + } + + public function testWrapComplicatedSring() + { + $this->assertResult( + $var = 'He said<br />perhaps<br />we should <b>nuke</b> them.', + "<p>$var</p>" + ); + } + + public function testWrapAndRemoveInvalidTagsComplex() + { + $this->assertResult( + '<foo>Bar</foo><bas /><b>People</b>Conniving.'. '<p>Fools!</p>', + '<p>Bar'. '<b>People</b>Conniving.</p><p>Fools!</p>' + ); + } + + public function testAlternateWrapper() + { + $this->config->set('HTML.BlockWrapper', 'div'); + $this->assertResult('Needs wrap', '<div>Needs wrap</div>'); + + } + + public function testError() + { + $this->expectError('Cannot use non-block element as block wrapper'); + $this->obj = new HTMLPurifier_ChildDef_StrictBlockquote('div | p'); + $this->config->set('HTML.BlockWrapper', 'dav'); + $this->config->set('Cache.DefinitionImpl', null); + $this->assertResult('Needs wrap', '<p>Needs wrap</p>'); + } + +} + +// vim: et sw=4 sts=4 diff --git a/library/ezyang/htmlpurifier/tests/HTMLPurifier/ChildDef/TableTest.php b/library/ezyang/htmlpurifier/tests/HTMLPurifier/ChildDef/TableTest.php new file mode 100644 index 0000000000..49e96b6526 --- /dev/null +++ b/library/ezyang/htmlpurifier/tests/HTMLPurifier/ChildDef/TableTest.php @@ -0,0 +1,86 @@ +<?php + +// we're using empty tags to compact the tests: under real circumstances +// there would be contents in them + +class HTMLPurifier_ChildDef_TableTest extends HTMLPurifier_ChildDefHarness +{ + + public function setUp() + { + parent::setUp(); + $this->obj = new HTMLPurifier_ChildDef_Table(); + } + + public function testEmptyInput() + { + $this->assertResult('', false); + } + + public function testSingleRow() + { + $this->assertResult('<tr />'); + } + + public function testComplexContents() + { + $this->assertResult('<caption /><col /><thead /><tfoot /><tbody>'. + '<tr><td>asdf</td></tr></tbody>'); + $this->assertResult('<col /><col /><col /><tr />'); + } + + public function testReorderContents() + { + $this->assertResult( + '<col /><colgroup /><tbody /><tfoot /><thead /><tr>1</tr><caption /><tr />', + '<caption /><col /><colgroup /><thead /><tfoot /><tbody /><tbody><tr>1</tr><tr /></tbody>'); + } + + public function testXhtml11Illegal() + { + $this->assertResult( + '<thead><tr><th>a</th></tr></thead><tr><td>a</td></tr>', + '<thead><tr><th>a</th></tr></thead><tbody><tr><td>a</td></tr></tbody>' + ); + } + + public function testTrOverflowAndClose() + { + $this->assertResult( + '<tr><td>a</td></tr><tr><td>b</td></tr><tbody><tr><td>c</td></tr></tbody><tr><td>d</td></tr>', + '<tbody><tr><td>a</td></tr><tr><td>b</td></tr></tbody><tbody><tr><td>c</td></tr></tbody><tbody><tr><td>d</td></tr></tbody>' + ); + } + + public function testDuplicateProcessing() + { + $this->assertResult( + '<caption>1</caption><caption /><tbody /><tbody /><tfoot>1</tfoot><tfoot />', + '<caption>1</caption><tfoot>1</tfoot><tbody /><tbody /><tbody />' + ); + } + + public function testRemoveText() + { + $this->assertResult('foo', false); + } + + public function testStickyWhitespaceOnTr() + { + $this->config->set('Output.Newline', "\n"); + $this->assertResult("\n <tr />\n <tr />\n "); + } + + public function testStickyWhitespaceOnTSection() + { + $this->config->set('Output.Newline', "\n"); + $this->assertResult( + "\n\t<tbody />\n\t\t<tfoot />\n\t\t\t", + "\n\t<tfoot />\n\t\t\t<tbody />\n\t\t" + ); + + } + +} + +// vim: et sw=4 sts=4 |
