0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2025-01-03 13:21:51 +00:00

[1.7.0] Implement DoctypeRegistry. Add transparent constructor to Doctype.

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1059 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang 2007-05-14 22:36:35 +00:00
parent e7b15068c2
commit 6b9c5ec603
4 changed files with 164 additions and 5 deletions

View File

@ -11,11 +11,6 @@ class HTMLPurifier_Doctype
*/
var $name;
/**
* List of aliases to doctype name
*/
var $aliases = array();
/**
* List of standard modules (string identifiers or literal objects)
* that this doctype uses
@ -28,6 +23,20 @@ class HTMLPurifier_Doctype
* is enabled, such as lenient or correctional.
*/
var $modulesForModes = array();
/**
* List of aliases to doctype name
*/
var $aliases = array();
function HTMLPurifier_Doctype($name = null, $modules = array(),
$modules_for_modes = array(), $aliases = array()
) {
$this->name = $name;
$this->modules = $modules;
$this->modulesForModes = $modules_for_modes;
$this->aliases = $aliases;
}
}
?>

View File

@ -0,0 +1,69 @@
<?php
require_once 'HTMLPurifier/Doctype.php';
class HTMLPurifier_DoctypeRegistry
{
/**
* Hash of doctype names to doctype objects
* @protected
*/
var $doctypes;
/**
* Lookup table of aliases to real doctype names
* @protected
*/
var $aliases;
/**
* Registers a doctype to the registry
* @note Accepts a fully-formed doctype object, or the
* parameters for constructing a doctype object
* @param $doctype Name of doctype or literal doctype object
* @param $modules Modules doctype will load
* @param $modules_for_modes Modules doctype will load for certain modes
* @param $aliases Alias names for doctype
* @return Reference to registered doctype (usable for further editing)
*/
function &register($doctype, $modules = array(),
$modules_for_modes = array(), $aliases = array()
) {
if (!is_array($modules)) $modules = array($modules);
if (!is_array($aliases)) $aliases = array($aliases);
if (!is_object($doctype)) {
$doctype = new HTMLPurifier_Doctype(
$doctype, $modules, $modules_for_modes, $aliases
);
}
$this->doctypes[$doctype->name] =& $doctype;
$name = $doctype->name;
// hookup aliases
foreach ($doctype->aliases as $alias) {
if (isset($this->doctypes[$alias])) continue;
$this->aliases[$alias] = $name;
}
// remove old aliases
if (isset($this->aliases[$name])) unset($this->aliases[$name]);
return $doctype;
}
/**
* Retrieves reference to a doctype of a certain name
* @note This function resolves aliases
* @param $doctype Name of doctype
* @return Reference to doctype object
*/
function &get($doctype) {
if (isset($this->aliases[$doctype])) $doctype = $this->aliases[$doctype];
if (!isset($this->doctypes[$doctype])) {
trigger_error('Doctype ' . htmlspecialchars($doctype) . ' does not exist');
$null = null; return $null;
}
return $this->doctypes[$doctype];
}
}
?>

View File

@ -0,0 +1,80 @@
<?php
require_once 'HTMLPurifier/DoctypeRegistry.php';
class HTMLPurifier_DoctypeRegistryTest extends UnitTestCase
{
function test_register() {
$registry = new HTMLPurifier_DoctypeRegistry();
$d =& $registry->register(
$name = 'XHTML 1.0 Transitional',
$modules = array('module-one', 'module-two'),
$modulesForModes = array(
'lenient' => array('lenient-module'),
),
$aliases = array('X10T')
);
$d2 = new HTMLPurifier_Doctype($name, $modules, $modulesForModes, $aliases);
$this->assertIdentical($d, $d2);
$this->assertReference($d, $registry->get('XHTML 1.0 Transitional'));
// test shorthand
$d =& $registry->register(
$name = 'XHTML 1.0 Strict', 'module', array(), 'X10S'
);
$d2 = new HTMLPurifier_Doctype($name, array('module'), array(), array('X10S'));
$this->assertIdentical($d, $d2);
}
function test_get() {
// see also alias and register tests
$registry = new HTMLPurifier_DoctypeRegistry();
$this->expectError('Doctype XHTML 2.0 does not exist');
$registry->get('XHTML 2.0');
// prevent XSS
$this->expectError('Doctype &lt;foo&gt; does not exist');
$registry->get('<foo>');
}
function testAliases() {
$registry = new HTMLPurifier_DoctypeRegistry();
$d1 =& $registry->register('Doc1', array(), array(), array('1'));
$this->assertReference($d1, $registry->get('Doc1'));
$this->assertReference($d1, $registry->get('1'));
$d2 =& $registry->register('Doc2', array(), array(), array('2'));
$this->assertReference($d2, $registry->get('Doc2'));
$this->assertReference($d2, $registry->get('2'));
$d3 =& $registry->register('1', array(), array(), array());
// literal name overrides alias
$this->assertReference($d3, $registry->get('1'));
$d4 =& $registry->register('One', array(), array(), array('1'));
$this->assertReference($d4, $registry->get('One'));
// still it overrides
$this->assertReference($d3, $registry->get('1'));
}
}
?>

View File

@ -55,6 +55,7 @@ $test_files[] = 'ChildDef/TableTest.php';
$test_files[] = 'ConfigSchemaTest.php';
$test_files[] = 'ConfigTest.php';
$test_files[] = 'ContextTest.php';
$test_files[] = 'DoctypeRegistryTest.php';
$test_files[] = 'ElementDefTest.php';
$test_files[] = 'EncoderTest.php';
$test_files[] = 'EntityLookupTest.php';