From 577000100665a77e307fbb3e999f407563784615 Mon Sep 17 00:00:00 2001 From: "Edward Z. Yang" Date: Fri, 1 Sep 2006 16:58:29 +0000 Subject: [PATCH] Merged revisions 366:367 from trunk/ to branches/1.0 - Fixed rejection of case-insensitive configuration values when there is a set of allowed values. This manifested in %Core.Encoding. - Added Test namespace git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/branches/1.0@369 48356398-32a2-884e-a903-53898d9a118a --- NEWS | 2 ++ library/HTMLPurifier/Config.php | 4 ++-- library/HTMLPurifier/ConfigDef.php | 1 + library/HTMLPurifier/Encoder.php | 21 ++++++++++++++++----- tests/HTMLPurifier/ConfigTest.php | 11 +++++++++++ tests/HTMLPurifier/EncoderTest.php | 16 ++++++++++++++++ 6 files changed, 48 insertions(+), 7 deletions(-) diff --git a/NEWS b/NEWS index 7ae5b345..c8fbeed8 100644 --- a/NEWS +++ b/NEWS @@ -3,6 +3,8 @@ NEWS ( CHANGELOG and HISTORY ) HTMLPurifier 1.0.1, unknown release date - Fixed slight bug in DOMLex attribute parsing +- Fixed rejection of case-insensitive configuration values when there is a + set of allowed values. This manifested in %Core.Encoding. 1.0.0, released 2006-09-01 - Fixed broken numeric entity conversion diff --git a/library/HTMLPurifier/Config.php b/library/HTMLPurifier/Config.php index 040f0b3e..b7e0be04 100644 --- a/library/HTMLPurifier/Config.php +++ b/library/HTMLPurifier/Config.php @@ -80,6 +80,8 @@ class HTMLPurifier_Config E_USER_WARNING); return; } + $value = $this->def->validate($value, + $this->def->info[$namespace][$key]->type); if (is_string($value)) { // resolve value alias if defined if (isset($this->def->info[$namespace][$key]->aliases[$value])) { @@ -93,8 +95,6 @@ class HTMLPurifier_Config } } } - $value = $this->def->validate($value, - $this->def->info[$namespace][$key]->type); if ($value === null) { trigger_error('Value is of invalid type', E_USER_WARNING); return; diff --git a/library/HTMLPurifier/ConfigDef.php b/library/HTMLPurifier/ConfigDef.php index b19e97a5..fb47aef1 100644 --- a/library/HTMLPurifier/ConfigDef.php +++ b/library/HTMLPurifier/ConfigDef.php @@ -46,6 +46,7 @@ class HTMLPurifier_ConfigDef { $this->defineNamespace('URI', 'Features regarding Uniform Resource Identifiers.'); $this->defineNamespace('HTML', 'Configuration regarding allowed HTML.'); $this->defineNamespace('CSS', 'Configuration regarding allowed CSS.'); + $this->defineNamespace('Test', 'Testing configuration for our unit tests.'); } /** diff --git a/library/HTMLPurifier/Encoder.php b/library/HTMLPurifier/Encoder.php index 7ed0eda1..7507f7c7 100644 --- a/library/HTMLPurifier/Encoder.php +++ b/library/HTMLPurifier/Encoder.php @@ -23,8 +23,19 @@ if ( !function_exists('iconv') ) { 'iso-8859-1' ) ); + HTMLPurifier_ConfigDef::defineValueAliases( + 'Core', 'Encoding', array( + 'iso8859-1' => 'iso-8859-1' + ) + ); } +HTMLPurifier_ConfigDef::define( + 'Test', 'ForceNoIconv', false, 'bool', + 'When set to true, HTMLPurifier_Encoder will act as if iconv does not '. + 'exist and use only pure PHP implementations.' +); + /** * A UTF-8 specific character encoder that handles cleaning and transforming. */ @@ -260,9 +271,9 @@ class HTMLPurifier_Encoder if ($iconv === null) $iconv = function_exists('iconv'); $encoding = $config->get('Core', 'Encoding'); if ($encoding === 'utf-8') return $str; - if ($iconv) { + if ($iconv && !$config->get('Test', 'ForceNoIconv')) { return @iconv($encoding, 'utf-8//IGNORE', $str); - } elseif ($encoding === 'iso-8895-1') { + } elseif ($encoding === 'iso-8859-1') { return @utf8_encode($str); } } @@ -277,10 +288,10 @@ class HTMLPurifier_Encoder if ($iconv === null) $iconv = function_exists('iconv'); $encoding = $config->get('Core', 'Encoding'); if ($encoding === 'utf-8') return $str; - if ($iconv) { + if ($iconv && !$config->get('Test', 'ForceNoIconv')) { return @iconv('utf-8', $encoding . '//IGNORE', $str); - } elseif ($encoding === 'iso-8895-1') { - return @utf8_encode($str); + } elseif ($encoding === 'iso-8859-1') { + return @utf8_decode($str); } } diff --git a/tests/HTMLPurifier/ConfigTest.php b/tests/HTMLPurifier/ConfigTest.php index 7421a0c9..00ef4c01 100644 --- a/tests/HTMLPurifier/ConfigTest.php +++ b/tests/HTMLPurifier/ConfigTest.php @@ -32,6 +32,9 @@ class HTMLPurifier_ConfigTest extends UnitTestCase HTMLPurifier_ConfigDef::define( 'Extension', 'Pert', 'foo', 'string', 'A string directive.' ); + HTMLPurifier_ConfigDef::define( + 'Core', 'Encoding', 'utf-8', 'istring', 'Case insensitivity!' + ); HTMLPurifier_ConfigDef::defineAllowedValues( 'Extension', 'Pert', array('foo', 'moo') @@ -39,6 +42,9 @@ class HTMLPurifier_ConfigTest extends UnitTestCase HTMLPurifier_ConfigDef::defineValueAliases( 'Extension', 'Pert', array('cow' => 'moo') ); + HTMLPurifier_ConfigDef::defineAllowedValues( + 'Core', 'Encoding', array('utf-8', 'iso-8859-1') + ); $config = HTMLPurifier_Config::createDefault(); @@ -80,6 +86,11 @@ class HTMLPurifier_ConfigTest extends UnitTestCase $this->assertNoErrors(); $this->assertIdentical($config->get('Extension', 'Pert'), 'moo'); + // case-insensitive attempt to set value that is allowed + $config->set('Core', 'Encoding', 'ISO-8859-1'); + $this->assertNoErrors(); + $this->assertIdentical($config->get('Core', 'Encoding'), 'iso-8859-1'); + } } diff --git a/tests/HTMLPurifier/EncoderTest.php b/tests/HTMLPurifier/EncoderTest.php index 3d8a2af8..b0978fef 100644 --- a/tests/HTMLPurifier/EncoderTest.php +++ b/tests/HTMLPurifier/EncoderTest.php @@ -46,6 +46,14 @@ class HTMLPurifier_EncoderTest extends UnitTestCase $this->Encoder->convertToUTF8("\xF6", $config), "\xC3\xB6" ); + + $config->set('Test', 'ForceNoIconv', true); + + $this->assertIdentical( + $this->Encoder->convertToUTF8("\xF6", $config), + "\xC3\xB6" + ); + } function test_convertFromUTF8() { @@ -64,6 +72,14 @@ class HTMLPurifier_EncoderTest extends UnitTestCase $this->Encoder->convertFromUTF8("\xC3\xB6", $config), "\xF6" ); + + $config->set('Test', 'ForceNoIconv', true); + + $this->assertIdentical( + $this->Encoder->convertFromUTF8("\xC3\xB6", $config), + "\xF6" + ); + } }