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

[1.7.0] Eliminated modes in favor for special-case "Tidy" modules

- Add $xml property to Doctype, make more serialize friendly in preparation for stuffing into Config object
- Add FIXME markers for areas of further development, code is hooked so this is easy
- Document what the new Tidy classes will be

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1065 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang 2007-05-16 03:00:18 +00:00
parent 2d035483dd
commit a5136b65e4
4 changed files with 70 additions and 52 deletions

View File

@ -18,23 +18,27 @@ class HTMLPurifier_Doctype
var $modules = array();
/**
* Associative array of mode names to lists of modules; these are
* the modules added into the standard list if a particular mode
* is enabled, such as lenient or correctional.
* List of modules to use for tidying up code
*/
var $modulesForModes = array();
var $tidyModules = array();
/**
* Is the language derived from XML (i.e. XHTML)?
*/
var $xml = true;
/**
* List of aliases for this doctype
*/
var $aliases = array();
function HTMLPurifier_Doctype($name = null, $modules = array(),
$modules_for_modes = array(), $aliases = array()
function HTMLPurifier_Doctype($name = null, $xml = true, $modules = array(),
$tidyModules = array(), $aliases = array()
) {
$this->name = $name;
$this->xml = $xml;
$this->modules = $modules;
$this->modulesForModes = $modules_for_modes;
$this->tidyModules = $tidyModules;
$this->aliases = $aliases;
}
@ -43,7 +47,7 @@ class HTMLPurifier_Doctype
*/
function copy() {
return new HTMLPurifier_Doctype(
$this->name, $this->modules, $this->modulesForModes, $this->aliases
$this->name, $this->xml, $this->modules, $this->tidyModules, $this->aliases
);
}
}

View File

