mirror of
https://github.com/ezyang/htmlpurifier.git
synced 2025-01-20 12:31:53 +00:00
Add ParseType validator.
git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1600 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
parent
c0b5bc3eea
commit
b9eb44bf03
@ -135,6 +135,7 @@ require 'HTMLPurifier/ConfigSchema/Validator/Alnum.php';
|
|||||||
require 'HTMLPurifier/ConfigSchema/Validator/Exists.php';
|
require 'HTMLPurifier/ConfigSchema/Validator/Exists.php';
|
||||||
require 'HTMLPurifier/ConfigSchema/Validator/NamespaceExists.php';
|
require 'HTMLPurifier/ConfigSchema/Validator/NamespaceExists.php';
|
||||||
require 'HTMLPurifier/ConfigSchema/Validator/ParseId.php';
|
require 'HTMLPurifier/ConfigSchema/Validator/ParseId.php';
|
||||||
|
require 'HTMLPurifier/ConfigSchema/Validator/ParseType.php';
|
||||||
require 'HTMLPurifier/ConfigSchema/Validator/Unique.php';
|
require 'HTMLPurifier/ConfigSchema/Validator/Unique.php';
|
||||||
require 'HTMLPurifier/DefinitionCache/Decorator.php';
|
require 'HTMLPurifier/DefinitionCache/Decorator.php';
|
||||||
require 'HTMLPurifier/DefinitionCache/Null.php';
|
require 'HTMLPurifier/DefinitionCache/Null.php';
|
||||||
|
@ -57,17 +57,28 @@ class HTMLPurifier_ConfigSchema_Interchange
|
|||||||
$validator = new HTMLPurifier_ConfigSchema_InterchangeValidator($this);
|
$validator = new HTMLPurifier_ConfigSchema_InterchangeValidator($this);
|
||||||
|
|
||||||
// Validators should be defined in the order they are to be called.
|
// Validators should be defined in the order they are to be called.
|
||||||
|
|
||||||
|
// ID tests
|
||||||
$validator->addValidator($this->make('Exists', 'ID'));
|
$validator->addValidator($this->make('Exists', 'ID'));
|
||||||
$validator->addValidator($this->make('Unique'));
|
$validator->addValidator($this->make('Unique'));
|
||||||
$validator->addNamespaceValidator($this->make('Alnum', 'ID'));
|
$validator->addNamespaceValidator($this->make('Alnum', 'ID'));
|
||||||
$validator->addValidator($this->make('ParseId'));
|
$validator->addValidator($this->make('ParseId'));
|
||||||
$validator->addValidator($this->make('Exists', '_NAMESPACE'));
|
$validator->addValidator($this->make('Exists', '_NAMESPACE'));
|
||||||
$validator->addValidator($this->make('Alnum', '_NAMESPACE'));
|
$validator->addValidator($this->make('Alnum', '_NAMESPACE'));
|
||||||
|
|
||||||
|
// Directive tests
|
||||||
$validator->addDirectiveValidator($this->make('Exists', '_DIRECTIVE'));
|
$validator->addDirectiveValidator($this->make('Exists', '_DIRECTIVE'));
|
||||||
$validator->addDirectiveValidator($this->make('Alnum', '_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->addDirectiveValidator($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'));
|
||||||
|
|
||||||
|
// Common tests
|
||||||
$validator->addValidator($this->make('Exists', 'DESCRIPTION'));
|
$validator->addValidator($this->make('Exists', 'DESCRIPTION'));
|
||||||
|
|
||||||
return $validator;
|
return $validator;
|
||||||
|
19
library/HTMLPurifier/ConfigSchema/Validator/ParseType.php
Normal file
19
library/HTMLPurifier/ConfigSchema/Validator/ParseType.php
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parses TYPE into _TYPE and _NULL. Expects TYPE and ID to exist.
|
||||||
|
*/
|
||||||
|
class HTMLPurifier_ConfigSchema_Validator_ParseType extends HTMLPurifier_ConfigSchema_Validator
|
||||||
|
{
|
||||||
|
|
||||||
|
public function validate(&$arr, $interchange) {
|
||||||
|
$r = explode('/', $arr['TYPE'], 2);
|
||||||
|
if (!isset($interchange->types[$r[0]])) {
|
||||||
|
$this->error('Invalid type ' . $r[0] . ' for configuration directive ' . $arr['ID']);
|
||||||
|
}
|
||||||
|
$arr['_TYPE'] = $r[0];
|
||||||
|
$arr['_NULL'] = (isset($r[1]) && $r[1] === 'null');
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
34
tests/HTMLPurifier/ConfigSchema/Validator/ParseTypeTest.php
Normal file
34
tests/HTMLPurifier/ConfigSchema/Validator/ParseTypeTest.php
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
class HTMLPurifier_ConfigSchema_Validator_ParseTypeTest extends HTMLPurifier_ConfigSchema_ValidatorHarness
|
||||||
|
{
|
||||||
|
|
||||||
|
public function testValidatePlain() {
|
||||||
|
$arr = array('ID' => 'N.D', 'TYPE' => 'string');
|
||||||
|
$this->validator->validate($arr, $this->interchange);
|
||||||
|
$this->assertIdentical($arr, array(
|
||||||
|
'ID' => 'N.D',
|
||||||
|
'TYPE' => 'string',
|
||||||
|
'_TYPE' => 'string',
|
||||||
|
'_NULL' => false,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testValidateWithNull() {
|
||||||
|
$arr = array('ID' => 'N.D', 'TYPE' => 'int/null');
|
||||||
|
$this->validator->validate($arr, $this->interchange);
|
||||||
|
$this->assertIdentical($arr, array(
|
||||||
|
'ID' => 'N.D',
|
||||||
|
'TYPE' => 'int/null',
|
||||||
|
'_TYPE' => 'int',
|
||||||
|
'_NULL' => true,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testValidateInvalidType() {
|
||||||
|
$arr = array('ID' => 'N.D', 'TYPE' => 'aint/null');
|
||||||
|
$this->expectSchemaException('Invalid type aint for configuration directive N.D');
|
||||||
|
$this->validator->validate($arr, $this->interchange);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user