0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2024-11-10 07:38:41 +00:00

Implement Composite attribute definition.

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@228 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang 2006-08-13 18:16:30 +00:00
parent 441a0cbe94
commit 8b45c7601a
3 changed files with 104 additions and 0 deletions

View File

@ -0,0 +1,22 @@
<?php
class HTMLPurifier_AttrDef_Composite extends HTMLPurifier_AttrDef
{
var $defs;
function HTMLPurifier_AttrDef_Composite(&$defs) {
$this->defs =& $defs;
}
function validate($string, $config, &$context) {
foreach ($this->defs as $i => $def) {
$result = $this->defs[$i]->validate($string, $config, $context);
if ($result !== false) return $result;
}
return false;
}
}
?>

View File

@ -0,0 +1,81 @@
<?php
require_once 'HTMLPurifier/AttrDef/Composite.php';
class HTMLPurifier_AttrDef_CompositeTest extends HTMLPurifier_AttrDefHarness
{
var $def1, $def2;
function test() {
generate_mock_once('HTMLPurifier_AttrDef');
$config = HTMLPurifier_Config::createDefault();
$context = new HTMLPurifier_AttrContext();
// first test: value properly validates on first definition
// so second def is never called
$def1 =& new HTMLPurifier_AttrDefMock($this);
$def2 =& new HTMLPurifier_AttrDefMock($this);
$defs = array(&$def1, &$def2);
$def =& new HTMLPurifier_AttrDef_Composite($defs);
$input = 'FOOBAR';
$output = 'foobar';
$def1_params = array($input, $config, $context);
$def1->expectOnce('validate', $def1_params);
$def1->setReturnValue('validate', $output, $def1_params);
$def2->expectNever('validate');
$this->assertIdentical($output,
$def->validate($input, $config, $context));
$def1->tally();
$def2->tally();
// second test, first def fails, second def works
$def1 =& new HTMLPurifier_AttrDefMock($this);
$def2 =& new HTMLPurifier_AttrDefMock($this);
$defs = array(&$def1, &$def2);
$def =& new HTMLPurifier_AttrDef_Composite($defs);
$input = 'BOOMA';
$output = 'booma';
$def_params = array($input, $config, $context);
$def1->expectOnce('validate', $def_params);
$def1->setReturnValue('validate', false, $def_params);
$def2->expectOnce('validate', $def_params);
$def2->setReturnValue('validate', $output, $def_params);
$this->assertIdentical($output,
$def->validate($input, $config, $context));
$def1->tally();
$def2->tally();
// third test, all fail, so composite faiils
$def1 =& new HTMLPurifier_AttrDefMock($this);
$def2 =& new HTMLPurifier_AttrDefMock($this);
$defs = array(&$def1, &$def2);
$def =& new HTMLPurifier_AttrDef_Composite($defs);
$input = 'BOOMA';
$output = false;
$def_params = array($input, $config, $context);
$def1->expectOnce('validate', $def_params);
$def1->setReturnValue('validate', false, $def_params);
$def2->expectOnce('validate', $def_params);
$def2->setReturnValue('validate', false, $def_params);
$this->assertIdentical($output,
$def->validate($input, $config, $context));
$def1->tally();
$def2->tally();
}
}
?>

View File

@ -64,6 +64,7 @@ $test_files[] = 'AttrDef/LengthTest.php';
$test_files[] = 'AttrDef/NumberSpanTest.php'; $test_files[] = 'AttrDef/NumberSpanTest.php';
$test_files[] = 'AttrDef/URITest.php'; $test_files[] = 'AttrDef/URITest.php';
$test_files[] = 'AttrDef/CSSTest.php'; $test_files[] = 'AttrDef/CSSTest.php';
$test_files[] = 'AttrDef/CompositeTest.php';
$test_files[] = 'IDAccumulatorTest.php'; $test_files[] = 'IDAccumulatorTest.php';
$test_files[] = 'TagTransformTest.php'; $test_files[] = 'TagTransformTest.php';
$test_files[] = 'AttrTransform/LangTest.php'; $test_files[] = 'AttrTransform/LangTest.php';