1
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2025-08-01 11:50:28 +02:00

[2.0.1] Implement error collection for RemoveForeignElements.

- Register Generator context variable.
- Implement special substitutions for error collector.
- Also sort by order the errors came in.
- Fix line number determination bug in Lexer::create().
- Remove vestigial variables.
- Force all tag transforms to use copy(), implement serialize, unserialize algorithm for copy() in tokens.

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1238 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang
2007-06-26 02:49:21 +00:00
parent 98b4e70a93
commit 7a8edc88f9
15 changed files with 203 additions and 42 deletions

View File

@@ -7,6 +7,7 @@ class HTMLPurifier_ErrorCollectorTest extends UnitTestCase
function setup() {
generate_mock_once('HTMLPurifier_Language');
generate_mock_once('HTMLPurifier_Generator');
}
function test() {
@@ -25,6 +26,9 @@ class HTMLPurifier_ErrorCollectorTest extends UnitTestCase
$context->register('Locale', $language);
$context->register('CurrentLine', $line);
$generator = new HTMLPurifier_GeneratorMock();
$context->register('Generator', $generator);
$collector = new HTMLPurifier_ErrorCollector($context);
$line = 23;
@@ -56,6 +60,9 @@ class HTMLPurifier_ErrorCollectorTest extends UnitTestCase
$context = new HTMLPurifier_Context();
$context->register('Locale', $language);
$generator = new HTMLPurifier_GeneratorMock();
$context->register('Generator', $generator);
$collector = new HTMLPurifier_ErrorCollector($context);
$formatted_result = '<p>No errors</p>';
$config = HTMLPurifier_Config::createDefault();
@@ -70,6 +77,9 @@ class HTMLPurifier_ErrorCollectorTest extends UnitTestCase
$context = new HTMLPurifier_Context();
$context->register('Locale', $language);
$generator = new HTMLPurifier_GeneratorMock();
$context->register('Generator', $generator);
$collector = new HTMLPurifier_ErrorCollector($context);
$collector->send(E_ERROR, 'message-1');
$collector->send(E_ERROR, 'message-2');
@@ -87,6 +97,42 @@ class HTMLPurifier_ErrorCollectorTest extends UnitTestCase
$this->assertIdentical($collector->getHTMLFormatted($config), $formatted_result);
}
function testContextSubstitutions() {
$language = new HTMLPurifier_LanguageMock();
$language->setReturnValue('getMessage',
'$CurrentToken.Name, $CurrentToken.Serialized', array('message-token'));
$language->setReturnValue('getMessage',
'$CurrentAttr.Name => $CurrentAttr.Value', array('message-attr'));
$context = new HTMLPurifier_Context();
$context->register('Locale', $language);
$current_token = new HTMLPurifier_Token_Start('a', array('href' => 'http://example.com'));
$current_token->line = 32;
$current_attr = 'href';
$generator = new HTMLPurifier_GeneratorMock();
$generator->setReturnValue('generateFromToken', '<a href="http://example.com">', array($current_token));
$context->register('Generator', $generator);
$collector = new HTMLPurifier_ErrorCollector($context);
$context->register('CurrentToken', $current_token);
$collector->send(E_NOTICE, 'message-token');
$collector->send(E_NOTICE, 'message-attr'); // test when context isn't available
$context->register('CurrentAttr', $current_attr);
$collector->send(E_NOTICE, 'message-attr');
$result = array(
0 => array(32, E_NOTICE, 'a, <a href="http://example.com">'),
1 => array(32, E_NOTICE, '$CurrentAttr.Name => $CurrentAttr.Value'),
2 => array(32, E_NOTICE, 'href => http://example.com')
);
$this->assertIdentical($collector->getRaw(), $result);
}
}
?>

View File

@@ -0,0 +1,27 @@
<?php
require_once 'HTMLPurifier/ErrorCollector.php';
require_once 'HTMLPurifier/Lexer/DirectLex.php';
class HTMLPurifier_ErrorsHarness extends UnitTestCase
{
var $config, $context;
var $collector, $generator;
function setup() {
$this->config = HTMLPurifier_Config::create(array('Core.CollectErrors' => true));
$this->context = new HTMLPurifier_Context();
generate_mock_once('HTMLPurifier_ErrorCollector');
$this->collector = new HTMLPurifier_ErrorCollectorMock($this);
$this->context->register('ErrorCollector', $this->collector);
}
function expectErrorCollection() {
$args = func_get_args();
$this->collector->expectOnce('send', $args);
}
}
?>

