0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2024-11-09 15:28:40 +00:00
! HTMLPurifier object now accepts configuration arrays, no need to manually instantiate a configuration object
! Context object now accessible to outside
. HTMLPurifier_Config::create() added, takes mixed variable and converts into a HTMLPurifier_Config object.

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@611 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang 2006-12-15 02:12:03 +00:00
parent 41a25cb6b8
commit 360f984f63
5 changed files with 70 additions and 12 deletions

5
NEWS
View File

@ -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

View File

@ -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;
}

View File

@ -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.

View File

@ -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);
}
}
?>

View File

@ -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(
'<u>Illegal underline</u>',
@ -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(
'<p>Par.</p><p>Para<a href="http://google.com/">gr</a>aph</p>Text<b>Bol<i>d</i></b>'
@ -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(
'<img src="foobar"/>',