0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2025-03-23 14:27:02 +00:00

[3.1.0] Implement NamespaceExists and ParseId

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1599 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang 2008-03-04 05:21:04 +00:00
parent 14437cbf47
commit c0b5bc3eea
12 changed files with 114 additions and 18 deletions

View File

@ -133,6 +133,8 @@ require 'HTMLPurifier/ConfigSchema/StringHashReverseAdapter.php';
require 'HTMLPurifier/ConfigSchema/Validator.php'; require 'HTMLPurifier/ConfigSchema/Validator.php';
require 'HTMLPurifier/ConfigSchema/Validator/Alnum.php'; 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/ParseId.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';

View File

@ -57,17 +57,32 @@ 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.
$validator->addValidator($this->make('Exists', 'ID'));
// Common validators $validator->addValidator($this->make('Unique'));
$validator->addValidator(new HTMLPurifier_ConfigSchema_Validator_Exists('ID')); $validator->addNamespaceValidator($this->make('Alnum', 'ID'));
$validator->addValidator(new HTMLPurifier_ConfigSchema_Validator_Unique()); $validator->addValidator($this->make('ParseId'));
$validator->addValidator(new HTMLPurifier_ConfigSchema_Validator_Exists('DESCRIPTION')); $validator->addValidator($this->make('Exists', '_NAMESPACE'));
$validator->addValidator($this->make('Alnum', '_NAMESPACE'));
// Namespace validators $validator->addDirectiveValidator($this->make('Exists', '_DIRECTIVE'));
$validator->addDirectiveValidator($this->make('Alnum', '_DIRECTIVE'));
// Directive validators $validator->addDirectiveValidator($this->make('Exists', 'TYPE'));
$validator->addDirectiveValidator($this->make('Exists', 'DEFAULT'));
$validator->addDirectiveValidator($this->make('NamespaceExists'));
$validator->addValidator($this->make('Exists', 'DESCRIPTION'));
return $validator; return $validator;
} }
/**
* Creates a validator.
* @warning
* Only *one* argument is supported; multiple args shouldn't use
* this function.
*/
protected function make($name, $arg = null) {
$class = "HTMLPurifier_ConfigSchema_Validator_$name";
if ($arg === null) return new $class();
else return new $class($arg);
}
} }

View File

