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

[1.6.1] Refactor AttrTransform to reduce duplication.

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1016 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang 2007-05-05 02:25:55 +00:00
parent bbea02f55c
commit 0426985c81
11 changed files with 84 additions and 47 deletions

1
NEWS
View File

@ -34,6 +34,7 @@ NEWS ( CHANGELOG and HISTORY ) HTMLPurifier
an empty string in them. an empty string in them.
. Demo script removed: it has been added to the website's repository . Demo script removed: it has been added to the website's repository
. Basic.php script modified to work out of the box . Basic.php script modified to work out of the box
. Refactor AttrTransform classes to reduce duplication
1.6.0, released 2007-04-01 1.6.0, released 2007-04-01
! Support for most common deprecated attributes via transformations: ! Support for most common deprecated attributes via transformations:

View File

@ -29,6 +29,30 @@ class HTMLPurifier_AttrTransform
function transform($attr, $config, &$context) { function transform($attr, $config, &$context) {
trigger_error('Cannot call abstract function', E_USER_ERROR); trigger_error('Cannot call abstract function', E_USER_ERROR);
} }
/**
* Prepends CSS properties to the style attribute, creating the
* attribute if it doesn't exist.
* @param $attr Attribute array to process (passed by reference)
* @param $css CSS to prepend
*/
function prependCSS(&$attr, $css) {
$attr['style'] = isset($attr['style']) ? $attr['style'] : '';
$attr['style'] = $css . $attr['style'];
}
/**
* Retrieves and removes an attribute
* @param $attr Attribute array to process (passed by reference)
* @param $key Key of attribute to confiscate
*/
function confiscateAttr(&$attr, $key) {
if (!isset($attr[$key])) return null;
$value = $attr[$key];
unset($attr[$key]);
return $value;
}
} }
?> ?>

View File

@ -12,12 +12,10 @@ extends HTMLPurifier_AttrTransform {
if (!isset($attr['bgcolor'])) return $attr; if (!isset($attr['bgcolor'])) return $attr;
$bgcolor = $attr['bgcolor']; $bgcolor = $this->confiscateAttr($attr, 'bgcolor');
unset($attr['bgcolor']);
// some validation should happen here // some validation should happen here
$attr['style'] = isset($attr['style']) ? $attr['style'] : ''; $this->prependCSS($attr, "background-color:$bgcolor;");
$attr['style'] = "background-color:$bgcolor;" . $attr['style'];
return $attr; return $attr;

View File

@ -28,13 +28,10 @@ extends HTMLPurifier_AttrTransform {
} }
function transform($attr, $config, &$context) { function transform($attr, $config, &$context) {
if (!isset($attr[$this->attr])) return $attr; if (!isset($attr[$this->attr])) return $attr;
unset($attr[$this->attr]); unset($attr[$this->attr]);
if (!isset($attr['style'])) $attr['style'] = ''; $this->prependCSS($attr, $this->css);
$attr['style'] = $this->css . $attr['style'];
return $attr; return $attr;
} }
} }

View File

@ -5,22 +5,14 @@ require_once 'HTMLPurifier/AttrTransform.php';
/** /**
* Pre-transform that changes deprecated border attribute to CSS. * Pre-transform that changes deprecated border attribute to CSS.
*/ */
class HTMLPurifier_AttrTransform_Border class HTMLPurifier_AttrTransform_Border extends HTMLPurifier_AttrTransform {
extends HTMLPurifier_AttrTransform {
function transform($attr, $config, &$context) { function transform($attr, $config, &$context) {
if (!isset($attr['border'])) return $attr; if (!isset($attr['border'])) return $attr;
$border_width = $this->confiscateAttr($attr, 'border');
$border_width = $attr['border'];
unset($attr['border']);
// some validation should happen here // some validation should happen here
$this->prependCSS($attr, "border:{$border_width}px solid;");
$attr['style'] = isset($attr['style']) ? $attr['style'] : '';
$attr['style'] = "border:{$border_width}px solid;" . $attr['style'];
return $attr; return $attr;
} }
} }

View File

