0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2024-11-08 06:48:42 +00:00

[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
This commit is contained in:
Edward Z. Yang 2007-05-20 21:22:54 +00:00
parent 71326abec1
commit 2945f6a930
7 changed files with 33 additions and 42 deletions

1
NEWS
View File

@ -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:

View File

@ -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 =============================

View File

@ -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'];
}
}
?>

View File

@ -1,34 +0,0 @@
<?php
require_once 'HTMLPurifier/TagTransform.php';
/**
* Transforms CENTER tags into proper version (DIV with text-align CSS)
*
* Takes a CENTER tag, parses the align attribute, and then if it's valid
* assigns it to the CSS property text-align.
*/
class HTMLPurifier_TagTransform_Center extends HTMLPurifier_TagTransform
{
var $transform_to = 'div';
function transform($tag, $config, &$context) {
if ($tag->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;
}
}
?>

View File

@ -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;
}

View File

@ -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,

View File

@ -29,7 +29,7 @@ class HTMLPurifier_Test extends UnitTestCase
$this->assertPurification(
'<u>Illegal underline</u>',
'Illegal underline'
'<span style="text-decoration:underline;">Illegal underline</span>'
);
$this->assertPurification(