0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2025-03-11 17:18:44 +00:00

Added ValidateAttributes strategy and associated unit tests. Amended Generator with some sanity checks. Made Definition include all necessary definitions. Note the two elements (bdo and br) that only use coreattrs.

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@132 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang 2006-07-30 18:37:42 +00:00
parent bb0435bdd4
commit 70bd80e66a
7 changed files with 121 additions and 6 deletions

@ -74,14 +74,14 @@ be translated intelligently -->
>
<!ATTLIST bdo
%coreattrs;
%coreattrs; // !#!
lang %LanguageCode; #IMPLIED
xml:lang %LanguageCode; #IMPLIED
dir (ltr|rtl) #REQUIRED
>
<!ATTLIST br
%coreattrs;
%coreattrs; // !#!
DEPRECATED clear (left|all|right|none) "none"
>

@ -1,6 +1,8 @@
<?php
require_once 'HTMLPurifier/AttrDef.php';
require_once 'HTMLPurifier/AttrDef/Enum.php';
require_once 'HTMLPurifier/AttrDef/ID.php';
require_once 'HTMLPurifier/ChildDef.php';
require_once 'HTMLPurifier/Generator.php';
require_once 'HTMLPurifier/Token.php';
@ -156,8 +158,16 @@ class HTMLPurifier_Definition
$this->info['child']['a'] = $e_a_content;
// attribute info
// this doesn't include REQUIRED declarations, those are handled
// by the transform classes
$a_dir = new HTMLPurifier_AttrDef_Enum(array('ltr','rtl'), false);
// attrs, included in almost every single one except for a few
$this->info['attr']['*'] = array(
// core attrs
'id' => new HTMLPurifier_AttrDef_ID(),
// i18n
'dir' => new HTMLPurifier_AttrDef_Enum(array('ltr','rtl'), false),
);
}

@ -5,6 +5,7 @@ class HTMLPurifier_Generator
function generateFromTokens($tokens) {
$html = '';
if (!$tokens) return '';
foreach ($tokens as $token) {
$html .= $this->generateFromToken($token);
}

@ -0,0 +1,52 @@
<?php
require_once 'HTMLPurifier/Strategy.php';
require_once 'HTMLPurifier/Definition.php';
require_once 'HTMLPurifier/IDAccumulator.php';
class HTMLPurifier_Strategy_ValidateAttributes extends HTMLPurifier_Strategy
{
var $definition;
function HTMLPurifier_Strategy_ValidateAttributes() {
$this->definition = HTMLPurifier_Definition::instance();
}
function execute($tokens) {
$accumulator = new HTMLPurifier_IDAccumulator();
$d_defs = $this->definition->info['attr']['*'];
foreach ($tokens as $key => $token) {
if ($token->type !== 'start' && $token->type !== 'end') continue;
$name = $token->name;
$attr = $token->attributes;
$defs = isset($this->definition->info['attr'][$name]) ?
$this->definition->attr[$name] : array();
$changed = false;
foreach ($attr as $attr_key => $value) {
if ( isset($defs[$attr_key]) ) {
if (!$defs[$attr_key]) {
$result = false;
} else {
$result = $defs[$attr_key]->validate($value, $accumulator);
}
} elseif ( isset($d_defs[$attr_key]) ) {
$result = $d_defs[$attr_key]->validate($value, $accumulator);
} else {
$result = false;
}
if (!$result) {
$changed = true;
unset($attr[$attr_key]);
}
}
if ($changed) {
$tokens[$key]->attributes = $attr;
}
}
return $tokens;
}
}
?>

@ -74,13 +74,25 @@ class HTMLPurifier_GeneratorTest extends UnitTestCase
function test_generateFromTokens() {
$tokens = array(
$inputs = array();
$expect = array();
$inputs[0] = array(
new HTMLPurifier_Token_Start('b'),
new HTMLPurifier_Token_Text('Foobar!'),
new HTMLPurifier_Token_End('b')
);
$expect = '<b>Foobar!</b>';
$this->assertEqual($expect, $this->gen->generateFromTokens($tokens));
$expect[0] = '<b>Foobar!</b>';
$inputs[1] = array();
$expect[1] = '';
foreach ($inputs as $i => $input) {
$result = $this->gen->generateFromTokens($input);
$this->assertEqual($expect[$i], $result);
paintIf($result, $result != $expect[$i]);
}
}

@ -0,0 +1,39 @@
<?php
require_once('HTMLPurifier/StrategyAbstractTest.php');
require_once('HTMLPurifier/Strategy/ValidateAttributes.php');
class HTMLPurifier_Strategy_ValidateAttributesTest extends
HTMLPurifier_StrategyAbstractTest
{
function test() {
$strategy = new HTMLPurifier_Strategy_ValidateAttributes();
$inputs = array();
$expect = array();
$inputs[0] = '';
$expect[0] = '';
$inputs[1] = '<div id="valid">Preserve the ID.</div>';
$expect[1] = $inputs[1];
$inputs[2] = '<div id="0invalid">Kill the ID.</div>';
$expect[2] = '<div>Kill the ID.</div>';
// test accumulator
$inputs[3] = '<div id="valid">Valid</div><div id="valid">Invalid</div>';
$expect[3] = '<div id="valid">Valid</div><div>Invalid</div>';
$inputs[4] = '<span dir="up-to-down">Bad dir.</span>';
$expect[4] = '<span>Bad dir.</span>';
$this->assertStrategyWorks($strategy, $inputs, $expect);
}
}
?>

@ -32,6 +32,7 @@ $test->addTestFile('HTMLPurifier/Strategy/MakeWellFormedTest.php');
$test->addTestFile('HTMLPurifier/Strategy/FixNestingTest.php');
$test->addTestFile('HTMLPurifier/Strategy/CompositeTest.php');
$test->addTestFile('HTMLPurifier/Strategy/CoreTest.php');
$test->addTestFile('HTMLPurifier/Strategy/ValidateAttributesTest.php');
$test->addTestFile('HTMLPurifier/AttrDef/EnumTest.php');
$test->addTestFile('HTMLPurifier/AttrDef/IDTest.php');
$test->addTestFile('HTMLPurifier/IDAccumulatorTest.php');