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

Generalize IDAccumulator into AttrContext. Modify tests and classes accordingly. Also, this allows us to make the validate() parameters uniform among all AttrDef subclasses.

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@212 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang 2006-08-12 16:04:40 +00:00
parent 77f2833f36
commit a2880bdff2
15 changed files with 43 additions and 24 deletions

View File

@ -0,0 +1,15 @@
<?php
/**
* Internal data-structure used in attribute validation to accumulate state.
*
* All it is is a data-structure that holds objects that accumulate state, like
* HTMLPurifier_IDAccumulator.
*/
class HTMLPurifier_AttrContext
{
var $id_accumulator;
}
?>

View File

@ -1,11 +1,13 @@
<?php
require_once 'HTMLPurifier/AttrContext.php';
// AttrDef = Attribute Definition
class HTMLPurifier_AttrDef
{
function HTMLPurifier_AttrDef() {}
function validate($string, $config = null) {
function validate($string, $config, &$context) {
trigger_error('Cannot call abstract function', E_USER_ERROR);
}

View File

@ -6,9 +6,7 @@ require_once 'HTMLPurifier/Config.php';
class HTMLPurifier_AttrDef_Class extends HTMLPurifier_AttrDef
{
function validate($string, $config = null) {
if (!$config) $config = HTMLPurifier_Config::createDefault();
function validate($string, $config, &$context) {
$string = trim($string);

View File

@ -14,7 +14,7 @@ class HTMLPurifier_AttrDef_Enum extends HTMLPurifier_AttrDef
$this->case_sensitive = $case_sensitive;
}
function validate($string, $config = null) {
function validate($string, $config, &$context) {
$string = trim($string);
if (!$this->case_sensitive) {
$string = ctype_lower($string) ? $string : strtolower($string);

View File

@ -12,12 +12,12 @@ require_once 'HTMLPurifier/IDAccumulator.php';
class HTMLPurifier_AttrDef_ID extends HTMLPurifier_AttrDef
{
function validate($id, $config, &$accumulator) {
function validate($id, $config, &$context) {
$id = trim($id); // trim it first
if ($id === '') return false;
if (isset($accumulator->ids[$id])) return false;
if (isset($context->id_accumulator->ids[$id])) return false;
// we purposely avoid using regex, hopefully this is faster
@ -32,7 +32,7 @@ class HTMLPurifier_AttrDef_ID extends HTMLPurifier_AttrDef
$result = ($trim === '');
}
if ($result) $accumulator->add($id);
if ($result) $context->id_accumulator->add($id);
// if no change was made to the ID, return the result
// else, return the new id if stripping whitespace made it

View File

@ -7,7 +7,7 @@ require_once 'HTMLPurifier/AttrDef.php';
class HTMLPurifier_AttrDef_Lang extends HTMLPurifier_AttrDef
{
function validate($string, $config = null) {
function validate($string, $config, &$context) {
$string = trim($string);
if (!$string) return false;

View File

@ -6,7 +6,7 @@ require_once 'HTMLPurifier/AttrDef/Pixels.php';
class HTMLPurifier_AttrDef_Length extends HTMLPurifier_AttrDef_Pixels
{
function validate($string, $config = null) {
function validate($string, $config, &$context) {
$string = trim($string);
if ($string === '') return false;

View File

@ -6,7 +6,7 @@ require_once 'HTMLPurifier/AttrDef/Length.php';
class HTMLPurifier_AttrDef_MultiLength extends HTMLPurifier_AttrDef_Length
{
function validate($string, $config = null) {
function validate($string, $config, &$context) {
$string = trim($string);
if ($string === '') return false;

View File

@ -6,7 +6,7 @@ require_once 'HTMLPurifier/AttrDef.php';
class HTMLPurifier_AttrDef_NumberSpan extends HTMLPurifier_AttrDef
{
function validate($string, $config = null) {
function validate($string, $config, &$context) {
$string = trim($string);
if ($string === '') return false;

View File

@ -5,7 +5,7 @@ require_once 'HTMLPurifier/AttrDef.php';
class HTMLPurifier_AttrDef_Pixels extends HTMLPurifier_AttrDef
{
function validate($string, $config = null) {
function validate($string, $config, &$context) {
$string = trim($string);
if ($string === '0') return $string;

View File

@ -5,7 +5,7 @@ require_once 'HTMLPurifier/AttrDef.php';
class HTMLPurifier_AttrDef_Text extends HTMLPurifier_AttrDef
{
function validate($string, $config = null) {
function validate($string, $config, &$context) {
return $this->parseCDATA($string);
}

View File

@ -12,13 +12,11 @@ HTMLPurifier_ConfigDef::define(
class HTMLPurifier_AttrDef_URI extends HTMLPurifier_AttrDef
{
function validate($uri, $config = null) {
function validate($uri, $config, &$context) {
// We'll write stack-based parsers later, for now, use regexps to
// get things working as fast as possible (irony)
if (!$config) $config = HTMLPurifier_Config::createDefault();
// parse as CDATA
$uri = $this->parseCDATA($uri);

View File

@ -4,6 +4,7 @@ require_once 'HTMLPurifier/Strategy.php';
require_once 'HTMLPurifier/Definition.php';
require_once 'HTMLPurifier/IDAccumulator.php';
require_once 'HTMLPurifier/ConfigDef.php';
require_once 'HTMLPurifier/AttrContext.php';
HTMLPurifier_ConfigDef::define(
'Attr', 'IDBlacklist', array(),
@ -27,11 +28,14 @@ class HTMLPurifier_Strategy_ValidateAttributes extends HTMLPurifier_Strategy
// load default configuration object if none passed
if (!$config) $config = HTMLPurifier_Config::createDefault();
// setup StrategyContext
$context = new HTMLPurifier_AttrContext();
// setup ID accumulator and load it with blacklisted IDs
// eventually, we'll have a dedicated context object to hold
// all these accumulators and caches. For now, just an IDAccumulator
$accumulator = new HTMLPurifier_IDAccumulator();
$accumulator->load($config->get('Attr', 'IDBlacklist'));
$context->id_accumulator = new HTMLPurifier_IDAccumulator();
$context->id_accumulator->load($config->get('Attr', 'IDBlacklist'));
// create alias to global definition array, see also $defs
// DEFINITION CALL
@ -84,14 +88,14 @@ class HTMLPurifier_Strategy_ValidateAttributes extends HTMLPurifier_Strategy
} else {
// validate according to the element's definition
$result = $defs[$attr_key]->validate(
$value, $config, $accumulator
$value, $config, $context
);
}
} elseif ( isset($d_defs[$attr_key]) ) {
// there is a global definition defined, validate according
// to the global definition
$result = $d_defs[$attr_key]->validate(
$value, $config, $accumulator
$value, $config, $context
);
} else {
// system never heard of the attribute? DELETE!

View File

@ -9,7 +9,8 @@ class HTMLPurifier_AttrDef_IDTest extends HTMLPurifier_AttrDefHarness
function test() {
$this->id_accumulator = new HTMLPurifier_IDAccumulator();
$this->context = new HTMLPurifier_AttrContext();
$this->context->id_accumulator = new HTMLPurifier_IDAccumulator();
$this->def = new HTMLPurifier_AttrDef_ID();
// valid ID names

View File

@ -4,14 +4,15 @@ class HTMLPurifier_AttrDefHarness extends UnitTestCase
{
var $def;
var $id_accumulator;
var $context;
var $config;
// cannot be used for accumulator
function assertDef($string, $expect = true) {
// $expect can be a string or bool
if (!$this->config) $this->config = HTMLPurifier_Config::createDefault();
$result = $this->def->validate($string, $this->config, $this->id_accumulator);
if (!$this->context) $this->context = new HTMLPurifier_AttrContext();
$result = $this->def->validate($string, $this->config, $this->context);
if ($expect === true) {
$this->assertIdentical($string, $result);
} else {