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

[1.7.0] Refactor HTMLDefinition and CSSDefinition to have a common Definition parent, rename setup() to doSetup() and make setup() call the template method after setting the setup variable. Test for references in ConfigTest.

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1091 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang 2007-05-24 21:50:43 +00:00
parent 7a3e06d4d0
commit a62f8971e4
5 changed files with 28 additions and 39 deletions

1
NEWS
View File

@ -32,6 +32,7 @@ NEWS ( CHANGELOG and HISTORY ) HTMLPurifier
of the operational document type of the operational document type
. Lexer is now pre-emptively included, with a conditional include for the . Lexer is now pre-emptively included, with a conditional include for the
PHP5 only version. PHP5 only version.
. HTMLDefinition and CSSDefinition have a common parent class: Definition.
1.6.1, released 2007-05-05 1.6.1, released 2007-05-05
! Support for more deprecated attributes via transformations: ! Support for more deprecated attributes via transformations:

View File

@ -1,5 +1,7 @@
<?php <?php
require_once 'HTMLPurifier/Definition.php';
require_once 'HTMLPurifier/AttrDef/CSS/Background.php'; require_once 'HTMLPurifier/AttrDef/CSS/Background.php';
require_once 'HTMLPurifier/AttrDef/CSS/BackgroundPosition.php'; require_once 'HTMLPurifier/AttrDef/CSS/BackgroundPosition.php';
require_once 'HTMLPurifier/AttrDef/CSS/Border.php'; require_once 'HTMLPurifier/AttrDef/CSS/Border.php';
@ -19,7 +21,7 @@ require_once 'HTMLPurifier/AttrDef/Enum.php';
* Defines allowed CSS attributes and what their values are. * Defines allowed CSS attributes and what their values are.
* @see HTMLPurifier_HTMLDefinition * @see HTMLPurifier_HTMLDefinition
*/ */
class HTMLPurifier_CSSDefinition class HTMLPurifier_CSSDefinition extends HTMLPurifier_Definition
{ {
/** /**
@ -30,7 +32,7 @@ class HTMLPurifier_CSSDefinition
/** /**
* Constructs the info array. The meat of this class. * Constructs the info array. The meat of this class.
*/ */
function setup($config) { function doSetup($config) {
$this->info['text-align'] = new HTMLPurifier_AttrDef_Enum( $this->info['text-align'] = new HTMLPurifier_AttrDef_Enum(
array('left', 'right', 'center', 'justify'), false); array('left', 'right', 'center', 'justify'), false);

View File

@ -200,11 +200,11 @@ class HTMLPurifier_Config
$this->html_definition = HTMLPurifier_HTMLDefinition::getCache($this); $this->html_definition = HTMLPurifier_HTMLDefinition::getCache($this);
if ($this->html_definition) return $this->html_definition; if ($this->html_definition) return $this->html_definition;
} }
$this->html_definition = new HTMLPurifier_HTMLDefinition($this); $this->html_definition = new HTMLPurifier_HTMLDefinition();
if ($raw) return $this->html_definition; // no setup! if ($raw) return $this->html_definition; // no setup!
} }
if (!$this->html_definition->setup) { if (!$this->html_definition->setup) {
$this->html_definition->setup(); $this->html_definition->setup($this);
$this->html_definition->saveCache($this); $this->html_definition->saveCache($this);
} }
return $this->html_definition; return $this->html_definition;

View File

