diff --git a/NEWS b/NEWS index 61104e87..10794b77 100644 --- a/NEWS +++ b/NEWS @@ -14,7 +14,12 @@ NEWS ( CHANGELOG and HISTORY ) HTMLPurifier 1.3.2, unknown release date, may be dropped (security/bugfix/minor feature release) +! HTMLPurifier object now accepts configuration arrays, no need to manually + instantiate a configuration object +! Context object now accessible to outside - printDefinition.php: added labels, added better clarification +. HTMLPurifier_Config::create() added, takes mixed variable and converts into + a HTMLPurifier_Config object. 1.3.1, released 2006-12-06 ! Added HTMLPurifier.func.php stub for a convenient function to call the library diff --git a/library/HTMLPurifier.php b/library/HTMLPurifier.php index 8577e560..f335b2b3 100644 --- a/library/HTMLPurifier.php +++ b/library/HTMLPurifier.php @@ -68,15 +68,23 @@ class HTMLPurifier var $lexer, $strategy, $generator; + /** + * Final HTMLPurifier_Context of last run purification. Might be an array. + * @public + */ + var $context; + /** * Initializes the purifier. * @param $config Optional HTMLPurifier_Config object for all instances of * the purifier, if omitted, a default configuration is * supplied (which can be overridden on a per-use basis). + * The parameter can also be any type that + * HTMLPurifier_Config::create() supports. */ function HTMLPurifier($config = null) { - $this->config = $config ? $config : HTMLPurifier_Config::createDefault(); + $this->config = HTMLPurifier_Config::create($config); $this->lexer = HTMLPurifier_Lexer::create(); $this->strategy = new HTMLPurifier_Strategy_Core(); @@ -91,22 +99,35 @@ class HTMLPurifier * @param $html String of HTML to purify * @param $config HTMLPurifier_Config object for this operation, if omitted, * defaults to the config object specified during this - * object's construction. + * object's construction. The parameter can also be any type + * that HTMLPurifier_Config::create() supports. * @return Purified HTML */ function purify($html, $config = null) { - $config = $config ? $config : $this->config; + + $config = $config ? HTMLPurifier_Config::create($config) : $this->config; + + $context =& new HTMLPurifier_Context(); $html = $this->encoder->convertToUTF8($html, $config, $context); + + // purified HTML $html = $this->generator->generateFromTokens( + // list of tokens $this->strategy->execute( - $this->lexer->tokenizeHTML($html, $config, $context), + // list of un-purified tokens + $this->lexer->tokenizeHTML( + // un-purified HTML + $html, $config, $context + ), $config, $context ), $config, $context ); + $html = $this->encoder->convertFromUTF8($html, $config, $context); + $this->context =& $context; return $html; } diff --git a/library/HTMLPurifier/Config.php b/library/HTMLPurifier/Config.php index c6a5eba1..39f62855 100644 --- a/library/HTMLPurifier/Config.php +++ b/library/HTMLPurifier/Config.php @@ -44,6 +44,20 @@ class HTMLPurifier_Config $this->def = $definition; // keep a copy around for checking } + /** + * Convenience constructor that creates a config object based on a mixed var + * @param mixed $config Variable that defines the state of the config + * object. Can be: a HTMLPurifier_Config() object or + * an array of directives based on loadArray(). + * @return Configured HTMLPurifier_Config object + */ + function create($config) { + if (is_a($config, 'HTMLPurifier_Config')) return $config; + $ret = HTMLPurifier_Config::createDefault(); + if (is_array($config)) $ret->loadArray($config); + return $ret; + } + /** * Convenience constructor that creates a default configuration object. * @return Default HTMLPurifier_Config object. diff --git a/tests/HTMLPurifier/ConfigTest.php b/tests/HTMLPurifier/ConfigTest.php index 280d79d9..a7eb6115 100644 --- a/tests/HTMLPurifier/ConfigTest.php +++ b/tests/HTMLPurifier/ConfigTest.php @@ -180,6 +180,25 @@ class HTMLPurifier_ConfigTest extends UnitTestCase } + function test_create() { + + HTMLPurifier_ConfigSchema::defineNamespace('Cake', 'Properties of it.'); + HTMLPurifier_ConfigSchema::define('Cake', 'Sprinkles', 666, 'int', 'Number of.'); + HTMLPurifier_ConfigSchema::define('Cake', 'Flavor', 'vanilla', 'string', 'Flavor of the batter.'); + + $config = HTMLPurifier_Config::createDefault(); + $config->set('Cake', 'Sprinkles', 42); + + // test flat pass-through + $created_config = HTMLPurifier_Config::create($config); + $this->assertEqual($config, $created_config); + + // test loadArray + $created_config = HTMLPurifier_Config::create(array('Cake.Sprinkles' => 42)); + $this->assertEqual($config, $created_config); + + } + } ?> \ No newline at end of file diff --git a/tests/HTMLPurifier/Test.php b/tests/HTMLPurifier/Test.php index 13ed14ed..9760c6cb 100644 --- a/tests/HTMLPurifier/Test.php +++ b/tests/HTMLPurifier/Test.php @@ -25,7 +25,7 @@ class HTMLPurifier_Test extends UnitTestCase function testStrict() { $config = HTMLPurifier_Config::createDefault(); $config->set('HTML', 'Strict', true); - $this->purifier = new HTMLPurifier($config); + $this->purifier = new HTMLPurifier( $config ); // verbose syntax $this->assertPurification( 'Illegal underline', @@ -40,10 +40,11 @@ class HTMLPurifier_Test extends UnitTestCase } function testDifferentAllowedElements() { - $config = HTMLPurifier_Config::createDefault(); - $config->set('HTML', 'AllowedElements', array('b', 'i', 'p', 'a')); - $config->set('HTML', 'AllowedAttributes', array('a.href', '*.id')); - $this->purifier = new HTMLPurifier($config); + + $this->purifier = new HTMLPurifier(array( + 'HTML.AllowedElements' => array('b', 'i', 'p', 'a'), + 'HTML.AllowedAttributes' => array('a.href', '*.id') + )); $this->assertPurification( '

Par.

Paragraph

TextBold' @@ -58,9 +59,7 @@ class HTMLPurifier_Test extends UnitTestCase function testDisableURI() { - $config = HTMLPurifier_Config::createDefault(); - $config->set('Attr', 'DisableURI', true); - $this->purifier = new HTMLPurifier($config); + $this->purifier = new HTMLPurifier( array('Attr.DisableURI' => true) ); $this->assertPurification( '',