diff --git a/library/HTMLPurifier/Generator.php b/library/HTMLPurifier/Generator.php index e35ce8cf..4bb7ff26 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/LanguageFactory.php b/library/HTMLPurifier/LanguageFactory.php index ac820bb8..e7128224 100644 --- a/library/HTMLPurifier/LanguageFactory.php +++ b/library/HTMLPurifier/LanguageFactory.php @@ -5,6 +5,7 @@ * 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 { @@ -77,41 +78,44 @@ 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. */ - public function create($config, $context) { + public 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, false)) { - include_once $file; + if (file_exists($file) && class_exists($class)) { + $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'; + $depth++; + $lang = $this->create($config, $context, $fallback); + $depth--; } } - if (!class_exists($class, false)) { - // go fallback - $fallback = HTMLPurifier_LanguageFactory::getFallbackFor($code); - $depth++; - $lang = HTMLPurifier_LanguageFactory::factory( $fallback ); - $depth--; - } else { - $lang = new $class($config, $context); - } $lang->code = $code; return $lang; diff --git a/tests/HTMLPurifier/GeneratorTest.php b/tests/HTMLPurifier/GeneratorTest.php index e6a0645c..99c19a25 100644 --- a/tests/HTMLPurifier/GeneratorTest.php +++ b/tests/HTMLPurifier/GeneratorTest.php @@ -234,8 +234,7 @@ class HTMLPurifier_GeneratorTest extends HTMLPurifier_Harness // abort test if tidy isn't loaded if (!extension_loaded('tidy')) return; - $this->config = HTMLPurifier_Config::createDefault(); - $this->config->set('Core', 'TidyFormat', true); + $this->config->set('Output', 'TidyFormat', true); $this->config->set('Output', 'Newline', "\n"); // nice wrapping please @@ -243,9 +242,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'), ), - "