0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2025-01-05 06:01:52 +00:00

[3.1.0] Implement more validators, add in missing DEFAULTs for many tests.

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1626 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang 2008-03-23 01:29:57 +00:00
parent aedfbd1e93
commit 82c9a737f4
11 changed files with 72 additions and 17 deletions

View File

@ -48,6 +48,16 @@ class HTMLPurifier_ConfigSchema_InterchangeBuilder
$type = explode('/', $hash->offsetGet('TYPE')); $type = explode('/', $hash->offsetGet('TYPE'));
if (isset($type[1])) $directive->typeAllowsNull = true; if (isset($type[1])) $directive->typeAllowsNull = true;
$directive->type = $type[0]; $directive->type = $type[0];
} else {
throw new HTMLPurifier_ConfigSchema_Exception("TYPE in directive hash '{$directive->id}' not defined");
}
if (isset($hash['DEFAULT'])) {
try {
$directive->default = $this->varParser->parse($hash->offsetGet('DEFAULT'), $directive->type, $directive->typeAllowsNull);
} catch (HTMLPurifier_VarParserException $e) {
throw new HTMLPurifier_ConfigSchema_Exception($e->getMessage() . " in DEFAULT in directive hash '{$directive->id}'");
}
} }
if (isset($hash['DESCRIPTION'])) { if (isset($hash['DESCRIPTION'])) {
@ -59,7 +69,15 @@ class HTMLPurifier_ConfigSchema_InterchangeBuilder
} }
if (isset($hash['VALUE-ALIASES'])) { if (isset($hash['VALUE-ALIASES'])) {
$directive->valueAliases = $this->evalArray($hash->offsetGet('VALUE-ALIASES')); $value_aliases = $this->evalArray($hash->offsetGet('VALUE-ALIASES'));
try {
foreach ($value_aliases as $alias => $real) {
$directive->valueAliases[$this->varParser->parse($alias, $directive->type, $directive->typeAllowsNull)] =
$this->varParser->parse($real, $directive->type, $directive->typeAllowsNull);
}
} catch (HTMLPurifier_VarParserException $e) {
throw new HTMLPurifier_ConfigSchema_Exception($e->getMessage() . " in $alias => $real in VALUE-ALIASES in directive hash '{$directive->id}'");
}
} }
if (isset($hash['ALIASES'])) { if (isset($hash['ALIASES'])) {

View File

@ -9,9 +9,18 @@ class HTMLPurifier_ConfigSchema_Validator
protected $interchange; protected $interchange;
/** /**
* Volatile context variables to provide a fluent interface. * Context-stack to provide easy to read error messages.
*/ */
protected $context = array(), $obj, $member; protected $context = array();
/**
* HTMLPurifier_VarParser to test variable types.
*/
protected $parser;
public function __construct() {
$this->parser = new HTMLPurifier_VarParser();
}
/** /**
* Validates a fully-formed interchange object. Throws an * Validates a fully-formed interchange object. Throws an
@ -38,17 +47,6 @@ class HTMLPurifier_ConfigSchema_Validator
array_pop($this->context); array_pop($this->context);
} }
public function validateDirective($d) {
$this->context[] = "directive '{$d->id}'";
$this->validateId($d->id);
$this->with($d, 'description')
->assertNotEmpty();
if (!isset(HTMLPurifier_VarParser::$types[$d->type])) {
$this->error('type', 'is invalid');
}
array_pop($this->context);
}
public function validateId($id) { public function validateId($id) {
$this->context[] = "id '$id'"; $this->context[] = "id '$id'";
if (!isset($this->interchange->namespaces[$id->namespace])) { if (!isset($this->interchange->namespaces[$id->namespace])) {
@ -63,6 +61,21 @@ class HTMLPurifier_ConfigSchema_Validator
array_pop($this->context); array_pop($this->context);
} }
public function validateDirective($d) {
$this->context[] = "directive '{$d->id}'";
$this->validateId($d->id);
$this->with($d, 'description')
->assertNotEmpty();
$this->with($d, 'type')
->assertNotEmpty();
if (!isset(HTMLPurifier_VarParser::$types[$d->type])) {
$this->error('type', 'is invalid');
}
$this->parser->parse($d->default, $d->type, $d->typeAllowsNull);
array_pop($this->context);
}
// protected helper functions // protected helper functions
protected function with($obj, $member) { protected function with($obj, $member) {

View File

@ -36,7 +36,7 @@ class HTMLPurifier_VarParser
*/ */
final public function parse($var, $type, $allow_null = false) { final public function parse($var, $type, $allow_null = false) {
if (!isset(HTMLPurifier_VarParser::$types[$type])) { if (!isset(HTMLPurifier_VarParser::$types[$type])) {
throw new HTMLPurifier_VarParserException("Invalid type $type"); throw new HTMLPurifier_VarParserException("Invalid type '$type'");
} }
$var = $this->parseImplementation($var, $type, $allow_null); $var = $this->parseImplementation($var, $type, $allow_null);
if ($allow_null && $var === null) return null; if ($allow_null && $var === null) return null;
@ -73,7 +73,7 @@ class HTMLPurifier_VarParser
case 'mixed': case 'mixed':
return $var; return $var;
default: default:
$this->errorInconsistent(__CLASS__, $type); $this->errorInconsistent(get_class($this), $type);
} }
$this->errorGeneric($var, $type); $this->errorGeneric($var, $type);
} }

