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

@@ -20,7 +20,7 @@
class HTMLPurifier_Zipper
{
private $front, $back;
public $front, $back;
public function __construct($front, $back) {
$this->front = $front;
@@ -95,6 +95,14 @@ class HTMLPurifier_Zipper
return empty($this->back) ? NULL : array_pop($this->back);
}
/**
* Returns true if we are at the end of the list.
* @return bool
*/
public function done() {
return empty($this->back);
}
/**
* Insert element before hole.
* @param Element to insert
@@ -115,14 +123,16 @@ class HTMLPurifier_Zipper
* Splice in multiple elements at hole. Functional specification
* in terms of array_splice:
*
* $r1 = array_splice($arr, $i, $delete, $replacement);
* $arr1 = $arr;
* $old1 = array_splice($arr1, $i, $delete, $replacement);
*
* list($z, $t) = HTMLPurifier_Zipper::fromArray($arr);
* $t = $z->advance($t, $i);
* $t = $z->splice($t, $delete, $replacement);
* $r2 = $z->toArray($t);
* list($old2, $t) = $z->splice($t, $delete, $replacement);
* $arr2 = $z->toArray($t);
*
* assert($r1 === $r2);
* assert($old1 === $old2);
* assert($arr1 === $arr2);
*
* NB: the absolute index location after this operation is
* *unchanged!*
@@ -131,8 +141,10 @@ class HTMLPurifier_Zipper
*/
public function splice($t, $delete, $replacement) {
// delete
$old = array();
$r = $t;
for ($i = $delete; $i > 0; $i--) {
$old[] = $r;
$r = $this->delete();
}
// insert
@@ -140,6 +152,6 @@ class HTMLPurifier_Zipper
$this->insertAfter($r);
$r = $replacement[$i];
}
return $r;
return array($old, $r);
}
}