View File

@@ -1,21 +1,11 @@
<?php
require_once 'HTMLPurifier/ErrorsHarness.php';
require_once 'HTMLPurifier/Lexer/DirectLex.php';
class HTMLPurifier_Lexer_DirectLex_ErrorsTest extends UnitTestCase
class HTMLPurifier_Lexer_DirectLex_ErrorsTest extends HTMLPurifier_ErrorsHarness
{
var $config, $context;
var $collector;
function setup() {
$this->config = HTMLPurifier_Config::create(array('Core.CollectErrors' => true));
$this->context = new HTMLPurifier_Context();
generate_mock_once('HTMLPurifier_ErrorCollector');
$this->collector = new HTMLPurifier_ErrorCollectorMock($this);
$this->context->register('ErrorCollector', $this->collector);
}
function invoke($input) {
$lexer = new HTMLPurifier_Lexer_DirectLex();
$lexer->tokenizeHTML($input, $this->config, $this->context);
@@ -26,10 +16,6 @@ class HTMLPurifier_Lexer_DirectLex_ErrorsTest extends UnitTestCase
$lexer->parseAttributeString($input, $this->config, $this->context);
}
function expectErrorCollection($severity, $msg) {
$this->collector->expectOnce('send', array($severity, $msg));
}
function testUnclosedComment() {
$this->expectErrorCollection(E_WARNING, 'Lexer: Unclosed comment');
$this->invoke('<!-- >');

View File

@@ -0,0 +1,57 @@
<?php
require_once 'HTMLPurifier/ErrorsHarness.php';
require_once 'HTMLPurifier/Strategy/RemoveForeignElements.php';
class HTMLPurifier_Strategy_RemoveForeignElements_ErrorsTest extends HTMLPurifier_ErrorsHarness
{
function setup() {
parent::setup();
$this->config->set('HTML', 'TidyLevel', 'heavy');
}
function invoke($input) {
$strategy = new HTMLPurifier_Strategy_RemoveForeignElements();
$lexer = new HTMLPurifier_Lexer_DirectLex();
$tokens = $lexer->tokenizeHTML($input, $this->config, $this->context);
$strategy->execute($tokens, $this->config, $this->context);
}
function testTagTransform() {
// uses $CurrentToken.Serialized
$this->expectErrorCollection(E_NOTICE, 'Strategy_RemoveForeignElements: Tag transform', 'center');
$this->invoke('<center>');
}
function testMissingRequiredAttr() {
// a little fragile, since img has two required attributes
$this->expectErrorCollection(E_ERROR, 'Strategy_RemoveForeignElements: Missing required attribute', 'img', 'alt');
$this->invoke('<img />');
}
function testForeignElementToText() {
$this->config->set('Core', 'EscapeInvalidTags', true);
$this->expectErrorCollection(E_WARNING, 'Strategy_RemoveForeignElements: Foreign element to text', 'cannot-possibly-exist-element');
$this->invoke('<cannot-possibly-exist-element>');
}
function testForeignElementRemoved() {
$this->expectErrorCollection(E_ERROR, 'Strategy_RemoveForeignElements: Foreign element removed', 'cannot-possibly-exist-element');
$this->invoke('<cannot-possibly-exist-element>');
}
function testCommentRemoved() {
$this->expectErrorCollection(E_ERROR, 'Strategy_RemoveForeignElements: Comment removed', ' test ');
$this->invoke('<!-- test -->');
}
function testScriptRemoved() {
$this->collector->expectAt(0, 'send', array(E_ERROR, 'Strategy_RemoveForeignElements: Script removed'));
$this->collector->expectAt(1, 'send', array(E_ERROR, 'Strategy_RemoveForeignElements: Token removed to end', 'script'));
$this->invoke('<script>asdf');
}
}
?>