1
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2025-08-01 11:50:28 +02:00
- Add tests for the atoms.
- Add Id validation for Directives

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1623 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang
2008-03-22 21:06:55 +00:00
parent 34ba0e408f
commit b8f00ace1a
4 changed files with 98 additions and 19 deletions

View File

@@ -30,7 +30,6 @@ class HTMLPurifier_ConfigSchema_Validator
public function validateNamespace($n) {
$this->context = "namespace '{$n->namespace}'";
$this->with($n, 'namespace')
->assertIsString()
->assertNotEmpty()
->assertAlnum();
$this->with($n, 'description')
@@ -40,12 +39,27 @@ class HTMLPurifier_ConfigSchema_Validator
public function validateDirective($d) {
$this->context = "directive '{$d->id}'";
$this->validateId($d->id);
}
public function validateId($id) {
$this->context = "id '$id'";
$this->with($id, 'namespace')
->assertNotEmpty()
->assertAlnum();
$this->with($id, 'directive')
->assertNotEmpty()
->assertAlnum();
}
// protected helper functions
protected function with($obj, $member) {
return new HTMLPurifier_ConfigSchema_ValidatorAtom($this->interchange, $this->context, $obj, $member);
return new HTMLPurifier_ConfigSchema_ValidatorAtom($this->context, $obj, $member);
}
protected function error($msg) {
throw new HTMLPurifier_ConfigSchema_Exception($msg . ' in ' . $this->context);
}
}

View File

@@ -3,15 +3,15 @@
/**
* Fluent interface for validating the contents of member variables.
* This should be immutable. See HTMLPurifier_ConfigSchema_Validator for
* use-cases.
* use-cases. We name this an 'atom' because it's ONLY for validations that
* are independent and usually scalar.
*/
class HTMLPurifier_ConfigSchema_ValidatorAtom
{
protected $interchange, $context, $obj, $member, $contents;
protected $context, $obj, $member, $contents;
public function __construct($interchange, $context, $obj, $member) {
$this->interchange = $interchange;
public function __construct($context, $obj, $member) {
$this->context = $context;
$this->obj = $obj;
$this->member = $member;
@@ -24,11 +24,12 @@ class HTMLPurifier_ConfigSchema_ValidatorAtom
}
public function assertNotNull() {
if (is_null($this->contents)) $this->error('must not be null');
if ($this->contents === null) $this->error('must not be null');
return $this;
}
public function assertAlnum() {
$this->assertIsString();
if (!ctype_alnum($this->contents)) $this->error('must be alphanumeric');
return $this;
}