0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2024-12-23 00:41:52 +00:00

[1.7.0] Wire in DoctypeRegistry to HTMLModuleManager, convert doctype declarations, migrate some related functionality to proper class

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1060 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang 2007-05-15 00:31:53 +00:00
parent 6b9c5ec603
commit 65252d6fbd
3 changed files with 89 additions and 73 deletions

View File

@ -25,7 +25,7 @@ class HTMLPurifier_Doctype
var $modulesForModes = array(); var $modulesForModes = array();
/** /**
* List of aliases to doctype name * List of aliases for this doctype
*/ */
var $aliases = array(); var $aliases = array();
@ -37,6 +37,15 @@ class HTMLPurifier_Doctype
$this->modulesForModes = $modules_for_modes; $this->modulesForModes = $modules_for_modes;
$this->aliases = $aliases; $this->aliases = $aliases;
} }
/**
* Clones the doctype, use before resolving modes and the like
*/
function copy() {
return new HTMLPurifier_Doctype(
$this->name, $this->modules, $this->modulesForModes, $this->aliases
);
}
} }
?> ?>

View File

@ -52,6 +52,7 @@ class HTMLPurifier_DoctypeRegistry
/** /**
* Retrieves reference to a doctype of a certain name * Retrieves reference to a doctype of a certain name
* @note This function resolves aliases * @note This function resolves aliases
* @note When possible, use the more fully-featured make()
* @param $doctype Name of doctype * @param $doctype Name of doctype
* @return Reference to doctype object * @return Reference to doctype object
*/ */
@ -64,6 +65,44 @@ class HTMLPurifier_DoctypeRegistry
return $this->doctypes[$doctype]; return $this->doctypes[$doctype];
} }
/**
* Creates a doctype based on a configuration object,
* will perform initialization on the doctype
*/
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);
}
return $doctype;
}
/**
* Retrieves the doctype from the configuration object
*/
function getDoctypeFromConfig($config) {
// recommended test
$doctype = $config->get('HTML', 'Doctype');
if ($doctype !== null) {
return $doctype;
}
// backwards-compatibility
if ($config->get('Core', 'XHTML')) {
$doctype = 'XHTML 1.0';
} else {
$doctype = 'HTML 4.01';
}
if ($config->get('HTML', 'Strict')) {
$doctype .= ' Strict';
} else {
$doctype .= ' Transitional';
}
return $doctype;
}
} }
?> ?>

View File

