0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2024-11-09 23:28:42 +00:00

Implement AttrDef_Class.

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@157 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang 2006-08-04 02:48:20 +00:00
parent 784b756b3f
commit a2fc5da060
6 changed files with 91 additions and 2 deletions

View File

@ -0,0 +1,43 @@
<?php
require_once 'HTMLPurifier/AttrDef.php';
require_once 'HTMLPurifier/Config.php';
class HTMLPurifier_AttrDef_Class extends HTMLPurifier_AttrDef
{
function validate($raw_string, $config = null) {
if (!$config) $config = HTMLPurifier_Config::createDefault();
$string = trim($raw_string);
// early abort: '' and '0' (strings that convert to false) are invalid
if (!$string) return false;
// OPTIMIZABLE!
// do the preg_match, capture all subpatterns for reformulation
// we don't support U+00A1 and up codepoints or
// escaping because I don't know how to do that with regexps
// and plus it would complicate optimization efforts (you never
// see that anyway).
$matches = array();
$pattern = '/(?:\s|\A)'.
'((?:-?[A-Za-z_]|--)[A-Za-z_\-0-9]*)'.
'(?:\s|\z)/';
preg_match_all($pattern, $string, $matches);
$new_string = '';
foreach ($matches[1] as $class_names) {
$new_string .= $class_names . ' ';
}
$new_string = rtrim($new_string);
return ($new_string == $raw_string) ? true : $new_string ? $new_string : false;
}
}
?>

View File

@ -2,6 +2,12 @@
require_once 'HTMLPurifier/AttrDef.php';
require_once 'HTMLPurifier/IDAccumulator.php';
// NOTE QUIRKY BEHAVIOR: even though this is the id processor, it
// will ignore HTMLPurifier_Config::$attr_id_blacklist: it will only
// go according to the ID accumulator. Since the accumulator is
// automatically generated, it will have already absorbed the
// blacklist. If you're hacking around, make sure you use load()!
class HTMLPurifier_AttrDef_ID extends HTMLPurifier_AttrDef
{

View File

@ -237,6 +237,7 @@ class HTMLPurifier_Definition
$this->info_global_attr = array(
// core attrs
'id' => new HTMLPurifier_AttrDef_ID(),
'class' => new HTMLPurifier_AttrDef_Class(),
// i18n
'dir' => new HTMLPurifier_AttrDef_Enum(array('ltr','rtl'), false),
);

View File

@ -0,0 +1,32 @@
<?php
require_once 'HTMLPurifier/AttrDef/Class.php';
require_once 'HTMLPurifier/Config.php';
class HTMLPurifier_AttrDef_ClassTest extends UnitTestCase
{
function testDefault() {
$def = new HTMLPurifier_AttrDef_Class();
$this->assertTrue($def->validate('valid'));
$this->assertTrue($def->validate('a0-_'));
$this->assertTrue($def->validate('-valid'));
$this->assertTrue($def->validate('_valid'));
$this->assertTrue($def->validate('double valid'));
$this->assertFalse($def->validate('0invalid'));
$this->assertFalse($def->validate('-0'));
// test conditional replacement
$this->assertEqual('validassoc', $def->validate('validassoc 0invalid'));
// test whitespace leniency
$this->assertTrue('double valid', $def->validate(" double\nvalid\r"));
}
}
?>

View File

@ -19,20 +19,22 @@ class HTMLPurifier_Strategy_ValidateAttributesTest extends
$inputs[0] = '';
$expect[0] = '';
// test ids
$inputs[1] = '<div id="valid">Preserve the ID.</div>';
$expect[1] = $inputs[1];
$inputs[2] = '<div id="0invalid">Kill the ID.</div>';
$expect[2] = '<div>Kill the ID.</div>';
// test accumulator
// test id accumulator
$inputs[3] = '<div id="valid">Valid</div><div id="valid">Invalid</div>';
$expect[3] = '<div id="valid">Valid</div><div>Invalid</div>';
$inputs[4] = '<span dir="up-to-down">Bad dir.</span>';
$expect[4] = '<span>Bad dir.</span>';
// test case sensitivity
// test attribute case sensitivity
$inputs[5] = '<div ID="valid">Convert ID to lowercase.</div>';
$expect[5] = '<div id="valid">Convert ID to lowercase.</div>';
@ -46,6 +48,10 @@ class HTMLPurifier_Strategy_ValidateAttributesTest extends
$config[7] = HTMLPurifier_Config::createDefault();
$config[7]->attr_id_blacklist = array('invalid');
// test classes
$inputs[8] = '<div class="valid">Valid</div>';
$expect[8] = $inputs[8];
$this->assertStrategyWorks($strategy, $inputs, $expect, $config);
}

View File

@ -39,6 +39,7 @@ $test->addTestFile('HTMLPurifier/Strategy/CoreTest.php');
$test->addTestFile('HTMLPurifier/Strategy/ValidateAttributesTest.php');
$test->addTestFile('HTMLPurifier/AttrDef/EnumTest.php');
$test->addTestFile('HTMLPurifier/AttrDef/IDTest.php');
$test->addTestFile('HTMLPurifier/AttrDef/ClassTest.php');
$test->addTestFile('HTMLPurifier/IDAccumulatorTest.php');
$test->addTestFile('HTMLPurifier/TagTransformTest.php');