0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2024-09-18 18:25:18 +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.
. Demo script removed: it has been added to the website's repository
. Basic.php script modified to work out of the box
. Refactor AttrTransform classes to reduce duplication
1.6.0, released 2007-04-01
! Support for most common deprecated attributes via transformations:

View File

@ -29,6 +29,30 @@ class HTMLPurifier_AttrTransform
function transform($attr, $config, &$context) {
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;
$bgcolor = $attr['bgcolor'];
unset($attr['bgcolor']);
$bgcolor = $this->confiscateAttr($attr, 'bgcolor');
// some validation should happen here
$attr['style'] = isset($attr['style']) ? $attr['style'] : '';
$attr['style'] = "background-color:$bgcolor;" . $attr['style'];
$this->prependCSS($attr, "background-color:$bgcolor;");
return $attr;

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -12,8 +12,8 @@ extends HTMLPurifier_AttrTransform {
if (!isset($attr['align'])) return $attr;
$align = strtolower(trim($attr['align']));
unset($attr['align']);
$align = $this->confiscateAttr($attr, 'align');
$align = strtolower(trim($align));
$values = array('left' => 1,
'right' => 1,
@ -24,8 +24,7 @@ extends HTMLPurifier_AttrTransform {
return $attr;
}
$attr['style'] = isset($attr['style']) ? $attr['style'] : '';
$attr['style'] = "text-align:$align;" . $attr['style'];
$this->prependCSS($attr, "text-align:$align;");
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/URITest.php';
$test_files[] = 'AttrDefTest.php';
$test_files[] = 'AttrTransformTest.php';
$test_files[] = 'AttrTransform/BdoDirTest.php';
$test_files[] = 'AttrTransform/BgColorTest.php';
$test_files[] = 'AttrTransform/BoolToCSSTest.php';