0
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2025-03-23 14:27:02 +00:00

Refactor MakeWellFormed/Injector for performance and as little code duplication as possible. Also, make AutoParagraph smarter about root nodes that don't like p tags.

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1221 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang 2007-06-24 17:44:27 +00:00
parent 75e52a12a6
commit 5f0663cad7
5 changed files with 169 additions and 128 deletions

View File

@ -8,15 +8,78 @@
class HTMLPurifier_Injector class HTMLPurifier_Injector
{ {
/**
* Amount of tokens the injector needs to skip + 1. Because
* the decrement is the first thing that happens, this needs to
* be one greater than the "real" skip count.
*/
var $skip = 1;
/**
* Instance of HTMLPurifier_HTMLDefinition
*/
var $htmlDefinition;
/**
* Reference to CurrentNesting variable in Context. This is an array
* list of tokens that we are currently "inside"
*/
var $currentNesting;
/**
* Reference to InputTokens variable in Context. This is an array
* list of the input tokens that are being processed.
*/
var $inputTokens;
/**
* Reference to InputIndex variable in Context. This is an integer
* array index for $this->inputTokens that indicates what token
* is currently being processed.
*/
var $inputIndex;
/**
* Prepares the injector by giving it the config and context objects,
* so that important variables can be extracted and not passed via
* parameter constantly. Remember: always instantiate a new injector
* when handling a set of HTML.
*/
function prepare($config, &$context) {
$this->htmlDefinition = $config->getHTMLDefinition();
$this->currentNesting =& $context->get('CurrentNesting');
$this->inputTokens =& $context->get('InputTokens');
$this->inputIndex =& $context->get('InputIndex');
}
/**
* Tests if the context node allows a certain element
* @param $name Name of element to test for
* @return True if element is allowed, false if it is not
*/
function allowsElement($name) {
if (!empty($this->currentNesting)) {
$parent_token = array_pop($this->currentNesting);
$this->currentNesting[] = $parent_token;
$parent = $this->htmlDefinition->info[$parent_token->name];
} else {
$parent = $this->htmlDefinition->info_parent_def;
}
if (!isset($parent->child->elements[$name]) || isset($parent->excludes[$name])) {
return false;
}
return true;
}
/** /**
* Handler that is called when a text token is processed * Handler that is called when a text token is processed
*/ */
function handleText(&$token, $config, &$context) {} function handleText(&$token) {}
/** /**
* Handler that is called when a start token is processed * Handler that is called when a start token is processed
*/ */
function handleStart(&$token, $config, &$context) {} function handleStart(&$token) {}
} }

View File

