0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2024-11-09 23:28:42 +00:00

Refactor AttrTransform_Lang slightly by changing input output from token to attribute.

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@163 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang 2006-08-05 02:16:28 +00:00
parent 8a23710405
commit 9d390fca56
4 changed files with 50 additions and 52 deletions

View File

@ -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;
}

View File

@ -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;
}

View File

@ -1,57 +1,41 @@
<?php
require_once 'HTMLPurifier/Token.php';
require_once 'HTMLPurifier/AttrTransformHarness.php';
require_once 'HTMLPurifier/AttrTransform/Lang.php';
class HTMLPurifier_AttrTransform_LangTest extends UnitTestCase
class HTMLPurifier_AttrTransform_LangTest
extends HTMLPurifier_AttrTransformHarness
{
function test() {
$transform = new HTMLPurifier_AttrTransform_Lang();
$this->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);
}

View File

@ -0,0 +1,21 @@
<?php
class HTMLPurifier_AttrTransformHarness extends UnitTestCase
{
var $transform;
function assertTransform($inputs, $expect) {
foreach ($inputs as $i => $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");
}
}
}
}
?>