mirror of
https://github.com/ezyang/htmlpurifier.git
synced 2024-12-22 08:21: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:
parent
1b434f0ecc
commit
4c798bd17e
@ -55,9 +55,14 @@ class HTMLPurifier_ConfigSchema_Interchange
|
||||
*/
|
||||
public function getValidatorAdapter() {
|
||||
$validator = new HTMLPurifier_ConfigSchema_InterchangeValidator($this);
|
||||
|
||||
// Validators should be defined in the order they are to be called.
|
||||
|
||||
// Common validators
|
||||
$validator->addValidator(new HTMLPurifier_ConfigSchema_Validator_Exists('ID'));
|
||||
$validator->addValidator(new HTMLPurifier_ConfigSchema_Validator_Duplicate());
|
||||
$validator->addValidator(new HTMLPurifier_ConfigSchema_Validator_Exists('DESCRIPTION'));
|
||||
|
||||
// Namespace validators
|
||||
|
||||
// Directive validators
|
||||
|
@ -8,8 +8,7 @@ class HTMLPurifier_ConfigSchema_InterchangeValidator
|
||||
protected $interchange;
|
||||
protected $validators = array();
|
||||
protected $namespaceValidators = array();
|
||||
protected $directiveVaildators = array();
|
||||
protected $index = 0;
|
||||
protected $directiveValidators = array();
|
||||
|
||||
/**
|
||||
* @param $interchange Instance of HTMLPurifier_ConfigSchema_Interchange
|
||||
@ -23,31 +22,29 @@ class HTMLPurifier_ConfigSchema_InterchangeValidator
|
||||
* Registers a HTMLPurifier_ConfigSchema_Validator to run when adding.
|
||||
*/
|
||||
public function addValidator($validator) {
|
||||
$this->validators[$this->index++] = $validator;
|
||||
$this->addNamespaceValidator($validator);
|
||||
$this->addDirectiveValidator($validator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register validators to be used only on directives
|
||||
*/
|
||||
public function addDirectiveValidator($validator) {
|
||||
$this->directiveValidators[$this->index++] = $validator;
|
||||
$this->directiveValidators[] = $validator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register validators to be used only on namespaces
|
||||
*/
|
||||
public function addNamespaceValidator($validator) {
|
||||
$this->namespaceValidators[$this->index++] = $validator;
|
||||
$this->namespaceValidators[] = $validator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates and adds a namespace hash
|
||||
*/
|
||||
public function addNamespace($hash) {
|
||||
for ($i = 0; $i < $this->index; $i++) {
|
||||
if (isset($this->validators[$i])) $validator = $this->validators[$i];
|
||||
elseif (isset($this->namespaceValidators[$i])) $validator = $this->namespaceValidators[$i];
|
||||
else continue;
|
||||
foreach ($this->namespaceValidators as $validator) {
|
||||
$validator->validate($hash, $this->interchange);
|
||||
}
|
||||
$this->interchange->addNamespace($hash);
|
||||
@ -57,10 +54,7 @@ class HTMLPurifier_ConfigSchema_InterchangeValidator
|
||||
* Validates and adds a directive hash
|
||||
*/
|
||||
public function addDirective($hash) {
|
||||
for ($i = 0; $i < $this->index; $i++) {
|
||||
if (isset($this->validators[$i])) $validator = $this->validators[$i];
|
||||
elseif (isset($this->directiveValidators[$i])) $validator = $this->directiveValidators[$i];
|
||||
else continue;
|
||||
foreach ($this->directiveValidators as $validator) {
|
||||
$validator->validate($hash, $this->interchange);
|
||||
}
|
||||
$this->interchange->addDirective($hash);
|
||||
|
@ -16,4 +16,11 @@ class HTMLPurifier_ConfigSchema_Validator
|
||||
*/
|
||||
public function validate(&$arr, $interchange) {}
|
||||
|
||||
/**
|
||||
* Throws a HTMLPurifier_ConfigSchema_Exception
|
||||
*/
|
||||
protected function error($msg) {
|
||||
throw new HTMLPurifier_ConfigSchema_Exception($msg);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -15,7 +15,7 @@ class HTMLPurifier_ConfigSchema_Validator_Alnum extends HTMLPurifier_ConfigSchem
|
||||
|
||||
public function validate(&$arr, $interchange) {
|
||||
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');
|
||||
}
|
||||
}
|
||||
|
||||
|
21
library/HTMLPurifier/ConfigSchema/Validator/Duplicate.php
Normal file
21
library/HTMLPurifier/ConfigSchema/Validator/Duplicate.php
Normal 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');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -14,7 +14,7 @@ class HTMLPurifier_ConfigSchema_Validator_Exists extends HTMLPurifier_ConfigSche
|
||||
|
||||
public function validate(&$arr, $interchange) {
|
||||
if (empty($arr[$this->index])) {
|
||||
throw new HTMLPurifier_ConfigSchema_Exception($this->index . ' must exist');
|
||||
$this->error($this->index . ' must exist');
|
||||
}
|
||||
}
|
||||
|
||||
|
25
tests/HTMLPurifier/ConfigSchema/Validator/DuplicateTest.php
Normal file
25
tests/HTMLPurifier/ConfigSchema/Validator/DuplicateTest.php
Normal 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);
|
||||
}
|
||||
|
||||
}
|
@ -6,8 +6,7 @@ class HTMLPurifier_ConfigSchema_ValidatorHarness extends UnitTestCase
|
||||
protected $interchange, $validator;
|
||||
|
||||
public function setup() {
|
||||
generate_mock_once('HTMLPurifier_ConfigSchema_Interchange');
|
||||
$this->interchange = new HTMLPurifier_ConfigSchema_InterchangeMock();
|
||||
$this->interchange = new HTMLPurifier_ConfigSchema_Interchange();
|
||||
}
|
||||
|
||||
protected function expectSchemaException($msg) {
|
||||
|
Loading…
Reference in New Issue
Block a user