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] = '