@ -1,6 +1,6 @@
<?php <?php
// components require_once 'HTMLPurifier/Definition.php';
require_once 'HTMLPurifier/HTMLModuleManager.php'; require_once 'HTMLPurifier/HTMLModuleManager.php';
// this definition and its modules MUST NOT define configuration directives // this definition and its modules MUST NOT define configuration directives
@ -85,7 +85,7 @@ HTMLPurifier_ConfigSchema::define(
* HTMLPurifier_Printer_HTMLDefinition is a notable exception to this * HTMLPurifier_Printer_HTMLDefinition is a notable exception to this
* rule: in the interest of comprehensiveness, it will sniff everything. * rule: in the interest of comprehensiveness, it will sniff everything.
*/ */
class HTMLPurifier_HTMLDefinition class HTMLPurifier_HTMLDefinition extends HTMLPurifier_Definition
{ {
/** FULLY-PUBLIC VARIABLES */ /** FULLY-PUBLIC VARIABLES */
@ -155,17 +155,12 @@ class HTMLPurifier_HTMLDefinition
/** PUBLIC BUT INTERNAL VARIABLES */ /** PUBLIC BUT INTERNAL VARIABLES */
var $setup = false; /**< Has setup() been called yet? */
var $config; /**< Temporary instance of HTMLPurifier_Config */
var $manager; /**< Instance of HTMLPurifier_HTMLModuleManager */ var $manager; /**< Instance of HTMLPurifier_HTMLModuleManager */
/** /**
* Performs low-cost, preliminary initialization. * Performs low-cost, preliminary initialization.
* @param $config Instance of HTMLPurifier_Config
*/ */
function HTMLPurifier_HTMLDefinition($config) { function HTMLPurifier_HTMLDefinition() {
$this->config = $config;
$this->manager = new HTMLPurifier_HTMLModuleManager(); $this->manager = new HTMLPurifier_HTMLModuleManager();
} }
@ -207,31 +202,18 @@ class HTMLPurifier_HTMLDefinition
fclose($fh); fclose($fh);
} }
/** function doSetup($config) {
* Processes internals into form usable by HTMLPurifier internals. $this->processModules($config);
* Modifying the definition after calling this function should not $this->setupConfigStuff($config);
* be done.
*/
function setup() {
// multiple call guard
if ($this->setup) {return;} else {$this->setup = true;}
$this->processModules();
$this->setupConfigStuff();
// remove complicated variables to ease serialization
unset($this->config);
unset($this->manager); unset($this->manager);
} }
/** /**
* Extract out the information from the manager * Extract out the information from the manager
*/ */
function processModules() { function processModules($config) {
$this->manager->setup($this->config); $this->manager->setup($config);
$this->doctype = $this->manager->doctype; $this->doctype = $this->manager->doctype;
foreach ($this->manager->modules as $module) { foreach ($this->manager->modules as $module) {
@ -257,9 +239,9 @@ class HTMLPurifier_HTMLDefinition
/** /**
* Sets up stuff based on config. We need a better way of doing this. * Sets up stuff based on config. We need a better way of doing this.
*/ */
function setupConfigStuff() { function setupConfigStuff($config) {
$block_wrapper = $this->config->get('HTML', 'BlockWrapper'); $block_wrapper = $config->get('HTML', 'BlockWrapper');
if (isset($this->info_content_sets['Block'][$block_wrapper])) { if (isset($this->info_content_sets['Block'][$block_wrapper])) {
$this->info_block_wrapper = $block_wrapper; $this->info_block_wrapper = $block_wrapper;
} else { } else {
@ -267,7 +249,7 @@ class HTMLPurifier_HTMLDefinition
E_USER_ERROR); E_USER_ERROR);
} }
$parent = $this->config->get('HTML', 'Parent'); $parent = $config->get('HTML', 'Parent');
$def = $this->manager->getElement($parent, true); $def = $this->manager->getElement($parent, true);
if ($def) { if ($def) {
$this->info_parent = $parent; $this->info_parent = $parent;
@ -283,7 +265,7 @@ class HTMLPurifier_HTMLDefinition
"support forums) "; "support forums) ";
// setup allowed elements, SubtractiveWhitelist module(?) // setup allowed elements, SubtractiveWhitelist module(?)
$allowed_elements = $this->config->get('HTML', 'AllowedElements'); $allowed_elements = $config->get('HTML', 'AllowedElements');
if (is_array($allowed_elements)) { if (is_array($allowed_elements)) {
foreach ($this->info as $name => $d) { foreach ($this->info as $name => $d) {
if(!isset($allowed_elements[$name])) unset($this->info[$name]); if(!isset($allowed_elements[$name])) unset($this->info[$name]);
@ -296,7 +278,7 @@ class HTMLPurifier_HTMLDefinition
} }
} }
$allowed_attributes = $this->config->get('HTML', 'AllowedAttributes'); $allowed_attributes = $config->get('HTML', 'AllowedAttributes');
$allowed_attributes_mutable = $allowed_attributes; // by copy! $allowed_attributes_mutable = $allowed_attributes; // by copy!
if (is_array($allowed_attributes)) { if (is_array($allowed_attributes)) {
foreach ($this->info_global_attr as $attr_key => $info) { foreach ($this->info_global_attr as $attr_key => $info) {

View File

@ -228,19 +228,23 @@ class HTMLPurifier_ConfigTest extends UnitTestCase
$this->old_copy = HTMLPurifier_ConfigSchema::instance($this->old_copy); $this->old_copy = HTMLPurifier_ConfigSchema::instance($this->old_copy);
$config = HTMLPurifier_Config::createDefault(); $config = HTMLPurifier_Config::createDefault();
$config->set('HTML', 'Doctype', 'XHTML 1.0 Strict');
$config->autoFinalize = false; $config->autoFinalize = false;
$def = $config->getCSSDefinition(); $def = $config->getCSSDefinition();
$this->assertIsA($def, 'HTMLPurifier_CSSDefinition'); $this->assertIsA($def, 'HTMLPurifier_CSSDefinition');
$def = $config->getHTMLDefinition(); $def =& $config->getHTMLDefinition();
$def2 = $config->getHTMLDefinition(); $def2 =& $config->getHTMLDefinition();
$this->assertIsA($def, 'HTMLPurifier_HTMLDefinition'); $this->assertIsA($def, 'HTMLPurifier_HTMLDefinition');
$this->assertIdentical($def, $def2); $this->assertReference($def, $def2);
$this->assertTrue($def->setup); $this->assertTrue($def->setup);
// test re-calculation if HTML changes // test re-calculation if HTML changes
$config->set('HTML', 'Strict', true); unset($def, $def2);
$def2 = $config->getHTMLDefinition(); // forcibly de-reference
$config->set('HTML', 'Doctype', 'HTML 4.01 Transitional');
$def = $config->getHTMLDefinition(); $def = $config->getHTMLDefinition();
$this->assertIsA($def, 'HTMLPurifier_HTMLDefinition'); $this->assertIsA($def, 'HTMLPurifier_HTMLDefinition');
$this->assertNotEqual($def, $def2); $this->assertNotEqual($def, $def2);