From 4f0a5c0e223cc2d8f75016792a79c1f04cc04f3c Mon Sep 17 00:00:00 2001 From: "Edward Z. Yang" Date: Fri, 1 Sep 2006 16:54:23 +0000 Subject: [PATCH] - Add Test namespace - Further fix the no iconv problem, and extend test cases to cover that case. git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@368 48356398-32a2-884e-a903-53898d9a118a --- library/HTMLPurifier/ConfigDef.php | 1 + library/HTMLPurifier/Encoder.php | 16 +++++++++++----- tests/HTMLPurifier/EncoderTest.php | 16 ++++++++++++++++ 3 files changed, 28 insertions(+), 5 deletions(-) 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 da191f85..7507f7c7 100644 --- a/library/HTMLPurifier/Encoder.php +++ b/library/HTMLPurifier/Encoder.php @@ -30,6 +30,12 @@ if ( !function_exists('iconv') ) { ); } +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. */ @@ -265,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); } } @@ -282,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/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" + ); + } }