From 058f1eba7d8069d1b8045e4ebf0a4b467c28dd1b Mon Sep 17 00:00:00 2001 From: "Edward Z. Yang" Date: Thu, 29 Mar 2007 23:48:54 +0000 Subject: [PATCH] [1.6.0] Implement width/height attribute transforms with Length.php - Also, enabled 'height' CSS attribute git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@922 48356398-32a2-884e-a903-53898d9a118a --- NEWS | 3 ++ docs/dev-progress.html | 8 ++-- library/HTMLPurifier/AttrTransform/Length.php | 33 +++++++++++++++++ library/HTMLPurifier/CSSDefinition.php | 4 +- .../HTMLModule/TransformToStrict.php | 10 ++++- .../HTMLPurifier/AttrTransform/LengthTest.php | 37 +++++++++++++++++++ .../Strategy/ValidateAttributesTest.php | 6 +++ tests/test_files.php | 1 + 8 files changed, 96 insertions(+), 6 deletions(-) create mode 100644 library/HTMLPurifier/AttrTransform/Length.php create mode 100644 tests/HTMLPurifier/AttrTransform/LengthTest.php diff --git a/NEWS b/NEWS index 9098e537..21746f6e 100644 --- a/NEWS +++ b/NEWS @@ -14,7 +14,10 @@ NEWS ( CHANGELOG and HISTORY ) HTMLPurifier + bgcolor in td, th, tr and table + border in img + name in a and img + + width in td, th and hr + + height in td, th + (incomplete) +! Support for CSS attribute 'height' added 1.5.1, unknown release date - Fix segfault in unit test. The problem is not very reproduceable and diff --git a/docs/dev-progress.html b/docs/dev-progress.html index 9587739b..2099c0e8 100644 --- a/docs/dev-progress.html +++ b/docs/dev-progress.html @@ -151,7 +151,7 @@ thead th {text-align:left;padding:0.1em;background-color:#EEE;} will not implement list-item, run-in (Opera only) or table (no IE); inline-block has incomplete IE6 support and requires -moz-inline-box for Mozilla. Unknown target milestone. -heightInteresting, why use it? Unknown target milestone. +heightInteresting, why use it? Unknown target milestone. list-style-imageDangerous? max-heightNo IE 5/6 min-height @@ -276,7 +276,7 @@ Mozilla on inside and needs -moz-outline, no IE support. clearBRNear-equiv style 'clear', transform 'all' into 'both' 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 +heightTD, THNear-equiv style 'height', needs px suffix if original was in pixels hspaceIMGNear-equiv styles 'margin-top' and 'margin-bottom', needs px suffix lang*Copy value to xml:lang nameIMGTurn into ID @@ -291,8 +291,8 @@ Mozilla on inside and needs -moz-outline, no IE support. UL valueLIPoorly supported 'counter-reset', allowed in loose, dropped in strict 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 +widthHRNear-equiv style 'width', needs px suffix if original was pixels + TD, TH diff --git a/library/HTMLPurifier/AttrTransform/Length.php b/library/HTMLPurifier/AttrTransform/Length.php new file mode 100644 index 00000000..16d3d1d8 --- /dev/null +++ b/library/HTMLPurifier/AttrTransform/Length.php @@ -0,0 +1,33 @@ +name = $name; + $this->cssName = $css_name ? $css_name : $name; + } + + function transform($attr, $config, &$context) { + if (!isset($attr[$this->name])) return $attr; + $length = $attr[$this->name]; + unset($attr[$this->name]); + if(ctype_digit($length)) $length .= 'px'; + + $attr['style'] = isset($attr['style']) ? $attr['style'] : ''; + $attr['style'] = $this->cssName . ":$length;" . $attr['style']; + + return $attr; + } + +} + +?> \ No newline at end of file diff --git a/library/HTMLPurifier/CSSDefinition.php b/library/HTMLPurifier/CSSDefinition.php index 55f0adc9..5de49b69 100644 --- a/library/HTMLPurifier/CSSDefinition.php +++ b/library/HTMLPurifier/CSSDefinition.php @@ -162,7 +162,9 @@ class HTMLPurifier_CSSDefinition new HTMLPurifier_AttrDef_CSS_Percentage() )); - $this->info['width'] = new HTMLPurifier_AttrDef_CSS_Composite(array( + $this->info['width'] = + $this->info['height'] = + new HTMLPurifier_AttrDef_CSS_Composite(array( new HTMLPurifier_AttrDef_CSS_Length(true), new HTMLPurifier_AttrDef_CSS_Percentage(true), new HTMLPurifier_AttrDef_Enum(array('auto')) diff --git a/library/HTMLPurifier/HTMLModule/TransformToStrict.php b/library/HTMLPurifier/HTMLModule/TransformToStrict.php index 08a64fa1..cdbe3733 100644 --- a/library/HTMLPurifier/HTMLModule/TransformToStrict.php +++ b/library/HTMLPurifier/HTMLModule/TransformToStrict.php @@ -11,6 +11,7 @@ require_once 'HTMLPurifier/AttrTransform/TextAlign.php'; require_once 'HTMLPurifier/AttrTransform/BgColor.php'; require_once 'HTMLPurifier/AttrTransform/Border.php'; require_once 'HTMLPurifier/AttrTransform/Name.php'; +require_once 'HTMLPurifier/AttrTransform/Length.php'; /** * Proprietary module that transforms deprecated elements into Strict @@ -24,7 +25,7 @@ class HTMLPurifier_HTMLModule_TransformToStrict extends HTMLPurifier_HTMLModule // we're actually modifying these elements, not defining them var $elements = array('h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', - 'blockquote', 'table', 'td', 'th', 'tr', 'img', 'a'); + 'blockquote', 'table', 'td', 'th', 'tr', 'img', 'a', 'hr'); var $info_tag_transform = array( // placeholders, see constructor for definitions @@ -87,6 +88,13 @@ class HTMLPurifier_HTMLModule_TransformToStrict extends HTMLPurifier_HTMLModule $this->info['img']->attr_transform_pre['name'] = $this->info['a']->attr_transform_pre['name'] = new HTMLPurifier_AttrTransform_Name(); + $this->info['td']->attr_transform_pre['width'] = + $this->info['th']->attr_transform_pre['width'] = + $this->info['hr']->attr_transform_pre['width'] = new HTMLPurifier_AttrTransform_Length('width'); + + $this->info['td']->attr_transform_pre['height'] = + $this->info['th']->attr_transform_pre['height'] = new HTMLPurifier_AttrTransform_Length('height'); + } var $defines_child_def = true; diff --git a/tests/HTMLPurifier/AttrTransform/LengthTest.php b/tests/HTMLPurifier/AttrTransform/LengthTest.php new file mode 100644 index 00000000..e0c9474b --- /dev/null +++ b/tests/HTMLPurifier/AttrTransform/LengthTest.php @@ -0,0 +1,37 @@ +obj = new HTMLPurifier_AttrTransform_Length('width'); + } + + function test() { + $this->assertResult( array() ); + $this->assertResult( + array('width' => '10'), + array('style' => 'width:10px;') + ); + $this->assertResult( + array('width' => '10%'), + array('style' => 'width:10%;') + ); + $this->assertResult( + array('width' => '10%', 'style' => 'font-weight:bold'), + array('style' => 'width:10%;font-weight:bold') + ); + // this behavior might change + $this->assertResult( + array('width' => 'asdf'), + array('style' => 'width:asdf;') + ); + } + +} + +?> \ No newline at end of file diff --git a/tests/HTMLPurifier/Strategy/ValidateAttributesTest.php b/tests/HTMLPurifier/Strategy/ValidateAttributesTest.php index 2ece54d4..76614f8c 100644 --- a/tests/HTMLPurifier/Strategy/ValidateAttributesTest.php +++ b/tests/HTMLPurifier/Strategy/ValidateAttributesTest.php @@ -179,6 +179,12 @@ class HTMLPurifier_Strategy_ValidateAttributesTest extends array('HTML.EnableAttrID' => true) ); + // lengths + $this->assertResult( + '
', + '
' + ); + } } diff --git a/tests/test_files.php b/tests/test_files.php index 9f357bbb..2b870dca 100644 --- a/tests/test_files.php +++ b/tests/test_files.php @@ -38,6 +38,7 @@ $test_files[] = 'AttrTransform/BgColorTest.php'; $test_files[] = 'AttrTransform/BorderTest.php'; $test_files[] = 'AttrTransform/ImgRequiredTest.php'; $test_files[] = 'AttrTransform/LangTest.php'; +$test_files[] = 'AttrTransform/LengthTest.php'; $test_files[] = 'AttrTransform/NameTest.php'; $test_files[] = 'AttrTransform/TextAlignTest.php'; $test_files[] = 'ChildDef/ChameleonTest.php';