mirror of
https://github.com/ezyang/htmlpurifier.git
synced 2025-07-30 19:00:10 +02: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:
@@ -48,6 +48,16 @@ class HTMLPurifier_ConfigSchema_InterchangeBuilder
|
||||
$type = explode('/', $hash->offsetGet('TYPE'));
|
||||
if (isset($type[1])) $directive->typeAllowsNull = true;
|
||||
$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'])) {
|
||||
@@ -59,7 +69,15 @@ class HTMLPurifier_ConfigSchema_InterchangeBuilder
|
||||
}
|
||||
|
||||
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'])) {
|
||||
|
@@ -9,9 +9,18 @@ class HTMLPurifier_ConfigSchema_Validator
|
||||
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
|
||||
@@ -38,17 +47,6 @@ class HTMLPurifier_ConfigSchema_Validator
|
||||
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) {
|
||||
$this->context[] = "id '$id'";
|
||||
if (!isset($this->interchange->namespaces[$id->namespace])) {
|
||||
@@ -63,6 +61,21 @@ class HTMLPurifier_ConfigSchema_Validator
|
||||
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 function with($obj, $member) {
|
||||
|
@@ -36,7 +36,7 @@ class HTMLPurifier_VarParser
|
||||
*/
|
||||
final public function parse($var, $type, $allow_null = false) {
|
||||
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);
|
||||
if ($allow_null && $var === null) return null;
|
||||
@@ -73,7 +73,7 @@ class HTMLPurifier_VarParser
|
||||
case 'mixed':
|
||||
return $var;
|
||||
default:
|
||||
$this->errorInconsistent(__CLASS__, $type);
|
||||
$this->errorInconsistent(get_class($this), $type);
|
||||
}
|
||||
$this->errorGeneric($var, $type);
|
||||
}
|
||||
|
Reference in New Issue
Block a user