mirror of
https://github.com/ezyang/htmlpurifier.git
synced 2025-01-05 14:11:52 +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:
parent
8a23710405
commit
9d390fca56
@ -5,24 +5,18 @@ require_once 'HTMLPurifier/AttrTransform.php';
|
|||||||
class HTMLPurifier_AttrTransform_Lang extends HTMLPurifier_AttrTransform
|
class HTMLPurifier_AttrTransform_Lang extends HTMLPurifier_AttrTransform
|
||||||
{
|
{
|
||||||
|
|
||||||
function transform($token) {
|
function transform($attr) {
|
||||||
|
|
||||||
$lang = isset($token->attributes['lang']) ?
|
$lang = isset($attr['lang']) ? $attr['lang'] : false;
|
||||||
$token->attributes['lang'] : false;
|
$xml_lang = isset($attr['xml:lang']) ? $attr['xml: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();
|
|
||||||
|
|
||||||
if ($lang !== false && $xml_lang === false) {
|
if ($lang !== false && $xml_lang === false) {
|
||||||
$new_token->attributes['xml:lang'] = $lang;
|
$attr['xml:lang'] = $lang;
|
||||||
} elseif ($xml_lang !== false) {
|
} elseif ($xml_lang !== false) {
|
||||||
$new_token->attributes['lang'] = $xml_lang;
|
$attr['lang'] = $xml_lang;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $new_token;
|
return $attr;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -31,13 +31,13 @@ class HTMLPurifier_Strategy_ValidateAttributes extends HTMLPurifier_Strategy
|
|||||||
// DEFINITION CALL
|
// DEFINITION CALL
|
||||||
$defs = $this->definition->info[$token->name]->attr;
|
$defs = $this->definition->info[$token->name]->attr;
|
||||||
|
|
||||||
|
$attr = $token->attributes;
|
||||||
|
|
||||||
// DEFINITION CALL
|
// DEFINITION CALL
|
||||||
foreach ($this->definition->info_attr_transform as $transformer) {
|
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) {
|
foreach ($attr as $attr_key => $value) {
|
||||||
|
|
||||||
// call the definition
|
// call the definition
|
||||||
@ -55,21 +55,20 @@ class HTMLPurifier_Strategy_ValidateAttributes extends HTMLPurifier_Strategy
|
|||||||
|
|
||||||
// put the results into effect
|
// put the results into effect
|
||||||
if ($result === false || $result === null) {
|
if ($result === false || $result === null) {
|
||||||
$changed = true;
|
|
||||||
unset($attr[$attr_key]);
|
unset($attr[$attr_key]);
|
||||||
} elseif (is_string($result)) {
|
} elseif (is_string($result)) {
|
||||||
// simple substitution
|
// simple substitution
|
||||||
$changed = true;
|
|
||||||
$attr[$attr_key] = $result;
|
$attr[$attr_key] = $result;
|
||||||
}
|
}
|
||||||
// we'd also want slightly more complicated substitution,
|
// we'd also want slightly more complicated substitution,
|
||||||
// although we're not sure how colliding attributes would
|
// although we're not sure how colliding attributes would
|
||||||
// resolve
|
// resolve
|
||||||
}
|
}
|
||||||
if ($changed) {
|
|
||||||
|
// commit changes
|
||||||
|
// could interfere with flyweight implementation
|
||||||
$tokens[$key]->attributes = $attr;
|
$tokens[$key]->attributes = $attr;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return $tokens;
|
return $tokens;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,57 +1,41 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
require_once 'HTMLPurifier/Token.php';
|
require_once 'HTMLPurifier/Token.php';
|
||||||
|
require_once 'HTMLPurifier/AttrTransformHarness.php';
|
||||||
require_once 'HTMLPurifier/AttrTransform/Lang.php';
|
require_once 'HTMLPurifier/AttrTransform/Lang.php';
|
||||||
|
|
||||||
class HTMLPurifier_AttrTransform_LangTest extends UnitTestCase
|
class HTMLPurifier_AttrTransform_LangTest
|
||||||
|
extends HTMLPurifier_AttrTransformHarness
|
||||||
{
|
{
|
||||||
|
|
||||||
function test() {
|
function test() {
|
||||||
|
|
||||||
$transform = new HTMLPurifier_AttrTransform_Lang();
|
$this->transform = new HTMLPurifier_AttrTransform_Lang();
|
||||||
|
|
||||||
$inputs = array();
|
$inputs = array();
|
||||||
$expect = array();
|
$expect = array();
|
||||||
|
|
||||||
// leave non-lang'ed elements alone
|
// leave non-lang'ed elements alone
|
||||||
$inputs[0] = new HTMLPurifier_Token_Start('b');
|
$inputs[0] = array();
|
||||||
$expect[0] = $inputs[0];
|
$expect[0] = true;
|
||||||
|
|
||||||
// copy lang to xml:lang
|
// copy lang to xml:lang
|
||||||
$inputs[1] = new HTMLPurifier_Token_Start('span',
|
$inputs[1] = array('lang' => 'en');
|
||||||
array('lang' => 'en'));
|
$expect[1] = array('lang' => 'en', 'xml:lang' => 'en');
|
||||||
$expect[1] = new HTMLPurifier_Token_Start('span',
|
|
||||||
array('lang' => 'en',
|
|
||||||
'xml:lang' => 'en'));
|
|
||||||
|
|
||||||
// empty tags must work too, also test attribute preservation
|
// preserve attributes
|
||||||
$inputs[2] = new HTMLPurifier_Token_Empty('img',
|
$inputs[2] = array('src' => 'vert.png', 'lang' => 'fr');
|
||||||
array('src' => 'seine.png',
|
$expect[2] = array('src' => 'vert.png', 'lang' => 'fr', 'xml:lang' => 'fr');
|
||||||
'lang' => 'fr'));
|
|
||||||
$expect[2] = new HTMLPurifier_Token_Empty('img',
|
|
||||||
array('src' => 'seine.png',
|
|
||||||
'lang' => 'fr',
|
|
||||||
'xml:lang' => 'fr'));
|
|
||||||
|
|
||||||
// copy xml:lang to lang
|
// copy xml:lang to lang
|
||||||
$inputs[3] = new HTMLPurifier_Token_Start('span',
|
$inputs[3] = array('xml:lang' => 'en');
|
||||||
array('xml:lang' => 'en'));
|
$expect[3] = array('lang' => 'en', 'xml:lang' => 'en');
|
||||||
$expect[3] = new HTMLPurifier_Token_Start('span',
|
|
||||||
array('lang' => 'en',
|
|
||||||
'xml:lang' => 'en'));
|
|
||||||
|
|
||||||
// both set, override lang with xml:lang
|
// both set, override lang with xml:lang
|
||||||
$inputs[4] = new HTMLPurifier_Token_Start('span',
|
$inputs[4] = array('lang' => 'fr', 'xml:lang' => 'de');
|
||||||
array('lang' => 'fr',
|
$expect[4] = array('lang' => 'de', 'xml:lang' => 'de');
|
||||||
'xml:lang' => 'de'));
|
|
||||||
$expect[4] = new HTMLPurifier_Token_Start('span',
|
|
||||||
array('lang' => 'de',
|
|
||||||
'xml:lang' => 'de'));
|
|
||||||
|
|
||||||
foreach ($inputs as $i => $input) {
|
$this->assertTransform($inputs, $expect);
|
||||||
$result = $transform->transform($input);
|
|
||||||
$this->assertEqual($expect[$i], $result, "Test $i: %s");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
21
tests/HTMLPurifier/AttrTransformHarness.php
Normal file
21
tests/HTMLPurifier/AttrTransformHarness.php
Normal 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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
Loading…
Reference in New Issue
Block a user