From ad1169c711c9780d002f2f92107db2c11184d374 Mon Sep 17 00:00:00 2001 From: "Edward Z. Yang" Date: Thu, 18 Jan 2007 22:55:44 +0000 Subject: [PATCH] [1.4.0] Make all functions in Encoder static. Affects branches/strict git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@656 48356398-32a2-884e-a903-53898d9a118a --- library/HTMLPurifier.php | 5 ++--- library/HTMLPurifier/Encoder.php | 10 ++++++++++ library/HTMLPurifier/Lexer.php | 5 +---- tests/HTMLPurifier/EncoderTest.php | 17 ++++++++--------- 4 files changed, 21 insertions(+), 16 deletions(-) diff --git a/library/HTMLPurifier.php b/library/HTMLPurifier.php index c758b439..3bd32887 100644 --- a/library/HTMLPurifier.php +++ b/library/HTMLPurifier.php @@ -91,7 +91,6 @@ class HTMLPurifier $this->lexer = HTMLPurifier_Lexer::create(); $this->strategy = new HTMLPurifier_Strategy_Core(); $this->generator = new HTMLPurifier_Generator(); - $this->encoder = new HTMLPurifier_Encoder(); } @@ -110,7 +109,7 @@ class HTMLPurifier $config = $config ? HTMLPurifier_Config::create($config) : $this->config; $context = new HTMLPurifier_Context(); - $html = $this->encoder->convertToUTF8($html, $config, $context); + $html = HTMLPurifier_Encoder::convertToUTF8($html, $config, $context); // purified HTML $html = @@ -127,7 +126,7 @@ class HTMLPurifier $config, $context ); - $html = $this->encoder->convertFromUTF8($html, $config, $context); + $html = HTMLPurifier_Encoder::convertFromUTF8($html, $config, $context); $this->context =& $context; return $html; } diff --git a/library/HTMLPurifier/Encoder.php b/library/HTMLPurifier/Encoder.php index d9cc655c..b818e199 100644 --- a/library/HTMLPurifier/Encoder.php +++ b/library/HTMLPurifier/Encoder.php @@ -38,10 +38,18 @@ HTMLPurifier_ConfigSchema::define( /** * A UTF-8 specific character encoder that handles cleaning and transforming. + * @note All functions in this class should be static. */ class HTMLPurifier_Encoder { + /** + * Constructor throws fatal error if you attempt to instantiate class + */ + function HTMLPurifier_Encoder() { + trigger_error('Cannot instantiate encoder, call methods statically', E_USER_ERROR); + } + /** * Cleans a UTF-8 string for well-formedness and SGML validity * @@ -290,6 +298,7 @@ class HTMLPurifier_Encoder /** * Converts a string to UTF-8 based on configuration. + * @static */ function convertToUTF8($str, $config, &$context) { static $iconv = null; @@ -305,6 +314,7 @@ class HTMLPurifier_Encoder /** * Converts a string from UTF-8 based on configuration. + * @static * @note Currently, this is a lossy conversion, with unexpressable * characters being omitted. */ diff --git a/library/HTMLPurifier/Lexer.php b/library/HTMLPurifier/Lexer.php index 122bbb5d..29e92b66 100644 --- a/library/HTMLPurifier/Lexer.php +++ b/library/HTMLPurifier/Lexer.php @@ -56,7 +56,6 @@ class HTMLPurifier_Lexer { function HTMLPurifier_Lexer() { - $this->_encoder = new HTMLPurifier_Encoder(); $this->_entity_parser = new HTMLPurifier_EntityParser(); } @@ -114,8 +113,6 @@ class HTMLPurifier_Lexer return $string; } - var $_encoder; - /** * Lexes an HTML string into tokens. * @@ -216,7 +213,7 @@ class HTMLPurifier_Lexer // clean into wellformed UTF-8 string for an SGML context: this has // to be done after entity expansion because the entities sometimes // represent non-SGML characters (horror, horror!) - $html = $this->_encoder->cleanUTF8($html); + $html = HTMLPurifier_Encoder::cleanUTF8($html); return $html; } diff --git a/tests/HTMLPurifier/EncoderTest.php b/tests/HTMLPurifier/EncoderTest.php index 2e0eae8d..b8437fb2 100644 --- a/tests/HTMLPurifier/EncoderTest.php +++ b/tests/HTMLPurifier/EncoderTest.php @@ -8,14 +8,13 @@ class HTMLPurifier_EncoderTest extends UnitTestCase var $Encoder; function setUp() { - $this->Encoder = new HTMLPurifier_Encoder(); $this->_entity_lookup = HTMLPurifier_EntityLookup::instance(); } function assertCleanUTF8($string, $expect = null) { if ($expect === null) $expect = $string; - $this->assertIdentical($this->Encoder->cleanUTF8($string), $expect, 'iconv: %s'); - $this->assertIdentical($this->Encoder->cleanUTF8($string, true), $expect, 'PHP: %s'); + $this->assertIdentical(HTMLPurifier_Encoder::cleanUTF8($string), $expect, 'iconv: %s'); + $this->assertIdentical(HTMLPurifier_Encoder::cleanUTF8($string, true), $expect, 'PHP: %s'); } function test_cleanUTF8() { @@ -35,7 +34,7 @@ class HTMLPurifier_EncoderTest extends UnitTestCase // UTF-8 means that we don't touch it $this->assertIdentical( - $this->Encoder->convertToUTF8("\xF6", $config, $context), + HTMLPurifier_Encoder::convertToUTF8("\xF6", $config, $context), "\xF6" // this is invalid ); $this->assertNoErrors(); @@ -44,14 +43,14 @@ class HTMLPurifier_EncoderTest extends UnitTestCase // Now it gets converted $this->assertIdentical( - $this->Encoder->convertToUTF8("\xF6", $config, $context), + HTMLPurifier_Encoder::convertToUTF8("\xF6", $config, $context), "\xC3\xB6" ); $config->set('Test', 'ForceNoIconv', true); $this->assertIdentical( - $this->Encoder->convertToUTF8("\xF6", $config, $context), + HTMLPurifier_Encoder::convertToUTF8("\xF6", $config, $context), "\xC3\xB6" ); @@ -63,7 +62,7 @@ class HTMLPurifier_EncoderTest extends UnitTestCase // UTF-8 means that we don't touch it $this->assertIdentical( - $this->Encoder->convertFromUTF8("\xC3\xB6", $config, $context), + HTMLPurifier_Encoder::convertFromUTF8("\xC3\xB6", $config, $context), "\xC3\xB6" ); @@ -71,14 +70,14 @@ class HTMLPurifier_EncoderTest extends UnitTestCase // Now it gets converted $this->assertIdentical( - $this->Encoder->convertFromUTF8("\xC3\xB6", $config, $context), + HTMLPurifier_Encoder::convertFromUTF8("\xC3\xB6", $config, $context), "\xF6" ); $config->set('Test', 'ForceNoIconv', true); $this->assertIdentical( - $this->Encoder->convertFromUTF8("\xC3\xB6", $config, $context), + HTMLPurifier_Encoder::convertFromUTF8("\xC3\xB6", $config, $context), "\xF6" );