From f2df669eec2d507153fc95bef8816f5504d19464 Mon Sep 17 00:00:00 2001 From: "Edward Z. Yang" Date: Wed, 26 Sep 2007 23:36:37 +0000 Subject: [PATCH] Refactor IDAccumulator so that unit tests now work, and initialization is inside the class. git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1421 48356398-32a2-884e-a903-53898d9a118a --- library/HTMLPurifier.php | 3 +-- library/HTMLPurifier/AttrValidator.php | 7 ++++++ library/HTMLPurifier/IDAccumulator.php | 23 ++++++++++++++++--- .../Strategy/ValidateAttributes.php | 4 ---- tests/HTMLPurifier/IDAccumulatorTest.php | 6 +++++ tests/HTMLPurifierTest.php | 1 + 6 files changed, 35 insertions(+), 9 deletions(-) diff --git a/library/HTMLPurifier.php b/library/HTMLPurifier.php index d06af2a5..64544dcf 100644 --- a/library/HTMLPurifier.php +++ b/library/HTMLPurifier.php @@ -159,8 +159,7 @@ class HTMLPurifier // setup id_accumulator context, necessary due to the fact that // AttrValidator can be called from many places - $id_accumulator = new HTMLPurifier_IDAccumulator(); - $id_accumulator->load($config->get('Attr', 'IDBlacklist')); + $id_accumulator = HTMLPurifier_IDAccumulator::build($config, $context); $context->register('IDAccumulator', $id_accumulator); $html = HTMLPurifier_Encoder::convertToUTF8($html, $config, $context); diff --git a/library/HTMLPurifier/AttrValidator.php b/library/HTMLPurifier/AttrValidator.php index f02bd208..a471b093 100644 --- a/library/HTMLPurifier/AttrValidator.php +++ b/library/HTMLPurifier/AttrValidator.php @@ -23,6 +23,13 @@ class HTMLPurifier_AttrValidator $definition = $config->getHTMLDefinition(); $e =& $context->get('ErrorCollector', true); + // initialize IDAccumulator if necessary + $ok =& $context->get('IDAccumulator', true); + if (!$ok) { + $id_accumulator = HTMLPurifier_IDAccumulator::build($config, $context); + $context->register('IDAccumulator', $id_accumulator); + } + // initialize CurrentToken if necessary $current_token =& $context->get('CurrentToken', true); if (!$current_token) $context->register('CurrentToken', $token); diff --git a/library/HTMLPurifier/IDAccumulator.php b/library/HTMLPurifier/IDAccumulator.php index 525c9aa0..60715afc 100644 --- a/library/HTMLPurifier/IDAccumulator.php +++ b/library/HTMLPurifier/IDAccumulator.php @@ -1,11 +1,15 @@ load($config->get('Attr', 'IDBlacklist')); + return $id_accumulator; + } + /** * Add an ID to the lookup table. * @param $id ID to be added. diff --git a/library/HTMLPurifier/Strategy/ValidateAttributes.php b/library/HTMLPurifier/Strategy/ValidateAttributes.php index 87772406..6debcc33 100644 --- a/library/HTMLPurifier/Strategy/ValidateAttributes.php +++ b/library/HTMLPurifier/Strategy/ValidateAttributes.php @@ -6,10 +6,6 @@ require_once 'HTMLPurifier/IDAccumulator.php'; require_once 'HTMLPurifier/AttrValidator.php'; -HTMLPurifier_ConfigSchema::define( - 'Attr', 'IDBlacklist', array(), 'list', - 'Array of IDs not allowed in the document.'); - /** * Validate all attributes in the tokens. */ diff --git a/tests/HTMLPurifier/IDAccumulatorTest.php b/tests/HTMLPurifier/IDAccumulatorTest.php index 006d689c..c6249eca 100644 --- a/tests/HTMLPurifier/IDAccumulatorTest.php +++ b/tests/HTMLPurifier/IDAccumulatorTest.php @@ -30,5 +30,11 @@ class HTMLPurifier_IDAccumulatorTest extends HTMLPurifier_Harness } + function testBuild() { + $this->config->set('Attr', 'IDBlacklist', array('foo')); + $accumulator = HTMLPurifier_IDAccumulator::build($this->config, $this->context); + $this->assertTrue( isset($accumulator->ids['foo']) ); + } + } diff --git a/tests/HTMLPurifierTest.php b/tests/HTMLPurifierTest.php index 3ad307bb..6a221b24 100644 --- a/tests/HTMLPurifierTest.php +++ b/tests/HTMLPurifierTest.php @@ -94,6 +94,7 @@ class HTMLPurifierTest extends HTMLPurifier_Harness $this->purifier = new HTMLPurifier(array('HTML.EnableAttrID' => true)); $this->assertPurification('foobar'); + $this->assertPurification('Omigosh!'); }