@ -5,38 +5,36 @@ require_once 'HTMLPurifier/Injector.php';
/** /**
* Injector that auto paragraphs text in the root node based on * Injector that auto paragraphs text in the root node based on
* double-spacing. * double-spacing.
* @todo Don't assume that root node means paragraphing is alright
*/ */
class HTMLPurifier_Injector_AutoParagraph extends HTMLPurifier_Injector class HTMLPurifier_Injector_AutoParagraph extends HTMLPurifier_Injector
{ {
function handleText(&$token, $config, &$context) { function handleText(&$token) {
$current_nesting =& $context->get('CurrentNesting');
$text = $token->data; $text = $token->data;
// $token is the focus: if processing is needed, it gets // $token is the focus: if processing is needed, it gets
// turned into an array of tokens that will replace the // turned into an array of tokens that will replace the
// original token // original token
if (empty($current_nesting)) { if (empty($this->currentNesting)) {
// we're in root node, great time to start a paragraph if (!$this->allowsElement('p')) return;
// since we're also dealing with a text node // we're in root node, and the root node allows paragraphs
// start a paragraph since we just hit some text
$token = array(new HTMLPurifier_Token_Start('p')); $token = array(new HTMLPurifier_Token_Start('p'));
$this->_splitText($text, $token, $config, $context); $this->_splitText($text, $token);
} elseif ($current_nesting[count($current_nesting)-1]->name == 'p') { } elseif ($this->currentNesting[count($this->currentNesting)-1]->name == 'p') {
// we're not in root node but we're in a paragraph, so don't // we're not in root node but we're in a paragraph, so don't
// add a paragraph start tag but still perform processing // add a paragraph start tag but still perform processing
$token = array(); $token = array();
$this->_splitText($text, $token, $config, $context); $this->_splitText($text, $token);
} }
} }
function handleStart(&$token, $config, &$context) { function handleStart(&$token) {
// check if we're inside a tag already, if so, don't add // check if we're inside a tag already, if so, don't add
// paragraph tags // paragraph tags
$current_nesting = $context->get('CurrentNesting'); if (!empty($this->currentNesting)) return;
if (!empty($current_nesting)) return;
// check if the start tag counts as a "block" element // check if the start tag counts as a "block" element
if (!$this->_isInline($token, $config)) return; if (!$this->_isInline($token)) return;
// append a paragraph tag before the token // append a paragraph tag before the token
$token = array(new HTMLPurifier_Token_Start('p'), $token); $token = array(new HTMLPurifier_Token_Start('p'), $token);
@ -53,7 +51,7 @@ class HTMLPurifier_Injector_AutoParagraph extends HTMLPurifier_Injector
* @param $context Instance of HTMLPurifier_Context * @param $context Instance of HTMLPurifier_Context
* @private * @private
*/ */
function _splitText($data, &$result, $config, &$context) { function _splitText($data, &$result) {
$raw_paragraphs = explode(PHP_EOL . PHP_EOL, $data); $raw_paragraphs = explode(PHP_EOL . PHP_EOL, $data);
// remove empty paragraphs // remove empty paragraphs
@ -78,7 +76,7 @@ class HTMLPurifier_Injector_AutoParagraph extends HTMLPurifier_Injector
// check the outside to determine whether or not the // check the outside to determine whether or not the
// end paragraph tag should be removed // end paragraph tag should be removed
if ($this->_removeParagraphEnd($config, $context)) { if ($this->_removeParagraphEnd()) {
array_pop($result); array_pop($result);
} }
@ -89,19 +87,15 @@ class HTMLPurifier_Injector_AutoParagraph extends HTMLPurifier_Injector
* Returns boolean whether or not to remove the paragraph end tag * Returns boolean whether or not to remove the paragraph end tag
* that was automatically added. The paragraph end tag should be * that was automatically added. The paragraph end tag should be
* removed unless the next token is a paragraph or block element. * removed unless the next token is a paragraph or block element.
* @param $config Instance of HTMLPurifier_Config
* @param $context Instance of HTMLPurifier_Context
* @private * @private
*/ */
function _removeParagraphEnd($config, &$context) { function _removeParagraphEnd() {
$tokens = $context->get('InputTokens'); $tokens =& $this->inputTokens;
$i = $context->get('InputIndex');
$remove_paragraph_end = true; $remove_paragraph_end = true;
// Start of the checks one after the current token's index // Start of the checks one after the current token's index
for ($i++; isset($tokens[$i]); $i++) { for ($i = $this->inputIndex + 1; isset($tokens[$i]); $i++) {
if ($tokens[$i]->type == 'start' || $tokens[$i]->type == 'empty') { if ($tokens[$i]->type == 'start' || $tokens[$i]->type == 'empty') {
$definition = $config->getHTMLDefinition(); $remove_paragraph_end = $this->_isInline($tokens[$i]);
$remove_paragraph_end = $this->_isInline($tokens[$i], $config);
break; break;
} }
// check if we can abort early (whitespace means we carry-on!) // check if we can abort early (whitespace means we carry-on!)
@ -114,10 +108,10 @@ class HTMLPurifier_Injector_AutoParagraph extends HTMLPurifier_Injector
/** /**
* Returns true if passed token is inline (and, ergo, allowed in * Returns true if passed token is inline (and, ergo, allowed in
* paragraph tags) * paragraph tags)
* @private
*/ */
function _isInline($token, $config) { function _isInline($token) {
$definition = $config->getHTMLDefinition(); return isset($this->htmlDefinition->info['p']->child->elements[$token->name]);
return isset($definition->info['p']->child->elements[$token->name]);
} }
} }

View File

@ -9,28 +9,17 @@ class HTMLPurifier_Injector_Linkify extends HTMLPurifier_Injector
{ {
function handleText(&$token, $config, &$context) { function handleText(&$token, $config, &$context) {
$current_nesting =& $context->get('CurrentNesting'); if (!$this->allowsElement('a')) return;
// this snippet could be factored out
$definition = $config->getHTMLDefinition();
if (!empty($current_nesting)) {
$parent_token = array_pop($current_nesting);
$current_nesting[] = $parent_token;
$parent = $definition->info[$parent_token->name];
} else {
$parent = $definition->info_parent_def;
}
if (!isset($parent->child->elements['a']) || isset($parent->excludes['a'])) {
// parent element does not allow link elements, don't bother
return;
}
if (strpos($token->data, '://') === false) { if (strpos($token->data, '://') === false) {
// our really quick heuristic failed, abort // our really quick heuristic failed, abort
// this may not work so well if we want to match things like // this may not work so well if we want to match things like
// "google.com" // "google.com", but then again, most people don't
return; return;
} }
// there is/are URL(s). Let's split the string: // there is/are URL(s). Let's split the string:
// Note: this regex is extremely permissive
$bits = preg_split('#((?:https?|ftp)://[^\s\'"<>()]+)#S', $token->data, -1, PREG_SPLIT_DELIM_CAPTURE); $bits = preg_split('#((?:https?|ftp)://[^\s\'"<>()]+)#S', $token->data, -1, PREG_SPLIT_DELIM_CAPTURE);
$token = array(); $token = array();

View File

@ -33,38 +33,49 @@ HTMLPurifier_ConfigSchema::define(
class HTMLPurifier_Strategy_MakeWellFormed extends HTMLPurifier_Strategy class HTMLPurifier_Strategy_MakeWellFormed extends HTMLPurifier_Strategy
{ {
/**
* Locally shared variable references
* @private
*/
var $inputTokens, $inputIndex, $outputTokens, $currentNesting,
$currentInjector, $injectors;
function execute($tokens, $config, &$context) { function execute($tokens, $config, &$context) {
$definition = $config->getHTMLDefinition(); $definition = $config->getHTMLDefinition();
// CurrentNesting
$this->currentNesting = array();
$context->register('CurrentNesting', $this->currentNesting);
// InputIndex
$this->inputIndex = false;
$context->register('InputIndex', $this->inputIndex);
// InputTokens
$context->register('InputTokens', $tokens);
$this->inputTokens =& $tokens;
// OutputTokens
$result = array();
$this->outputTokens =& $result;
// %Core.EscapeInvalidTags
$escape_invalid_tags = $config->get('Core', 'EscapeInvalidTags');
$generator = new HTMLPurifier_Generator(); $generator = new HTMLPurifier_Generator();
$current_nesting = array();
$context->register('CurrentNesting', $current_nesting);
$tokens_index = null;
$context->register('InputIndex', $tokens_index);
$context->register('InputTokens', $tokens);
$result = array();
$context->register('OutputTokens', $result);
$escape_invalid_tags = $config->get('Core', 'EscapeInvalidTags');
// -- begin INJECTOR -- // -- begin INJECTOR --
// factor this stuff out to its own class
$injector = array(); $this->injectors = array();
$injector_skip = array();
// we need a generic way of adding injectors, and also its own
// configuration namespace
if ($config->get('Core', 'AutoParagraph')) { if ($config->get('Core', 'AutoParagraph')) {
$injector[] = new HTMLPurifier_Injector_AutoParagraph(); $this->injectors[] = new HTMLPurifier_Injector_AutoParagraph();
// decrement happens first, so set to one so we start at zero
$injector_skip[] = 1;
} }
if ($config->get('Core', 'AutoLinkify')) { if ($config->get('Core', 'AutoLinkify')) {
$injector[] = new HTMLPurifier_Injector_Linkify(); $this->injectors[] = new HTMLPurifier_Injector_Linkify();
$injector_skip[] = 1;
} }
// array index of the injector that resulted in an array // array index of the injector that resulted in an array
@ -72,64 +83,55 @@ class HTMLPurifier_Strategy_MakeWellFormed extends HTMLPurifier_Strategy
// injectors are affected by the added tokens and which are // injectors are affected by the added tokens and which are
// not (namely, the ones after the current injector are not // not (namely, the ones after the current injector are not
// affected) // affected)
$current_injector = false; $this->currentInjector = false;
$context->register('Injector', $injector); // give the injectors references to the definition and context
$context->register('CurrentInjector', $current_injector); // variables for performance reasons
foreach ($this->injectors as $i => $x) {
// number of tokens to skip + 1 $this->injectors[$i]->prepare($config, $context);
// 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 -- // -- end INJECTOR --
for ($tokens_index = 0; isset($tokens[$tokens_index]); $tokens_index++) { for ($this->inputIndex = 0; isset($tokens[$this->inputIndex]); $this->inputIndex++) {
// 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[$this->inputIndex];
foreach ($injector as $i => $x) { foreach ($this->injectors as $i => $x) {
if ($injector_skip[$i] > 0) $injector_skip[$i]--; if ($x->skip > 0) $this->injectors[$i]->skip--;
} }
// 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) { // injector handler code; duplicated for performance reasons
if (!$injector_skip[$i]) { foreach ($this->injectors as $i => $x) {
$x->handleText($token, $config, $context); if (!$x->skip) $x->handleText($token, $config, $context);
}
if (is_array($token)) { if (is_array($token)) {
$current_injector = $i; $this->currentInjector = $i;
break; break;
} }
} }
} }
$this->processToken($token, $config, $context); $this->processToken($token, $config, $context);
continue; continue;
} }
$info = $definition->info[$token->name]->child; $info = $definition->info[$token->name]->child;
// quick checks:
// test if it claims to be a start tag but is empty // test if it claims to be a start tag but is empty
if ($info->type == 'empty' && $token->type == 'start') { if ($info->type == 'empty' && $token->type == 'start') {
$result[] = new HTMLPurifier_Token_Empty($token->name, $token->attr); $result[] = new HTMLPurifier_Token_Empty($token->name, $token->attr);
continue; continue;
} }
// test if it claims to be empty but really is a start tag // test if it claims to be empty but really is a start tag
if ($info->type != 'empty' && $token->type == 'empty' ) { if ($info->type != 'empty' && $token->type == 'empty' ) {
$result[] = new HTMLPurifier_Token_Start($token->name, $token->attr); $result[] = new HTMLPurifier_Token_Start($token->name, $token->attr);
$result[] = new HTMLPurifier_Token_End($token->name); $result[] = new HTMLPurifier_Token_End($token->name);
continue; continue;
} }
// automatically insert empty tags // automatically insert empty tags
if ($token->type == 'empty') { if ($token->type == 'empty') {
$result[] = $token; $result[] = $token;
@ -140,9 +142,9 @@ class HTMLPurifier_Strategy_MakeWellFormed extends HTMLPurifier_Strategy
if ($token->type == 'start') { if ($token->type == 'start') {
// ...unless they also have to close their parent // ...unless they also have to close their parent
if (!empty($current_nesting)) { if (!empty($this->currentNesting)) {
$parent = array_pop($current_nesting); $parent = array_pop($this->currentNesting);
$parent_info = $definition->info[$parent->name]; $parent_info = $definition->info[$parent->name];
// this can be replaced with a more general algorithm: // this can be replaced with a more general algorithm:
@ -152,20 +154,18 @@ class HTMLPurifier_Strategy_MakeWellFormed extends HTMLPurifier_Strategy
// close the parent, then append the token // close the parent, then append the token
$result[] = new HTMLPurifier_Token_End($parent->name); $result[] = new HTMLPurifier_Token_End($parent->name);
$result[] = $token; $result[] = $token;
$current_nesting[] = $token; $this->currentNesting[] = $token;
continue; continue;
} }
$current_nesting[] = $parent; // undo the pop $this->currentNesting[] = $parent; // undo the pop
} }
// injectors // injector handler code; duplicated for performance reasons
foreach ($injector as $i => $x) { foreach ($this->injectors as $i => $x) {
if (!$injector_skip[$i]) { if (!$x->skip[$i]) $x->handleStart($token, $config, $context);
$x->handleStart($token, $config, $context);
}
if (is_array($token)) { if (is_array($token)) {
$current_injector = $i; $this->currentInjector = $i;
break; break;
} }
} }
@ -178,7 +178,7 @@ class HTMLPurifier_Strategy_MakeWellFormed extends HTMLPurifier_Strategy
if ($token->type != 'end') continue; if ($token->type != 'end') continue;
// make sure that we have something open // make sure that we have something open
if (empty($current_nesting)) { if (empty($this->currentNesting)) {
if ($escape_invalid_tags) { if ($escape_invalid_tags) {
$result[] = new HTMLPurifier_Token_Text( $result[] = new HTMLPurifier_Token_Text(
$generator->generateFromToken($token, $config, $context) $generator->generateFromToken($token, $config, $context)
@ -188,7 +188,7 @@ class HTMLPurifier_Strategy_MakeWellFormed extends HTMLPurifier_Strategy
} }
// first, check for the simplest case: everything closes neatly // first, check for the simplest case: everything closes neatly
$current_parent = array_pop($current_nesting); $current_parent = array_pop($this->currentNesting);
if ($current_parent->name == $token->name) { if ($current_parent->name == $token->name) {
$result[] = $token; $result[] = $token;
continue; continue;
@ -197,17 +197,17 @@ class HTMLPurifier_Strategy_MakeWellFormed extends HTMLPurifier_Strategy
// okay, so we're trying to close the wrong tag // okay, so we're trying to close the wrong tag
// undo the pop previous pop // undo the pop previous pop
$current_nesting[] = $current_parent; $this->currentNesting[] = $current_parent;
// scroll back the entire nest, trying to find our tag. // scroll back the entire nest, trying to find our tag.
// (feature could be to specify how far you'd like to go) // (feature could be to specify how far you'd like to go)
$size = count($current_nesting); $size = count($this->currentNesting);
// -2 because -1 is the last element, but we already checked that // -2 because -1 is the last element, but we already checked that
$skipped_tags = false; $skipped_tags = false;
for ($i = $size - 2; $i >= 0; $i--) { for ($i = $size - 2; $i >= 0; $i--) {
if ($current_nesting[$i]->name == $token->name) { if ($this->currentNesting[$i]->name == $token->name) {
// current nesting is modified // current nesting is modified
$skipped_tags = array_splice($current_nesting, $i); $skipped_tags = array_splice($this->currentNesting, $i);
break; break;
} }
} }
@ -234,22 +234,20 @@ class HTMLPurifier_Strategy_MakeWellFormed extends HTMLPurifier_Strategy
// we're at the end now, fix all still unclosed tags // we're at the end now, fix all still unclosed tags
// not using processToken() because at this point we don't // not using processToken() because at this point we don't
// care about current nesting // care about current nesting
if (!empty($current_nesting)) { if (!empty($this->currentNesting)) {
$size = count($current_nesting); $size = count($this->currentNesting);
for ($i = $size - 1; $i >= 0; $i--) { for ($i = $size - 1; $i >= 0; $i--) {
$result[] = $result[] =
new HTMLPurifier_Token_End($current_nesting[$i]->name); new HTMLPurifier_Token_End($this->currentNesting[$i]->name);
} }
} }
$context->destroy('CurrentNesting'); $context->destroy('CurrentNesting');
$context->destroy('InputTokens'); $context->destroy('InputTokens');
$context->destroy('InputIndex'); $context->destroy('InputIndex');
$context->destroy('OutputTokens');
$context->destroy('Injector'); unset($this->outputTokens, $this->injectors, $this->currentInjector,
$context->destroy('CurrentInjector'); $this->currentNesting, $this->inputTokens, $this->inputIndex);
$context->destroy('InjectorSkip');
return $result; return $result;
} }
@ -259,33 +257,22 @@ class HTMLPurifier_Strategy_MakeWellFormed extends HTMLPurifier_Strategy
// the original token was overloaded by an injector, time // the original token was overloaded by an injector, time
// to some fancy acrobatics // to some fancy acrobatics
$tokens =& $context->get('InputTokens'); // $this->inputIndex is decremented so that the entire set gets
$tokens_index =& $context->get('InputIndex');
// $tokens_index is decremented so that the entire set gets
// re-processed // re-processed
array_splice($tokens, $tokens_index--, 1, $token); array_splice($this->inputTokens, $this->inputIndex--, 1, $token);
// adjust the injector skips based on the array substitution // adjust the injector skips based on the array substitution
$injector_skip =& $context->get('InjectorSkip');
$current_injector =& $context->get('CurrentInjector');
$offset = count($token) + 1; $offset = count($token) + 1;
for ($i = 0; $i <= $current_injector; $i++) { for ($i = 0; $i <= $this->currentInjector; $i++) {
$injector_skip[$i] += $offset; $this->injectors[$i]->skip += $offset;
} }
} elseif ($token) { } elseif ($token) {
// regular case // regular case
$result =& $context->get('OutputTokens'); $this->outputTokens[] = $token;
$current_nesting =& $context->get('CurrentNesting');
$result[] = $token;
if ($token->type == 'start') { if ($token->type == 'start') {
$current_nesting[] = $token; $this->currentNesting[] = $token;
} elseif ($token->type == 'end') { } elseif ($token->type == 'end') {
// theoretical: this code doesn't get run because performing array_pop($this->currentNesting); // not actually used
// the calculations inline is more efficient, and
// end tokens (currently) do not cause a handler invocation
array_pop($current_nesting);
} }
} }
} }

View File

@ -175,6 +175,14 @@ Par
'<p>Par</p>' '<p>Par</p>'
); );
$this->assertResult(
'Par
Par2',
true,
array('Core.AutoParagraph' => true, 'HTML.Parent' => 'span')
);
} }
function testLinkify() { function testLinkify() {