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

Make context errors more friendly; factor out disabled; fix broken test cases; update TODO.

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1220 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang 2007-06-24 04:22:28 +00:00
parent 269268b843
commit 75e52a12a6
5 changed files with 51 additions and 37 deletions

13
TODO
View File

@ -43,14 +43,10 @@ TODO List
AttrDef class) AttrDef class)
# More control over allowed CSS properties (maybe modularize it in the # More control over allowed CSS properties (maybe modularize it in the
same fashion!) same fashion!)
# Formatters for plaintext (COMPLEX) # Formatters for plaintext
- Auto-paragraphing (be sure to leverage fact that we know when things
shouldn't be paragraphed, such as lists and tables).
- Linkify URLs
- Smileys - Smileys
- Linkification for HTML Purifier docs: notably configuration and classes - Linkification for HTML Purifier docs: notably configuration and classes
- Allow tags to be "armored", an internal flag that protects them - Standardize token armor for all areas of armor
from validation and passes them out unharmed
- Fixes for Firefox's inability to handle COL alignment props (Bug 915) - Fixes for Firefox's inability to handle COL alignment props (Bug 915)
- Automatically add non-breaking spaces to empty table cells when - Automatically add non-breaking spaces to empty table cells when
empty-cells:show is applied to have compatibility with Internet Explorer empty-cells:show is applied to have compatibility with Internet Explorer
@ -68,6 +64,8 @@ Ongoing
- Lots of profiling, make it faster! - Lots of profiling, make it faster!
- Plugins for major CMSes (COMPLEX) - Plugins for major CMSes (COMPLEX)
- WordPress (mostly written, needs beta-testing) - WordPress (mostly written, needs beta-testing)
- phpBB
- Phorum
- eFiction - eFiction
- more! (look for ones that use WYSIWYGs) - more! (look for ones that use WYSIWYGs)
- Complete basic smoketests - Complete basic smoketests
@ -76,7 +74,8 @@ Unknown release (on a scratch-an-itch basis)
? Semi-lossy dumb alternate character encoding transfor ? Semi-lossy dumb alternate character encoding transfor
? Have 'lang' attribute be checked against official lists, achieved by ? Have 'lang' attribute be checked against official lists, achieved by
encoding all characters that have string entity equivalents encoding all characters that have string entity equivalents
- Explain how to use HTML Purifier in non-PHP languages - Explain how to use HTML Purifier in non-PHP languages / create
a simple command line stub
- Abstract ChildDef_BlockQuote to work with all elements that only - Abstract ChildDef_BlockQuote to work with all elements that only
allow blocks in them, required or optional allow blocks in them, required or optional
- Reorganize Unit Tests - Reorganize Unit Tests

View File

@ -2,6 +2,8 @@
/** /**
* Registry object that contains information about the current context. * Registry object that contains information about the current context.
* @warning Is a bit buggy when variables are set to null: it thinks
* they don't exist! So use false instead, please.
*/ */
class HTMLPurifier_Context class HTMLPurifier_Context
{ {
@ -19,7 +21,7 @@ class HTMLPurifier_Context
*/ */
function register($name, &$ref) { function register($name, &$ref) {
if (isset($this->_storage[$name])) { if (isset($this->_storage[$name])) {
trigger_error('Name collision, cannot re-register', trigger_error("Name $name produces collision, cannot re-register",
E_USER_ERROR); E_USER_ERROR);
return; return;
} }
@ -32,7 +34,7 @@ class HTMLPurifier_Context
*/ */
function &get($name) { function &get($name) {
if (!isset($this->_storage[$name])) { if (!isset($this->_storage[$name])) {
trigger_error('Attempted to retrieve non-existent variable', 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;
@ -46,7 +48,7 @@ class HTMLPurifier_Context
*/ */
function destroy($name) { function destroy($name) {
if (!isset($this->_storage[$name])) { if (!isset($this->_storage[$name])) {
trigger_error('Attempted to destroy non-existent variable', trigger_error("Attempted to destroy non-existent variable $name",
E_USER_ERROR); E_USER_ERROR);
return; return;
} }

View File

@ -50,48 +50,58 @@ class HTMLPurifier_Strategy_MakeWellFormed extends HTMLPurifier_Strategy
$escape_invalid_tags = $config->get('Core', 'EscapeInvalidTags'); $escape_invalid_tags = $config->get('Core', 'EscapeInvalidTags');
// -- begin INJECTOR --
// factor this stuff out to its own class
$injector = array(); $injector = array();
$injector_skip = array(); $injector_skip = array();
$injector_disabled = array();
if ($config->get('Core', 'AutoParagraph')) { if ($config->get('Core', 'AutoParagraph')) {
$injector[] = new HTMLPurifier_Injector_AutoParagraph(); $injector[] = new HTMLPurifier_Injector_AutoParagraph();
$injector_skip[] = 0; // decrement happens first, so set to one so we start at zero
$injector_disabled[] = false; $injector_skip[] = 1;
} }
if ($config->get('Core', 'AutoLinkify')) { if ($config->get('Core', 'AutoLinkify')) {
$injector[] = new HTMLPurifier_Injector_Linkify(); $injector[] = new HTMLPurifier_Injector_Linkify();
$injector_skip[] = 0; $injector_skip[] = 1;
$injector_disabled[] = false;
} }
$current_injector = 0; // array index of the injector that resulted in an array
// substitution. This enables processTokens() to know which
// injectors are affected by the added tokens and which are
// not (namely, the ones after the current injector are not
// affected)
$current_injector = false;
$context->register('Injector', $injector); $context->register('Injector', $injector);
$context->register('InjectorSkip', $injector_skip);
$context->register('CurrentInjector', $current_injector); $context->register('CurrentInjector', $current_injector);
// number of tokens to skip + 1
// before processing, this gets decremented: if it equals zero,
// it means the injector is active and is processing tokens, if
// it is greater than zero, then it is inactive, presumably having
// been the source of the tokens
$context->register('InjectorSkip', $injector_skip);
// -- end INJECTOR --
for ($tokens_index = 0; isset($tokens[$tokens_index]); $tokens_index++) { for ($tokens_index = 0; isset($tokens[$tokens_index]); $tokens_index++) {
// if all goes well, this token will be passed through unharmed // if all goes well, this token will be passed through unharmed
$token = $tokens[$tokens_index]; $token = $tokens[$tokens_index];
foreach ($injector as $i => $x) { foreach ($injector as $i => $x) {
if ($injector_skip[$i] > 0) { if ($injector_skip[$i] > 0) $injector_skip[$i]--;
$injector_skip[$i]--;
$injector_disabled[$i] = true;
} else {
$injector_disabled[$i] = false;
}
} }
// quick-check: if it's not a tag, no need to process // quick-check: if it's not a tag, no need to process
if (empty( $token->is_tag )) { if (empty( $token->is_tag )) {
// duplicated with handleStart
if ($token->type === 'text') { if ($token->type === 'text') {
foreach ($injector as $i => $x) { foreach ($injector as $i => $x) {
if (!$injector_disabled[$i]) { if (!$injector_skip[$i]) {
$x->handleText($token, $config, $context); $x->handleText($token, $config, $context);
} }
if (is_array($token)) { if (is_array($token)) {
@ -149,8 +159,9 @@ class HTMLPurifier_Strategy_MakeWellFormed extends HTMLPurifier_Strategy
$current_nesting[] = $parent; // undo the pop $current_nesting[] = $parent; // undo the pop
} }
// injectors
foreach ($injector as $i => $x) { foreach ($injector as $i => $x) {
if (!$injector_disabled[$i]) { if (!$injector_skip[$i]) {
$x->handleStart($token, $config, $context); $x->handleStart($token, $config, $context);
} }
if (is_array($token)) { if (is_array($token)) {
@ -236,12 +247,16 @@ class HTMLPurifier_Strategy_MakeWellFormed extends HTMLPurifier_Strategy
$context->destroy('InputIndex'); $context->destroy('InputIndex');
$context->destroy('OutputTokens'); $context->destroy('OutputTokens');
$context->destroy('Injector');
$context->destroy('CurrentInjector');
$context->destroy('InjectorSkip');
return $result; return $result;
} }
function processToken($token, $config, &$context) { function processToken($token, $config, &$context) {
if (is_array($token)) { if (is_array($token)) {
// the original token was overloaded by a formatter, time // the original token was overloaded by an injector, time
// to some fancy acrobatics // to some fancy acrobatics
$tokens =& $context->get('InputTokens'); $tokens =& $context->get('InputTokens');
@ -250,13 +265,11 @@ class HTMLPurifier_Strategy_MakeWellFormed extends HTMLPurifier_Strategy
// re-processed // re-processed
array_splice($tokens, $tokens_index--, 1, $token); array_splice($tokens, $tokens_index--, 1, $token);
// this will be a bit more complicated when we add more formatters // adjust the injector skips based on the array substitution
// we need to prevent the same formatter from running twice on it
$injector_skip =& $context->get('InjectorSkip'); $injector_skip =& $context->get('InjectorSkip');
$injector =& $context->get('Injector');
$current_injector =& $context->get('CurrentInjector'); $current_injector =& $context->get('CurrentInjector');
$offset = count($token); $offset = count($token) + 1;
for ($i = 0; $i <= $current_injector; $i++) { for ($i = 0; $i <= $current_injector; $i++) {
$injector_skip[$i] += $offset; $injector_skip[$i] += $offset;
} }
@ -269,9 +282,9 @@ class HTMLPurifier_Strategy_MakeWellFormed extends HTMLPurifier_Strategy
if ($token->type == 'start') { if ($token->type == 'start') {
$current_nesting[] = $token; $current_nesting[] = $token;
} elseif ($token->type == 'end') { } elseif ($token->type == 'end') {
// theoretical: this isn't used because performing // theoretical: this code doesn't get run because performing
// the calculations inline is more efficient, and // the calculations inline is more efficient, and
// end tokens currently do not cause a handler invocation // end tokens (currently) do not cause a handler invocation
array_pop($current_nesting); array_pop($current_nesting);
} }
} }

View File

@ -30,11 +30,11 @@ class HTMLPurifier_ContextTest extends UnitTestCase
$this->context->destroy('IDAccumulator'); $this->context->destroy('IDAccumulator');
$this->assertFalse($this->context->exists('IDAccumulator')); $this->assertFalse($this->context->exists('IDAccumulator'));
$this->expectError('Attempted to retrieve non-existent variable'); $this->expectError('Attempted to retrieve non-existent variable IDAccumulator');
$accumulator_3 =& $this->context->get('IDAccumulator'); $accumulator_3 =& $this->context->get('IDAccumulator');
$this->assertNull($accumulator_3); $this->assertNull($accumulator_3);
$this->expectError('Attempted to destroy non-existent variable'); $this->expectError('Attempted to destroy non-existent variable IDAccumulator');
$this->context->destroy('IDAccumulator'); $this->context->destroy('IDAccumulator');
} }
@ -44,7 +44,7 @@ class HTMLPurifier_ContextTest extends UnitTestCase
$var = true; $var = true;
$this->context->register('OnceOnly', $var); $this->context->register('OnceOnly', $var);
$this->expectError('Name collision, cannot re-register'); $this->expectError('Name OnceOnly produces collision, cannot re-register');
$this->context->register('OnceOnly', $var); $this->context->register('OnceOnly', $var);
// destroy it, now registration is okay // destroy it, now registration is okay

View File

@ -235,7 +235,7 @@ http://dev.example.com',
$this->assertResult( $this->assertResult(
'http://example.com <div>http://example.com</div>', 'http://example.com <div>http://example.com</div>',
'<p><a href="http://example.com">http://example.com</a></p><div><a href="http://example.com">http://example.com</a></div>' '<p><a href="http://example.com">http://example.com</a> </p><div><a href="http://example.com">http://example.com</a></div>'
); );
$this->assertResult( $this->assertResult(