diff --git a/library/HTMLPurifier/AttrTransform/Lang.php b/library/HTMLPurifier/AttrTransform/Lang.php index fc0b72ba..dbc5c3c6 100644 --- a/library/HTMLPurifier/AttrTransform/Lang.php +++ b/library/HTMLPurifier/AttrTransform/Lang.php @@ -5,24 +5,18 @@ require_once 'HTMLPurifier/AttrTransform.php'; class HTMLPurifier_AttrTransform_Lang extends HTMLPurifier_AttrTransform { - function transform($token) { + function transform($attr) { - $lang = isset($token->attributes['lang']) ? - $token->attributes['lang'] : false; - $xml_lang = isset($token->attributes['xml:lang']) ? - $token->attributes['xml:lang'] : false; - - if ($lang === false && $xml_lang == false) return $token; - - $new_token = $token->copy(); + $lang = isset($attr['lang']) ? $attr['lang'] : false; + $xml_lang = isset($attr['xml:lang']) ? $attr['xml:lang'] : false; if ($lang !== false && $xml_lang === false) { - $new_token->attributes['xml:lang'] = $lang; + $attr['xml:lang'] = $lang; } elseif ($xml_lang !== false) { - $new_token->attributes['lang'] = $xml_lang; + $attr['lang'] = $xml_lang; } - return $new_token; + return $attr; } diff --git a/library/HTMLPurifier/Strategy/ValidateAttributes.php b/library/HTMLPurifier/Strategy/ValidateAttributes.php index 8793f802..140fa8db 100644 --- a/library/HTMLPurifier/Strategy/ValidateAttributes.php +++ b/library/HTMLPurifier/Strategy/ValidateAttributes.php @@ -31,13 +31,13 @@ class HTMLPurifier_Strategy_ValidateAttributes extends HTMLPurifier_Strategy // DEFINITION CALL $defs = $this->definition->info[$token->name]->attr; + $attr = $token->attributes; + // DEFINITION CALL foreach ($this->definition->info_attr_transform as $transformer) { - $token = $transformer->transform($token); + $attr = $transformer->transform($attr); } - $attr = $token->attributes; - $changed = false; foreach ($attr as $attr_key => $value) { // call the definition @@ -55,20 +55,19 @@ class HTMLPurifier_Strategy_ValidateAttributes extends HTMLPurifier_Strategy // put the results into effect if ($result === false || $result === null) { - $changed = true; unset($attr[$attr_key]); } elseif (is_string($result)) { // simple substitution - $changed = true; $attr[$attr_key] = $result; } // we'd also want slightly more complicated substitution, // although we're not sure how colliding attributes would // resolve } - if ($changed) { - $tokens[$key]->attributes = $attr; - } + + // commit changes + // could interfere with flyweight implementation + $tokens[$key]->attributes = $attr; } return $tokens; } diff --git a/tests/HTMLPurifier/AttrTransform/LangTest.php b/tests/HTMLPurifier/AttrTransform/LangTest.php index fa9273c9..9609e137 100644 --- a/tests/HTMLPurifier/AttrTransform/LangTest.php +++ b/tests/HTMLPurifier/AttrTransform/LangTest.php @@ -1,57 +1,41 @@ transform = new HTMLPurifier_AttrTransform_Lang(); $inputs = array(); $expect = array(); // leave non-lang'ed elements alone - $inputs[0] = new HTMLPurifier_Token_Start('b'); - $expect[0] = $inputs[0]; + $inputs[0] = array(); + $expect[0] = true; // copy lang to xml:lang - $inputs[1] = new HTMLPurifier_Token_Start('span', - array('lang' => 'en')); - $expect[1] = new HTMLPurifier_Token_Start('span', - array('lang' => 'en', - 'xml:lang' => 'en')); + $inputs[1] = array('lang' => 'en'); + $expect[1] = array('lang' => 'en', 'xml:lang' => 'en'); - // empty tags must work too, also test attribute preservation - $inputs[2] = new HTMLPurifier_Token_Empty('img', - array('src' => 'seine.png', - 'lang' => 'fr')); - $expect[2] = new HTMLPurifier_Token_Empty('img', - array('src' => 'seine.png', - 'lang' => 'fr', - 'xml:lang' => 'fr')); + // preserve attributes + $inputs[2] = array('src' => 'vert.png', 'lang' => 'fr'); + $expect[2] = array('src' => 'vert.png', 'lang' => 'fr', 'xml:lang' => 'fr'); // copy xml:lang to lang - $inputs[3] = new HTMLPurifier_Token_Start('span', - array('xml:lang' => 'en')); - $expect[3] = new HTMLPurifier_Token_Start('span', - array('lang' => 'en', - 'xml:lang' => 'en')); + $inputs[3] = array('xml:lang' => 'en'); + $expect[3] = array('lang' => 'en', 'xml:lang' => 'en'); // both set, override lang with xml:lang - $inputs[4] = new HTMLPurifier_Token_Start('span', - array('lang' => 'fr', - 'xml:lang' => 'de')); - $expect[4] = new HTMLPurifier_Token_Start('span', - array('lang' => 'de', - 'xml:lang' => 'de')); + $inputs[4] = array('lang' => 'fr', 'xml:lang' => 'de'); + $expect[4] = array('lang' => 'de', 'xml:lang' => 'de'); - foreach ($inputs as $i => $input) { - $result = $transform->transform($input); - $this->assertEqual($expect[$i], $result, "Test $i: %s"); - } + $this->assertTransform($inputs, $expect); } diff --git a/tests/HTMLPurifier/AttrTransformHarness.php b/tests/HTMLPurifier/AttrTransformHarness.php new file mode 100644 index 00000000..138d9280 --- /dev/null +++ b/tests/HTMLPurifier/AttrTransformHarness.php @@ -0,0 +1,21 @@ + $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"); + } + } + } + +} + +?> \ No newline at end of file