View File

@ -0,0 +1,9 @@
ERROR: Expected type string, got integer in DEFAULT in directive hash 'Ns.Dir'
----
Ns
DESCRIPTION: Namespace
----
Ns.Dir
DESCRIPTION: Directive
TYPE: string
DEFAULT: 0

View File

@ -5,3 +5,4 @@ DESCRIPTION: Our namespace.
---- ----
Ns.Dir Ns.Dir
TYPE: int TYPE: int
DEFAULT: 0

View File

@ -6,3 +6,4 @@ DESCRIPTION: Generic namespace.
ID: Ns.+ ID: Ns.+
TYPE: int TYPE: int
DESCRIPTION: Description DESCRIPTION: Description
DEFAULT: 0

View File

@ -6,3 +6,4 @@ DESCRIPTION: Our namespace
ID: Ns. ID: Ns.
TYPE: int TYPE: int
DESCRIPTION: Description. DESCRIPTION: Description.
DEFAULT: 0

View File

@ -3,3 +3,4 @@ ERROR: Namespace in id 'Rd.Dir' in directive 'Rd.Dir' does not exist
ID: Rd.Dir ID: Rd.Dir
TYPE: int TYPE: int
DESCRIPTION: Description DESCRIPTION: Description
DEFAULT: 0

View File

@ -0,0 +1,8 @@
ERROR: TYPE in directive hash 'Ns.Dir' not defined
----
Ns
DESCRIPTION: Namespace
----
Ns.Dir
DESCRIPTION: Notice that TYPE is missing
DEFAULT: 0

View File

@ -1,4 +1,4 @@
ERROR: Type in directive 'Ns.Dir' is invalid ERROR: Invalid type 'foobar' in DEFAULT in directive hash 'Ns.Dir'
---- ----
Ns Ns
DESCRIPTION: Namespace DESCRIPTION: Namespace
@ -6,3 +6,4 @@ DESCRIPTION: Namespace
Ns.Dir Ns.Dir
DESCRIPTION: Directive DESCRIPTION: Directive
TYPE: foobar TYPE: foobar
DEFAULT: 0

View File

@ -3,7 +3,9 @@ ERROR: Cannot redefine directive 'Ns.Dir'
ID: Ns.Dir ID: Ns.Dir
DESCRIPTION: Version 1 DESCRIPTION: Version 1
TYPE: int TYPE: int
DEFAULT: 0
---- ----
ID: Ns.Dir ID: Ns.Dir
DESCRIPTION: Version 2 DESCRIPTION: Version 2
TYPE: int TYPE: int
DEFAULT: 0