diff --git a/NEWS b/NEWS index 4013cecc..2a1b0b5f 100644 --- a/NEWS +++ b/NEWS @@ -17,6 +17,8 @@ NEWS ( CHANGELOG and HISTORY ) HTMLPurifier are now preserved. ! %Core.RemoveInvalidImg is now operational, when set to false invalid images will hang around with an empty src +! Support for more deprecated attributes via transformations: + + hspace and vspace in img - Possibly fatal bug with __autoload() fixed in module manager - Invert HTMLModuleManager->addModule() processing order to check prefixes first and then the literal module diff --git a/TODO b/TODO index c023fb24..dc8174e2 100644 --- a/TODO +++ b/TODO @@ -8,7 +8,6 @@ TODO List ========================== 1.6.1 [Oh Dear, We Missed Something!] - # vspace and hspace in img # align in img and table # target in a # noshade and size in hr diff --git a/docs/dev-progress.html b/docs/dev-progress.html index 6981afe5..96949d90 100644 --- a/docs/dev-progress.html +++ b/docs/dev-progress.html @@ -277,7 +277,7 @@ Mozilla on inside and needs -moz-outline, no IE support. compactDL, OL, ULBoolean, needs custom CSS class; rarely used anyway dirBDORequired, insert ltr (or configuration value) if none heightTD, THNear-equiv style 'height', needs px suffix if original was in pixels -hspaceIMGNear-equiv styles 'margin-top' and 'margin-bottom', needs px suffix +hspaceIMGNear-equiv styles 'margin-top' and 'margin-bottom', needs px suffix lang*Copy value to xml:lang nameIMGTurn into ID ATurn into ID @@ -290,7 +290,7 @@ Mozilla on inside and needs -moz-outline, no IE support. OL UL valueLIPoorly supported 'counter-reset', allowed in loose, dropped in strict -vspaceIMGNear-equiv styles 'margin-left' and 'margin-right', needs px suffix, see hspace +vspaceIMGNear-equiv styles 'margin-left' and 'margin-right', needs px suffix, see hspace widthHRNear-equiv style 'width', needs px suffix if original was pixels TD, TH diff --git a/library/HTMLPurifier/AttrTransform/ImgSpace.php b/library/HTMLPurifier/AttrTransform/ImgSpace.php new file mode 100644 index 00000000..aa02b320 --- /dev/null +++ b/library/HTMLPurifier/AttrTransform/ImgSpace.php @@ -0,0 +1,50 @@ + array('left', 'right'), + 'vspace' => array('top', 'bottom') + ); + + function HTMLPurifier_AttrTransform_ImgSpace($attr) { + $this->attr = $attr; + if (!isset($this->css[$attr])) { + trigger_error(htmlspecialchars($attr) . ' is not valid space attribute'); + } + } + + function transform($attr, $config, &$context) { + + if (!isset($attr[$this->attr])) return $attr; + + $width = $attr[$this->attr]; + unset($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']; + + return $attr; + + } + +} + +?> \ No newline at end of file diff --git a/library/HTMLPurifier/HTMLModule/TransformToStrict.php b/library/HTMLPurifier/HTMLModule/TransformToStrict.php index cdbe3733..7b65012e 100644 --- a/library/HTMLPurifier/HTMLModule/TransformToStrict.php +++ b/library/HTMLPurifier/HTMLModule/TransformToStrict.php @@ -12,6 +12,7 @@ require_once 'HTMLPurifier/AttrTransform/BgColor.php'; require_once 'HTMLPurifier/AttrTransform/Border.php'; require_once 'HTMLPurifier/AttrTransform/Name.php'; require_once 'HTMLPurifier/AttrTransform/Length.php'; +require_once 'HTMLPurifier/AttrTransform/ImgSpace.php'; /** * Proprietary module that transforms deprecated elements into Strict @@ -95,6 +96,9 @@ class HTMLPurifier_HTMLModule_TransformToStrict extends HTMLPurifier_HTMLModule $this->info['td']->attr_transform_pre['height'] = $this->info['th']->attr_transform_pre['height'] = new HTMLPurifier_AttrTransform_Length('height'); + $this->info['img']->attr_transform_pre['hspace'] = new HTMLPurifier_AttrTransform_ImgSpace('hspace'); + $this->info['img']->attr_transform_pre['vspace'] = new HTMLPurifier_AttrTransform_ImgSpace('vspace'); + } var $defines_child_def = true; diff --git a/tests/HTMLPurifier/AttrTransform/ImgSpaceTest.php b/tests/HTMLPurifier/AttrTransform/ImgSpaceTest.php new file mode 100644 index 00000000..8426f15c --- /dev/null +++ b/tests/HTMLPurifier/AttrTransform/ImgSpaceTest.php @@ -0,0 +1,57 @@ +obj = new HTMLPurifier_AttrTransform_ImgSpace('vspace'); + + $this->assertResult( array() ); + + $this->assertResult( + array('vspace' => '1'), + array('style' => 'margin-top:1px;margin-bottom:1px;') + ); + + // no validation done here, we expect CSS validator to catch it + $this->assertResult( + array('vspace' => '10%'), + array('style' => 'margin-top:10%px;margin-bottom:10%px;') + ); + + $this->assertResult( + array('vspace' => '23', 'style' => 'font-weight:bold;'), + array('style' => 'margin-top:23px;margin-bottom:23px;font-weight:bold;') + ); + + } + + function testHorizontal() { + $this->obj = new HTMLPurifier_AttrTransform_ImgSpace('hspace'); + $this->assertResult( + array('hspace' => '1'), + array('style' => 'margin-left:1px;margin-right:1px;') + ); + } + + function testInvalid() { + $this->expectError('ispace is not valid space attribute'); + $this->obj = new HTMLPurifier_AttrTransform_ImgSpace('ispace'); + $this->assertResult( + array('ispace' => '1'), + array() + ); + } + +} + +?> \ No newline at end of file diff --git a/tests/HTMLPurifier/Strategy/ValidateAttributesTest.php b/tests/HTMLPurifier/Strategy/ValidateAttributesTest.php index 7d800bab..44c9f630 100644 --- a/tests/HTMLPurifier/Strategy/ValidateAttributesTest.php +++ b/tests/HTMLPurifier/Strategy/ValidateAttributesTest.php @@ -192,6 +192,13 @@ class HTMLPurifier_Strategy_ValidateAttributesTest extends array('Attr.AllowedRel' => 'nofollow') ); + // border + $this->assertResult( + 'foo', + 'foo', + array('Attr.AllowedRel' => 'nofollow') + ); + } } diff --git a/tests/test_files.php b/tests/test_files.php index c2fc532a..543a9dd6 100644 --- a/tests/test_files.php +++ b/tests/test_files.php @@ -38,6 +38,7 @@ $test_files[] = 'AttrTransform/BdoDirTest.php'; $test_files[] = 'AttrTransform/BgColorTest.php'; $test_files[] = 'AttrTransform/BorderTest.php'; $test_files[] = 'AttrTransform/ImgRequiredTest.php'; +$test_files[] = 'AttrTransform/ImgSpaceTest.php'; $test_files[] = 'AttrTransform/LangTest.php'; $test_files[] = 'AttrTransform/LengthTest.php'; $test_files[] = 'AttrTransform/NameTest.php';