diff --git a/library/HTMLPurifier.includes.php b/library/HTMLPurifier.includes.php index 6e86c36e..8039d459 100644 --- a/library/HTMLPurifier.includes.php +++ b/library/HTMLPurifier.includes.php @@ -133,6 +133,8 @@ require 'HTMLPurifier/ConfigSchema/StringHashReverseAdapter.php'; require 'HTMLPurifier/ConfigSchema/Validator.php'; require 'HTMLPurifier/ConfigSchema/Validator/Alnum.php'; require 'HTMLPurifier/ConfigSchema/Validator/Exists.php'; +require 'HTMLPurifier/ConfigSchema/Validator/NamespaceExists.php'; +require 'HTMLPurifier/ConfigSchema/Validator/ParseId.php'; require 'HTMLPurifier/ConfigSchema/Validator/Unique.php'; require 'HTMLPurifier/DefinitionCache/Decorator.php'; require 'HTMLPurifier/DefinitionCache/Null.php'; diff --git a/library/HTMLPurifier/ConfigSchema/Interchange.php b/library/HTMLPurifier/ConfigSchema/Interchange.php index d44cf4c3..e2fc6c05 100644 --- a/library/HTMLPurifier/ConfigSchema/Interchange.php +++ b/library/HTMLPurifier/ConfigSchema/Interchange.php @@ -57,17 +57,32 @@ class HTMLPurifier_ConfigSchema_Interchange $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_Unique()); - $validator->addValidator(new HTMLPurifier_ConfigSchema_Validator_Exists('DESCRIPTION')); - - // Namespace validators - - // Directive validators + $validator->addValidator($this->make('Exists', 'ID')); + $validator->addValidator($this->make('Unique')); + $validator->addNamespaceValidator($this->make('Alnum', 'ID')); + $validator->addValidator($this->make('ParseId')); + $validator->addValidator($this->make('Exists', '_NAMESPACE')); + $validator->addValidator($this->make('Alnum', '_NAMESPACE')); + $validator->addDirectiveValidator($this->make('Exists', '_DIRECTIVE')); + $validator->addDirectiveValidator($this->make('Alnum', '_DIRECTIVE')); + $validator->addDirectiveValidator($this->make('Exists', 'TYPE')); + $validator->addDirectiveValidator($this->make('Exists', 'DEFAULT')); + $validator->addDirectiveValidator($this->make('NamespaceExists')); + $validator->addValidator($this->make('Exists', 'DESCRIPTION')); return $validator; } + /** + * Creates a validator. + * @warning + * Only *one* argument is supported; multiple args shouldn't use + * this function. + */ + protected function make($name, $arg = null) { + $class = "HTMLPurifier_ConfigSchema_Validator_$name"; + if ($arg === null) return new $class(); + else return new $class($arg); + } + } diff --git a/library/HTMLPurifier/ConfigSchema/Validator/Alnum.php b/library/HTMLPurifier/ConfigSchema/Validator/Alnum.php index 10a7a306..ed25339a 100644 --- a/library/HTMLPurifier/ConfigSchema/Validator/Alnum.php +++ b/library/HTMLPurifier/ConfigSchema/Validator/Alnum.php @@ -1,8 +1,8 @@ namespaces[$arr['_NAMESPACE']])) { + $this->error('Cannot define directive for undefined namespace ' . $arr['_NAMESPACE']); + } + } + +} diff --git a/library/HTMLPurifier/ConfigSchema/Validator/ParseId.php b/library/HTMLPurifier/ConfigSchema/Validator/ParseId.php new file mode 100644 index 00000000..56d6ef3f --- /dev/null +++ b/library/HTMLPurifier/ConfigSchema/Validator/ParseId.php @@ -0,0 +1,15 @@ +validator = new HTMLPurifier_ConfigSchema_Validator_Alnum('ID'); + parent::setup(); } public function testValidate() { diff --git a/tests/HTMLPurifier/ConfigSchema/Validator/ExistsTest.php b/tests/HTMLPurifier/ConfigSchema/Validator/ExistsTest.php index ed0c9e21..4e6a73ba 100644 --- a/tests/HTMLPurifier/ConfigSchema/Validator/ExistsTest.php +++ b/tests/HTMLPurifier/ConfigSchema/Validator/ExistsTest.php @@ -4,8 +4,8 @@ class HTMLPurifier_ConfigSchema_Validator_ExistsTest extends HTMLPurifier_Config { public function setup() { - parent::setup(); $this->validator = new HTMLPurifier_ConfigSchema_Validator_Exists('ID'); + parent::setup(); } public function testValidate() { diff --git a/tests/HTMLPurifier/ConfigSchema/Validator/NamespaceExistsTest.php b/tests/HTMLPurifier/ConfigSchema/Validator/NamespaceExistsTest.php new file mode 100644 index 00000000..cc78ac8c --- /dev/null +++ b/tests/HTMLPurifier/ConfigSchema/Validator/NamespaceExistsTest.php @@ -0,0 +1,18 @@ + 'Namespace'); + $this->expectSchemaException('Cannot define directive for undefined namespace Namespace'); + $this->validator->validate($arr, $this->interchange); + } + + public function testValidatePass() { + $arr = array('_NAMESPACE' => 'Namespace'); + $this->interchange->addNamespace(array('ID' => 'Namespace')); + $this->validator->validate($arr, $this->interchange); + } + +} diff --git a/tests/HTMLPurifier/ConfigSchema/Validator/ParseIdTest.php b/tests/HTMLPurifier/ConfigSchema/Validator/ParseIdTest.php new file mode 100644 index 00000000..3e7bf429 --- /dev/null +++ b/tests/HTMLPurifier/ConfigSchema/Validator/ParseIdTest.php @@ -0,0 +1,25 @@ + 'Namespace'); + $this->validator->validate($arr, $this->interchange); + $this->assertIdentical($arr, array( + 'ID' => 'Namespace', + '_NAMESPACE' => 'Namespace' + )); + } + + public function testValidateDirective() { + $arr = array('ID' => 'Namespace.Directive'); + $this->validator->validate($arr, $this->interchange); + $this->assertIdentical($arr, array( + 'ID' => 'Namespace.Directive', + '_NAMESPACE' => 'Namespace', + '_DIRECTIVE' => 'Directive' + )); + } + +} diff --git a/tests/HTMLPurifier/ConfigSchema/Validator/UniqueTest.php b/tests/HTMLPurifier/ConfigSchema/Validator/UniqueTest.php index fce84e11..b71aa428 100644 --- a/tests/HTMLPurifier/ConfigSchema/Validator/UniqueTest.php +++ b/tests/HTMLPurifier/ConfigSchema/Validator/UniqueTest.php @@ -3,11 +3,6 @@ class HTMLPurifier_ConfigSchema_Validator_UniqueTest extends HTMLPurifier_ConfigSchema_ValidatorHarness { - public function setup() { - parent::setup(); - $this->validator = new HTMLPurifier_ConfigSchema_Validator_Unique(); - } - public function testValidateNamespace() { $this->interchange->addNamespace(array('ID' => 'Namespace')); $this->expectSchemaException('Cannot redefine namespace'); diff --git a/tests/HTMLPurifier/ConfigSchema/ValidatorHarness.php b/tests/HTMLPurifier/ConfigSchema/ValidatorHarness.php index 33865cec..f9dd0922 100644 --- a/tests/HTMLPurifier/ConfigSchema/ValidatorHarness.php +++ b/tests/HTMLPurifier/ConfigSchema/ValidatorHarness.php @@ -7,6 +7,14 @@ class HTMLPurifier_ConfigSchema_ValidatorHarness extends UnitTestCase public function setup() { $this->interchange = new HTMLPurifier_ConfigSchema_Interchange(); + if (empty($this->validator)) { + $class_to_test = substr(get_class($this), 0, -4); + $this->validator = new $class_to_test; + } + } + + public function teardown() { + unset($this->validator, $this->interchange); } protected function expectSchemaException($msg) {