@ -25,21 +25,18 @@ extends HTMLPurifier_AttrTransform {
if (!isset($attr[$this->attr])) return $attr; if (!isset($attr[$this->attr])) return $attr;
$width = $attr[$this->attr]; $width = $this->confiscateAttr($attr, $this->attr);
unset($attr[$this->attr]);
// some validation could happen here // some validation could happen here
if (!isset($this->css[$this->attr])) return $attr; if (!isset($this->css[$this->attr])) return $attr;
$attr['style'] = isset($attr['style']) ? $attr['style'] : '';
$style = ''; $style = '';
foreach ($this->css[$this->attr] as $suffix) { foreach ($this->css[$this->attr] as $suffix) {
$property = "margin-$suffix"; $property = "margin-$suffix";
$style .= "$property:{$width}px;"; $style .= "$property:{$width}px;";
} }
$attr['style'] = $style . $attr['style']; $this->prependCSS($attr, $style);
return $attr; return $attr;

View File

@ -18,13 +18,9 @@ class HTMLPurifier_AttrTransform_Length extends HTMLPurifier_AttrTransform
function transform($attr, $config, &$context) { function transform($attr, $config, &$context) {
if (!isset($attr[$this->name])) return $attr; if (!isset($attr[$this->name])) return $attr;
$length = $attr[$this->name]; $length = $this->confiscateAttr($attr, $this->name);
unset($attr[$this->name]);
if(ctype_digit($length)) $length .= 'px'; if(ctype_digit($length)) $length .= 'px';
$this->prependCSS($attr, $this->cssName . ":$length;");
$attr['style'] = isset($attr['style']) ? $attr['style'] : '';
$attr['style'] = $this->cssName . ":$length;" . $attr['style'];
return $attr; return $attr;
} }

View File

@ -9,23 +9,13 @@ class HTMLPurifier_AttrTransform_Name extends HTMLPurifier_AttrTransform
{ {
function transform($attr, $config, &$context) { function transform($attr, $config, &$context) {
if (!isset($attr['name'])) return $attr; if (!isset($attr['name'])) return $attr;
$id = $this->confiscateAttr($attr, 'name');
$name = $attr['name']; if ( isset($attr['id'])) return $attr;
unset($attr['name']); $attr['id'] = $id;
if (isset($attr['id'])) {
// ID already set, discard name
return $attr; return $attr;
} }
$attr['id'] = $name;
return $attr;
}
} }
?> ?>

View File

@ -12,8 +12,8 @@ extends HTMLPurifier_AttrTransform {
if (!isset($attr['align'])) return $attr; if (!isset($attr['align'])) return $attr;
$align = strtolower(trim($attr['align'])); $align = $this->confiscateAttr($attr, 'align');
unset($attr['align']); $align = strtolower(trim($align));
$values = array('left' => 1, $values = array('left' => 1,
'right' => 1, 'right' => 1,
@ -24,8 +24,7 @@ extends HTMLPurifier_AttrTransform {
return $attr; return $attr;
} }
$attr['style'] = isset($attr['style']) ? $attr['style'] : ''; $this->prependCSS($attr, "text-align:$align;");
$attr['style'] = "text-align:$align;" . $attr['style'];
return $attr; return $attr;

View File

@ -0,0 +1,42 @@
<?php
require_once 'HTMLPurifier/AttrTransform.php';
class HTMLPurifier_AttrTransformTest extends UnitTestCase
{
function test_prependCSS() {
$t = new HTMLPurifier_AttrTransform();
$attr = array();
$t->prependCSS($attr, 'style:new;');
$this->assertIdentical(array('style' => 'style:new;'), $attr);
$attr = array('style' => 'style:original;');
$t->prependCSS($attr, 'style:new;');
$this->assertIdentical(array('style' => 'style:new;style:original;'), $attr);
$attr = array('style' => 'style:original;', 'misc' => 'un-related');
$t->prependCSS($attr, 'style:new;');
$this->assertIdentical(array('style' => 'style:new;style:original;', 'misc' => 'un-related'), $attr);
}
function test_confiscateAttr() {
$t = new HTMLPurifier_AttrTransform();
$attr = array('flavor' => 'sweet');
$this->assertIdentical('sweet', $t->confiscateAttr($attr, 'flavor'));
$this->assertIdentical(array(), $attr);
$attr = array('flavor' => 'sweet');
$this->assertIdentical(null, $t->confiscateAttr($attr, 'color'));
$this->assertIdentical(array('flavor' => 'sweet'), $attr);
}
}
?>

View File

@ -35,6 +35,7 @@ $test_files[] = 'AttrDef/URI/IPv4Test.php';
$test_files[] = 'AttrDef/URI/IPv6Test.php'; $test_files[] = 'AttrDef/URI/IPv6Test.php';
$test_files[] = 'AttrDef/URITest.php'; $test_files[] = 'AttrDef/URITest.php';
$test_files[] = 'AttrDefTest.php'; $test_files[] = 'AttrDefTest.php';
$test_files[] = 'AttrTransformTest.php';
$test_files[] = 'AttrTransform/BdoDirTest.php'; $test_files[] = 'AttrTransform/BdoDirTest.php';
$test_files[] = 'AttrTransform/BgColorTest.php'; $test_files[] = 'AttrTransform/BgColorTest.php';
$test_files[] = 'AttrTransform/BoolToCSSTest.php'; $test_files[] = 'AttrTransform/BoolToCSSTest.php';