diff --git a/library/HTMLPurifier/Doctype.php b/library/HTMLPurifier/Doctype.php index ebc91fae..d54d5fe1 100644 --- a/library/HTMLPurifier/Doctype.php +++ b/library/HTMLPurifier/Doctype.php @@ -18,24 +18,28 @@ 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->modules = $modules; - $this->modulesForModes = $modules_for_modes; - $this->aliases = $aliases; + $this->name = $name; + $this->xml = $xml; + $this->modules = $modules; + $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 ); } } diff --git a/library/HTMLPurifier/DoctypeRegistry.php b/library/HTMLPurifier/DoctypeRegistry.php index f60fc3d5..0697f89a 100644 --- a/library/HTMLPurifier/DoctypeRegistry.php +++ b/library/HTMLPurifier/DoctypeRegistry.php @@ -27,14 +27,15 @@ class HTMLPurifier_DoctypeRegistry * @param $aliases Alias names for doctype * @return Reference to registered doctype (usable for further editing) */ - function ®ister($doctype, $modules = array(), - $modules_for_modes = array(), $aliases = array() + function ®ister($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; } diff --git a/library/HTMLPurifier/HTMLModuleManager.php b/library/HTMLPurifier/HTMLModuleManager.php index 2820da06..1d127857 100644 --- a/library/HTMLPurifier/HTMLModuleManager.php +++ b/library/HTMLPurifier/HTMLModuleManager.php @@ -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->modules[$module] = $this->registeredModules[$module]; + $this->processModule($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 diff --git a/tests/HTMLPurifier/DoctypeRegistryTest.php b/tests/HTMLPurifier/DoctypeRegistryTest.php index b94b10e1..385859c9 100644 --- a/tests/HTMLPurifier/DoctypeRegistryTest.php +++ b/tests/HTMLPurifier/DoctypeRegistryTest.php @@ -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