diff --git a/NEWS b/NEWS index 6d8e2fcf..bf7b744b 100644 --- a/NEWS +++ b/NEWS @@ -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: diff --git a/library/HTMLPurifier/AttrTransform.php b/library/HTMLPurifier/AttrTransform.php index 3513669a..2fa07b47 100644 --- a/library/HTMLPurifier/AttrTransform.php +++ b/library/HTMLPurifier/AttrTransform.php @@ -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; + } + } ?> \ No newline at end of file diff --git a/library/HTMLPurifier/AttrTransform/BgColor.php b/library/HTMLPurifier/AttrTransform/BgColor.php index abfd0342..a7bb2b45 100644 --- a/library/HTMLPurifier/AttrTransform/BgColor.php +++ b/library/HTMLPurifier/AttrTransform/BgColor.php @@ -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; diff --git a/library/HTMLPurifier/AttrTransform/BoolToCSS.php b/library/HTMLPurifier/AttrTransform/BoolToCSS.php index 77b714a4..f4a16a7f 100644 --- a/library/HTMLPurifier/AttrTransform/BoolToCSS.php +++ b/library/HTMLPurifier/AttrTransform/BoolToCSS.php @@ -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; - } } diff --git a/library/HTMLPurifier/AttrTransform/Border.php b/library/HTMLPurifier/AttrTransform/Border.php index 0b745d30..10c62e3c 100644 --- a/library/HTMLPurifier/AttrTransform/Border.php +++ b/library/HTMLPurifier/AttrTransform/Border.php @@ -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; - } } diff --git a/library/HTMLPurifier/AttrTransform/ImgSpace.php b/library/HTMLPurifier/AttrTransform/ImgSpace.php index aa02b320..53c787e2 100644 --- a/library/HTMLPurifier/AttrTransform/ImgSpace.php +++ b/library/HTMLPurifier/AttrTransform/ImgSpace.php @@ -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; diff --git a/library/HTMLPurifier/AttrTransform/Length.php b/library/HTMLPurifier/AttrTransform/Length.php index 16d3d1d8..2292aa13 100644 --- a/library/HTMLPurifier/AttrTransform/Length.php +++ b/library/HTMLPurifier/AttrTransform/Length.php @@ -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; } diff --git a/library/HTMLPurifier/AttrTransform/Name.php b/library/HTMLPurifier/AttrTransform/Name.php index 0f815b69..f14c1479 100644 --- a/library/HTMLPurifier/AttrTransform/Name.php +++ b/library/HTMLPurifier/AttrTransform/Name.php @@ -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; - } } diff --git a/library/HTMLPurifier/AttrTransform/TextAlign.php b/library/HTMLPurifier/AttrTransform/TextAlign.php index d0633279..de8dcb48 100644 --- a/library/HTMLPurifier/AttrTransform/TextAlign.php +++ b/library/HTMLPurifier/AttrTransform/TextAlign.php @@ -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; diff --git a/tests/HTMLPurifier/AttrTransformTest.php b/tests/HTMLPurifier/AttrTransformTest.php new file mode 100644 index 00000000..fac01633 --- /dev/null +++ b/tests/HTMLPurifier/AttrTransformTest.php @@ -0,0 +1,42 @@ +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); + + } + +} + +?> \ No newline at end of file diff --git a/tests/test_files.php b/tests/test_files.php index 45c32611..14c4b756 100644 --- a/tests/test_files.php +++ b/tests/test_files.php @@ -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';