From 70bd80e66a6c304c1ba587e7112bedce81bf5e7a Mon Sep 17 00:00:00 2001 From: "Edward Z. Yang" Date: Sun, 30 Jul 2006 18:37:42 +0000 Subject: [PATCH] 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 --- docs/dtd/xhtml1-purified.dtd | 4 +- library/HTMLPurifier/Definition.php | 12 ++++- library/HTMLPurifier/Generator.php | 1 + .../Strategy/ValidateAttributes.php | 52 +++++++++++++++++++ tests/HTMLPurifier/GeneratorTest.php | 18 +++++-- .../Strategy/ValidateAttributesTest.php | 39 ++++++++++++++ tests/index.php | 1 + 7 files changed, 121 insertions(+), 6 deletions(-) create mode 100644 library/HTMLPurifier/Strategy/ValidateAttributes.php create mode 100644 tests/HTMLPurifier/Strategy/ValidateAttributesTest.php diff --git a/docs/dtd/xhtml1-purified.dtd b/docs/dtd/xhtml1-purified.dtd index 8751b294..abffd273 100644 --- a/docs/dtd/xhtml1-purified.dtd +++ b/docs/dtd/xhtml1-purified.dtd @@ -74,14 +74,14 @@ be translated intelligently --> > diff --git a/library/HTMLPurifier/Definition.php b/library/HTMLPurifier/Definition.php index acbcbbbc..6012bec3 100644 --- a/library/HTMLPurifier/Definition.php +++ b/library/HTMLPurifier/Definition.php @@ -1,6 +1,8 @@ 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), + ); } diff --git a/library/HTMLPurifier/Generator.php b/library/HTMLPurifier/Generator.php index d8eabd61..deecc084 100644 --- a/library/HTMLPurifier/Generator.php +++ b/library/HTMLPurifier/Generator.php @@ -5,6 +5,7 @@ class HTMLPurifier_Generator function generateFromTokens($tokens) { $html = ''; + if (!$tokens) return ''; foreach ($tokens as $token) { $html .= $this->generateFromToken($token); } diff --git a/library/HTMLPurifier/Strategy/ValidateAttributes.php b/library/HTMLPurifier/Strategy/ValidateAttributes.php new file mode 100644 index 00000000..e36dcb3c --- /dev/null +++ b/library/HTMLPurifier/Strategy/ValidateAttributes.php @@ -0,0 +1,52 @@ +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; + } + +} + +?> \ No newline at end of file diff --git a/tests/HTMLPurifier/GeneratorTest.php b/tests/HTMLPurifier/GeneratorTest.php index ac1bb0c1..d93decf0 100644 --- a/tests/HTMLPurifier/GeneratorTest.php +++ b/tests/HTMLPurifier/GeneratorTest.php @@ -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 = 'Foobar!'; - $this->assertEqual($expect, $this->gen->generateFromTokens($tokens)); + $expect[0] = 'Foobar!'; + + $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]); + } + } diff --git a/tests/HTMLPurifier/Strategy/ValidateAttributesTest.php b/tests/HTMLPurifier/Strategy/ValidateAttributesTest.php new file mode 100644 index 00000000..f79bdb81 --- /dev/null +++ b/tests/HTMLPurifier/Strategy/ValidateAttributesTest.php @@ -0,0 +1,39 @@ +Preserve the ID.'; + $expect[1] = $inputs[1]; + + $inputs[2] = '
Kill the ID.
'; + $expect[2] = '
Kill the ID.
'; + + // test accumulator + $inputs[3] = '
Valid
Invalid
'; + $expect[3] = '
Valid
Invalid
'; + + $inputs[4] = 'Bad dir.'; + $expect[4] = 'Bad dir.'; + + $this->assertStrategyWorks($strategy, $inputs, $expect); + + } + +} + +?> \ No newline at end of file diff --git a/tests/index.php b/tests/index.php index d87e2d0d..1c152605 100644 --- a/tests/index.php +++ b/tests/index.php @@ -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');