0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2024-12-23 00:41:52 +00:00

[3.1.0] Implement Duplicate validator, also modify some design things

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1596 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang 2008-03-04 04:13:07 +00:00
parent 1b434f0ecc
commit 4c798bd17e
8 changed files with 68 additions and 17 deletions

View File

@ -55,9 +55,14 @@ class HTMLPurifier_ConfigSchema_Interchange
*/ */
public function getValidatorAdapter() { public function getValidatorAdapter() {
$validator = new HTMLPurifier_ConfigSchema_InterchangeValidator($this); $validator = new HTMLPurifier_ConfigSchema_InterchangeValidator($this);
// Validators should be defined in the order they are to be called.
// Common validators // Common validators
$validator->addValidator(new HTMLPurifier_ConfigSchema_Validator_Exists('ID')); $validator->addValidator(new HTMLPurifier_ConfigSchema_Validator_Exists('ID'));
$validator->addValidator(new HTMLPurifier_ConfigSchema_Validator_Duplicate());
$validator->addValidator(new HTMLPurifier_ConfigSchema_Validator_Exists('DESCRIPTION')); $validator->addValidator(new HTMLPurifier_ConfigSchema_Validator_Exists('DESCRIPTION'));
// Namespace validators // Namespace validators
// Directive validators // Directive validators

View File

@ -8,8 +8,7 @@ class HTMLPurifier_ConfigSchema_InterchangeValidator
protected $interchange; protected $interchange;
protected $validators = array(); protected $validators = array();
protected $namespaceValidators = array(); protected $namespaceValidators = array();
protected $directiveVaildators = array(); protected $directiveValidators = array();
protected $index = 0;
/** /**
* @param $interchange Instance of HTMLPurifier_ConfigSchema_Interchange * @param $interchange Instance of HTMLPurifier_ConfigSchema_Interchange
@ -23,31 +22,29 @@ class HTMLPurifier_ConfigSchema_InterchangeValidator
* Registers a HTMLPurifier_ConfigSchema_Validator to run when adding. * Registers a HTMLPurifier_ConfigSchema_Validator to run when adding.
*/ */
public function addValidator($validator) { public function addValidator($validator) {
$this->validators[$this->index++] = $validator; $this->addNamespaceValidator($validator);
$this->addDirectiveValidator($validator);
} }
/** /**
* Register validators to be used only on directives * Register validators to be used only on directives
*/ */
public function addDirectiveValidator($validator) { public function addDirectiveValidator($validator) {
$this->directiveValidators[$this->index++] = $validator; $this->directiveValidators[] = $validator;
} }
/** /**
* Register validators to be used only on namespaces * Register validators to be used only on namespaces
*/ */
public function addNamespaceValidator($validator) { public function addNamespaceValidator($validator) {
$this->namespaceValidators[$this->index++] = $validator; $this->namespaceValidators[] = $validator;
} }
/** /**
* Validates and adds a namespace hash * Validates and adds a namespace hash
*/ */
public function addNamespace($hash) { public function addNamespace($hash) {
for ($i = 0; $i < $this->index; $i++) { foreach ($this->namespaceValidators as $validator) {
if (isset($this->validators[$i])) $validator = $this->validators[$i];
elseif (isset($this->namespaceValidators[$i])) $validator = $this->namespaceValidators[$i];
else continue;
$validator->validate($hash, $this->interchange); $validator->validate($hash, $this->interchange);
} }
$this->interchange->addNamespace($hash); $this->interchange->addNamespace($hash);
@ -57,10 +54,7 @@ class HTMLPurifier_ConfigSchema_InterchangeValidator
* Validates and adds a directive hash * Validates and adds a directive hash
*/ */
public function addDirective($hash) { public function addDirective($hash) {
for ($i = 0; $i < $this->index; $i++) { foreach ($this->directiveValidators as $validator) {
if (isset($this->validators[$i])) $validator = $this->validators[$i];
elseif (isset($this->directiveValidators[$i])) $validator = $this->directiveValidators[$i];
else continue;
$validator->validate($hash, $this->interchange); $validator->validate($hash, $this->interchange);
} }
$this->interchange->addDirective($hash); $this->interchange->addDirective($hash);

View File

@ -16,4 +16,11 @@ class HTMLPurifier_ConfigSchema_Validator
*/ */
public function validate(&$arr, $interchange) {} public function validate(&$arr, $interchange) {}
/**
* Throws a HTMLPurifier_ConfigSchema_Exception
*/
protected function error($msg) {
throw new HTMLPurifier_ConfigSchema_Exception($msg);
}
} }

View File

@ -15,7 +15,7 @@ class HTMLPurifier_ConfigSchema_Validator_Alnum extends HTMLPurifier_ConfigSchem
public function validate(&$arr, $interchange) { public function validate(&$arr, $interchange) {
if (!ctype_alnum($arr[$this->index])) { if (!ctype_alnum($arr[$this->index])) {
throw new HTMLPurifier_ConfigSchema_Exception($arr[$this->index] . ' in '. $this->index .' must be alphanumeric'); $this->error($arr[$this->index] . ' in '. $this->index .' must be alphanumeric');
} }
} }

View File

@ -0,0 +1,21 @@
<?php
/**
* Validates that this ID does not exist already in the interchange object.
* @note
* Although this tests both possible values, in practice the ID
* will only be in one or the other. We do this to keep things simple.
*/
class HTMLPurifier_ConfigSchema_Validator_Duplicate extends HTMLPurifier_ConfigSchema_Validator
{
public function validate(&$arr, $interchange) {
if (isset($interchange->namespaces[$arr['ID']])) {
$this->error('Cannot redefine namespace');
}
if (isset($interchange->directives[$arr['ID']])) {
$this->error('Cannot redefine directive');
}
}
}

View File

@ -14,7 +14,7 @@ class HTMLPurifier_ConfigSchema_Validator_Exists extends HTMLPurifier_ConfigSche
public function validate(&$arr, $interchange) { public function validate(&$arr, $interchange) {
if (empty($arr[$this->index])) { if (empty($arr[$this->index])) {
throw new HTMLPurifier_ConfigSchema_Exception($this->index . ' must exist'); $this->error($this->index . ' must exist');
} }
} }

View File

@ -0,0 +1,25 @@
<?php
class HTMLPurifier_ConfigSchema_Validator_DuplicateTest extends HTMLPurifier_ConfigSchema_ValidatorHarness
{
public function setup() {
parent::setup();
$this->validator = new HTMLPurifier_ConfigSchema_Validator_Duplicate();
}
public function testValidateNamespace() {
$this->interchange->addNamespace(array('ID' => 'Namespace'));
$this->expectSchemaException('Cannot redefine namespace');
$arr = array('ID' => 'Namespace');
$this->validator->validate($arr, $this->interchange);
}
public function testValidateDirective() {
$this->interchange->addDirective(array('ID' => 'Namespace.Directive'));
$this->expectSchemaException('Cannot redefine directive');
$arr = array('ID' => 'Namespace.Directive');
$this->validator->validate($arr, $this->interchange);
}
}

View File

@ -6,8 +6,7 @@ class HTMLPurifier_ConfigSchema_ValidatorHarness extends UnitTestCase
protected $interchange, $validator; protected $interchange, $validator;
public function setup() { public function setup() {
generate_mock_once('HTMLPurifier_ConfigSchema_Interchange'); $this->interchange = new HTMLPurifier_ConfigSchema_Interchange();
$this->interchange = new HTMLPurifier_ConfigSchema_InterchangeMock();
} }
protected function expectSchemaException($msg) { protected function expectSchemaException($msg) {