0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2024-12-22 16:31:53 +00:00

[1.7.0] Wire in Language and ErrorCollector to main class, now, the only thing to do is actually implement the stuff

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1157 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang 2007-06-18 03:05:18 +00:00
parent 7699efd593
commit 0e5491b20c
8 changed files with 149 additions and 4 deletions

2
NEWS
View File

@ -54,6 +54,8 @@ NEWS ( CHANGELOG and HISTORY ) HTMLPurifier
PHP5 only version. PHP5 only version.
. HTMLDefinition and CSSDefinition have a common parent class: Definition. . HTMLDefinition and CSSDefinition have a common parent class: Definition.
. DirectLex can now track line-numbers . DirectLex can now track line-numbers
. Preliminary error collector is in place, although no code actually reports
errors yet
1.6.1, released 2007-05-05 1.6.1, released 2007-05-05
! Support for more deprecated attributes via transformations: ! Support for more deprecated attributes via transformations:

View File

@ -51,6 +51,23 @@ require_once 'HTMLPurifier/Generator.php';
require_once 'HTMLPurifier/Strategy/Core.php'; require_once 'HTMLPurifier/Strategy/Core.php';
require_once 'HTMLPurifier/Encoder.php'; require_once 'HTMLPurifier/Encoder.php';
require_once 'HTMLPurifier/LanguageFactory.php';
HTMLPurifier_ConfigSchema::define(
'Core', 'Language', 'en', 'string', '
ISO 639 language code for localizable things in HTML Purifier to use,
which is mainly error reporting. There is currently only an English (en)
translation, so this directive is currently useless.
This directive has been available since 1.7.0.
');
HTMLPurifier_ConfigSchema::define(
'Core', 'CollectErrors', false, 'bool', '
Whether or not to collect errors found while filtering the document. This
is a useful way to give feedback to your users. CURRENTLY NOT IMPLEMENTED.
This directive has been available since 1.7.0.
');
/** /**
* Main library execution class. * Main library execution class.
* *
@ -121,6 +138,18 @@ class HTMLPurifier
$lexer = HTMLPurifier_Lexer::create($config); $lexer = HTMLPurifier_Lexer::create($config);
$context = new HTMLPurifier_Context(); $context = new HTMLPurifier_Context();
// set up global context variables
if ($config->get('Core', 'CollectErrors')) {
// may get moved out if other facilities use it
$language_factory = HTMLPurifier_LanguageFactory::instance();
$language = $language_factory->create($config->get('Core', 'Language'));
$context->register('Locale', $language);
$error_collector = new HTMLPurifier_ErrorCollector();
$context->register('ErrorCollector', $language);
}
$html = HTMLPurifier_Encoder::convertToUTF8($html, $config, $context); $html = HTMLPurifier_Encoder::convertToUTF8($html, $config, $context);
for ($i = 0, $size = count($this->filters); $i < $size; $i++) { for ($i = 0, $size = count($this->filters); $i < $size; $i++) {

View File

@ -0,0 +1,73 @@
<?php
require_once 'HTMLPurifier/Generator.php';
/**
* Error collection class that enables HTML Purifier to report HTML
* problems back to the user
*/
class HTMLPurifier_ErrorCollector
{
var $errors = array();
/**
* Sends an error message to the collector for later use
* @param string Error message text
* @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);
}
/**
* Retrieves raw error data for custom formatter to use
* @param List of arrays in format of array(Error message text,
* token that caused error, tokens surrounding token)
*/
function getRaw() {
return $this->errors;
}
/**
* Default HTML formatting implementation for error messages
* @param $config Configuration array, vital for HTML output nature
*/
function getHTMLFormatted($config) {
$generator = new HTMLPurifier_Generator();
$context = new HTMLPurifier_Context();
$generator->generateFromTokens(array(), $config, $context); // initialize
$ret = array();
$errors = $this->errors;
// sort error array by line
if ($config->get('Core', 'MaintainLineNumbers')) {
$lines = array();
foreach ($errors as $error) $lines[] = $error[1]->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;
}
$string .= ' (<code>';
foreach ($error[2] as $token) {
if ($token !== true) {
$string .= $generator->escape($generator->generateFromToken($token));
} else {
$string .= '<strong>' . $generator->escape($generator->generateFromToken($error[1])) . '</strong>';
}
}
$string .= '</code>)';
$ret[] = $string;
}
return $ret;
}
}
?>

View File

@ -2,8 +2,6 @@
require_once 'HTMLPurifier/LanguageFactory.php'; require_once 'HTMLPurifier/LanguageFactory.php';
// UNUSED
class HTMLPurifier_Language class HTMLPurifier_Language
{ {

View File

@ -7,6 +7,8 @@ $messages = array(
'htmlpurifier' => 'HTML Purifier', 'htmlpurifier' => 'HTML Purifier',
'pizza' => 'Pizza', // for unit testing purposes 'pizza' => 'Pizza', // for unit testing purposes
); );
?> ?>

View File

@ -3,8 +3,6 @@
require_once 'HTMLPurifier/Language.php'; require_once 'HTMLPurifier/Language.php';
require_once 'HTMLPurifier/AttrDef/Lang.php'; require_once 'HTMLPurifier/AttrDef/Lang.php';
// UNUSED
/** /**
* Class responsible for generating HTMLPurifier_Language objects, managing * Class responsible for generating HTMLPurifier_Language objects, managing
* caching and fallbacks. * caching and fallbacks.

View File

@ -0,0 +1,42 @@
<?php
require_once 'HTMLPurifier/ErrorCollector.php';
class HTMLPurifier_ErrorCollectorTest extends UnitTestCase
{
function test() {
$tok1 = new HTMLPurifier_Token_Text('Token that caused error');
$tok1->line = 23;
$tok2 = new HTMLPurifier_Token_Start('a'); // also caused error
$tok2->line = 3;
$tok3 = new HTMLPurifier_Token_Text('Context before'); // before $tok2
$tok3->line = 3;
$tok4 = new HTMLPurifier_Token_Text('Context after'); // after $tok2
$tok4->line = 3;
$collector = new HTMLPurifier_ErrorCollector();
$collector->send('Big fat error', $tok1);
$collector->send('Another <error>', $tok2, array($tok3, true, $tok4));
$result = array(
0 => array('Big fat error', $tok1, array(true)),
1 => array('Another <error>', $tok2, array($tok3, true, $tok4))
);
$this->assertIdentical($collector->getRaw(), $result);
$formatted_result = array(
0 => 'Another &lt;error&gt; at line 3 (<code>Context before<strong>&lt;a&gt;</strong>Context after</code>)',
1 => 'Big fat error at line 23 (<code><strong>Token that caused error</strong></code>)'
);
$config = HTMLPurifier_Config::create(array('Core.MaintainLineNumbers' => true));
$this->assertIdentical($collector->getHTMLFormatted($config), $formatted_result);
}
}
?>

View File

@ -70,6 +70,7 @@ $test_files[] = 'HTMLPurifier/DefinitionCache/SerializerTest.php';
$test_files[] = 'HTMLPurifier/DefinitionTest.php'; $test_files[] = 'HTMLPurifier/DefinitionTest.php';
$test_files[] = 'HTMLPurifier/DoctypeRegistryTest.php'; $test_files[] = 'HTMLPurifier/DoctypeRegistryTest.php';
$test_files[] = 'HTMLPurifier/ElementDefTest.php'; $test_files[] = 'HTMLPurifier/ElementDefTest.php';
$test_files[] = 'HTMLPurifier/ErrorCollectorTest.php';
$test_files[] = 'HTMLPurifier/EncoderTest.php'; $test_files[] = 'HTMLPurifier/EncoderTest.php';
$test_files[] = 'HTMLPurifier/EntityLookupTest.php'; $test_files[] = 'HTMLPurifier/EntityLookupTest.php';
$test_files[] = 'HTMLPurifier/EntityParserTest.php'; $test_files[] = 'HTMLPurifier/EntityParserTest.php';