@ -3,6 +3,7 @@
require_once 'HTMLPurifier/HTMLModule.php'; require_once 'HTMLPurifier/HTMLModule.php';
require_once 'HTMLPurifier/ElementDef.php'; require_once 'HTMLPurifier/ElementDef.php';
require_once 'HTMLPurifier/Doctype.php'; require_once 'HTMLPurifier/Doctype.php';
require_once 'HTMLPurifier/DoctypeRegistry.php';
require_once 'HTMLPurifier/ContentSets.php'; require_once 'HTMLPurifier/ContentSets.php';
require_once 'HTMLPurifier/AttrTypes.php'; require_once 'HTMLPurifier/AttrTypes.php';
@ -44,12 +45,16 @@ class HTMLPurifier_HTMLModuleManager
{ {
/** /**
* Associative array of doctype names to doctype definitions. * Instance of HTMLPurifier_DoctypeRegistry
* @note This may be replaced by a DoctypeManager * @public
*/ */
var $doctypes; var $doctypes;
var $doctype; /**< String doctype name to determine modules to load */
var $doctypeAliases = array(); /**< Lookup array of strings to real doctypes */ /**
* Instance of HTMLPurifier_AttrTypes
* @public
*/
var $attrTypes;
/** /**
* Active instances of modules for the specified doctype are * Active instances of modules for the specified doctype are
@ -74,7 +79,6 @@ class HTMLPurifier_HTMLModuleManager
var $prefixes = array('HTMLPurifier_HTMLModule_'); var $prefixes = array('HTMLPurifier_HTMLModule_');
var $contentSets; /**< Instance of HTMLPurifier_ContentSets */ var $contentSets; /**< Instance of HTMLPurifier_ContentSets */
var $attrTypes; /**< Instance of HTMLPurifier_AttrTypes */
var $attrCollections; /**< Instance of HTMLPurifier_AttrCollections */ var $attrCollections; /**< Instance of HTMLPurifier_AttrCollections */
/** If set to true, unsafe elements and attributes will be allowed */ /** If set to true, unsafe elements and attributes will be allowed */
@ -82,13 +86,13 @@ class HTMLPurifier_HTMLModuleManager
function HTMLPurifier_HTMLModuleManager() { function HTMLPurifier_HTMLModuleManager() {
// the only editable internal object. The rest need to // editable internal objects
// be manipulated through modules
$this->attrTypes = new HTMLPurifier_AttrTypes(); $this->attrTypes = new HTMLPurifier_AttrTypes();
$this->doctypes = new HTMLPurifier_DoctypeRegistry();
// these doctype definitions should be placed somewhere else, // setup default HTML doctypes
// and instead, a DoctypeManager instantiated during construction
// module reuse
$common = array( $common = array(
'CommonAttributes', 'Text', 'Hypertext', 'List', 'CommonAttributes', 'Text', 'Hypertext', 'List',
'Presentation', 'Edit', 'Bdo', 'Tables', 'Image', 'Presentation', 'Edit', 'Bdo', 'Tables', 'Image',
@ -96,40 +100,38 @@ class HTMLPurifier_HTMLModuleManager
); );
$transitional = array('Legacy', 'Target'); $transitional = array('Legacy', 'Target');
$d =& $this->addDoctype('HTML 4.01 Transitional'); $this->doctypes->register(
$d->modules = array_merge($common, $transitional); 'HTML 4.01 Transitional',
$d->modulesForMode['correctional'] = array('TransformToStrict'); array_merge($common, $transitional),
array('correctional' => array('TransformToStrict'))
);
$d =& $this->addDoctype('XHTML 1.0 Transitional'); $this->doctypes->register(
$d->modules = array_merge($common, $transitional); 'XHTML 1.0 Transitional',
$d->modulesForMode['correctional'] = array('TransformToStrict'); array_merge($common, $transitional),
array('correctional' => array('TransformToStrict'))
);
$d =& $this->addDoctype('HTML 4.01 Strict'); $this->doctypes->register(
$d->modules = array_merge($common); 'HTML 4.01 Strict',
$d->modulesForMode['lenient'] = array('TransformToStrict'); array_merge($common),
array('lenient' => array('TransformToStrict'))
);
$d =& $this->addDoctype('XHTML 1.0 Strict'); $this->doctypes->register(
$d->modules = array_merge($common); 'XHTML 1.0 Strict',
$d->modulesForMode['lenient'] = array('TransformToStrict'); array_merge($common),
array('lenient' => array('TransformToStrict'))
);
$d =& $this->addDoctype('XHTML 1.1'); $this->doctypes->register(
$d->modules = array_merge($common); 'XHTML 1.1',
$d->modulesForMode['lenient'] = array('TransformToStrict', 'TransformToXHTML11'); array_merge($common),
array('lenient' => array('TransformToStrict', 'TransformToXHTML11'))
);
} }
/**
* Temporary function that creates a new doctype and returns a
* reference to it.
* @note Real version should retrieve a fully formed instance of
* the doctype and register its aliases
*/
function &addDoctype($name) {
$this->doctypes[$name] = new HTMLPurifier_Doctype();
$this->doctypes[$name]->name = $name;
return $this->doctypes[$name];
}
/** /**
* Registers a module to the recognized module list, useful for * Registers a module to the recognized module list, useful for
* overloading pre-existing modules. * overloading pre-existing modules.
@ -219,20 +221,9 @@ class HTMLPurifier_HTMLModuleManager
*/ */
function setup($config) { function setup($config) {
// retrieve the doctype // generate
$this->doctype = $this->getDoctype($config); $doctype = $this->doctypes->make($config);
if (isset($this->doctypeAliases[$this->doctype])) {
// resolve alias
$this->doctype = $this->doctypeAliases[$this->doctype];
}
// retrieve object instance of doctype
$doctype = $this->doctypes[$this->doctype];
$modules = $doctype->modules; $modules = $doctype->modules;
foreach ($doctype->modulesForMode as $mode => $mode_modules) {
// TODO: test if $mode is active
$modules = array_merge($modules, $mode_modules);
}
foreach ($modules as $module) { foreach ($modules as $module) {
if (is_object($module)) { if (is_object($module)) {
@ -272,29 +263,6 @@ class HTMLPurifier_HTMLModuleManager
} }
/**
* Retrieves the doctype from the configuration object
*/
function getDoctype($config) {
// simplistic test
$doctype = $config->get('HTML', 'Doctype');
if ($doctype !== null) {
return $doctype;
}
// this is backwards-compatibility stuff
if ($config->get('Core', 'XHTML')) {
$doctype = 'XHTML 1.0';
} else {
$doctype = 'HTML 4.01';
}
if ($config->get('HTML', 'Strict')) {
$doctype .= ' Strict';
} else {
$doctype .= ' Transitional';
}
return $doctype;
}
/** /**
* Retrieves merged element definitions. * Retrieves merged element definitions.
* @param $config Instance of HTMLPurifier_Config, for determining * @param $config Instance of HTMLPurifier_Config, for determining