0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2024-09-18 18:25:18 +00:00

[1.4.0] Support for configuration directive aliases added.

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@668 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang 2007-01-20 18:43:58 +00:00
parent 2e16c4a968
commit 5e366b25f8
7 changed files with 130 additions and 6 deletions

4
NEWS
View File

@ -17,7 +17,9 @@ NEWS ( CHANGELOG and HISTORY ) HTMLPurifier
! Configuration documentation looks nicer
! Added %Core.EscapeNonASCIICharacters to workaround loss of Unicode
characters while %Core.Encoding is set to a non-UTF-8 encoding.
- Replaced version check with functionality check for DOM
! Support for configuration directive aliases added
- Replaced version check with functionality check for DOM (thanks Stephen
Khoo)
. Added smoketest 'all.php', which loads all other smoketests via frames
. Implemented AttrDef_CSSURI for url(http://google.com) style declarations
. Added convenient single test selector form on test runner

4
TODO
View File

@ -9,13 +9,13 @@ TODO List
1.4 release
# Add hooks for custom behavior (for instance, YouTube preservation)
# Implement all non-essential attribute transforms
- Aggressive caching
- Upgrade SimpleTest testing code to newest version
? Rich set* methods and config file loaders for HTMLPurifier_Config
? Configuration profiles: sets of directives that get set with one func call
? ConfigSchema directive aliases (so we can rename some of them)
1.5 release
# Implement all non-essential attribute transforms
# URI validation routines tighter (see docs/dev-code-quality.html) (COMPLEX)
# Advanced URI filtering schemes (see docs/proposal-new-directives.txt)
# Error logging for filtering/cleanup procedures

View File

@ -99,6 +99,8 @@ foreach($schema->info as $namespace_name => $namespace_info) {
foreach ($namespace_info as $name => $info) {
if ($info->class == 'alias') continue;
$dom_directive = $dom_document->createElement('directive');
$dom_namespace->appendChild($dom_directive);

View File

@ -75,12 +75,17 @@ class HTMLPurifier_Config
* @param $namespace String namespace
* @param $key String key
*/
function get($namespace, $key) {
function get($namespace, $key, $from_alias = false) {
if (!isset($this->def->info[$namespace][$key])) {
trigger_error('Cannot retrieve value of undefined directive',
E_USER_WARNING);
return;
}
if ($this->def->info[$namespace][$key]->class == 'alias') {
trigger_error('Cannot get value from aliased directive, use real name',
E_USER_ERROR);
return;
}
return $this->conf[$namespace][$key];
}
@ -103,12 +108,22 @@ class HTMLPurifier_Config
* @param $key String key
* @param $value Mixed value
*/
function set($namespace, $key, $value) {
function set($namespace, $key, $value, $from_alias = false) {
if (!isset($this->def->info[$namespace][$key])) {
trigger_error('Cannot set undefined directive to value',
E_USER_WARNING);
return;
}
if ($this->def->info[$namespace][$key]->class == 'alias') {
if ($from_alias) {
trigger_error('Double-aliases not allowed, please fix '.
'ConfigSchema bug');
}
$this->set($this->def->info[$namespace][$key]->namespace,
$this->def->info[$namespace][$key]->name,
$value, true);
return;
}
$value = $this->def->validate(
$value,
$this->def->info[$namespace][$key]->type,

View File

@ -224,6 +224,46 @@ class HTMLPurifier_ConfigSchema {
}
}
/**
* Defines a directive alias for backwards compatibility
* @static
* @param $namespace
* @param $name Directive that will be aliased
* @param $new_namespace
* @param $new_name Directive that the alias will be to
*/
function defineAlias($namespace, $name, $new_namespace, $new_name) {
$def =& HTMLPurifier_ConfigSchema::instance();
if (!isset($def->info[$namespace])) {
trigger_error('Cannot define directive alias for undefined namespace',
E_USER_ERROR);
return;
}
if (!ctype_alnum($name)) {
trigger_error('Directive name must be alphanumeric',
E_USER_ERROR);
return;
}
if (isset($def->info[$namespace][$name])) {
trigger_error('Cannot define alias over directive',
E_USER_ERROR);
return;
}
if (!isset($def->info[$new_namespace][$new_name])) {
trigger_error('Cannot define alias to undefined directive',
E_USER_ERROR);
return;
}
if ($def->info[$new_namespace][$new_name]->class == 'alias') {
trigger_error('Cannot define alias to alias',
E_USER_ERROR);
return;
}
$def->info[$namespace][$name] = new HTMLPurifier_ConfigEntity_DirectiveAlias();
$def->info[$namespace][$name]->namespace = $new_namespace;
$def->info[$namespace][$name]->name = $new_name;
}
/**
* Validate a variable according to type. Return null if invalid.
*/
@ -318,13 +358,17 @@ class HTMLPurifier_ConfigSchema {
/**
* Base class for configuration entity
*/
class HTMLPurifier_ConfigEntity {}
class HTMLPurifier_ConfigEntity {
var $class = false;
}
/**
* Structure object describing of a namespace
*/
class HTMLPurifier_ConfigEntity_Namespace extends HTMLPurifier_ConfigEntity {
var $class = 'namespace';
/**
* String description of what kinds of directives go in this namespace.
*/
@ -339,6 +383,8 @@ class HTMLPurifier_ConfigEntity_Namespace extends HTMLPurifier_ConfigEntity {
class HTMLPurifier_ConfigEntity_Directive extends HTMLPurifier_ConfigEntity
{
var $class = 'directive';
/**
* Hash of value aliases, i.e. values that are equivalent.
*/
@ -385,4 +431,21 @@ class HTMLPurifier_ConfigEntity_Directive extends HTMLPurifier_ConfigEntity
}
/**
* Structure object describing a directive alias
*/
class HTMLPurifier_ConfigEntity_DirectiveAlias extends HTMLPurifier_ConfigEntity
{
var $class = 'alias';
/**
* Namespace being aliased to
*/
var $namespace;
/**
* Directive being aliased to
*/
var $name;
}
?>

View File

@ -251,6 +251,30 @@ class HTMLPurifier_ConfigSchemaTest extends UnitTestCase
$this->assertError('Namespace name must be alphanumeric');
$this->assertNoErrors();
// alias related tests
HTMLPurifier_ConfigSchema::defineNamespace('Home', 'Sweet home.');
HTMLPurifier_ConfigSchema::define('Home', 'Rug', 3, 'int', 'ID.');
HTMLPurifier_ConfigSchema::defineAlias('Home', 'Carpet', 'Home', 'Rug');
$this->expectError('Cannot define directive alias for undefined namespace');
HTMLPurifier_ConfigSchema::defineAlias('Store', 'Rug', 'Home', 'Rug');
$this->expectError('Directive name must be alphanumeric');
HTMLPurifier_ConfigSchema::defineAlias('Home', 'R.g', 'Home', 'Rug');
HTMLPurifier_ConfigSchema::define('Home', 'Rugger', 'Bob Max', 'string', 'Name of.');
$this->expectError('Cannot define alias over directive');
HTMLPurifier_ConfigSchema::defineAlias('Home', 'Rugger', 'Home', 'Rug');
$this->expectError('Cannot define alias to undefined directive');
HTMLPurifier_ConfigSchema::defineAlias('Home', 'Rug2', 'Home', 'Rugavan');
$this->expectError('Cannot define alias to alias');
HTMLPurifier_ConfigSchema::defineAlias('Home', 'Rug2', 'Home', 'Carpet');
}
function assertValid($var, $type, $ret = null) {

View File

@ -199,6 +199,24 @@ class HTMLPurifier_ConfigTest extends UnitTestCase
}
function testAliases() {
HTMLPurifier_ConfigSchema::defineNamespace('Home', 'Sweet home.');
HTMLPurifier_ConfigSchema::define('Home', 'Rug', 3, 'int', 'ID.');
HTMLPurifier_ConfigSchema::defineAlias('Home', 'Carpet', 'Home', 'Rug');
$config = HTMLPurifier_Config::createDefault();
$this->assertEqual($config->get('Home', 'Rug'), 3);
$this->expectError('Cannot get value from aliased directive, use real name');
$config->get('Home', 'Carpet');
$config->set('Home', 'Carpet', 999);
$this->assertEqual($config->get('Home', 'Rug'), 999);
}
}
?>