@ -27,14 +27,15 @@ class HTMLPurifier_DoctypeRegistry
* @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()
function &register($doctype, $xml = true, $modules = array(),
$tidy_modules = array(), $aliases = array()
) {
if (!is_array($modules)) $modules = array($modules);
if (!is_array($tidy_modules)) $tidy_modules = array($tidy_modules);
if (!is_array($aliases)) $aliases = array($aliases);
if (!is_object($doctype)) {
$doctype = new HTMLPurifier_Doctype(
$doctype, $modules, $modules_for_modes, $aliases
$doctype, $xml, $modules, $tidy_modules, $aliases
);
}
$this->doctypes[$doctype->name] =& $doctype;
@ -68,15 +69,16 @@ class HTMLPurifier_DoctypeRegistry
/**
* Creates a doctype based on a configuration object,
* will perform initialization on the doctype
* @note Use this function to get a copy of doctype that config
* can hold on to (this is necessary in order to tell
* Generator whether or not the current document is XML
* based or not).
*/
function make($config) {
$original_doctype = $this->get($this->getDoctypeFromConfig($config));
$doctype = $original_doctype->copy();
// initialization goes here
foreach ($doctype->modulesForModes as $mode => $mode_modules) {
// TODO: test if $mode is active
$doctype->modules = array_merge($doctype->modules, $mode_modules);
}
// FIXME!!! Set $doctype into $config so others can use it,
// you might need to use two copies
return $doctype;
}

View File

@ -115,33 +115,38 @@ class HTMLPurifier_HTMLModuleManager
$transitional = array('Legacy', 'Target');
$this->doctypes->register(
'HTML 4.01 Transitional',
'HTML 4.01 Transitional', false,
array_merge($common, $transitional),
array('correctional' => array('TransformToStrict'))
array('TransformToStrict')
// Tidy: Transitional
);
$this->doctypes->register(
'XHTML 1.0 Transitional',
'HTML 4.01 Strict', false,
array_merge($common),
array('TransformToStrict')
// Tidy: Strict
);
$this->doctypes->register(
'XHTML 1.0 Transitional', true,
array_merge($common, $transitional),
array('correctional' => array('TransformToStrict'))
array('TransformToStrict')
// Tidy: Transitional, XHTML
);
$this->doctypes->register(
'HTML 4.01 Strict',
'XHTML 1.0 Strict', true,
array_merge($common),
array('lenient' => array('TransformToStrict'))
array('TransformToStrict')
// Tidy: Strict, XHTML
);
$this->doctypes->register(
'XHTML 1.0 Strict',
'XHTML 1.1', true,
array_merge($common),
array('lenient' => array('TransformToStrict'))
);
$this->doctypes->register(
'XHTML 1.1',
array_merge($common),
array('lenient' => array('TransformToStrict', 'TransformToXHTML11'))
array('TransformToStrict', 'TransformToXHTML11')
// Tidy: Strict, XHTML1_1
);
}
@ -247,15 +252,12 @@ class HTMLPurifier_HTMLModuleManager
$modules = array_merge($modules, $this->userModules);
foreach ($modules as $module) {
if (is_object($module)) {
$this->registeredModules[$module->name] = $module;
continue;
} else {
if (!isset($this->registeredModules[$module])) {
$this->registerModule($module);
$this->processModule($module);
}
}
$this->modules[$module] = $this->registeredModules[$module];
foreach ($doctype->tidyModules as $module) {
$this->processModule($module);
// FIXME!!! initialize the tidy modules here
}
// setup lookup table based on all valid modules
@ -284,6 +286,17 @@ class HTMLPurifier_HTMLModuleManager
}
/**
* Takes a module and adds it to the active module collection,
* registering it if necessary.
*/
function processModule($module) {
if (!isset($this->registeredModules[$module]) || is_object($module)) {
$this->registerModule($module);
}
$this->modules[$module] = $this->registeredModules[$module];
}
/**
* Retrieves merged element definitions.
* @return Array of HTMLPurifier_ElementDef

View File

@ -11,23 +11,22 @@ class HTMLPurifier_DoctypeRegistryTest extends UnitTestCase
$d =& $registry->register(
$name = 'XHTML 1.0 Transitional',
$xml = true,
$modules = array('module-one', 'module-two'),
$modulesForModes = array(
'lenient' => array('lenient-module'),
),
$tidyModules = array('lenient-module'),
$aliases = array('X10T')
);
$d2 = new HTMLPurifier_Doctype($name, $modules, $modulesForModes, $aliases);
$d2 = new HTMLPurifier_Doctype($name, $xml, $modules, $tidyModules, $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'
$name = 'XHTML 1.0 Strict', true, 'module', 'Tidy', 'X10S'
);
$d2 = new HTMLPurifier_Doctype($name, array('module'), array(), array('X10S'));
$d2 = new HTMLPurifier_Doctype($name, true, array('module'), array('Tidy'), array('X10S'));
$this->assertIdentical($d, $d2);
@ -52,22 +51,22 @@ class HTMLPurifier_DoctypeRegistryTest extends UnitTestCase
$registry = new HTMLPurifier_DoctypeRegistry();
$d1 =& $registry->register('Doc1', array(), array(), array('1'));
$d1 =& $registry->register('Doc1', true, array(), array(), array('1'));
$this->assertReference($d1, $registry->get('Doc1'));
$this->assertReference($d1, $registry->get('1'));
$d2 =& $registry->register('Doc2', array(), array(), array('2'));
$d2 =& $registry->register('Doc2', true, array(), array(), array('2'));
$this->assertReference($d2, $registry->get('Doc2'));
$this->assertReference($d2, $registry->get('2'));
$d3 =& $registry->register('1', array(), array(), array());
$d3 =& $registry->register('1', true, array(), array(), array());
// literal name overrides alias
$this->assertReference($d3, $registry->get('1'));
$d4 =& $registry->register('One', array(), array(), array('1'));
$d4 =& $registry->register('One', true, array(), array(), array('1'));
$this->assertReference($d4, $registry->get('One'));
// still it overrides