0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2024-12-23 00:41:52 +00:00

Implement center, menu and dir tag transformations. Font transform pending.

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@140 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang 2006-08-02 02:24:03 +00:00
parent d243545142
commit 626cfc1172
6 changed files with 220 additions and 1 deletions

View File

@ -6,6 +6,7 @@ require_once 'HTMLPurifier/AttrDef.php';
require_once 'HTMLPurifier/ChildDef.php'; require_once 'HTMLPurifier/ChildDef.php';
require_once 'HTMLPurifier/Generator.php'; require_once 'HTMLPurifier/Generator.php';
require_once 'HTMLPurifier/Token.php'; require_once 'HTMLPurifier/Token.php';
require_once 'HTMLPurifier/TagTransform.php';
/** /**
* Defines the purified HTML type with large amounts of objects. * Defines the purified HTML type with large amounts of objects.
@ -39,6 +40,9 @@ class HTMLPurifier_Definition
// used solely by HTMLPurifier_Strategy_FixNesting // used solely by HTMLPurifier_Strategy_FixNesting
var $info_parent = 'div'; var $info_parent = 'div';
// used solely by HTMLPurifier_Strategy_RemoveForeignElements
var $info_tag_transform = array();
function instance() { function instance() {
static $instance = null; static $instance = null;
if (!$instance) { if (!$instance) {
@ -212,6 +216,11 @@ class HTMLPurifier_Definition
// dir -> ul // dir -> ul
// center -> div / css: text-align: center; // center -> div / css: text-align: center;
//$this->info_tag_transform['font'] = new HTMLPurifier_TagTransform_Font();
$this->info_tag_transform['menu'] = new HTMLPurifier_TagTransform_Simple('ul');
$this->info_tag_transform['dir'] = new HTMLPurifier_TagTransform_Simple('ul');
$this->info_tag_transform['center'] = new HTMLPurifier_TagTransform_Center();
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
// info[]->auto_close : tags that automatically close another // info[]->auto_close : tags that automatically close another

View File

@ -3,6 +3,7 @@
require_once 'HTMLPurifier/Strategy.php'; require_once 'HTMLPurifier/Strategy.php';
require_once 'HTMLPurifier/Definition.php'; require_once 'HTMLPurifier/Definition.php';
require_once 'HTMLPurifier/Generator.php'; require_once 'HTMLPurifier/Generator.php';
require_once 'HTMLPurifier/TagTransform.php';
/** /**
* Removes all unrecognized tags from the list of tokens. * Removes all unrecognized tags from the list of tokens.
@ -27,7 +28,18 @@ class HTMLPurifier_Strategy_RemoveForeignElements extends HTMLPurifier_Strategy
foreach($tokens as $token) { foreach($tokens as $token) {
if (!empty( $token->is_tag )) { if (!empty( $token->is_tag )) {
// DEFINITION CALL // DEFINITION CALL
if (!isset($this->definition->info[$token->name])) { if (isset($this->definition->info[$token->name])) {
// leave untouched
} elseif (
isset($this->definition->info_tag_transform[$token->name])
) {
// there is a transformation for this tag
// DEFINITION CALL
$token = $this->
definition->
info_tag_transform[$token->name]->
transform($token);
} else {
// invalid tag, generate HTML and insert in // invalid tag, generate HTML and insert in
$token = new HTMLPurifier_Token_Text( $token = new HTMLPurifier_Token_Text(
$this->generator->generateFromToken($token) $this->generator->generateFromToken($token)

View File

@ -0,0 +1,88 @@
<?php
require_once('HTMLPurifier/Token.php');
class HTMLPurifier_TagTransform
{
function transform($tag) {
trigger_error('Call to abstract function', E_USER_ERROR);
}
function normalizeAttributes($attributes) {
$keys = array_keys($attributes);
foreach ($keys as $key) {
// normalization only necessary when key is not lowercase
if (!ctype_lower($key)) {
$new_key = strtolower($key);
if (!isset($attributes[$new_key])) {
$attributes[$new_key] = $attributes[$key];
}
unset($attributes[$key]);
}
}
return $attributes;
}
}
class HTMLPurifier_TagTransform_Simple extends HTMLPurifier_TagTransform
{
var $transform_to;
function HTMLPurifier_TagTransform_Simple($transform_to) {
$this->transform_to = $transform_to;
}
function transform($tag) {
switch ($tag->type) {
case 'end':
$new_tag = new HTMLPurifier_Token_End($this->transform_to);
break;
case 'start':
$new_tag = new HTMLPurifier_Token_Start($this->transform_to,
$tag->attributes);
break;
case 'empty':
$new_tag = new HTMLPurifier_Token_Empty($this->transform_to,
$tag->attributes);
break;
default:
trigger_error('Failed tag transformation', E_USER_WARNING);
return;
}
return $new_tag;
}
}
class HTMLPurifier_TagTransform_Center extends HTMLPurifier_TagTransform
{
function transform($tag) {
$attributes = $this->normalizeAttributes($tag->attributes);
$prepend_css = 'text-align:center;';
if (isset($attributes['style'])) {
$attributes['style'] = $prepend_css . $attributes['style'];
} else {
$attributes['style'] = $prepend_css;
}
switch ($tag->type) {
case 'end':
$new_tag = new HTMLPurifier_Token_End('div');
break;
case 'start':
$new_tag = new HTMLPurifier_Token_Start('div', $attributes);
break;
case 'empty':
$new_tag = new HTMLPurifier_Token_Empty('div', $attributes);
break;
default:
trigger_error('Failed tag transformation', E_USER_WARNING);
return;
}
return $new_tag;
}
}
?>

View File

@ -24,6 +24,14 @@ class HTMLPurifier_Strategy_RemoveForeignElementsTest
$inputs[2] = '<asdf>Bling</asdf><d href="bang">Bong</d><foobar />'; $inputs[2] = '<asdf>Bling</asdf><d href="bang">Bong</d><foobar />';
$expect[2] = htmlspecialchars($inputs[2]); $expect[2] = htmlspecialchars($inputs[2]);
// test simple transform
$inputs[3] = '<menu><li>Item 1</li></menu>';
$expect[3] = '<ul><li>Item 1</li></ul>';
// test center transform
$inputs[4] = '<center>Look I am Centered!</center>';
$expect[4] = '<div style="text-align:center;">Look I am Centered!</div>';
$this->assertStrategyWorks($strategy, $inputs, $expect); $this->assertStrategyWorks($strategy, $inputs, $expect);
} }

View File

@ -0,0 +1,98 @@
<?php
require_once 'HTMLPurifier/TagTransform.php';
class HTMLPurifier_TagTransformTest extends UnitTestCase
{
function assertTransformation($transformer,
$name, $attributes,
$expect_name, $expect_attributes,
$expect_added_attributes = array()) {
// start tag transform
$this->assertEqual(
new HTMLPurifier_Token_Start($expect_name, $expect_added_attributes),
$transformer->transform(
new HTMLPurifier_Token_Start($name))
);
// start tag transform with attributes
$this->assertEqual(
new HTMLPurifier_Token_Start($expect_name, $expect_attributes),
$transformer->transform(
new HTMLPurifier_Token_Start($name, $attributes)
)
);
// end tag transform
$this->assertEqual(
new HTMLPurifier_Token_End($expect_name),
$transformer->transform(new HTMLPurifier_Token_End($name))
);
// empty tag transform
$this->assertEqual(
new HTMLPurifier_Token_Empty($expect_name, $expect_added_attributes),
$transformer->transform(new HTMLPurifier_Token_Empty($name))
);
// empty tag transform with attributes
$this->assertEqual(
new HTMLPurifier_Token_Empty($expect_name, $expect_attributes),
$transformer->transform(
new HTMLPurifier_Token_Empty($name, $attributes))
);
}
function test_normalizeAttributes() {
$transformer = new HTMLPurifier_TagTransform();
$this->assertEqual(array(), $transformer->normalizeAttributes(array()));
$this->assertEqual(array('class'=>'foo'),
$transformer->normalizeAttributes(array('class'=>'foo')));
$this->assertEqual(array('class'=>'foo'),
$transformer->normalizeAttributes(array('CLASS'=>'foo')));
}
function testSimple() {
$transformer = new HTMLPurifier_TagTransform_Simple('ul');
$this->assertTransformation(
$transformer,
'menu', array('class' => 'boom'),
'ul', array('class' => 'boom')
);
}
function testCenter() {
$transformer = new HTMLPurifier_TagTransform_Center();
$this->assertTransformation(
$transformer,
'center', array('class' => 'boom', 'style'=>'font-weight:bold;'),
'div', array('class' => 'boom', 'style'=>'text-align:center;font-weight:bold;'),
array('style'=>'text-align:center;')
);
// test special case, uppercase attribute key
$this->assertTransformation(
$transformer,
'center', array('STYLE'=>'font-weight:bold;'),
'div', array('style'=>'text-align:center;font-weight:bold;'),
array('style'=>'text-align:center;')
);
}
}
?>

View File

@ -2,6 +2,9 @@
error_reporting(E_ALL); error_reporting(E_ALL);
// wishlist: automated calling of this file from multiple PHP versions so we
// don't have to constantly switch around
// load files, assume that simpletest directory is in path // load files, assume that simpletest directory is in path
require_once 'simpletest/unit_tester.php'; require_once 'simpletest/unit_tester.php';
require_once 'simpletest/reporter.php'; require_once 'simpletest/reporter.php';
@ -37,6 +40,7 @@ $test->addTestFile('HTMLPurifier/Strategy/ValidateAttributesTest.php');
$test->addTestFile('HTMLPurifier/AttrDef/EnumTest.php'); $test->addTestFile('HTMLPurifier/AttrDef/EnumTest.php');
$test->addTestFile('HTMLPurifier/AttrDef/IDTest.php'); $test->addTestFile('HTMLPurifier/AttrDef/IDTest.php');
$test->addTestFile('HTMLPurifier/IDAccumulatorTest.php'); $test->addTestFile('HTMLPurifier/IDAccumulatorTest.php');
$test->addTestFile('HTMLPurifier/TagTransformTest.php');
if (SimpleReporter::inCli()) $reporter = new TextReporter(); if (SimpleReporter::inCli()) $reporter = new TextReporter();
else $reporter = new HTMLReporter(); else $reporter = new HTMLReporter();