diff options
Diffstat (limited to 'lib/htmlpurifier/tests/HTMLPurifier/ChildDef')
7 files changed, 443 insertions, 0 deletions
diff --git a/lib/htmlpurifier/tests/HTMLPurifier/ChildDef/ChameleonTest.php b/lib/htmlpurifier/tests/HTMLPurifier/ChildDef/ChameleonTest.php new file mode 100644 index 000000000..82493f40e --- /dev/null +++ b/lib/htmlpurifier/tests/HTMLPurifier/ChildDef/ChameleonTest.php @@ -0,0 +1,40 @@ +<?php + +class HTMLPurifier_ChildDef_ChameleonTest extends HTMLPurifier_ChildDefHarness +{ + + protected $isInline; + + 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); + } + + function testInlineAlwaysAllowed() { + $this->isInline = true; + $this->assertResult( + '<b>Allowed.</b>' + ); + } + + function testBlockNotAllowedInInline() { + $this->isInline = true; + $this->assertResult( + '<div>Not allowed.</div>', '' + ); + } + + function testBlockAllowedInNonInline() { + $this->isInline = false; + $this->assertResult( + '<div>Allowed.</div>' + ); + } + +} + +// vim: et sw=4 sts=4 diff --git a/lib/htmlpurifier/tests/HTMLPurifier/ChildDef/CustomTest.php b/lib/htmlpurifier/tests/HTMLPurifier/ChildDef/CustomTest.php new file mode 100644 index 000000000..5b138a3c1 --- /dev/null +++ b/lib/htmlpurifier/tests/HTMLPurifier/ChildDef/CustomTest.php @@ -0,0 +1,89 @@ +<?php + +class HTMLPurifier_ChildDef_CustomTest extends HTMLPurifier_ChildDefHarness +{ + + 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>'); + + } + + 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); + } + + 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); + } + + 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); + } + + 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); + + } + + 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); + + } + + 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); + } + + 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/lib/htmlpurifier/tests/HTMLPurifier/ChildDef/ListTest.php b/lib/htmlpurifier/tests/HTMLPurifier/ChildDef/ListTest.php new file mode 100644 index 000000000..02dcab0fb --- /dev/null +++ b/lib/htmlpurifier/tests/HTMLPurifier/ChildDef/ListTest.php @@ -0,0 +1,50 @@ +<?php + +class HTMLPurifier_ChildDef_ListTest extends HTMLPurifier_ChildDefHarness +{ + + function setUp() { + parent::setUp(); + $this->obj = new HTMLPurifier_ChildDef_List(); + } + + function testEmptyInput() { + $this->assertResult('', false); + } + + function testSingleLi() { + $this->assertResult('<li />'); + } + + function testSomeLi() { + $this->assertResult('<li>asdf</li><li />'); + } + + function testIllegal() { + // XXX actually this never gets triggered in practice + $this->assertResult('<li /><b />', '<li /><li><b /></li>'); + } + + function testOlAtBeginning() { + $this->assertResult('<ol />', '<li><ol /></li>'); + } + + function testOlAtBeginningWithOtherJunk() { + $this->assertResult('<ol /><li />', '<li><ol /></li><li />'); + } + + function testOlInMiddle() { + $this->assertResult('<li>Foo</li><ol><li>Bar</li></ol>', '<li>Foo<ol><li>Bar</li></ol></li>'); + } + + function testMultipleOl() { + $this->assertResult('<li /><ol /><ol />', '<li><ol /><ol /></li>'); + } + + function testUlAtBeginning() { + $this->assertResult('<ul />', '<li><ul /></li>'); + } + +} + +// vim: et sw=4 sts=4 diff --git a/lib/htmlpurifier/tests/HTMLPurifier/ChildDef/OptionalTest.php b/lib/htmlpurifier/tests/HTMLPurifier/ChildDef/OptionalTest.php new file mode 100644 index 000000000..a5f34f7b1 --- /dev/null +++ b/lib/htmlpurifier/tests/HTMLPurifier/ChildDef/OptionalTest.php @@ -0,0 +1,33 @@ +<?php + +class HTMLPurifier_ChildDef_OptionalTest extends HTMLPurifier_ChildDefHarness +{ + + function setUp() { + parent::setUp(); + $this->obj = new HTMLPurifier_ChildDef_Optional('b | i'); + } + + function testBasicUsage() { + $this->assertResult('<b>Bold text</b><img />', '<b>Bold text</b>'); + } + + function testRemoveForbiddenText() { + $this->assertResult('Not allowed text', ''); + } + + function testEmpty() { + $this->assertResult(''); + } + + function testWhitespace() { + $this->assertResult(' '); + } + + function testMultipleWhitespace() { + $this->assertResult(' '); + } + +} + +// vim: et sw=4 sts=4 diff --git a/lib/htmlpurifier/tests/HTMLPurifier/ChildDef/RequiredTest.php b/lib/htmlpurifier/tests/HTMLPurifier/ChildDef/RequiredTest.php new file mode 100644 index 000000000..8bb4f45ee --- /dev/null +++ b/lib/htmlpurifier/tests/HTMLPurifier/ChildDef/RequiredTest.php @@ -0,0 +1,73 @@ +<?php + +class HTMLPurifier_ChildDef_RequiredTest extends HTMLPurifier_ChildDefHarness +{ + + function testPrepareString() { + $def = new HTMLPurifier_ChildDef_Required('foobar | bang |gizmo'); + $this->assertIdentical($def->elements, + array( + 'foobar' => true + ,'bang' => true + ,'gizmo' => true + )); + } + + function testPrepareArray() { + $def = new HTMLPurifier_ChildDef_Required(array('href', 'src')); + $this->assertIdentical($def->elements, + array( + 'href' => true + ,'src' => true + )); + } + + function setUp() { + parent::setUp(); + $this->obj = new HTMLPurifier_ChildDef_Required('dt | dd'); + } + + function testEmptyInput() { + $this->assertResult('', false); + } + + 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); + } + + function testIgnoreWhitespace() { + // whitespace shouldn't trigger it + $this->assertResult("\n<dd>Definition</dd> "); + } + + function testPreserveWhitespaceAfterRemoval() { + $this->assertResult( + '<dd>Definition</dd> <b></b> ', + '<dd>Definition</dd> ' + ); + } + + function testDeleteNodeIfOnlyWhitespace() { + $this->assertResult("\t ", false); + } + + function testPCDATAAllowed() { + $this->obj = new HTMLPurifier_ChildDef_Required('#PCDATA | b'); + $this->assertResult('Out <b>Bold text</b><img />', 'Out <b>Bold text</b>'); + } + + function testPCDATAAllowedWithEscaping() { + $this->obj = new HTMLPurifier_ChildDef_Required('#PCDATA | b'); + $this->config->set('Core.EscapeInvalidChildren', true); + $this->assertResult( + 'Out <b>Bold text</b><img />', + 'Out <b>Bold text</b><img />' + ); + } +} + +// vim: et sw=4 sts=4 diff --git a/lib/htmlpurifier/tests/HTMLPurifier/ChildDef/StrictBlockquoteTest.php b/lib/htmlpurifier/tests/HTMLPurifier/ChildDef/StrictBlockquoteTest.php new file mode 100644 index 000000000..52594b1a0 --- /dev/null +++ b/lib/htmlpurifier/tests/HTMLPurifier/ChildDef/StrictBlockquoteTest.php @@ -0,0 +1,83 @@ +<?php + +class HTMLPurifier_ChildDef_StrictBlockquoteTest +extends HTMLPurifier_ChildDefHarness +{ + + function setUp() { + parent::setUp(); + $this->obj = new HTMLPurifier_ChildDef_StrictBlockquote('div | p'); + } + + function testEmptyInput() { + $this->assertResult(''); + } + + function testPreserveValidP() { + $this->assertResult('<p>Valid</p>'); + } + + function testPreserveValidDiv() { + $this->assertResult('<div>Still valid</div>'); + } + + function testWrapTextWithP() { + $this->assertResult('Needs wrap', '<p>Needs wrap</p>'); + } + + function testNoWrapForWhitespaceOrValidElements() { + $this->assertResult('<p>Do not wrap</p> <p>Whitespace</p>'); + } + + function testWrapTextNextToValidElements() { + $this->assertResult( + 'Wrap'. '<p>Do not wrap</p>', + '<p>Wrap</p><p>Do not wrap</p>' + ); + } + + function testWrapInlineElements() { + $this->assertResult( + '<p>Do not</p>'.'<b>Wrap</b>', + '<p>Do not</p><p><b>Wrap</b></p>' + ); + } + + function testWrapAndRemoveInvalidTags() { + $this->assertResult( + '<li>Not allowed</li>Paragraph.<p>Hmm.</p>', + '<p>Not allowedParagraph.</p><p>Hmm.</p>' + ); + } + + function testWrapComplicatedSring() { + $this->assertResult( + $var = 'He said<br />perhaps<br />we should <b>nuke</b> them.', + "<p>$var</p>" + ); + } + + 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>' + ); + } + + function testAlternateWrapper() { + $this->config->set('HTML.BlockWrapper', 'div'); + $this->assertResult('Needs wrap', '<div>Needs wrap</div>'); + + } + + 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/lib/htmlpurifier/tests/HTMLPurifier/ChildDef/TableTest.php b/lib/htmlpurifier/tests/HTMLPurifier/ChildDef/TableTest.php new file mode 100644 index 000000000..2f72d187a --- /dev/null +++ b/lib/htmlpurifier/tests/HTMLPurifier/ChildDef/TableTest.php @@ -0,0 +1,75 @@ +<?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 +{ + + function setUp() { + parent::setUp(); + $this->obj = new HTMLPurifier_ChildDef_Table(); + } + + function testEmptyInput() { + $this->assertResult('', false); + } + + function testSingleRow() { + $this->assertResult('<tr />'); + } + + function testComplexContents() { + $this->assertResult('<caption /><col /><thead /><tfoot /><tbody>'. + '<tr><td>asdf</td></tr></tbody>'); + $this->assertResult('<col /><col /><col /><tr />'); + } + + 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>'); + } + + 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>' + ); + } + + 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>' + ); + } + + function testDuplicateProcessing() { + $this->assertResult( + '<caption>1</caption><caption /><tbody /><tbody /><tfoot>1</tfoot><tfoot />', + '<caption>1</caption><tfoot>1</tfoot><tbody /><tbody /><tbody />' + ); + } + + function testRemoveText() { + $this->assertResult('foo', false); + } + + function testStickyWhitespaceOnTr() { + $this->config->set('Output.Newline', "\n"); + $this->assertResult("\n <tr />\n <tr />\n "); + } + + function testStickyWhitespaceOnTSection() { + $this->config->set('Output.Newline', "\n"); + $this->assertResult( + "\n\t<tbody />\n\t\t<tfoot />\n\t\t\t", + "\n\t\t<tfoot />\n\t<tbody />\n\t\t\t" + ); + + } + +} + +// vim: et sw=4 sts=4 |