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

Define AttrDef_Text and parseCDATA().

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@160 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang 2006-08-04 03:13:04 +00:00
parent 50b3d5320e
commit 6232221c08
7 changed files with 76 additions and 0 deletions

View File

@ -8,6 +8,13 @@ class HTMLPurifier_AttrDef
function validate() {
trigger_error('Cannot call abstract function', E_USER_ERROR);
}
function parseCDATA($string) {
$string = trim($string);
$string = str_replace("\n", '', $string);
$string = str_replace(array("\r", "\t"), ' ', $string);
return $string;
}
}
?>

View File

@ -0,0 +1,15 @@
<?php
require_once 'HTMLPurifier/AttrDef.php';
class HTMLPurifier_AttrDef_Text extends HTMLPurifier_AttrDef
{
function validate($string) {
$new_string = $this->parseCDATA($string);
return ($string == $new_string) ? true : $new_string;
}
}
?>

View File

@ -3,6 +3,8 @@
require_once 'HTMLPurifier/AttrDef.php';
require_once 'HTMLPurifier/AttrDef/Enum.php';
require_once 'HTMLPurifier/AttrDef/ID.php';
require_once 'HTMLPurifier/AttrDef/Class.php';
require_once 'HTMLPurifier/AttrDef/Text.php';
require_once 'HTMLPurifier/ChildDef.php';
require_once 'HTMLPurifier/Generator.php';
require_once 'HTMLPurifier/Token.php';
@ -238,6 +240,7 @@ class HTMLPurifier_Definition
// core attrs
'id' => new HTMLPurifier_AttrDef_ID(),
'class' => new HTMLPurifier_AttrDef_Class(),
'title' => new HTMLPurifier_AttrDef_Text(),
// i18n
'dir' => new HTMLPurifier_AttrDef_Enum(array('ltr','rtl'), false),
);

View File

@ -0,0 +1,20 @@
<?php
require_once 'HTMLPurifier/AttrDef/Text.php';
class HTMLPurifier_AttrDef_TextTest extends UnitTestCase
{
function test() {
$def = new HTMLPurifier_AttrDef_Text();
$this->assertTrue($def->validate('This is spiffy text!'));
$this->assertEqual('Casual CDATA parsecheck.',
$def->validate(" Casual\tCDATA parse\ncheck. "));
}
}
?>

View File

@ -0,0 +1,22 @@
<?php
require_once 'HTMLPurifier/AttrDef.php';
class HTMLPurifier_AttrDefTest extends UnitTestCase
{
function test_parseCDATA() {
$def = new HTMLPurifier_AttrDef();
$this->assertEqual('', $def->parseCDATA(''));
$this->assertEqual('', $def->parseCDATA("\t\n\r \t\t"));
$this->assertEqual('foo', $def->parseCDATA("\t\n\r foo\t\t"));
$this->assertEqual('ignorelinefeeds', $def->parseCDATA("ignore\nline\nfeeds"));
$this->assertEqual('translate to space', $def->parseCDATA("translate\rto\tspace"));
}
}
?>

View File

@ -52,6 +52,13 @@ class HTMLPurifier_Strategy_ValidateAttributesTest extends
$inputs[8] = '<div class="valid">Valid</div>';
$expect[8] = $inputs[8];
$inputs[9] = '<div class="valid 0invalid">Keep valid.</div>';
$expect[9] = '<div class="valid">Keep valid.</div>';
// test title
$inputs[10] = '<acronym title="PHP: Hypertext Preprocessor">PHP</acronym>';
$expect[10] = $inputs[10];
$this->assertStrategyWorks($strategy, $inputs, $expect, $config);
}

View File

@ -37,9 +37,11 @@ $test->addTestFile('HTMLPurifier/Strategy/FixNestingTest.php');
$test->addTestFile('HTMLPurifier/Strategy/CompositeTest.php');
$test->addTestFile('HTMLPurifier/Strategy/CoreTest.php');
$test->addTestFile('HTMLPurifier/Strategy/ValidateAttributesTest.php');
$test->addTestFile('HTMLPurifier/AttrDefTest.php');
$test->addTestFile('HTMLPurifier/AttrDef/EnumTest.php');
$test->addTestFile('HTMLPurifier/AttrDef/IDTest.php');
$test->addTestFile('HTMLPurifier/AttrDef/ClassTest.php');
$test->addTestFile('HTMLPurifier/AttrDef/TextTest.php');
$test->addTestFile('HTMLPurifier/IDAccumulatorTest.php');
$test->addTestFile('HTMLPurifier/TagTransformTest.php');