From e4ab6d584e94816eff658edb7f5d2aed543445b5 Mon Sep 17 00:00:00 2001 From: "Edward Z. Yang" Date: Wed, 5 Mar 2008 05:20:28 +0000 Subject: [PATCH] Implement composite validator, and make Interchange use that. git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1606 48356398-32a2-884e-a903-53898d9a118a --- library/HTMLPurifier.includes.php | 1 + .../HTMLPurifier/ConfigSchema/Interchange.php | 26 ++++++++------ .../ConfigSchema/InterchangeValidator.php | 36 ++++++------------- .../ConfigSchema/Validator/Composite.php | 21 +++++++++++ .../ConfigSchema/InterchangeValidatorTest.php | 8 ++--- .../ConfigSchema/Validator/CompositeTest.php | 20 +++++++++++ 6 files changed, 72 insertions(+), 40 deletions(-) create mode 100644 library/HTMLPurifier/ConfigSchema/Validator/Composite.php create mode 100644 tests/HTMLPurifier/ConfigSchema/Validator/CompositeTest.php diff --git a/library/HTMLPurifier.includes.php b/library/HTMLPurifier.includes.php index 992c2dd7..e9916de0 100644 --- a/library/HTMLPurifier.includes.php +++ b/library/HTMLPurifier.includes.php @@ -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'; diff --git a/library/HTMLPurifier/ConfigSchema/Interchange.php b/library/HTMLPurifier/ConfigSchema/Interchange.php index 9bf45871..76fa828f 100644 --- a/library/HTMLPurifier/ConfigSchema/Interchange.php +++ b/library/HTMLPurifier/ConfigSchema/Interchange.php @@ -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')); diff --git a/library/HTMLPurifier/ConfigSchema/InterchangeValidator.php b/library/HTMLPurifier/ConfigSchema/InterchangeValidator.php index 1eed72c7..3009d06c 100644 --- a/library/HTMLPurifier/ConfigSchema/InterchangeValidator.php +++ b/library/HTMLPurifier/ConfigSchema/InterchangeValidator.php @@ -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); } } diff --git a/library/HTMLPurifier/ConfigSchema/Validator/Composite.php b/library/HTMLPurifier/ConfigSchema/Validator/Composite.php new file mode 100644 index 00000000..95625213 --- /dev/null +++ b/library/HTMLPurifier/ConfigSchema/Validator/Composite.php @@ -0,0 +1,21 @@ +validators[] = $validator; + } + + public function validate(&$arr, $interchange) { + foreach ($this->validators as $validator) { + $validator->validate($arr, $interchange); + } + } + +} diff --git a/tests/HTMLPurifier/ConfigSchema/InterchangeValidatorTest.php b/tests/HTMLPurifier/ConfigSchema/InterchangeValidatorTest.php index 8eab87aa..0776646a 100644 --- a/tests/HTMLPurifier/ConfigSchema/InterchangeValidatorTest.php +++ b/tests/HTMLPurifier/ConfigSchema/InterchangeValidatorTest.php @@ -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); } diff --git a/tests/HTMLPurifier/ConfigSchema/Validator/CompositeTest.php b/tests/HTMLPurifier/ConfigSchema/Validator/CompositeTest.php new file mode 100644 index 00000000..0150146f --- /dev/null +++ b/tests/HTMLPurifier/ConfigSchema/Validator/CompositeTest.php @@ -0,0 +1,20 @@ + '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); + } + +}