From bd58a7ba77c87ed96cfdf7dcda668f8a7ff91c59 Mon Sep 17 00:00:00 2001 From: "Edward Z. Yang" Date: Thu, 3 May 2007 04:07:47 +0000 Subject: [PATCH] [1.6.1] Implement BoolToCSS attribute transformations for td,th.nowrap and hr.noshade - Implement CSS property white-space:nowrap; - Update TODO with more ambitious goal: all transforms by 1.6.1 git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1012 48356398-32a2-884e-a903-53898d9a118a --- NEWS | 5 ++- TODO | 5 ++- docs/dev-progress.html | 8 ++-- .../HTMLPurifier/AttrTransform/BoolToCSS.php | 42 +++++++++++++++++++ .../HTMLPurifier/AttrTransform/TextAlign.php | 2 +- library/HTMLPurifier/CSSDefinition.php | 3 ++ .../HTMLModule/TransformToStrict.php | 4 ++ .../AttrTransform/BoolToCSSTest.php | 39 +++++++++++++++++ .../Strategy/ValidateAttributesTest.php | 10 +++++ tests/test_files.php | 1 + 10 files changed, 111 insertions(+), 8 deletions(-) create mode 100644 library/HTMLPurifier/AttrTransform/BoolToCSS.php create mode 100644 tests/HTMLPurifier/AttrTransform/BoolToCSSTest.php diff --git a/NEWS b/NEWS index d7ae22f5..dc7d33bf 100644 --- a/NEWS +++ b/NEWS @@ -19,9 +19,12 @@ NEWS ( CHANGELOG and HISTORY ) HTMLPurifier images will hang around with an empty src ! Support for more deprecated attributes via transformations: + hspace and vspace in img - + size in hr + + size and noshade in hr + + nowrap in td ! target attribute in a tag supported, use %Attr.AllowedFrameTargets to enable +! CSS property white-space now allows nowrap (supported in all modern + browsers) but not others (which have spotty browser implementations) - 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 78f30a7f..8c7bc6d2 100644 --- a/TODO +++ b/TODO @@ -8,8 +8,9 @@ TODO List ========================== 1.6.1 [Oh Dear, We Missed Something!] - # align in img and table - # noshade in hr + # align in img, table, hr and caption + # clear in br + # type in ul, ol, li 1.7 release [Advanced API] # Complete advanced API, and fully document it diff --git a/docs/dev-progress.html b/docs/dev-progress.html index df629494..0cb7da65 100644 --- a/docs/dev-progress.html +++ b/docs/dev-progress.html @@ -168,9 +168,9 @@ thead th {text-align:left;padding:0.1em;background-color:#EEE;} quotesMay be dropped from CSS2, fairly useless for inline context visibilityENUM(visible, hidden, collapse), Dangerous -white-spaceENUM(normal, pre, nowrap, pre-wrap, +white-spaceENUM(normal, pre, nowrap, pre-wrap, pre-line), Spotty implementation: - pre (no IE 5/6), nowrap (no IE 5), + pre (no IE 5/6), nowrap (no IE 5, supported), pre-wrap (only Opera), pre-line (no support). Fixable? Unknown target milestone. @@ -281,8 +281,8 @@ Mozilla on inside and needs -moz-outline, no IE support. lang*Copy value to xml:lang nameIMGTurn into ID ATurn into ID -noshadeHRBoolean, style 'border-style:solid;' -nowrapTD, THBoolean, style 'white-space:nowrap;' (not compat with IE5) +noshadeHRBoolean, style 'border-style:solid;' +nowrapTD, THBoolean, style 'white-space:nowrap;' (not compat with IE5) sizeHRNear-equiv 'height', needs px suffix if original was pixels srcIMGRequired, insert blank or default img if not set startOLPoorly supported 'counter-reset', allowed in loose, dropped in strict diff --git a/library/HTMLPurifier/AttrTransform/BoolToCSS.php b/library/HTMLPurifier/AttrTransform/BoolToCSS.php new file mode 100644 index 00000000..77b714a4 --- /dev/null +++ b/library/HTMLPurifier/AttrTransform/BoolToCSS.php @@ -0,0 +1,42 @@ +attr = $attr; + $this->css = $css; + } + + 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']; + return $attr; + + } + +} + +?> \ No newline at end of file diff --git a/library/HTMLPurifier/AttrTransform/TextAlign.php b/library/HTMLPurifier/AttrTransform/TextAlign.php index 09088fe1..d0633279 100644 --- a/library/HTMLPurifier/AttrTransform/TextAlign.php +++ b/library/HTMLPurifier/AttrTransform/TextAlign.php @@ -7,7 +7,7 @@ require_once 'HTMLPurifier/AttrTransform.php'; */ class HTMLPurifier_AttrTransform_TextAlign extends HTMLPurifier_AttrTransform { - + function transform($attr, $config, &$context) { if (!isset($attr['align'])) return $attr; diff --git a/library/HTMLPurifier/CSSDefinition.php b/library/HTMLPurifier/CSSDefinition.php index 5de49b69..23a66ab7 100644 --- a/library/HTMLPurifier/CSSDefinition.php +++ b/library/HTMLPurifier/CSSDefinition.php @@ -206,6 +206,9 @@ class HTMLPurifier_CSSDefinition new HTMLPurifier_AttrDef_CSS_Percentage() )); + // partial support + $this->info['white-space'] = new HTMLPurifier_AttrDef_Enum(array('nowrap')); + } } diff --git a/library/HTMLPurifier/HTMLModule/TransformToStrict.php b/library/HTMLPurifier/HTMLModule/TransformToStrict.php index 782a3fda..df3e38c7 100644 --- a/library/HTMLPurifier/HTMLModule/TransformToStrict.php +++ b/library/HTMLPurifier/HTMLModule/TransformToStrict.php @@ -9,6 +9,7 @@ require_once 'HTMLPurifier/TagTransform/Font.php'; require_once 'HTMLPurifier/AttrTransform/Lang.php'; require_once 'HTMLPurifier/AttrTransform/TextAlign.php'; require_once 'HTMLPurifier/AttrTransform/BgColor.php'; +require_once 'HTMLPurifier/AttrTransform/BoolToCSS.php'; require_once 'HTMLPurifier/AttrTransform/Border.php'; require_once 'HTMLPurifier/AttrTransform/Name.php'; require_once 'HTMLPurifier/AttrTransform/Length.php'; @@ -92,6 +93,8 @@ class HTMLPurifier_HTMLModule_TransformToStrict extends HTMLPurifier_HTMLModule $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['nowrap'] = + $this->info['th']->attr_transform_pre['nowrap'] = new HTMLPurifier_AttrTransform_BoolToCSS('nowrap', 'white-space:nowrap;'); $this->info['td']->attr_transform_pre['height'] = $this->info['th']->attr_transform_pre['height'] = new HTMLPurifier_AttrTransform_Length('height'); @@ -100,6 +103,7 @@ class HTMLPurifier_HTMLModule_TransformToStrict extends HTMLPurifier_HTMLModule $this->info['img']->attr_transform_pre['vspace'] = new HTMLPurifier_AttrTransform_ImgSpace('vspace'); $this->info['hr']->attr_transform_pre['size'] = new HTMLPurifier_AttrTransform_Length('size', 'height'); + $this->info['hr']->attr_transform_pre['noshade'] = new HTMLPurifier_AttrTransform_BoolToCSS('noshade', 'border-style:solid;'); } diff --git a/tests/HTMLPurifier/AttrTransform/BoolToCSSTest.php b/tests/HTMLPurifier/AttrTransform/BoolToCSSTest.php new file mode 100644 index 00000000..b6794600 --- /dev/null +++ b/tests/HTMLPurifier/AttrTransform/BoolToCSSTest.php @@ -0,0 +1,39 @@ +obj = new HTMLPurifier_AttrTransform_BoolToCSS('foo', 'bar:3in;'); + } + + function test() { + + $this->assertResult( array() ); + + $this->assertResult( + array('foo' => 'foo'), + array('style' => 'bar:3in;') + ); + + // boolean attribute just has to be set: we don't care about + // anything else + $this->assertResult( + array('foo' => 'no'), + array('style' => 'bar:3in;') + ); + + $this->assertResult( + array('foo' => 'foo', 'style' => 'background-color:#F00;'), + array('style' => 'bar:3in;background-color:#F00;') + ); + + } + +} + +?> \ No newline at end of file diff --git a/tests/HTMLPurifier/Strategy/ValidateAttributesTest.php b/tests/HTMLPurifier/Strategy/ValidateAttributesTest.php index 9514b2e1..8c2cbd30 100644 --- a/tests/HTMLPurifier/Strategy/ValidateAttributesTest.php +++ b/tests/HTMLPurifier/Strategy/ValidateAttributesTest.php @@ -220,6 +220,16 @@ class HTMLPurifier_Strategy_ValidateAttributesTest extends '
', '
' ); + $this->assertResult( + '
', + '
' + ); + + // td boolean transformation + $this->assertResult( + '', + '' + ); } diff --git a/tests/test_files.php b/tests/test_files.php index 5955d8da..45c32611 100644 --- a/tests/test_files.php +++ b/tests/test_files.php @@ -37,6 +37,7 @@ $test_files[] = 'AttrDef/URITest.php'; $test_files[] = 'AttrDefTest.php'; $test_files[] = 'AttrTransform/BdoDirTest.php'; $test_files[] = 'AttrTransform/BgColorTest.php'; +$test_files[] = 'AttrTransform/BoolToCSSTest.php'; $test_files[] = 'AttrTransform/BorderTest.php'; $test_files[] = 'AttrTransform/ImgRequiredTest.php'; $test_files[] = 'AttrTransform/ImgSpaceTest.php';