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:
parent
77f2833f36
commit
a2880bdff2
15
library/HTMLPurifier/AttrContext.php
Normal file
15
library/HTMLPurifier/AttrContext.php
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
@ -1,11 +1,13 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
require_once 'HTMLPurifier/AttrContext.php';
|
||||||
|
|
||||||
// AttrDef = Attribute Definition
|
// AttrDef = Attribute Definition
|
||||||
class HTMLPurifier_AttrDef
|
class HTMLPurifier_AttrDef
|
||||||
{
|
{
|
||||||
function HTMLPurifier_AttrDef() {}
|
function HTMLPurifier_AttrDef() {}
|
||||||
|
|
||||||
function validate($string, $config = null) {
|
function validate($string, $config, &$context) {
|
||||||
trigger_error('Cannot call abstract function', E_USER_ERROR);
|
trigger_error('Cannot call abstract function', E_USER_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,9 +6,7 @@ require_once 'HTMLPurifier/Config.php';
|
|||||||
class HTMLPurifier_AttrDef_Class extends HTMLPurifier_AttrDef
|
class HTMLPurifier_AttrDef_Class extends HTMLPurifier_AttrDef
|
||||||
{
|
{
|
||||||
|
|
||||||
function validate($string, $config = null) {
|
function validate($string, $config, &$context) {
|
||||||
|
|
||||||
if (!$config) $config = HTMLPurifier_Config::createDefault();
|
|
||||||
|
|
||||||
$string = trim($string);
|
$string = trim($string);
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@ class HTMLPurifier_AttrDef_Enum extends HTMLPurifier_AttrDef
|
|||||||
$this->case_sensitive = $case_sensitive;
|
$this->case_sensitive = $case_sensitive;
|
||||||
}
|
}
|
||||||
|
|
||||||
function validate($string, $config = null) {
|
function validate($string, $config, &$context) {
|
||||||
$string = trim($string);
|
$string = trim($string);
|
||||||
if (!$this->case_sensitive) {
|
if (!$this->case_sensitive) {
|
||||||
$string = ctype_lower($string) ? $string : strtolower($string);
|
$string = ctype_lower($string) ? $string : strtolower($string);
|
||||||
|
@ -12,12 +12,12 @@ require_once 'HTMLPurifier/IDAccumulator.php';
|
|||||||
class HTMLPurifier_AttrDef_ID extends HTMLPurifier_AttrDef
|
class HTMLPurifier_AttrDef_ID extends HTMLPurifier_AttrDef
|
||||||
{
|
{
|
||||||
|
|
||||||
function validate($id, $config, &$accumulator) {
|
function validate($id, $config, &$context) {
|
||||||
|
|
||||||
$id = trim($id); // trim it first
|
$id = trim($id); // trim it first
|
||||||
|
|
||||||
if ($id === '') return false;
|
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
|
// we purposely avoid using regex, hopefully this is faster
|
||||||
|
|
||||||
@ -32,7 +32,7 @@ class HTMLPurifier_AttrDef_ID extends HTMLPurifier_AttrDef
|
|||||||
$result = ($trim === '');
|
$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
|
// if no change was made to the ID, return the result
|
||||||
// else, return the new id if stripping whitespace made it
|
// else, return the new id if stripping whitespace made it
|
||||||
|
@ -7,7 +7,7 @@ require_once 'HTMLPurifier/AttrDef.php';
|
|||||||
class HTMLPurifier_AttrDef_Lang extends HTMLPurifier_AttrDef
|
class HTMLPurifier_AttrDef_Lang extends HTMLPurifier_AttrDef
|
||||||
{
|
{
|
||||||
|
|
||||||
function validate($string, $config = null) {
|
function validate($string, $config, &$context) {
|
||||||
|
|
||||||
$string = trim($string);
|
$string = trim($string);
|
||||||
if (!$string) return false;
|
if (!$string) return false;
|
||||||
|
@ -6,7 +6,7 @@ require_once 'HTMLPurifier/AttrDef/Pixels.php';
|
|||||||
class HTMLPurifier_AttrDef_Length extends HTMLPurifier_AttrDef_Pixels
|
class HTMLPurifier_AttrDef_Length extends HTMLPurifier_AttrDef_Pixels
|
||||||
{
|
{
|
||||||
|
|
||||||
function validate($string, $config = null) {
|
function validate($string, $config, &$context) {
|
||||||
|
|
||||||
$string = trim($string);
|
$string = trim($string);
|
||||||
if ($string === '') return false;
|
if ($string === '') return false;
|
||||||
|
@ -6,7 +6,7 @@ require_once 'HTMLPurifier/AttrDef/Length.php';
|
|||||||
class HTMLPurifier_AttrDef_MultiLength extends HTMLPurifier_AttrDef_Length
|
class HTMLPurifier_AttrDef_MultiLength extends HTMLPurifier_AttrDef_Length
|
||||||
{
|
{
|
||||||
|
|
||||||
function validate($string, $config = null) {
|
function validate($string, $config, &$context) {
|
||||||
|
|
||||||
$string = trim($string);
|
$string = trim($string);
|
||||||
if ($string === '') return false;
|
if ($string === '') return false;
|
||||||
|
@ -6,7 +6,7 @@ require_once 'HTMLPurifier/AttrDef.php';
|
|||||||
class HTMLPurifier_AttrDef_NumberSpan extends HTMLPurifier_AttrDef
|
class HTMLPurifier_AttrDef_NumberSpan extends HTMLPurifier_AttrDef
|
||||||
{
|
{
|
||||||
|
|
||||||
function validate($string, $config = null) {
|
function validate($string, $config, &$context) {
|
||||||
|
|
||||||
$string = trim($string);
|
$string = trim($string);
|
||||||
if ($string === '') return false;
|
if ($string === '') return false;
|
||||||
|
@ -5,7 +5,7 @@ require_once 'HTMLPurifier/AttrDef.php';
|
|||||||
class HTMLPurifier_AttrDef_Pixels extends HTMLPurifier_AttrDef
|
class HTMLPurifier_AttrDef_Pixels extends HTMLPurifier_AttrDef
|
||||||
{
|
{
|
||||||
|
|
||||||
function validate($string, $config = null) {
|
function validate($string, $config, &$context) {
|
||||||
|
|
||||||
$string = trim($string);
|
$string = trim($string);
|
||||||
if ($string === '0') return $string;
|
if ($string === '0') return $string;
|
||||||
|
@ -5,7 +5,7 @@ require_once 'HTMLPurifier/AttrDef.php';
|
|||||||
class HTMLPurifier_AttrDef_Text extends HTMLPurifier_AttrDef
|
class HTMLPurifier_AttrDef_Text extends HTMLPurifier_AttrDef
|
||||||
{
|
{
|
||||||
|
|
||||||
function validate($string, $config = null) {
|
function validate($string, $config, &$context) {
|
||||||
return $this->parseCDATA($string);
|
return $this->parseCDATA($string);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,13 +12,11 @@ HTMLPurifier_ConfigDef::define(
|
|||||||
class HTMLPurifier_AttrDef_URI extends HTMLPurifier_AttrDef
|
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
|
// We'll write stack-based parsers later, for now, use regexps to
|
||||||
// get things working as fast as possible (irony)
|
// get things working as fast as possible (irony)
|
||||||
|
|
||||||
if (!$config) $config = HTMLPurifier_Config::createDefault();
|
|
||||||
|
|
||||||
// parse as CDATA
|
// parse as CDATA
|
||||||
$uri = $this->parseCDATA($uri);
|
$uri = $this->parseCDATA($uri);
|
||||||
|
|
||||||
|
@ -4,6 +4,7 @@ require_once 'HTMLPurifier/Strategy.php';
|
|||||||
require_once 'HTMLPurifier/Definition.php';
|
require_once 'HTMLPurifier/Definition.php';
|
||||||
require_once 'HTMLPurifier/IDAccumulator.php';
|
require_once 'HTMLPurifier/IDAccumulator.php';
|
||||||
require_once 'HTMLPurifier/ConfigDef.php';
|
require_once 'HTMLPurifier/ConfigDef.php';
|
||||||
|
require_once 'HTMLPurifier/AttrContext.php';
|
||||||
|
|
||||||
HTMLPurifier_ConfigDef::define(
|
HTMLPurifier_ConfigDef::define(
|
||||||
'Attr', 'IDBlacklist', array(),
|
'Attr', 'IDBlacklist', array(),
|
||||||
@ -27,11 +28,14 @@ class HTMLPurifier_Strategy_ValidateAttributes extends HTMLPurifier_Strategy
|
|||||||
// load default configuration object if none passed
|
// load default configuration object if none passed
|
||||||
if (!$config) $config = HTMLPurifier_Config::createDefault();
|
if (!$config) $config = HTMLPurifier_Config::createDefault();
|
||||||
|
|
||||||
|
// setup StrategyContext
|
||||||
|
$context = new HTMLPurifier_AttrContext();
|
||||||
|
|
||||||
// setup ID accumulator and load it with blacklisted IDs
|
// setup ID accumulator and load it with blacklisted IDs
|
||||||
// eventually, we'll have a dedicated context object to hold
|
// eventually, we'll have a dedicated context object to hold
|
||||||
// all these accumulators and caches. For now, just an IDAccumulator
|
// all these accumulators and caches. For now, just an IDAccumulator
|
||||||
$accumulator = new HTMLPurifier_IDAccumulator();
|
$context->id_accumulator = new HTMLPurifier_IDAccumulator();
|
||||||
$accumulator->load($config->get('Attr', 'IDBlacklist'));
|
$context->id_accumulator->load($config->get('Attr', 'IDBlacklist'));
|
||||||
|
|
||||||
// create alias to global definition array, see also $defs
|
// create alias to global definition array, see also $defs
|
||||||
// DEFINITION CALL
|
// DEFINITION CALL
|
||||||
@ -84,14 +88,14 @@ class HTMLPurifier_Strategy_ValidateAttributes extends HTMLPurifier_Strategy
|
|||||||
} else {
|
} else {
|
||||||
// validate according to the element's definition
|
// validate according to the element's definition
|
||||||
$result = $defs[$attr_key]->validate(
|
$result = $defs[$attr_key]->validate(
|
||||||
$value, $config, $accumulator
|
$value, $config, $context
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
} elseif ( isset($d_defs[$attr_key]) ) {
|
} elseif ( isset($d_defs[$attr_key]) ) {
|
||||||
// there is a global definition defined, validate according
|
// there is a global definition defined, validate according
|
||||||
// to the global definition
|
// to the global definition
|
||||||
$result = $d_defs[$attr_key]->validate(
|
$result = $d_defs[$attr_key]->validate(
|
||||||
$value, $config, $accumulator
|
$value, $config, $context
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
// system never heard of the attribute? DELETE!
|
// system never heard of the attribute? DELETE!
|
||||||
|
@ -9,7 +9,8 @@ class HTMLPurifier_AttrDef_IDTest extends HTMLPurifier_AttrDefHarness
|
|||||||
|
|
||||||
function test() {
|
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();
|
$this->def = new HTMLPurifier_AttrDef_ID();
|
||||||
|
|
||||||
// valid ID names
|
// valid ID names
|
||||||
|
@ -4,14 +4,15 @@ class HTMLPurifier_AttrDefHarness extends UnitTestCase
|
|||||||
{
|
{
|
||||||
|
|
||||||
var $def;
|
var $def;
|
||||||
var $id_accumulator;
|
var $context;
|
||||||
var $config;
|
var $config;
|
||||||
|
|
||||||
// cannot be used for accumulator
|
// cannot be used for accumulator
|
||||||
function assertDef($string, $expect = true) {
|
function assertDef($string, $expect = true) {
|
||||||
// $expect can be a string or bool
|
// $expect can be a string or bool
|
||||||
if (!$this->config) $this->config = HTMLPurifier_Config::createDefault();
|
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) {
|
if ($expect === true) {
|
||||||
$this->assertIdentical($string, $result);
|
$this->assertIdentical($string, $result);
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
Reference in New Issue
Block a user