1
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2025-07-31 19:30:21 +02:00

Use a Zipper to process MakeWellFormed, removing quadratic behavior.

Signed-off-by: Edward Z. Yang <ezyang@mit.edu>
This commit is contained in:
Edward Z. Yang
2013-10-13 12:53:51 -07:00
parent 82bcc62058
commit 8f401f769e
10 changed files with 158 additions and 149 deletions

View File

@@ -307,13 +307,13 @@ class HTMLPurifier_Injector_AutoParagraph extends HTMLPurifier_Injector
*/
private function _pLookAhead()
{
$this->current($i, $current);
if ($current instanceof HTMLPurifier_Token_Start) {
if ($this->currentToken instanceof HTMLPurifier_Token_Start) {
$nesting = 1;
} else {
$nesting = 0;
}
$ok = false;
$i = null;
while ($this->forwardUntilEndToken($i, $current, $nesting)) {
$result = $this->_checkNeedsP($current);
if ($result !== null) {

View File

@@ -57,8 +57,9 @@ class HTMLPurifier_Injector_RemoveEmpty extends HTMLPurifier_Injector
return;
}
$next = false;
for ($i = $this->inputIndex + 1, $c = count($this->inputTokens); $i < $c; $i++) {
$next = $this->inputTokens[$i];
$deleted = 1; // the current tag
for ($i = count($this->inputZipper->back) - 1; $i >= 0; $i--, $deleted++) {
$next = $this->inputZipper->back[$i];
if ($next instanceof HTMLPurifier_Token_Text) {
if ($next->is_whitespace) {
continue;
@@ -82,16 +83,16 @@ class HTMLPurifier_Injector_RemoveEmpty extends HTMLPurifier_Injector
if (isset($token->attr['id']) || isset($token->attr['name'])) {
return;
}
$token = $i - $this->inputIndex + 1;
for ($b = $this->inputIndex - 1; $b > 0; $b--) {
$prev = $this->inputTokens[$b];
$token = $deleted + 1;
for ($b = 0, $c = count($this->inputZipper->front); $b < $c; $b++) {
$prev = $this->inputZipper->front[$b];
if ($prev instanceof HTMLPurifier_Token_Text && $prev->is_whitespace) {
continue;
}
break;
}
// This is safe because we removed the token that triggered this.
$this->rewind($b - 1);
$this->rewindOffset($b+$deleted);
return;
}
}