mirror of
https://github.com/ezyang/htmlpurifier.git
synced 2025-07-31 19:30:21 +02: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:
@@ -57,17 +57,32 @@ class HTMLPurifier_ConfigSchema_Interchange
|
||||
$validator = new HTMLPurifier_ConfigSchema_InterchangeValidator($this);
|
||||
|
||||
// Validators should be defined in the order they are to be called.
|
||||
|
||||
// Common validators
|
||||
$validator->addValidator(new HTMLPurifier_ConfigSchema_Validator_Exists('ID'));
|
||||
$validator->addValidator(new HTMLPurifier_ConfigSchema_Validator_Unique());
|
||||
$validator->addValidator(new HTMLPurifier_ConfigSchema_Validator_Exists('DESCRIPTION'));
|
||||
|
||||
// Namespace validators
|
||||
|
||||
// Directive validators
|
||||
$validator->addValidator($this->make('Exists', 'ID'));
|
||||
$validator->addValidator($this->make('Unique'));
|
||||
$validator->addNamespaceValidator($this->make('Alnum', 'ID'));
|
||||
$validator->addValidator($this->make('ParseId'));
|
||||
$validator->addValidator($this->make('Exists', '_NAMESPACE'));
|
||||
$validator->addValidator($this->make('Alnum', '_NAMESPACE'));
|
||||
$validator->addDirectiveValidator($this->make('Exists', '_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->addValidator($this->make('Exists', 'DESCRIPTION'));
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,8 +1,8 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Validates that a field is alphanumeric in the array (does not check
|
||||
* existence!)
|
||||
* Validates that a field is alphanumeric in the array. Expects $index
|
||||
* to exist.
|
||||
*/
|
||||
class HTMLPurifier_ConfigSchema_Validator_Alnum extends HTMLPurifier_ConfigSchema_Validator
|
||||
{
|
||||
|
@@ -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']);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
15
library/HTMLPurifier/ConfigSchema/Validator/ParseId.php
Normal file
15
library/HTMLPurifier/ConfigSchema/Validator/ParseId.php
Normal 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];
|
||||
}
|
||||
|
||||
}
|
@@ -2,6 +2,8 @@
|
||||
|
||||
/**
|
||||
* Validates that this ID does not exist already in the interchange object.
|
||||
* Expects ID to exist.
|
||||
*
|
||||
* @note
|
||||
* 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.
|
||||
|
Reference in New Issue
Block a user