diff --git a/NEWS b/NEWS index 39bdadcd..b314dac0 100644 --- a/NEWS +++ b/NEWS @@ -49,6 +49,7 @@ NEWS ( CHANGELOG and HISTORY ) HTMLPurifier of DOM), HTML Purifier punts to DirectLex - Fatal error with unserialization of ScriptRequired - Created directories are now chmod'ed properly +- Fixed bug with fallback languages in LanguageFactory . Out-of-date documentation revised . UTF-8 encoding check optimization as suggested by Diego . HTMLPurifier_Error removed in favor of exceptions diff --git a/library/HTMLPurifier/Generator.php b/library/HTMLPurifier/Generator.php index 4bb7ff26..e35ce8cf 100644 --- a/library/HTMLPurifier/Generator.php +++ b/library/HTMLPurifier/Generator.php @@ -77,7 +77,7 @@ class HTMLPurifier_Generator 'wrap' => 68, ), 'utf8'); $tidy->cleanRepair(); - $html = ((string) $tidy); // explicit cast necessary + $html = (string) $tidy; // explicit cast necessary } // Normalize newlines to system defined value diff --git a/library/HTMLPurifier/Language.php b/library/HTMLPurifier/Language.php index d42075f3..7337c5bc 100644 --- a/library/HTMLPurifier/Language.php +++ b/library/HTMLPurifier/Language.php @@ -27,6 +27,13 @@ class HTMLPurifier_Language */ public $errorNames = array(); + /** + * True if no message file was found for this language, so English + * is being used instead. Check this if you'd like to notify the + * user that they've used a non-supported language. + */ + public $error = false; + /** * Has the language object been loaded yet? * @todo Make it private, fix usage in HTMLPurifier_LanguageTest diff --git a/library/HTMLPurifier/Language/messages/en-x-testmini.php b/library/HTMLPurifier/Language/messages/en-x-testmini.php new file mode 100644 index 00000000..4b16cd20 --- /dev/null +++ b/library/HTMLPurifier/Language/messages/en-x-testmini.php @@ -0,0 +1,11 @@ + 'HTML Purifier XNone' +); + diff --git a/library/HTMLPurifier/LanguageFactory.php b/library/HTMLPurifier/LanguageFactory.php index e7128224..24ec7544 100644 --- a/library/HTMLPurifier/LanguageFactory.php +++ b/library/HTMLPurifier/LanguageFactory.php @@ -104,14 +104,13 @@ class HTMLPurifier_LanguageFactory $lang = new $class($config, $context); } else { // Go fallback - $fallback = $this->getFallbackFor($code); - // If a language doesn't exist, English's language settings - // file will automatically be used, which DOESN'T have any - // fallback defined. This is fine for settings merging, but - // not so good for figuring out a class to use. - if ($fallback === false) $fallback = 'en'; + $raw_fallback = $this->getFallbackFor($code); + $fallback = $raw_fallback ? $raw_fallback : 'en'; $depth++; $lang = $this->create($config, $context, $fallback); + if (!$raw_fallback) { + $lang->error = true; + } $depth--; } } diff --git a/tests/HTMLPurifier/GeneratorTest.php b/tests/HTMLPurifier/GeneratorTest.php index 99c19a25..e6a0645c 100644 --- a/tests/HTMLPurifier/GeneratorTest.php +++ b/tests/HTMLPurifier/GeneratorTest.php @@ -234,7 +234,8 @@ class HTMLPurifier_GeneratorTest extends HTMLPurifier_Harness // abort test if tidy isn't loaded if (!extension_loaded('tidy')) return; - $this->config->set('Output', 'TidyFormat', true); + $this->config = HTMLPurifier_Config::createDefault(); + $this->config->set('Core', 'TidyFormat', true); $this->config->set('Output', 'Newline', "\n"); // nice wrapping please @@ -242,9 +243,9 @@ class HTMLPurifier_GeneratorTest extends HTMLPurifier_Harness array( new HTMLPurifier_Token_Start('div'), new HTMLPurifier_Token_Text('Text'), - new HTMLPurifier_Token_End('div'), + new HTMLPurifier_Token_End('div') ), - "
\n Text\n
\n " + "
\n Text\n
\n" ); } diff --git a/tests/HTMLPurifier/LanguageFactoryTest.php b/tests/HTMLPurifier/LanguageFactoryTest.php index 5caa5177..0d699dfe 100644 --- a/tests/HTMLPurifier/LanguageFactoryTest.php +++ b/tests/HTMLPurifier/LanguageFactoryTest.php @@ -3,13 +3,20 @@ class HTMLPurifier_LanguageFactoryTest extends HTMLPurifier_Harness { + /** + * Protected reference of global factory we're testing. + */ + protected $factory; + + public function setUp() { + $this->factory = HTMLPurifier_LanguageFactory::instance(); + parent::setUp(); + } + function test() { - $factory = HTMLPurifier_LanguageFactory::instance(); - - $config = HTMLPurifier_Config::create(array('Core.Language' => 'en')); - $context = new HTMLPurifier_Context(); - $language = $factory->create($config, $context); + $this->config->set('Core', 'Language', 'en'); + $language = $this->factory->create($this->config, $this->context); $this->assertIsA($language, 'HTMLPurifier_Language'); $this->assertIdentical($language->code, 'en'); @@ -19,18 +26,12 @@ class HTMLPurifier_LanguageFactoryTest extends HTMLPurifier_Harness $language->load(); $this->assertNotEqual(count($language->messages), 0); - // actual tests for content can be found in LanguageTest - } function testFallback() { - $factory = HTMLPurifier_LanguageFactory::instance(); - - $config = HTMLPurifier_Config::create(array('Core.Language' => 'en-x-test')); - $context = new HTMLPurifier_Context(); - - $language = $factory->create($config, $context); + $this->config->set('Core', 'Language', 'en-x-test'); + $language = $this->factory->create($this->config, $this->context); $this->assertIsA($language, 'HTMLPurifier_Language_en_x_test'); $this->assertIdentical($language->code, 'en-x-test'); @@ -45,10 +46,23 @@ class HTMLPurifier_LanguageFactoryTest extends HTMLPurifier_Harness } - function testFullFallback() { - $factory = HTMLPurifier_LanguageFactory::instance(); - $this->config->set('Core', 'Language', 'en-x-none'); - $language = $factory->create($this->config, $this->context); + function testFallbackWithNoClass() { + $this->config->set('Core', 'Language', 'en-x-testmini'); + $language = $this->factory->create($this->config, $this->context); + $this->assertIsA($language, 'HTMLPurifier_Language'); + $this->assertIdentical($language->code, 'en-x-testmini'); + $language->load(); + $this->assertIdentical($language->getMessage('HTMLPurifier'), 'HTML Purifier XNone'); + $this->assertIdentical($language->getMessage('LanguageFactoryTest: Pizza'), 'Pizza'); + $this->assertIdentical($language->error, false); + } + + function testNoSuchLanguage() { + $this->config->set('Core', 'Language', 'en-x-testnone'); + $language = $this->factory->create($this->config, $this->context); + $this->assertIsA($language, 'HTMLPurifier_Language'); + $this->assertIdentical($language->code, 'en-x-testnone'); + $this->assertIdentical($language->error, true); } }