mirror of
https://github.com/ezyang/htmlpurifier.git
synced 2025-10-23 09:36:19 +02:00
Release 2.0.1, merged in 1181 to HEAD.
git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/branches/strict@1255 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
@@ -26,9 +26,9 @@ class HTMLPurifier_Strategy_CompositeTest extends UnitTestCase
|
||||
|
||||
// setup a bunch of mock strategies to inject into our composite test
|
||||
|
||||
$mock_1 = new HTMLPurifier_StrategyMock($this);
|
||||
$mock_2 = new HTMLPurifier_StrategyMock($this);
|
||||
$mock_3 = new HTMLPurifier_StrategyMock($this);
|
||||
$mock_1 = new HTMLPurifier_StrategyMock();
|
||||
$mock_2 = new HTMLPurifier_StrategyMock();
|
||||
$mock_3 = new HTMLPurifier_StrategyMock();
|
||||
|
||||
// setup the object
|
||||
|
||||
@@ -73,4 +73,3 @@ class HTMLPurifier_Strategy_CompositeTest extends UnitTestCase
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
@@ -20,7 +20,7 @@ class HTMLPurifier_Strategy_CoreTest extends HTMLPurifier_StrategyHarness
|
||||
);
|
||||
$this->assertResult(
|
||||
'<b><div>Fix nesting.</div></b>',
|
||||
'<b>Fix nesting.</b>'
|
||||
'<b></b><div>Fix nesting.</div>'
|
||||
);
|
||||
$this->assertResult(
|
||||
'<asdf>Foreign element removal.</asdf>',
|
||||
@@ -28,11 +28,10 @@ class HTMLPurifier_Strategy_CoreTest extends HTMLPurifier_StrategyHarness
|
||||
);
|
||||
$this->assertResult(
|
||||
'<foo><b><div>All three.</div></b>',
|
||||
'<b>All three.</b>'
|
||||
'<b></b><div>All three.</div>'
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
19
tests/HTMLPurifier/Strategy/ErrorsHarness.php
Normal file
19
tests/HTMLPurifier/Strategy/ErrorsHarness.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
require_once 'HTMLPurifier/ErrorsHarness.php';
|
||||
|
||||
class HTMLPurifier_Strategy_ErrorsHarness extends HTMLPurifier_ErrorsHarness
|
||||
{
|
||||
|
||||
// needs to be defined
|
||||
function getStrategy() {}
|
||||
|
||||
function invoke($input) {
|
||||
$strategy = $this->getStrategy();
|
||||
$lexer = new HTMLPurifier_Lexer_DirectLex();
|
||||
$tokens = $lexer->tokenizeHTML($input, $this->config, $this->context);
|
||||
$strategy->execute($tokens, $this->config, $this->context);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -102,12 +102,15 @@ class HTMLPurifier_Strategy_FixNestingTest extends HTMLPurifier_StrategyHarness
|
||||
$this->assertResult(
|
||||
'<div>Reject</div>', 'Reject', array('HTML.Parent' => 'span')
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
function testError() {
|
||||
// test fallback to div
|
||||
$this->expectError('Cannot use unrecognized element as parent.');
|
||||
$this->assertResult(
|
||||
'<div>Accept</div>', true, array('HTML.Parent' => 'obviously-impossible')
|
||||
);
|
||||
$this->swallowErrors();
|
||||
|
||||
}
|
||||
|
||||
@@ -130,4 +133,3 @@ class HTMLPurifier_Strategy_FixNestingTest extends HTMLPurifier_StrategyHarness
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
38
tests/HTMLPurifier/Strategy/FixNesting_ErrorsTest.php
Normal file
38
tests/HTMLPurifier/Strategy/FixNesting_ErrorsTest.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
require_once 'HTMLPurifier/Strategy/ErrorsHarness.php';
|
||||
require_once 'HTMLPurifier/Strategy/FixNesting.php';
|
||||
|
||||
class HTMLPurifier_Strategy_FixNesting_ErrorsTest extends HTMLPurifier_Strategy_ErrorsHarness
|
||||
{
|
||||
|
||||
function getStrategy() {
|
||||
return new HTMLPurifier_Strategy_FixNesting();
|
||||
}
|
||||
|
||||
function testNodeRemoved() {
|
||||
$this->expectErrorCollection(E_ERROR, 'Strategy_FixNesting: Node removed');
|
||||
$this->expectContext('CurrentToken', new HTMLPurifier_Token_Start('ul', array(), 1));
|
||||
$this->invoke('<ul></ul>');
|
||||
}
|
||||
|
||||
function testNodeExcluded() {
|
||||
$this->expectErrorCollection(E_ERROR, 'Strategy_FixNesting: Node excluded');
|
||||
$this->expectContext('CurrentToken', new HTMLPurifier_Token_Start('a', array(), 2));
|
||||
$this->invoke("<a>\n<a></a></a>");
|
||||
}
|
||||
|
||||
function testNodeReorganized() {
|
||||
$this->expectErrorCollection(E_WARNING, 'Strategy_FixNesting: Node reorganized');
|
||||
$this->expectContext('CurrentToken', new HTMLPurifier_Token_Start('span', array(), 1));
|
||||
$this->invoke("<span>Valid<div>Invalid</div></span>");
|
||||
}
|
||||
|
||||
function testNodeContentsRemoved() {
|
||||
$this->expectErrorCollection(E_ERROR, 'Strategy_FixNesting: Node contents removed');
|
||||
$this->expectContext('CurrentToken', new HTMLPurifier_Token_Start('span', array(), 1));
|
||||
$this->invoke("<span><div></div></span>");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ class HTMLPurifier_Strategy_MakeWellFormedTest extends HTMLPurifier_StrategyHarn
|
||||
function setUp() {
|
||||
parent::setUp();
|
||||
$this->obj = new HTMLPurifier_Strategy_MakeWellFormed();
|
||||
$this->config = array();
|
||||
}
|
||||
|
||||
function testNormalIntegration() {
|
||||
@@ -74,6 +75,48 @@ class HTMLPurifier_Strategy_MakeWellFormedTest extends HTMLPurifier_StrategyHarn
|
||||
|
||||
}
|
||||
|
||||
function testMultipleInjectors() {
|
||||
|
||||
$this->config = array('AutoFormat.AutoParagraph' => true, 'AutoFormat.Linkify' => true);
|
||||
|
||||
$this->assertResult(
|
||||
'Foobar',
|
||||
'<p>Foobar</p>'
|
||||
);
|
||||
|
||||
$this->assertResult(
|
||||
'http://example.com',
|
||||
'<p><a href="http://example.com">http://example.com</a></p>'
|
||||
);
|
||||
|
||||
$this->assertResult(
|
||||
'<b>http://example.com</b>',
|
||||
'<p><b><a href="http://example.com">http://example.com</a></b></p>'
|
||||
);
|
||||
|
||||
$this->assertResult(
|
||||
'<b>http://example.com',
|
||||
'<p><b><a href="http://example.com">http://example.com</a></b></p>'
|
||||
);
|
||||
|
||||
$this->assertResult(
|
||||
'http://example.com
|
||||
|
||||
http://dev.example.com',
|
||||
'<p><a href="http://example.com">http://example.com</a></p><p><a href="http://dev.example.com">http://dev.example.com</a></p>'
|
||||
);
|
||||
|
||||
$this->assertResult(
|
||||
'http://example.com <div>http://example.com</div>',
|
||||
'<p><a href="http://example.com">http://example.com</a> </p><div><a href="http://example.com">http://example.com</a></div>'
|
||||
);
|
||||
|
||||
$this->assertResult(
|
||||
'This URL http://example.com is what you need',
|
||||
'<p>This URL <a href="http://example.com">http://example.com</a> is what you need</p>'
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
56
tests/HTMLPurifier/Strategy/MakeWellFormed_ErrorsTest.php
Normal file
56
tests/HTMLPurifier/Strategy/MakeWellFormed_ErrorsTest.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
require_once 'HTMLPurifier/Strategy/ErrorsHarness.php';
|
||||
require_once 'HTMLPurifier/Strategy/MakeWellFormed.php';
|
||||
|
||||
class HTMLPurifier_Strategy_MakeWellFormed_ErrorsTest extends HTMLPurifier_Strategy_ErrorsHarness
|
||||
{
|
||||
|
||||
function getStrategy() {
|
||||
return new HTMLPurifier_Strategy_MakeWellFormed();
|
||||
}
|
||||
|
||||
function testUnnecessaryEndTagRemoved() {
|
||||
$this->expectErrorCollection(E_WARNING, 'Strategy_MakeWellFormed: Unnecessary end tag removed');
|
||||
$this->expectContext('CurrentToken', new HTMLPurifier_Token_End('b', array(), 1));
|
||||
$this->invoke('</b>');
|
||||
}
|
||||
|
||||
function testUnnecessaryEndTagToText() {
|
||||
$this->config->set('Core', 'EscapeInvalidTags', true);
|
||||
$this->expectErrorCollection(E_WARNING, 'Strategy_MakeWellFormed: Unnecessary end tag to text');
|
||||
$this->expectContext('CurrentToken', new HTMLPurifier_Token_End('b', array(), 1));
|
||||
$this->invoke('</b>');
|
||||
}
|
||||
|
||||
function testTagAutoClosed() {
|
||||
$this->expectErrorCollection(E_NOTICE, 'Strategy_MakeWellFormed: Tag auto closed', new HTMLPurifier_Token_Start('b', array(), 1));
|
||||
$this->expectContext('CurrentToken', new HTMLPurifier_Token_Start('div', array(), 1));
|
||||
$this->invoke('<b>Foo<div>Bar</div>');
|
||||
}
|
||||
|
||||
function testStrayEndTagRemoved() {
|
||||
$this->expectErrorCollection(E_WARNING, 'Strategy_MakeWellFormed: Stray end tag removed');
|
||||
$this->expectContext('CurrentToken', new HTMLPurifier_Token_End('b', array(), 1));
|
||||
$this->invoke('<i></b></i>');
|
||||
}
|
||||
|
||||
function testStrayEndTagToText() {
|
||||
$this->config->set('Core', 'EscapeInvalidTags', true);
|
||||
$this->expectErrorCollection(E_WARNING, 'Strategy_MakeWellFormed: Stray end tag to text');
|
||||
$this->expectContext('CurrentToken', new HTMLPurifier_Token_End('b', array(), 1));
|
||||
$this->invoke('<i></b></i>');
|
||||
}
|
||||
|
||||
function testTagClosedByElementEnd() {
|
||||
$this->expectErrorCollection(E_NOTICE, 'Strategy_MakeWellFormed: Tag closed by element end', new HTMLPurifier_Token_Start('b', array(), 1));
|
||||
$this->invoke('<i><b>Foobar</i>');
|
||||
}
|
||||
|
||||
function testTagClosedByDocumentEnd() {
|
||||
$this->expectErrorCollection(E_NOTICE, 'Strategy_MakeWellFormed: Tag closed by document end', new HTMLPurifier_Token_Start('b', array(), 1));
|
||||
$this->invoke('<b>Foobar');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -79,8 +79,19 @@ class HTMLPurifier_Strategy_RemoveForeignElementsTest
|
||||
array('HTML.Allowed' => 'div')
|
||||
);
|
||||
|
||||
// text-ify commented script contents ( the trailing comment gets
|
||||
// removed during generation )
|
||||
$this->assertResult(
|
||||
'<script type="text/javascript"><!--
|
||||
alert(<b>bold</b>);
|
||||
// --></script>',
|
||||
'<script type="text/javascript">
|
||||
alert(<b>bold</b>);
|
||||
// </script>',
|
||||
array('HTML.Trusted' => true, 'Output.CommentScriptContents' => false)
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
require_once 'HTMLPurifier/Strategy/ErrorsHarness.php';
|
||||
require_once 'HTMLPurifier/Strategy/RemoveForeignElements.php';
|
||||
|
||||
class HTMLPurifier_Strategy_RemoveForeignElements_ErrorsTest extends HTMLPurifier_Strategy_ErrorsHarness
|
||||
{
|
||||
|
||||
function setup() {
|
||||
parent::setup();
|
||||
$this->config->set('HTML', 'TidyLevel', 'heavy');
|
||||
}
|
||||
|
||||
function getStrategy() {
|
||||
return new HTMLPurifier_Strategy_RemoveForeignElements();
|
||||
}
|
||||
|
||||
function testTagTransform() {
|
||||
$this->expectErrorCollection(E_NOTICE, 'Strategy_RemoveForeignElements: Tag transform', 'center');
|
||||
$this->expectContext('CurrentToken', new HTMLPurifier_Token_Start('div', array('style' => 'text-align:center;'), 1));
|
||||
$this->invoke('<center>');
|
||||
}
|
||||
|
||||
function testMissingRequiredAttr() {
|
||||
// a little fragile, since img has two required attributes
|
||||
$this->expectErrorCollection(E_ERROR, 'Strategy_RemoveForeignElements: Missing required attribute', 'alt');
|
||||
$this->expectContext('CurrentToken', new HTMLPurifier_Token_Empty('img', array(), 1));
|
||||
$this->invoke('<img />');
|
||||
}
|
||||
|
||||
function testForeignElementToText() {
|
||||
$this->config->set('Core', 'EscapeInvalidTags', true);
|
||||
$this->expectErrorCollection(E_WARNING, 'Strategy_RemoveForeignElements: Foreign element to text');
|
||||
$this->expectContext('CurrentToken', new HTMLPurifier_Token_Start('invalid', array(), 1));
|
||||
$this->invoke('<invalid>');
|
||||
}
|
||||
|
||||
function testForeignElementRemoved() {
|
||||
// uses $CurrentToken.Serialized
|
||||
$this->expectErrorCollection(E_ERROR, 'Strategy_RemoveForeignElements: Foreign element removed');
|
||||
$this->expectContext('CurrentToken', new HTMLPurifier_Token_Start('invalid', array(), 1));
|
||||
$this->invoke('<invalid>');
|
||||
}
|
||||
|
||||
function testCommentRemoved() {
|
||||
$this->expectErrorCollection(E_NOTICE, 'Strategy_RemoveForeignElements: Comment removed');
|
||||
$this->expectContext('CurrentToken', new HTMLPurifier_Token_Comment(' test ', 1));
|
||||
$this->invoke('<!-- test -->');
|
||||
}
|
||||
|
||||
function testScriptRemoved() {
|
||||
$this->collector->expectAt(0, 'send', array(E_ERROR, 'Strategy_RemoveForeignElements: Script removed'));
|
||||
$this->collector->expectContextAt(0, 'CurrentToken', new HTMLPurifier_Token_Start('script', array(), 1));
|
||||
$this->collector->expectAt(1, 'send', array(E_ERROR, 'Strategy_RemoveForeignElements: Token removed to end', 'script'));
|
||||
$this->invoke('<script>asdf');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -439,4 +439,4 @@ class HTMLPurifier_Strategy_ValidateAttributesTest extends
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user