From 7b087c7bbedaa9c1064e6f683b517a65b858cdf4 Mon Sep 17 00:00:00 2001 From: "Edward Z. Yang" Date: Sun, 24 Jun 2007 23:20:35 +0000 Subject: [PATCH] [2.0.1] Add severity to error collector git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1226 48356398-32a2-884e-a903-53898d9a118a --- NEWS | 2 ++ TODO | 2 +- library/HTMLPurifier/ErrorCollector.php | 28 +++++++++++-------- library/HTMLPurifier/Language.php | 17 +++++++++++ library/HTMLPurifier/Language/messages/en.php | 6 ++++ library/HTMLPurifier/LanguageFactory.php | 2 +- tests/HTMLPurifier/ErrorCollectorTest.php | 23 ++++++++++----- 7 files changed, 59 insertions(+), 21 deletions(-) diff --git a/NEWS b/NEWS index 3f9636ff..ffa8f52e 100644 --- a/NEWS +++ b/NEWS @@ -34,6 +34,8 @@ NEWS ( CHANGELOG and HISTORY ) HTMLPurifier . Interlinking in configuration documentation added using Injector_PurifierLinkify . Directives now keep track of aliases to themselves +. Error collector now requires a severity to be passed, use PHP's internal + error constants for this 2.0.0, released 2007-06-20 # Completely refactored HTMLModuleManager, decentralizing safety diff --git a/TODO b/TODO index aff5e526..293c1d1c 100644 --- a/TODO +++ b/TODO @@ -45,7 +45,7 @@ TODO List same fashion!) # Formatters for plaintext - Smileys - - Linkification for HTML Purifier docs: notably configuration and classes + - Linkification for HTML Purifier docs: classes - Standardize token armor for all areas of processing - Fixes for Firefox's inability to handle COL alignment props (Bug 915) - Automatically add non-breaking spaces to empty table cells when diff --git a/library/HTMLPurifier/ErrorCollector.php b/library/HTMLPurifier/ErrorCollector.php index db0c091e..ddad3d8d 100644 --- a/library/HTMLPurifier/ErrorCollector.php +++ b/library/HTMLPurifier/ErrorCollector.php @@ -14,11 +14,12 @@ class HTMLPurifier_ErrorCollector /** * Sends an error message to the collector for later use * @param string Error message text + * @param int Error severity, PHP error style (don't use E_USER_) * @param HTMLPurifier_Token Token that caused error * @param array Tokens surrounding the offending token above, use true as placeholder */ - function send($msg, $token, $context_tokens = array(true)) { - $this->errors[] = array($msg, $token, $context_tokens); + function send($msg, $severity, $token, $context_tokens = array(true)) { + $this->errors[] = array($msg, $severity, $token, $context_tokens); } /** @@ -34,32 +35,35 @@ class HTMLPurifier_ErrorCollector * Default HTML formatting implementation for error messages * @param $config Configuration array, vital for HTML output nature */ - function getHTMLFormatted($config) { + function getHTMLFormatted($config, &$context) { $generator = new HTMLPurifier_Generator(); - $context = new HTMLPurifier_Context(); $generator->generateFromTokens(array(), $config, $context); // initialize $ret = array(); $errors = $this->errors; + $locale = $context->get('Locale'); // sort error array by line if ($config->get('Core', 'MaintainLineNumbers')) { $lines = array(); - foreach ($errors as $error) $lines[] = $error[1]->line; + foreach ($errors as $error) $lines[] = $error[2]->line; array_multisort($lines, SORT_ASC, $errors); } foreach ($errors as $error) { - $string = $generator->escape($error[0]); // message - if (!empty($error[1]->line)) { - $string .= ' at line ' . $error[1]->line; + list($msg, $severity, $token, $context_tokens) = $error; + $string = ''; + $string .= $locale->getErrorName($severity) . ': '; + $string .= $generator->escape($msg); + if (!empty($token->line)) { + $string .= ' at line ' . $token->line; } $string .= ' ('; - foreach ($error[2] as $token) { - if ($token !== true) { - $string .= $generator->escape($generator->generateFromToken($token)); + foreach ($context_tokens as $context_token) { + if ($context_token !== true) { + $string .= $generator->escape($generator->generateFromToken($context_token)); } else { - $string .= '' . $generator->escape($generator->generateFromToken($error[1])) . ''; + $string .= '' . $generator->escape($generator->generateFromToken($token)) . ''; } } $string .= ')'; diff --git a/library/HTMLPurifier/Language.php b/library/HTMLPurifier/Language.php index 6b0845fb..51051c97 100644 --- a/library/HTMLPurifier/Language.php +++ b/library/HTMLPurifier/Language.php @@ -20,6 +20,11 @@ class HTMLPurifier_Language */ var $messages = array(); + /** + * Array of localizable error codes + */ + var $errorNames = array(); + /** * Has the language object been loaded yet? * @private @@ -51,6 +56,18 @@ class HTMLPurifier_Language return $this->messages[$key]; } + /** + * Retrieves a localised error name. + * @param $int integer error number, corresponding to PHP's error + * reporting + * @return string localised message + */ + function getErrorName($int) { + if (!$this->_loaded) $this->load(); + if (!isset($this->errorNames[$int])) return "[Error: $int]"; + return $this->errorNames[$int]; + } + /** * Formats a localised message with passed parameters * @param $key string identifier of message diff --git a/library/HTMLPurifier/Language/messages/en.php b/library/HTMLPurifier/Language/messages/en.php index 3327460c..d62a30cb 100644 --- a/library/HTMLPurifier/Language/messages/en.php +++ b/library/HTMLPurifier/Language/messages/en.php @@ -9,6 +9,12 @@ $messages = array( +); + +$errorNames = array( + E_ERROR => 'Error', + E_WARNING => 'Warning', + E_NOTICE => 'Notice' ); ?> \ No newline at end of file diff --git a/library/HTMLPurifier/LanguageFactory.php b/library/HTMLPurifier/LanguageFactory.php index bf79efea..c8891ffd 100644 --- a/library/HTMLPurifier/LanguageFactory.php +++ b/library/HTMLPurifier/LanguageFactory.php @@ -43,7 +43,7 @@ class HTMLPurifier_LanguageFactory * Keys whose contents are a hash map and can be merged * @value array lookup */ - var $mergeable_keys_map = array('messages' => true); + var $mergeable_keys_map = array('messages' => true, 'errorNames' => true); /** * Keys whose contents are a list and can be merged diff --git a/tests/HTMLPurifier/ErrorCollectorTest.php b/tests/HTMLPurifier/ErrorCollectorTest.php index 31ac3052..f25579be 100644 --- a/tests/HTMLPurifier/ErrorCollectorTest.php +++ b/tests/HTMLPurifier/ErrorCollectorTest.php @@ -17,23 +17,32 @@ class HTMLPurifier_ErrorCollectorTest extends UnitTestCase $tok4->line = 3; $collector = new HTMLPurifier_ErrorCollector(); - $collector->send('Big fat error', $tok1); - $collector->send('Another ', $tok2, array($tok3, true, $tok4)); + $collector->send('Big fat error', E_ERROR, $tok1); + $collector->send('Another ', E_WARNING, $tok2, array($tok3, true, $tok4)); $result = array( - 0 => array('Big fat error', $tok1, array(true)), - 1 => array('Another ', $tok2, array($tok3, true, $tok4)) + 0 => array('Big fat error', E_ERROR, $tok1, array(true)), + 1 => array('Another ', E_WARNING, $tok2, array($tok3, true, $tok4)) ); $this->assertIdentical($collector->getRaw(), $result); $formatted_result = array( - 0 => 'Another <error> at line 3 (Context before<a>Context after)', - 1 => 'Big fat error at line 23 (Token that caused error)' + 0 => 'Warning: Another <warning> at line 3 (Context before<a>Context after)', + 1 => 'Error: Big fat error at line 23 (Token that caused error)' ); $config = HTMLPurifier_Config::create(array('Core.MaintainLineNumbers' => true)); - $this->assertIdentical($collector->getHTMLFormatted($config), $formatted_result); + + $context = new HTMLPurifier_Context(); + + generate_mock_once('HTMLPurifier_Language'); + $language = new HTMLPurifier_LanguageMock(); + $language->setReturnValue('getErrorName', 'Error', array(E_ERROR)); + $language->setReturnValue('getErrorName', 'Warning', array(E_WARNING)); + $context->register('Locale', $language); + + $this->assertIdentical($collector->getHTMLFormatted($config, $context), $formatted_result); }