mirror of
https://github.com/ezyang/htmlpurifier.git
synced 2025-07-31 19:30:21 +02:00
Implement composite validator, and make Interchange use that.
git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1606 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
@@ -40,26 +40,32 @@ class HTMLPurifier_ConfigSchema_Interchange
|
||||
$validator = new HTMLPurifier_ConfigSchema_InterchangeValidator($this);
|
||||
|
||||
// Validators should be defined in the order they are to be called.
|
||||
$namespace = $validator->namespace;
|
||||
$directive = $validator->directive;
|
||||
|
||||
// ID tests
|
||||
$validator->addValidator($this->make('Exists', 'ID'));
|
||||
$validator->addValidator($this->make('Unique'));
|
||||
$validator->addNamespaceValidator($this->make('Alnum', 'ID'));
|
||||
|
||||
// ID: Namespace test
|
||||
$namespace->addValidator($this->make('Alnum', 'ID'));
|
||||
|
||||
// ID: Common tests
|
||||
$validator->addValidator($this->make('ParseId'));
|
||||
$validator->addValidator($this->make('Exists', '_NAMESPACE'));
|
||||
$validator->addValidator($this->make('Alnum', '_NAMESPACE'));
|
||||
|
||||
// Directive tests
|
||||
$validator->addDirectiveValidator($this->make('Exists', '_DIRECTIVE'));
|
||||
$validator->addDirectiveValidator($this->make('Alnum', '_DIRECTIVE'));
|
||||
$validator->addDirectiveValidator($this->make('NamespaceExists'));
|
||||
// ID: Directive tests
|
||||
$directive->addValidator($this->make('Exists', '_DIRECTIVE'));
|
||||
$directive->addValidator($this->make('Alnum', '_DIRECTIVE'));
|
||||
$directive->addValidator($this->make('NamespaceExists'));
|
||||
|
||||
// Directive: Type tests
|
||||
$validator->addDirectiveValidator($this->make('Exists', 'TYPE'));
|
||||
$validator->addDirectiveValidator($this->make('ParseType'));
|
||||
$validator->addDirectiveValidator($this->make('Exists', '_TYPE'));
|
||||
$validator->addDirectiveValidator($this->make('Exists', '_NULL'));
|
||||
$validator->addDirectiveValidator($this->make('Exists', 'DEFAULT'));
|
||||
$directive->addValidator($this->make('Exists', 'TYPE'));
|
||||
$directive->addValidator($this->make('ParseType'));
|
||||
$directive->addValidator($this->make('Exists', '_TYPE'));
|
||||
$directive->addValidator($this->make('Exists', '_NULL'));
|
||||
$directive->addValidator($this->make('Exists', 'DEFAULT'));
|
||||
|
||||
// Common tests
|
||||
$validator->addValidator($this->make('Exists', 'DESCRIPTION'));
|
||||
|
@@ -6,9 +6,8 @@
|
||||
class HTMLPurifier_ConfigSchema_InterchangeValidator
|
||||
{
|
||||
protected $interchange;
|
||||
protected $validators = array();
|
||||
protected $namespaceValidators = array();
|
||||
protected $directiveValidators = array();
|
||||
public $namespace;
|
||||
public $directive;
|
||||
|
||||
/**
|
||||
* @param $interchange Instance of HTMLPurifier_ConfigSchema_Interchange
|
||||
@@ -16,37 +15,24 @@ class HTMLPurifier_ConfigSchema_InterchangeValidator
|
||||
*/
|
||||
public function __construct($interchange) {
|
||||
$this->interchange = $interchange;
|
||||
$this->namespace = new HTMLPurifier_ConfigSchema_Validator_Composite();
|
||||
$this->directive = new HTMLPurifier_ConfigSchema_Validator_Composite();
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a HTMLPurifier_ConfigSchema_Validator to run when adding.
|
||||
* Registers a HTMLPurifier_ConfigSchema_Validator for both
|
||||
* directive and namespace
|
||||
*/
|
||||
public function addValidator($validator) {
|
||||
$this->addNamespaceValidator($validator);
|
||||
$this->addDirectiveValidator($validator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register validators to be used only on directives
|
||||
*/
|
||||
public function addDirectiveValidator($validator) {
|
||||
$this->directiveValidators[] = $validator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register validators to be used only on namespaces
|
||||
*/
|
||||
public function addNamespaceValidator($validator) {
|
||||
$this->namespaceValidators[] = $validator;
|
||||
$this->directive->addValidator($validator);
|
||||
$this->namespace->addValidator($validator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates and adds a namespace hash
|
||||
*/
|
||||
public function addNamespace($hash) {
|
||||
foreach ($this->namespaceValidators as $validator) {
|
||||
$validator->validate($hash, $this->interchange);
|
||||
}
|
||||
$this->namespace->validate($hash, $this->interchange);
|
||||
$this->interchange->addNamespace($hash);
|
||||
}
|
||||
|
||||
@@ -54,9 +40,7 @@ class HTMLPurifier_ConfigSchema_InterchangeValidator
|
||||
* Validates and adds a directive hash
|
||||
*/
|
||||
public function addDirective($hash) {
|
||||
foreach ($this->directiveValidators as $validator) {
|
||||
$validator->validate($hash, $this->interchange);
|
||||
}
|
||||
$this->directive->validate($hash, $this->interchange);
|
||||
$this->interchange->addDirective($hash);
|
||||
}
|
||||
}
|
||||
|
21
library/HTMLPurifier/ConfigSchema/Validator/Composite.php
Normal file
21
library/HTMLPurifier/ConfigSchema/Validator/Composite.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Groups several validators together, which can be used with logical validators
|
||||
*/
|
||||
class HTMLPurifier_ConfigSchema_Validator_Composite extends HTMLPurifier_ConfigSchema_Validator
|
||||
{
|
||||
|
||||
protected $validators = array();
|
||||
|
||||
public function addValidator($validator) {
|
||||
$this->validators[] = $validator;
|
||||
}
|
||||
|
||||
public function validate(&$arr, $interchange) {
|
||||
foreach ($this->validators as $validator) {
|
||||
$validator->validate($arr, $interchange);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user