0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2024-12-22 16:31:53 +00: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:
Edward Z. Yang 2008-03-05 05:20:28 +00:00
parent e21e9b23ad
commit e4ab6d584e
6 changed files with 72 additions and 40 deletions

View File

@ -134,6 +134,7 @@ require 'HTMLPurifier/ConfigSchema/StringHashParser.php';
require 'HTMLPurifier/ConfigSchema/StringHashReverseAdapter.php';
require 'HTMLPurifier/ConfigSchema/Validator.php';
require 'HTMLPurifier/ConfigSchema/Validator/Alnum.php';
require 'HTMLPurifier/ConfigSchema/Validator/Composite.php';
require 'HTMLPurifier/ConfigSchema/Validator/Exists.php';
require 'HTMLPurifier/ConfigSchema/Validator/NamespaceExists.php';
require 'HTMLPurifier/ConfigSchema/Validator/ParseId.php';

View File

@ -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'));

View File

@ -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);
}
}

View 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);
}
}
}

View File

@ -26,8 +26,8 @@ class HTMLPurifier_ConfigSchema_InterchangeValidatorTest extends UnitTestCase
public function testAddNamespaceWithValidators() {
$hash = array('ID' => 'Namespace');
$this->validator->addValidator($this->makeValidator(array($hash, $this->mock)));
$this->validator->addNamespaceValidator($this->makeValidator(array($hash, $this->mock)));
$this->validator->addDirectiveValidator($this->makeValidator()); // not called
$this->validator->namespace->addValidator($this->makeValidator(array($hash, $this->mock)));
$this->validator->directive->addValidator($this->makeValidator()); // not called
$this->mock->expectOnce('addNamespace', array($hash));
$this->validator->addNamespace($hash);
}
@ -35,8 +35,8 @@ class HTMLPurifier_ConfigSchema_InterchangeValidatorTest extends UnitTestCase
public function testAddDirectiveWithValidators() {
$hash = array('ID' => 'Namespace.Directive');
$this->validator->addValidator($this->makeValidator(array($hash, $this->mock)));
$this->validator->addNamespaceValidator($this->makeValidator()); // not called
$this->validator->addDirectiveValidator($this->makeValidator(array($hash, $this->mock)));
$this->validator->namespace->addValidator($this->makeValidator()); // not called
$this->validator->directive->addValidator($this->makeValidator(array($hash, $this->mock)));
$this->mock->expectOnce('addDirective', array($hash));
$this->validator->addDirective($hash);
}

View File

@ -0,0 +1,20 @@
<?php
class HTMLPurifier_ConfigSchema_Validator_CompositeTest extends HTMLPurifier_ConfigSchema_ValidatorHarness
{
public function testValidate() {
$arr = array('ID' => 'RD');
generate_mock_once('HTMLPurifier_ConfigSchema_Validator');
$mock1 = new HTMLPurifier_ConfigSchema_ValidatorMock();
$mock2 = new HTMLPurifier_ConfigSchema_ValidatorMock();
$mock1->expectOnce('validate', array($arr, $this->interchange));
$mock2->expectOnce('validate', array($arr, $this->interchange));
$this->validator->addValidator($mock1);
$this->validator->addValidator($mock2);
$this->validator->validate($arr, $this->interchange);
}
}