1
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2025-08-01 11:50:28 +02:00

Fix case-sensitivity issues for attributes. Added TokenTest. Updated TODO. Removed TagTransform::normalizeAttributes

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@142 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang
2006-08-02 02:43:52 +00:00
parent 145a51da5a
commit 80281dda55
6 changed files with 53 additions and 34 deletions

View File

@@ -1,13 +1,9 @@
Todo List Todo List
Primary: Primary:
- Implement attribute validation - (In Progress) Implement attribute validation
- Implement HTMLPurifier - Implement HTMLPurifier (trivial)
Secondary:
- Migrate all unit tests to use the lexer and generator
Code issues: Code issues:
- Rename AbstractTest to Harness - Rename AbstractTest to Harness
- Reorganize Strategy hierarchy to minimize duplication
- (?) Create a TokenFactory to prevent really long lines - (?) Create a TokenFactory to prevent really long lines

View File

@@ -9,21 +9,6 @@ class HTMLPurifier_TagTransform
trigger_error('Call to abstract function', E_USER_ERROR); 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 class HTMLPurifier_TagTransform_Simple extends HTMLPurifier_TagTransform
@@ -60,7 +45,7 @@ class HTMLPurifier_TagTransform_Simple extends HTMLPurifier_TagTransform
class HTMLPurifier_TagTransform_Center extends HTMLPurifier_TagTransform class HTMLPurifier_TagTransform_Center extends HTMLPurifier_TagTransform
{ {
function transform($tag) { function transform($tag) {
$attributes = $this->normalizeAttributes($tag->attributes); $attributes = $tag->attributes;
$prepend_css = 'text-align:center;'; $prepend_css = 'text-align:center;';
if (isset($attributes['style'])) { if (isset($attributes['style'])) {
$attributes['style'] = $prepend_css . $attributes['style']; $attributes['style'] = $prepend_css . $attributes['style'];

View File

@@ -52,6 +52,16 @@ class HTMLPurifier_Token_Tag extends HTMLPurifier_Token // abstract
*/ */
function HTMLPurifier_Token_Tag($name, $attributes = array()) { function HTMLPurifier_Token_Tag($name, $attributes = array()) {
$this->name = ctype_lower($name) ? $name : strtolower($name); $this->name = ctype_lower($name) ? $name : strtolower($name);
foreach ($attributes as $key => $value) {
// 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]);
}
}
$this->attributes = $attributes; $this->attributes = $attributes;
} }
} }

View File

@@ -30,6 +30,10 @@ class HTMLPurifier_Strategy_ValidateAttributesTest extends
$inputs[4] = '<span dir="up-to-down">Bad dir.</span>'; $inputs[4] = '<span dir="up-to-down">Bad dir.</span>';
$expect[4] = '<span>Bad dir.</span>'; $expect[4] = '<span>Bad dir.</span>';
// test case sensitivity
$inputs[5] = '<div ID="valid">Convert ID to lowercase.</div>';
$expect[5] = '<div id="valid">Convert ID to lowercase.</div>';
$this->assertStrategyWorks($strategy, $inputs, $expect); $this->assertStrategyWorks($strategy, $inputs, $expect);
} }

View File

@@ -48,18 +48,6 @@ class HTMLPurifier_TagTransformTest extends UnitTestCase
} }
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() { function testSimple() {
$transformer = new HTMLPurifier_TagTransform_Simple('ul'); $transformer = new HTMLPurifier_TagTransform_Simple('ul');

View File

@@ -0,0 +1,36 @@
<?php
require_once 'HTMLPurifier/Token.php';
class HTMLPurifier_TokenTest extends UnitTestCase
{
function assertTokenConstruction($name, $attributes,
$expect_name = null, $expect_attributes = null
) {
if ($expect_name === null) $expect_name = $name;
if ($expect_attributes === null) $expect_attributes = $attributes;
$token = new HTMLPurifier_Token_Start($name, $attributes);
$this->assertEqual($expect_name, $token->name);
$this->assertEqual($expect_attributes, $token->attributes);
}
function testConstruct() {
// standard case
$this->assertTokenConstruction('a', array('href' => 'about:blank'));
// lowercase the tag's name
$this->assertTokenConstruction('A', array('href' => 'about:blank'),
'a');
// lowercase attributes
$this->assertTokenConstruction('a', array('HREF' => 'about:blank'),
'a', array('href' => 'about:blank'));
}
}
?>