From 2945f6a930284b6e7c470ec07f0804bf896d7b05 Mon Sep 17 00:00:00 2001 From: "Edward Z. Yang" Date: Sun, 20 May 2007 21:22:54 +0000 Subject: [PATCH] [1.7.0] Implement u, s, and strike tag transforms - Extend Simple so that it can accept some light CSS - Remove Center transform in favor of Simple git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1081 48356398-32a2-884e-a903-53898d9a118a --- NEWS | 1 + .../HTMLModule/Tidy/XHTMLAndHTML4.php | 6 ++-- library/HTMLPurifier/TagTransform.php | 12 +++++++ library/HTMLPurifier/TagTransform/Center.php | 34 ------------------- library/HTMLPurifier/TagTransform/Simple.php | 15 ++++++-- tests/HTMLPurifier/TagTransformTest.php | 5 ++- tests/HTMLPurifier/Test.php | 2 +- 7 files changed, 33 insertions(+), 42 deletions(-) delete mode 100644 library/HTMLPurifier/TagTransform/Center.php diff --git a/NEWS b/NEWS index 4ba36902..7d1cac0a 100644 --- a/NEWS +++ b/NEWS @@ -23,6 +23,7 @@ NEWS ( CHANGELOG and HISTORY ) HTMLPurifier . Added convenience functions for HTMLModule constructors . AttrTypes now has accessor functions that should be used instead of directly manipulating info +. TagTransform_Center deprecated in favor of generic TagTransform_Simple 1.6.1, released 2007-05-05 ! Support for more deprecated attributes via transformations: diff --git a/library/HTMLPurifier/HTMLModule/Tidy/XHTMLAndHTML4.php b/library/HTMLPurifier/HTMLModule/Tidy/XHTMLAndHTML4.php index f939d644..a486560e 100644 --- a/library/HTMLPurifier/HTMLModule/Tidy/XHTMLAndHTML4.php +++ b/library/HTMLPurifier/HTMLModule/Tidy/XHTMLAndHTML4.php @@ -3,7 +3,6 @@ require_once 'HTMLPurifier/HTMLModule/Tidy.php'; require_once 'HTMLPurifier/TagTransform/Simple.php'; -require_once 'HTMLPurifier/TagTransform/Center.php'; require_once 'HTMLPurifier/TagTransform/Font.php'; require_once 'HTMLPurifier/AttrTransform/BgColor.php'; @@ -27,7 +26,10 @@ class HTMLPurifier_HTMLModule_Tidy_XHTMLAndHTML4 extends $r['font'] = new HTMLPurifier_TagTransform_Font(); $r['menu'] = new HTMLPurifier_TagTransform_Simple('ul'); $r['dir'] = new HTMLPurifier_TagTransform_Simple('ul'); - $r['center'] = new HTMLPurifier_TagTransform_Center(); + $r['center'] = new HTMLPurifier_TagTransform_Simple('div', 'text-align:center;'); + $r['u'] = new HTMLPurifier_TagTransform_Simple('span', 'text-decoration:underline;'); + $r['s'] = new HTMLPurifier_TagTransform_Simple('span', 'text-decoration:line-through;'); + $r['strike'] = new HTMLPurifier_TagTransform_Simple('span', 'text-decoration:line-through;'); // == deprecated attribute transforms ============================= diff --git a/library/HTMLPurifier/TagTransform.php b/library/HTMLPurifier/TagTransform.php index f5dc5c97..367ec8be 100644 --- a/library/HTMLPurifier/TagTransform.php +++ b/library/HTMLPurifier/TagTransform.php @@ -24,6 +24,18 @@ class HTMLPurifier_TagTransform trigger_error('Call to abstract function', E_USER_ERROR); } + /** + * Prepends CSS properties to the style attribute, creating the + * attribute if it doesn't exist. + * @warning Copied over from AttrTransform, be sure to keep in sync + * @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']; + } + } ?> \ No newline at end of file diff --git a/library/HTMLPurifier/TagTransform/Center.php b/library/HTMLPurifier/TagTransform/Center.php deleted file mode 100644 index 571bb9df..00000000 --- a/library/HTMLPurifier/TagTransform/Center.php +++ /dev/null @@ -1,34 +0,0 @@ -type == 'end') { - $new_tag = new HTMLPurifier_Token_End($this->transform_to); - return $new_tag; - } - $attr = $tag->attr; - $prepend_css = 'text-align:center;'; - if (isset($attr['style'])) { - $attr['style'] = $prepend_css . $attr['style']; - } else { - $attr['style'] = $prepend_css; - } - $new_tag = $tag->copy(); - $new_tag->name = $this->transform_to; - $new_tag->attr = $attr; - return $new_tag; - } -} - -?> \ No newline at end of file diff --git a/library/HTMLPurifier/TagTransform/Simple.php b/library/HTMLPurifier/TagTransform/Simple.php index 6ffd0eab..f8299e11 100644 --- a/library/HTMLPurifier/TagTransform/Simple.php +++ b/library/HTMLPurifier/TagTransform/Simple.php @@ -3,21 +3,32 @@ require_once 'HTMLPurifier/TagTransform.php'; /** - * Simple transformation, just change tag name to something else. + * Simple transformation, just change tag name to something else, + * and possibly add some styling. This will cover most of the deprecated + * tag cases. */ class HTMLPurifier_TagTransform_Simple extends HTMLPurifier_TagTransform { + var $style; + /** * @param $transform_to Tag name to transform to. + * @param $style CSS style to add to the tag */ - function HTMLPurifier_TagTransform_Simple($transform_to) { + function HTMLPurifier_TagTransform_Simple($transform_to, $style = null) { $this->transform_to = $transform_to; + $this->style = $style; } function transform($tag, $config, &$context) { $new_tag = $tag->copy(); $new_tag->name = $this->transform_to; + if (!is_null($this->style) && + ($new_tag->type == 'start' || $new_tag->type == 'empty') + ) { + $this->prependCSS($new_tag->attr, $this->style); + } return $new_tag; } diff --git a/tests/HTMLPurifier/TagTransformTest.php b/tests/HTMLPurifier/TagTransformTest.php index 51317c04..ed9828ad 100644 --- a/tests/HTMLPurifier/TagTransformTest.php +++ b/tests/HTMLPurifier/TagTransformTest.php @@ -3,7 +3,6 @@ require_once 'HTMLPurifier/TagTransform.php'; // needs to be seperated into files -require_once 'HTMLPurifier/TagTransform/Center.php'; require_once 'HTMLPurifier/TagTransform/Font.php'; require_once 'HTMLPurifier/TagTransform/Simple.php'; @@ -104,9 +103,9 @@ class HTMLPurifier_TagTransformTest extends UnitTestCase } - function testCenter() { + function testSimpleWithCSS() { - $transformer = new HTMLPurifier_TagTransform_Center(); + $transformer = new HTMLPurifier_TagTransform_Simple('div', 'text-align:center;'); $this->assertTransformation( $transformer, diff --git a/tests/HTMLPurifier/Test.php b/tests/HTMLPurifier/Test.php index 0fc83b84..169d23ac 100644 --- a/tests/HTMLPurifier/Test.php +++ b/tests/HTMLPurifier/Test.php @@ -29,7 +29,7 @@ class HTMLPurifier_Test extends UnitTestCase $this->assertPurification( 'Illegal underline', - 'Illegal underline' + 'Illegal underline' ); $this->assertPurification(