0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2025-01-08 23:11:52 +00:00

[2.0.1] Rather than pass line number by parameter, have it be retrieved via Context. Add $ignore_error boolean to get().

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1228 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang 2007-06-25 01:08:57 +00:00
parent 8ae2604440
commit 728088f2ba
5 changed files with 59 additions and 37 deletions

View File

@ -147,7 +147,7 @@ class HTMLPurifier
$language = $language_factory->create($config->get('Core', 'Language')); $language = $language_factory->create($config->get('Core', 'Language'));
$context->register('Locale', $language); $context->register('Locale', $language);
$error_collector = new HTMLPurifier_ErrorCollector($language); $error_collector = new HTMLPurifier_ErrorCollector($context);
$context->register('ErrorCollector', $error_collector); $context->register('ErrorCollector', $error_collector);
} }

View File

@ -31,11 +31,14 @@ class HTMLPurifier_Context
/** /**
* Retrieves a variable reference from the context. * Retrieves a variable reference from the context.
* @param $name String name * @param $name String name
* @param $ignore_error Boolean whether or not to ignore error
*/ */
function &get($name) { function &get($name, $ignore_error = false) {
if (!isset($this->_storage[$name])) { if (!isset($this->_storage[$name])) {
if (!$ignore_error) {
trigger_error("Attempted to retrieve non-existent variable $name", trigger_error("Attempted to retrieve non-existent variable $name",
E_USER_ERROR); E_USER_ERROR);
}
$var = null; // so we can return by reference $var = null; // so we can return by reference
return $var; return $var;
} }

View File

@ -11,8 +11,12 @@ class HTMLPurifier_ErrorCollector
var $errors = array(); var $errors = array();
var $locale; var $locale;
var $context;
function HTMLPurifier_ErrorCollector(&$locale) {$this->locale =& $locale;} function HTMLPurifier_ErrorCollector(&$context) {
$this->locale =& $context->get('Locale');
$this->context =& $context;
}
/** /**
* Sends an error message to the collector for later use * Sends an error message to the collector for later use
@ -20,20 +24,23 @@ class HTMLPurifier_ErrorCollector
* @param $severity int Error severity, PHP error style (don't use E_USER_) * @param $severity int Error severity, PHP error style (don't use E_USER_)
* @param $msg string Error message text * @param $msg string Error message text
*/ */
function send($line, $severity, $msg) { function send($severity, $msg, $args = array()) {
$token = false; if (func_num_args() == 2) {
if ($line && !is_int($line)) { $msg = $this->locale->getMessage($msg);
$token = $line; } else {
$line = $token->line; // setup one-based array if necessary
if (!$line) $line = false; if (!is_array($args)) {
}
if (func_num_args() == 3) $msg = $this->locale->getMessage($msg);
else {
$args = func_get_args(); $args = func_get_args();
array_splice($args, 0, 2); // one less to make 1-based array_shift($args);
unset($args[0]); unset($args[0]);
}
$msg = $this->locale->formatMessage($msg, $args); $msg = $this->locale->formatMessage($msg, $args);
} }
$line = $this->context->get('CurrentLine', true);
$token = $this->context->get('CurrentToken', true);
$attr = $this->context->get('CurrentAttr', true);
$this->errors[] = array($line, $severity, $msg); $this->errors[] = array($line, $severity, $msg);
} }

View File

@ -138,7 +138,7 @@ class HTMLPurifier_Lexer_DirectLex extends HTMLPurifier_Lexer
// uh oh, we have a comment that extends to // uh oh, we have a comment that extends to
// infinity. Can't be helped: set comment // infinity. Can't be helped: set comment
// end position to end of string // end position to end of string
if ($e) $e->send($current_line, E_WARNING, 'Lexer: Unclosed comment'); if ($e) $e->send(E_WARNING, 'Lexer: Unclosed comment');
$position_comment_end = strlen($html); $position_comment_end = strlen($html);
$end = true; $end = true;
} else { } else {
@ -181,7 +181,7 @@ class HTMLPurifier_Lexer_DirectLex extends HTMLPurifier_Lexer
// have accidently grabbed an emoticon. Translate into // have accidently grabbed an emoticon. Translate into
// text and go our merry way // text and go our merry way
if (!ctype_alnum($segment[0])) { if (!ctype_alnum($segment[0])) {
if ($e) $e->send($current_line, E_NOTICE, 'Lexer: Unescaped lt'); if ($e) $e->send(E_NOTICE, 'Lexer: Unescaped lt');
$token = new $token = new
HTMLPurifier_Token_Text( HTMLPurifier_Token_Text(
'<' . '<' .
@ -261,7 +261,7 @@ class HTMLPurifier_Lexer_DirectLex extends HTMLPurifier_Lexer
continue; continue;
} else { } else {
// inside tag, but there's no ending > sign // inside tag, but there's no ending > sign
if ($e) $e->send($current_line, E_WARNING, 'Lexer: Missing gt'); if ($e) $e->send(E_WARNING, 'Lexer: Missing gt');
$token = new $token = new
HTMLPurifier_Token_Text( HTMLPurifier_Token_Text(
'<' . '<' .
@ -327,7 +327,7 @@ class HTMLPurifier_Lexer_DirectLex extends HTMLPurifier_Lexer
list($key, $quoted_value) = explode('=', $string); list($key, $quoted_value) = explode('=', $string);
$quoted_value = trim($quoted_value); $quoted_value = trim($quoted_value);
if (!$key) { if (!$key) {
if ($e) $e->send($current_line, E_ERROR, 'Lexer: Missing attribute key'); if ($e) $e->send(E_ERROR, 'Lexer: Missing attribute key');
return array(); return array();
} }
if (!$quoted_value) return array($key => ''); if (!$quoted_value) return array($key => '');
@ -343,7 +343,7 @@ class HTMLPurifier_Lexer_DirectLex extends HTMLPurifier_Lexer
} else { } else {
// not well behaved // not well behaved
if ($open_quote) { if ($open_quote) {
if ($e) $e->send($current_line, E_ERROR, 'Lexer: Missing end quote'); if ($e) $e->send(E_ERROR, 'Lexer: Missing end quote');
$value = substr($quoted_value, 1); $value = substr($quoted_value, 1);
} else { } else {
$value = $quoted_value; $value = $quoted_value;
@ -390,7 +390,7 @@ class HTMLPurifier_Lexer_DirectLex extends HTMLPurifier_Lexer
$key = substr($string, $key_begin, $key_end - $key_begin); $key = substr($string, $key_begin, $key_end - $key_begin);
if (!$key) { if (!$key) {
if ($e) $e->send($current_line, E_ERROR, 'Lexer: Missing attribute key'); if ($e) $e->send(E_ERROR, 'Lexer: Missing attribute key');
$cursor += strcspn($string, $this->_whitespace, $cursor + 1); // prevent infinite loop $cursor += strcspn($string, $this->_whitespace, $cursor + 1); // prevent infinite loop
continue; // empty key continue; // empty key
} }
@ -440,7 +440,7 @@ class HTMLPurifier_Lexer_DirectLex extends HTMLPurifier_Lexer
$array[$key] = $key; $array[$key] = $key;
} else { } else {
// purely theoretical // purely theoretical
if ($e) $e->send($current_line, E_ERROR, 'Lexer: Missing attribute key'); if ($e) $e->send(E_ERROR, 'Lexer: Missing attribute key');
} }
} }

View File

@ -11,9 +11,6 @@ class HTMLPurifier_ErrorCollectorTest extends UnitTestCase
function test() { function test() {
$tok = new HTMLPurifier_Token_Start('a'); // also caused error
$tok->line = 3;
$language = new HTMLPurifier_LanguageMock(); $language = new HTMLPurifier_LanguageMock();
$language->setReturnValue('getErrorName', 'Error', array(E_ERROR)); $language->setReturnValue('getErrorName', 'Error', array(E_ERROR));
$language->setReturnValue('getErrorName', 'Warning', array(E_WARNING)); $language->setReturnValue('getErrorName', 'Warning', array(E_WARNING));
@ -22,9 +19,19 @@ class HTMLPurifier_ErrorCollectorTest extends UnitTestCase
$language->setReturnValue('formatMessage', ' at line 23', array('ErrorCollector: At line', array('line' => 23))); $language->setReturnValue('formatMessage', ' at line 23', array('ErrorCollector: At line', array('line' => 23)));
$language->setReturnValue('formatMessage', ' at line 3', array('ErrorCollector: At line', array('line' => 3))); $language->setReturnValue('formatMessage', ' at line 3', array('ErrorCollector: At line', array('line' => 3)));
$collector = new HTMLPurifier_ErrorCollector($language); $line = false;
$collector->send(23, E_ERROR, 'message-1');
$collector->send($tok, E_WARNING, 'message-2', 'param'); $context = new HTMLPurifier_Context();
$context->register('Locale', $language);
$context->register('CurrentLine', $line);
$collector = new HTMLPurifier_ErrorCollector($context);
$line = 23;
$collector->send(E_ERROR, 'message-1');
$line = 3;
$collector->send(E_WARNING, 'message-2', 'param');
$result = array( $result = array(
0 => array(23, E_ERROR, 'Message 1'), 0 => array(23, E_ERROR, 'Message 1'),
@ -46,25 +53,30 @@ class HTMLPurifier_ErrorCollectorTest extends UnitTestCase
function testNoErrors() { function testNoErrors() {
$language = new HTMLPurifier_LanguageMock(); $language = new HTMLPurifier_LanguageMock();
$language->setReturnValue('getMessage', 'No errors', array('ErrorCollector: No errors')); $language->setReturnValue('getMessage', 'No errors', array('ErrorCollector: No errors'));
$collector = new HTMLPurifier_ErrorCollector($language); $context = new HTMLPurifier_Context();
$context->register('Locale', $language);
$collector = new HTMLPurifier_ErrorCollector($context);
$formatted_result = '<p>No errors</p>'; $formatted_result = '<p>No errors</p>';
$config = HTMLPurifier_Config::createDefault(); $config = HTMLPurifier_Config::createDefault();
$this->assertIdentical($collector->getHTMLFormatted($config), $formatted_result); $this->assertIdentical($collector->getHTMLFormatted($config), $formatted_result);
} }
function testNoLineNumbers() { function testNoLineNumbers() {
$token = new HTMLPurifier_Token_Start('a'); // no line number!
$language = new HTMLPurifier_LanguageMock(); $language = new HTMLPurifier_LanguageMock();
$language->setReturnValue('getMessage', 'Message 1', array('message-1')); $language->setReturnValue('getMessage', 'Message 1', array('message-1'));
$language->setReturnValue('getMessage', 'Message 2', array('message-2')); $language->setReturnValue('getMessage', 'Message 2', array('message-2'));
$language->setReturnValue('getErrorName', 'Error', array(E_ERROR)); $language->setReturnValue('getErrorName', 'Error', array(E_ERROR));
$collector = new HTMLPurifier_ErrorCollector($language); $context = new HTMLPurifier_Context();
$collector->send(false, E_ERROR, 'message-1'); $context->register('Locale', $language);
$collector->send($token, E_ERROR, 'message-2');
$collector = new HTMLPurifier_ErrorCollector($context);
$collector->send(E_ERROR, 'message-1');
$collector->send(E_ERROR, 'message-2');
$result = array( $result = array(
0 => array(false, E_ERROR, 'Message 1'), 0 => array(null, E_ERROR, 'Message 1'),
1 => array(false, E_ERROR, 'Message 2') 1 => array(null, E_ERROR, 'Message 2')
); );
$this->assertIdentical($collector->getRaw(), $result); $this->assertIdentical($collector->getRaw(), $result);