@ -1,8 +1,8 @@
<?php <?php
/** /**
* Validates that a field is alphanumeric in the array (does not check * Validates that a field is alphanumeric in the array. Expects $index
* existence!) * to exist.
*/ */
class HTMLPurifier_ConfigSchema_Validator_Alnum extends HTMLPurifier_ConfigSchema_Validator class HTMLPurifier_ConfigSchema_Validator_Alnum extends HTMLPurifier_ConfigSchema_Validator
{ {

View File

@ -0,0 +1,16 @@
<?php
/**
* Validates that the directive's namespace exists. Expects _NAMESPACE
* to have been created via HTMLPurifier_ConfigSchema_Validator_ParseId
*/
class HTMLPurifier_ConfigSchema_Validator_NamespaceExists extends HTMLPurifier_ConfigSchema_Validator
{
public function validate(&$arr, $interchange) {
if (!isset($interchange->namespaces[$arr['_NAMESPACE']])) {
$this->error('Cannot define directive for undefined namespace ' . $arr['_NAMESPACE']);
}
}
}

View File

@ -0,0 +1,15 @@
<?php
/**
* Parses ID into NAMESPACE and, if appropriate, DIRECTIVE. Expects ID to exist.
*/
class HTMLPurifier_ConfigSchema_Validator_ParseId extends HTMLPurifier_ConfigSchema_Validator
{
public function validate(&$arr, $interchange) {
$r = explode('.', $arr['ID'], 2);
$arr['_NAMESPACE'] = $r[0];
if (isset($r[1])) $arr['_DIRECTIVE'] = $r[1];
}
}

View File

@ -2,6 +2,8 @@
/** /**
* Validates that this ID does not exist already in the interchange object. * Validates that this ID does not exist already in the interchange object.
* Expects ID to exist.
*
* @note * @note
* Although this tests both possible values, in practice the ID * Although this tests both possible values, in practice the ID
* will only be in one or the other. We do this to keep things simple. * will only be in one or the other. We do this to keep things simple.

View File

@ -4,8 +4,8 @@ class HTMLPurifier_ConfigSchema_Validator_AlnumTest extends HTMLPurifier_ConfigS
{ {
public function setup() { public function setup() {
parent::setup();
$this->validator = new HTMLPurifier_ConfigSchema_Validator_Alnum('ID'); $this->validator = new HTMLPurifier_ConfigSchema_Validator_Alnum('ID');
parent::setup();
} }
public function testValidate() { public function testValidate() {

View File

@ -4,8 +4,8 @@ class HTMLPurifier_ConfigSchema_Validator_ExistsTest extends HTMLPurifier_Config
{ {
public function setup() { public function setup() {
parent::setup();
$this->validator = new HTMLPurifier_ConfigSchema_Validator_Exists('ID'); $this->validator = new HTMLPurifier_ConfigSchema_Validator_Exists('ID');
parent::setup();
} }
public function testValidate() { public function testValidate() {

View File

@ -0,0 +1,18 @@
<?php
class HTMLPurifier_ConfigSchema_Validator_NamespaceExistsTest extends HTMLPurifier_ConfigSchema_ValidatorHarness
{
public function testValidateFail() {
$arr = array('_NAMESPACE' => 'Namespace');
$this->expectSchemaException('Cannot define directive for undefined namespace Namespace');
$this->validator->validate($arr, $this->interchange);
}
public function testValidatePass() {
$arr = array('_NAMESPACE' => 'Namespace');
$this->interchange->addNamespace(array('ID' => 'Namespace'));
$this->validator->validate($arr, $this->interchange);
}
}

View File

@ -0,0 +1,25 @@
<?php
class HTMLPurifier_ConfigSchema_Validator_ParseIdTest extends HTMLPurifier_ConfigSchema_ValidatorHarness
{
public function testValidateNamespace() {
$arr = array('ID' => 'Namespace');
$this->validator->validate($arr, $this->interchange);
$this->assertIdentical($arr, array(
'ID' => 'Namespace',
'_NAMESPACE' => 'Namespace'
));
}
public function testValidateDirective() {
$arr = array('ID' => 'Namespace.Directive');
$this->validator->validate($arr, $this->interchange);
$this->assertIdentical($arr, array(
'ID' => 'Namespace.Directive',
'_NAMESPACE' => 'Namespace',
'_DIRECTIVE' => 'Directive'
));
}
}

View File

@ -3,11 +3,6 @@
class HTMLPurifier_ConfigSchema_Validator_UniqueTest extends HTMLPurifier_ConfigSchema_ValidatorHarness class HTMLPurifier_ConfigSchema_Validator_UniqueTest extends HTMLPurifier_ConfigSchema_ValidatorHarness
{ {
public function setup() {
parent::setup();
$this->validator = new HTMLPurifier_ConfigSchema_Validator_Unique();
}
public function testValidateNamespace() { public function testValidateNamespace() {
$this->interchange->addNamespace(array('ID' => 'Namespace')); $this->interchange->addNamespace(array('ID' => 'Namespace'));
$this->expectSchemaException('Cannot redefine namespace'); $this->expectSchemaException('Cannot redefine namespace');

View File

@ -7,6 +7,14 @@ class HTMLPurifier_ConfigSchema_ValidatorHarness extends UnitTestCase
public function setup() { public function setup() {
$this->interchange = new HTMLPurifier_ConfigSchema_Interchange(); $this->interchange = new HTMLPurifier_ConfigSchema_Interchange();
if (empty($this->validator)) {
$class_to_test = substr(get_class($this), 0, -4);
$this->validator = new $class_to_test;
}
}
public function teardown() {
unset($this->validator, $this->interchange);
} }
protected function expectSchemaException($msg) { protected function expectSchemaException($msg) {