mirror of
https://github.com/ezyang/htmlpurifier.git
synced 2025-08-04 21:28:06 +02:00
Implement attribute transforms for required attributes. I can now confidently say that output will always be valid.
git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@256 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
36
tests/HTMLPurifier/AttrTransform/BdoDirTest.php
Normal file
36
tests/HTMLPurifier/AttrTransform/BdoDirTest.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
require_once 'HTMLPurifier/AttrTransform/BdoDir.php';
|
||||
|
||||
class HTMLPurifier_AttrTransform_BdoDirTest extends HTMLPurifier_AttrTransformHarness
|
||||
{
|
||||
|
||||
function test() {
|
||||
|
||||
$this->transform = new HTMLPurifier_AttrTransform_BdoDir();
|
||||
|
||||
$inputs = array();
|
||||
$expect = array();
|
||||
$config = array();
|
||||
|
||||
// add dir
|
||||
$inputs[0] = array();
|
||||
$expect[0] = array('dir' => 'ltr');
|
||||
|
||||
// leave existing dir alone
|
||||
$inputs[1] = array('dir' => 'rtl');
|
||||
$expect[1] = array('dir' => 'rtl');
|
||||
|
||||
$config_rtl = HTMLPurifier_Config::createDefault();
|
||||
$config_rtl->set('Attr', 'DefaultTextDir', 'rtl');
|
||||
$inputs[2] = array();
|
||||
$expect[2] = array('dir' => 'rtl');
|
||||
$config[2] = $config_rtl;
|
||||
|
||||
$this->assertTransform($inputs, $expect, $config);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
35
tests/HTMLPurifier/AttrTransform/ImgRequiredTest.php
Normal file
35
tests/HTMLPurifier/AttrTransform/ImgRequiredTest.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
require_once 'HTMLPurifier/AttrTransform/ImgRequired.php';
|
||||
|
||||
class HTMLPurifier_AttrTransform_ImgRequiredTest extends HTMLPurifier_AttrTransformHarness
|
||||
{
|
||||
|
||||
function test() {
|
||||
|
||||
$this->transform = new HTMLPurifier_AttrTransform_ImgRequired();
|
||||
|
||||
$inputs = $expect = $config = array();
|
||||
|
||||
$inputs[0] = array();
|
||||
$expect[0] = array('src' => '', 'alt' => 'Invalid image');
|
||||
|
||||
$inputs[1] = array();
|
||||
$expect[1] = array('src' => 'blank.png', 'alt' => 'Pawned!');
|
||||
$config[1] = HTMLPurifier_Config::createDefault();
|
||||
$config[1]->set('Attr', 'DefaultInvalidImage', 'blank.png');
|
||||
$config[1]->set('Attr', 'DefaultInvalidImageAlt', 'Pawned!');
|
||||
|
||||
$inputs[2] = array('src' => '/path/to/foobar.png');
|
||||
$expect[2] = array('src' => '/path/to/foobar.png', 'alt' => 'foobar.png');
|
||||
|
||||
$inputs[3] = array('alt' => 'intrigue');
|
||||
$expect[3] = array('src' => '', 'alt' => 'intrigue');
|
||||
|
||||
$this->assertTransform($inputs, $expect, $config);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
?>
|
@@ -5,14 +5,13 @@ class HTMLPurifier_AttrTransformHarness extends UnitTestCase
|
||||
|
||||
var $transform;
|
||||
|
||||
function assertTransform($inputs, $expect) {
|
||||
function assertTransform($inputs, $expect, $config = array()) {
|
||||
$default_config = HTMLPurifier_Config::createDefault();
|
||||
foreach ($inputs as $i => $input) {
|
||||
$result = $this->transform->transform($input);
|
||||
if ($expect[$i] === true) {
|
||||
$this->assertEqual($input, $result, "Test $i: %s");
|
||||
} else {
|
||||
$this->assertEqual($expect[$i], $result, "Test $i: %s");
|
||||
}
|
||||
if (!isset($config[$i])) $config[$i] = $default_config;
|
||||
$result = $this->transform->transform($input, $config[$i]);
|
||||
if ($expect[$i] === true) $expect[$i] = $input;
|
||||
$this->assertEqual($expect[$i], $result, "Test $i: %s");
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -12,6 +12,9 @@ class HTMLPurifier_Strategy_ValidateAttributesTest extends
|
||||
|
||||
$strategy = new HTMLPurifier_Strategy_ValidateAttributes();
|
||||
|
||||
// attribute order is VERY fragile, perhaps we should define
|
||||
// an ordering scheme!
|
||||
|
||||
$inputs = array();
|
||||
$expect = array();
|
||||
$config = array();
|
||||
@@ -68,8 +71,9 @@ class HTMLPurifier_Strategy_ValidateAttributesTest extends
|
||||
$expect[12] = '<h1 style="text-align:center;">Centered Headline</h1>';
|
||||
|
||||
// test table
|
||||
$inputs[13] = <<<HTML
|
||||
<table frame="above" rules="rows" summary="A test table" border="2" cellpadding="5%" cellspacing="3" width="100%">
|
||||
$inputs[13] =
|
||||
|
||||
'<table frame="above" rules="rows" summary="A test table" border="2" cellpadding="5%" cellspacing="3" width="100%">
|
||||
<col align="right" width="4*" />
|
||||
<col charoff="5" align="char" width="1*" />
|
||||
<tr valign="top">
|
||||
@@ -83,8 +87,8 @@ class HTMLPurifier_Strategy_ValidateAttributesTest extends
|
||||
<tr>
|
||||
<td colspan="2">Taken off the market</td>
|
||||
</tr>
|
||||
</table>
|
||||
HTML;
|
||||
</table>';
|
||||
|
||||
$expect[13] = $inputs[13];
|
||||
|
||||
// test URI
|
||||
@@ -95,6 +99,27 @@ HTML;
|
||||
$inputs[15] = '<a href="javascript:badstuff();">Google</a>';
|
||||
$expect[15] = '<a>Google</a>';
|
||||
|
||||
// test required attributes for img
|
||||
$inputs[16] = '<img />';
|
||||
$expect[16] = '<img src="" alt="Invalid image" />';
|
||||
|
||||
$inputs[17] = '<img src="foobar.jpg" />';
|
||||
$expect[17] = '<img src="foobar.jpg" alt="foobar.jpg" />';
|
||||
|
||||
$inputs[18] = '<img alt="pretty picture" />';
|
||||
$expect[18] = '<img alt="pretty picture" src="" />';
|
||||
|
||||
// test required attributes for bdo
|
||||
$inputs[19] = '<bdo>Go left.</bdo>';
|
||||
$expect[19] = '<bdo dir="ltr">Go left.</bdo>';
|
||||
|
||||
$inputs[20] = '<bdo dir="blahblah">Invalid value!</bdo>';
|
||||
$expect[20] = '<bdo dir="ltr">Invalid value!</bdo>';
|
||||
|
||||
// comparison check for test 20
|
||||
$inputs[21] = '<span dir="blahblah">Invalid value!</span>';
|
||||
$expect[21] = '<span>Invalid value!</span>';
|
||||
|
||||
$this->assertStrategyWorks($strategy, $inputs, $expect, $config);
|
||||
|
||||
}
|
||||
|
@@ -74,6 +74,8 @@ $test_files[] = 'IDAccumulatorTest.php';
|
||||
$test_files[] = 'TagTransformTest.php';
|
||||
$test_files[] = 'AttrTransform/LangTest.php';
|
||||
$test_files[] = 'AttrTransform/TextAlignTest.php';
|
||||
$test_files[] = 'AttrTransform/BdoDirTest.php';
|
||||
$test_files[] = 'AttrTransform/ImgRequiredTest.php';
|
||||
$test_files[] = 'URISchemeRegistryTest.php';
|
||||
$test_files[] = 'URISchemeTest.php';
|
||||
|
||||
|
Reference in New Issue
Block a user