From d467af6c4b8bfd513e0904c6329a6f7ac9ff0cf9 Mon Sep 17 00:00:00 2001 From: "Edward Z. Yang" Date: Fri, 4 Apr 2008 22:04:10 +0000 Subject: [PATCH] [3.1.0] Feature-parity achieved for validator! Implement alias checking. git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1648 48356398-32a2-884e-a903-53898d9a118a --- library/HTMLPurifier/ConfigSchema/Validator.php | 63 +++++++++++++++++++++- .../directive/aliasesAliasCollision.vtest | 16 ++++++ .../directive/aliasesDirectiveCollision.vtest | 15 ++++++ .../Validator/directive/aliasesId.vtest | 10 ++++ .../Validator/directive/defaultIsAllowed.vtest | 10 ++++ 5 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 tests/HTMLPurifier/ConfigSchema/Validator/directive/aliasesAliasCollision.vtest create mode 100644 tests/HTMLPurifier/ConfigSchema/Validator/directive/aliasesDirectiveCollision.vtest create mode 100644 tests/HTMLPurifier/ConfigSchema/Validator/directive/aliasesId.vtest create mode 100644 tests/HTMLPurifier/ConfigSchema/Validator/directive/defaultIsAllowed.vtest diff --git a/library/HTMLPurifier/ConfigSchema/Validator.php b/library/HTMLPurifier/ConfigSchema/Validator.php index fdb7b69d..bb88b1ff 100644 --- a/library/HTMLPurifier/ConfigSchema/Validator.php +++ b/library/HTMLPurifier/ConfigSchema/Validator.php @@ -11,7 +11,10 @@ class HTMLPurifier_ConfigSchema_Validator { - protected $interchange; + /** + * Easy to access global objects. + */ + protected $interchange, $aliases; /** * Context-stack to provide easy to read error messages. @@ -19,7 +22,7 @@ class HTMLPurifier_ConfigSchema_Validator protected $context = array(); /** - * HTMLPurifier_VarParser to test variable types. + * HTMLPurifier_VarParser to test default's type. */ protected $parser; @@ -33,6 +36,7 @@ class HTMLPurifier_ConfigSchema_Validator */ public function validate($interchange) { $this->interchange = $interchange; + $this->aliases = array(); // PHP is a bit lax with integer <=> string conversions in // arrays, so we don't use the identical !== comparison foreach ($interchange->namespaces as $i => $namespace) { @@ -46,6 +50,9 @@ class HTMLPurifier_ConfigSchema_Validator } } + /** + * Validates a HTMLPurifier_ConfigSchema_Interchange_Namespace object. + */ public function validateNamespace($n) { $this->context[] = "namespace '{$n->namespace}'"; $this->with($n, 'namespace') @@ -57,6 +64,9 @@ class HTMLPurifier_ConfigSchema_Validator array_pop($this->context); } + /** + * Validates a HTMLPurifier_ConfigSchema_Interchange_Id object. + */ public function validateId($id) { $id_string = $id->toString(); $this->context[] = "id '$id_string'"; @@ -73,10 +83,14 @@ class HTMLPurifier_ConfigSchema_Validator array_pop($this->context); } + /** + * Validates a HTMLPurifier_ConfigSchema_Interchange_Directive object. + */ public function validateDirective($d) { $id = $d->id->toString(); $this->context[] = "directive '$id'"; $this->validateId($d->id); + $this->with($d, 'description') ->assertNotEmpty(); @@ -103,15 +117,23 @@ class HTMLPurifier_ConfigSchema_Validator $this->validateDirectiveAllowed($d); $this->validateDirectiveValueAliases($d); + $this->validateDirectiveAliases($d); array_pop($this->context); } + /** + * Extra validation if $allowed member variable of + * HTMLPurifier_ConfigSchema_Interchange_Directive is defined. + */ public function validateDirectiveAllowed($d) { if (is_null($d->allowed)) return; $this->with($d, 'allowed') ->assertNotEmpty() ->assertIsLookup(); // handled by InterchangeBuilder + if (!isset($d->allowed[$d->default])) { + $this->error('default', 'must be an allowed value'); + } $this->context[] = 'allowed'; foreach ($d->allowed as $val => $x) { if (!is_string($val)) $this->error("value $val", 'must be a string'); @@ -119,6 +141,10 @@ class HTMLPurifier_ConfigSchema_Validator array_pop($this->context); } + /** + * Extra validation if $valueAliases member variable of + * HTMLPurifier_ConfigSchema_Interchange_Directive is defined. + */ public function validateDirectiveValueAliases($d) { if (is_null($d->valueAliases)) return; $this->with($d, 'valueAliases') @@ -143,18 +169,51 @@ class HTMLPurifier_ConfigSchema_Validator array_pop($this->context); } + /** + * Extra validation if $aliases member variable of + * HTMLPurifier_ConfigSchema_Interchange_Directive is defined. + */ + public function validateDirectiveAliases($d) { + $this->with($d, 'aliases') + ->assertIsArray(); // handled by InterchangeBuilder + $this->context[] = 'aliases'; + foreach ($d->aliases as $alias) { + $this->validateId($alias); + $s = $alias->toString(); + if (isset($this->interchange->directives[$s])) { + $this->error("alias '$s'", 'collides with another directive'); + } + if (isset($this->aliases[$s])) { + $other_directive = $this->aliases[$s]; + $this->error("alias '$s'", "collides with alias for directive '$other_directive'"); + } + $this->aliases[$s] = $d->id->toString(); + } + array_pop($this->context); + } + // protected helper functions + /** + * Convenience function for generating HTMLPurifier_ConfigSchema_ValidatorAtom + * for validating simple member variables of objects. + */ protected function with($obj, $member) { return new HTMLPurifier_ConfigSchema_ValidatorAtom($this->getFormattedContext(), $obj, $member); } + /** + * Emits an error, providing helpful context. + */ protected function error($target, $msg) { if ($target !== false) $prefix = ucfirst($target) . ' in ' . $this->getFormattedContext(); else $prefix = ucfirst($this->getFormattedContext()); throw new HTMLPurifier_ConfigSchema_Exception(trim($prefix . ' ' . $msg)); } + /** + * Returns a formatted context string. + */ protected function getFormattedContext() { return implode(' in ', array_reverse($this->context)); } diff --git a/tests/HTMLPurifier/ConfigSchema/Validator/directive/aliasesAliasCollision.vtest b/tests/HTMLPurifier/ConfigSchema/Validator/directive/aliasesAliasCollision.vtest new file mode 100644 index 00000000..75506b83 --- /dev/null +++ b/tests/HTMLPurifier/ConfigSchema/Validator/directive/aliasesAliasCollision.vtest @@ -0,0 +1,16 @@ +ERROR: Alias 'Ns.BothWantThisName' in aliases in directive 'Ns.Dir2' collides with alias for directive 'Ns.Dir' +---- +Ns +DESCRIPTION: Namespace +---- +Ns.Dir +DESCRIPTION: Directive +TYPE: int +DEFAULT: 3 +ALIASES: Ns.BothWantThisName +---- +Ns.Dir2 +DESCRIPTION: Directive +TYPE: string +DEFAULT: 'a' +ALIASES: Ns.BothWantThisName diff --git a/tests/HTMLPurifier/ConfigSchema/Validator/directive/aliasesDirectiveCollision.vtest b/tests/HTMLPurifier/ConfigSchema/Validator/directive/aliasesDirectiveCollision.vtest new file mode 100644 index 00000000..9629c49d --- /dev/null +++ b/tests/HTMLPurifier/ConfigSchema/Validator/directive/aliasesDirectiveCollision.vtest @@ -0,0 +1,15 @@ +ERROR: Alias 'Ns.Innocent' in aliases in directive 'Ns.Dir' collides with another directive +---- +Ns +DESCRIPTION: Namespace +---- +Ns.Innocent +DESCRIPTION: Innocent directive +TYPE: int +DEFAULT: 3 +---- +Ns.Dir +DESCRIPTION: Directive +TYPE: string +DEFAULT: 'a' +ALIASES: Ns.Innocent diff --git a/tests/HTMLPurifier/ConfigSchema/Validator/directive/aliasesId.vtest b/tests/HTMLPurifier/ConfigSchema/Validator/directive/aliasesId.vtest new file mode 100644 index 00000000..ba1c9346 --- /dev/null +++ b/tests/HTMLPurifier/ConfigSchema/Validator/directive/aliasesId.vtest @@ -0,0 +1,10 @@ +ERROR: Directive in id 'Ns.R&D' in aliases in directive 'Ns.Dir' must be alphanumeric +---- +Ns +DESCRIPTION: Namespace +---- +Ns.Dir +DESCRIPTION: Directive +TYPE: string +DEFAULT: 'a' +ALIASES: Ns.R&D diff --git a/tests/HTMLPurifier/ConfigSchema/Validator/directive/defaultIsAllowed.vtest b/tests/HTMLPurifier/ConfigSchema/Validator/directive/defaultIsAllowed.vtest new file mode 100644 index 00000000..c2ab1477 --- /dev/null +++ b/tests/HTMLPurifier/ConfigSchema/Validator/directive/defaultIsAllowed.vtest @@ -0,0 +1,10 @@ +ERROR: Default in directive 'Ns.Dir' must be an allowed value +---- +Ns +DESCRIPTION: Namespace +---- +Ns.Dir +DESCRIPTION: Directive +TYPE: string +DEFAULT: 'a' +ALLOWED: 'b' -- 2.11.4.GIT