mirror of
https://github.com/ezyang/htmlpurifier.git
synced 2025-08-02 12:21:09 +02:00
[2.1.4] [MFH] Fixed bug with fallback languages in LanguageFactory
git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/branches/php4@1724 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
1
NEWS
1
NEWS
@@ -32,6 +32,7 @@ ERRATA
|
||||
- Iconv uses set_error_handler instead of shut-up operator
|
||||
- Add protection against imagecrash attack with CSS height/width
|
||||
- HTMLPurifier::getInstance() renamed to HTMLPurifier::instance() for consistency
|
||||
- Fixed bug with fallback languages in LanguageFactory
|
||||
|
||||
2.1.3, released 2007-11-05
|
||||
! tests/multitest.php allows you to test multiple versions by running
|
||||
|
@@ -25,6 +25,13 @@ class HTMLPurifier_Language
|
||||
*/
|
||||
var $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.
|
||||
*/
|
||||
var $error = false;
|
||||
|
||||
/**
|
||||
* Has the language object been loaded yet?
|
||||
* @private
|
||||
|
11
library/HTMLPurifier/Language/messages/en-x-testmini.php
Normal file
11
library/HTMLPurifier/Language/messages/en-x-testmini.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
// private language message file for unit testing purposes
|
||||
// this language file has no class associated with it
|
||||
|
||||
$fallback = 'en';
|
||||
|
||||
$messages = array(
|
||||
'HTMLPurifier' => 'HTML Purifier XNone'
|
||||
);
|
||||
|
@@ -16,6 +16,7 @@ This directive has been available since 2.0.0.
|
||||
* caching and fallbacks.
|
||||
* @note Thanks to MediaWiki for the general logic, although this version
|
||||
* has been entirely rewritten
|
||||
* @todo Serialized cache for languages
|
||||
*/
|
||||
class HTMLPurifier_LanguageFactory
|
||||
{
|
||||
@@ -89,40 +90,42 @@ class HTMLPurifier_LanguageFactory
|
||||
* Creates a language object, handles class fallbacks
|
||||
* @param $config Instance of HTMLPurifier_Config
|
||||
* @param $context Instance of HTMLPurifier_Context
|
||||
* @param $code Code to override configuration with. Private parameter.
|
||||
*/
|
||||
function create($config, &$context) {
|
||||
function create($config, &$context, $code = false) {
|
||||
|
||||
// validate language code
|
||||
$code = $this->validator->validate(
|
||||
$config->get('Core', 'Language'), $config, $context
|
||||
);
|
||||
if ($code === false) {
|
||||
$code = $this->validator->validate(
|
||||
$config->get('Core', 'Language'), $config, $context
|
||||
);
|
||||
} else {
|
||||
$code = $this->validator->validate($code, $config, $context);
|
||||
}
|
||||
if ($code === false) $code = 'en'; // malformed code becomes English
|
||||
|
||||
$pcode = str_replace('-', '_', $code); // make valid PHP classname
|
||||
static $depth = 0; // recursion protection
|
||||
|
||||
if ($code == 'en') {
|
||||
$class = 'HTMLPurifier_Language';
|
||||
$file = $this->dir . '/Language.php';
|
||||
$lang = new HTMLPurifier_Language($config, $context);
|
||||
} else {
|
||||
$class = 'HTMLPurifier_Language_' . $pcode;
|
||||
$file = $this->dir . '/Language/classes/' . $code . '.php';
|
||||
// PHP5/APC deps bug workaround can go here
|
||||
// you can bypass the conditional include by loading the
|
||||
// file yourself
|
||||
if (file_exists($file) && !class_exists($class)) {
|
||||
include_once $file;
|
||||
}
|
||||
}
|
||||
|
||||
if (!class_exists($class)) {
|
||||
// go fallback
|
||||
$fallback = HTMLPurifier_LanguageFactory::getFallbackFor($code);
|
||||
$depth++;
|
||||
$lang = HTMLPurifier_LanguageFactory::factory( $fallback );
|
||||
$depth--;
|
||||
} else {
|
||||
$lang = new $class($config, $context);
|
||||
if (file_exists($file)) {
|
||||
include $file;
|
||||
$lang = new $class($config, $context);
|
||||
} else {
|
||||
// Go fallback
|
||||
$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--;
|
||||
}
|
||||
}
|
||||
$lang->code = $code;
|
||||
|
||||
|
@@ -5,13 +5,20 @@ require_once 'HTMLPurifier/LanguageFactory.php';
|
||||
class HTMLPurifier_LanguageFactoryTest extends HTMLPurifier_Harness
|
||||
{
|
||||
|
||||
/**
|
||||
* Protected reference of global factory we're testing.
|
||||
*/
|
||||
var $factory;
|
||||
|
||||
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');
|
||||
@@ -21,18 +28,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');
|
||||
@@ -47,5 +48,24 @@ class HTMLPurifier_LanguageFactoryTest extends HTMLPurifier_Harness
|
||||
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user