2006-07-24 01:54:25 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
require_once 'HTMLPurifier/Strategy.php';
|
|
|
|
require_once 'HTMLPurifier/Definition.php';
|
|
|
|
|
2006-07-31 03:04:57 +00:00
|
|
|
// EXTRA: provide a mechanism for elements to be bubbled OUT of a node
|
|
|
|
// or "Replace Nodes while including the parent nodes too"
|
|
|
|
|
2006-07-24 01:54:25 +00:00
|
|
|
class HTMLPurifier_Strategy_FixNesting extends HTMLPurifier_Strategy
|
|
|
|
{
|
|
|
|
|
|
|
|
var $definition;
|
|
|
|
|
2006-07-24 02:49:37 +00:00
|
|
|
function HTMLPurifier_Strategy_FixNesting() {
|
2006-07-24 01:54:25 +00:00
|
|
|
$this->definition = HTMLPurifier_Definition::instance();
|
|
|
|
}
|
|
|
|
|
|
|
|
function execute($tokens) {
|
2006-07-31 03:04:57 +00:00
|
|
|
|
2006-07-24 01:54:25 +00:00
|
|
|
// insert implicit "parent" node, will be removed at end
|
2006-07-30 22:57:54 +00:00
|
|
|
$parent_name = $this->definition->info_parent;
|
|
|
|
array_unshift($tokens, new HTMLPurifier_Token_Start($parent_name));
|
|
|
|
$tokens[] = new HTMLPurifier_Token_End($parent_name);
|
2006-07-24 01:54:25 +00:00
|
|
|
|
2006-07-31 03:04:57 +00:00
|
|
|
// stack that contains the indexes of all parents,
|
|
|
|
// $stack[count($stack)-1] being the current parent
|
|
|
|
$stack = array();
|
|
|
|
|
2006-07-24 01:54:25 +00:00
|
|
|
for ($i = 0, $size = count($tokens) ; $i < $size; ) {
|
|
|
|
|
|
|
|
$child_tokens = array();
|
|
|
|
|
|
|
|
// scroll to the end of this node, and report number
|
|
|
|
for ($j = $i, $depth = 0; ; $j++) {
|
|
|
|
if ($tokens[$j]->type == 'start') {
|
|
|
|
$depth++;
|
|
|
|
// skip token assignment on first iteration
|
|
|
|
if ($depth == 1) continue;
|
|
|
|
} elseif ($tokens[$j]->type == 'end') {
|
|
|
|
$depth--;
|
|
|
|
// skip token assignment on last iteration
|
|
|
|
if ($depth == 0) break;
|
|
|
|
}
|
|
|
|
$child_tokens[] = $tokens[$j];
|
|
|
|
}
|
|
|
|
|
|
|
|
// $i is index of start token
|
|
|
|
// $j is index of end token
|
|
|
|
|
2006-08-03 01:03:23 +00:00
|
|
|
// calculate parent information
|
|
|
|
if ($count = count($stack)) {
|
|
|
|
$parent_index = $stack[$count-1];
|
|
|
|
$parent_name = $tokens[$parent_index]->name;
|
|
|
|
$parent_def = $this->definition->info[$parent_name];
|
|
|
|
} else {
|
|
|
|
$parent_index = $parent_name = $parent_def = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// calculate context
|
|
|
|
if (isset($parent_def)) {
|
|
|
|
$context = $parent_def->type;
|
|
|
|
} else {
|
|
|
|
$context = 'unknown';
|
|
|
|
}
|
2006-07-30 19:11:18 +00:00
|
|
|
|
|
|
|
// DEFINITION CALL
|
|
|
|
$child_def = $this->definition->info[$tokens[$i]->name]->child;
|
|
|
|
|
2006-07-24 01:54:25 +00:00
|
|
|
// have DTD child def validate children
|
2006-08-03 01:03:23 +00:00
|
|
|
$result = $child_def->validateChildren($child_tokens, $context);
|
2006-07-24 01:54:25 +00:00
|
|
|
|
|
|
|
// process result
|
|
|
|
if ($result === true) {
|
|
|
|
|
2006-07-31 03:04:57 +00:00
|
|
|
// leave the node as is
|
2006-07-24 01:54:25 +00:00
|
|
|
|
2006-07-31 03:04:57 +00:00
|
|
|
// register start token as a parental node start
|
|
|
|
$stack[] = $i;
|
2006-07-24 01:54:25 +00:00
|
|
|
|
2006-07-31 03:04:57 +00:00
|
|
|
// move cursor to next possible start node
|
|
|
|
$i++;
|
2006-07-24 01:54:25 +00:00
|
|
|
|
2006-07-31 03:04:57 +00:00
|
|
|
} elseif($result === false) {
|
2006-07-24 01:54:25 +00:00
|
|
|
|
|
|
|
$length = $j - $i + 1;
|
|
|
|
|
|
|
|
// remove entire node
|
|
|
|
array_splice($tokens, $i, $length);
|
|
|
|
|
|
|
|
// change size
|
|
|
|
$size -= $length;
|
|
|
|
|
2006-07-31 03:04:57 +00:00
|
|
|
// there is no start token to register,
|
|
|
|
// current node is now the next possible start node
|
|
|
|
// unless it turns out that we need to do a double-check
|
|
|
|
|
|
|
|
if (!$parent_def->child->allow_empty) {
|
|
|
|
// we need to do a double-check
|
|
|
|
$i = $parent_index;
|
|
|
|
}
|
2006-07-24 01:54:25 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
$length = $j - $i - 1;
|
|
|
|
|
|
|
|
// replace node with $result
|
|
|
|
array_splice($tokens, $i + 1, $length, $result);
|
|
|
|
|
|
|
|
// change size
|
|
|
|
$size -= $length;
|
|
|
|
$size += count($result);
|
|
|
|
|
2006-07-31 03:04:57 +00:00
|
|
|
// register start token as a parental node start
|
|
|
|
$stack[] = $i;
|
|
|
|
|
|
|
|
// move cursor to next possible start node
|
|
|
|
$i++;
|
|
|
|
|
2006-07-24 01:54:25 +00:00
|
|
|
}
|
|
|
|
|
2006-07-31 03:04:57 +00:00
|
|
|
// We assume, at this point, that $i is the index of the token
|
|
|
|
// that is the first possible new start point for a node.
|
|
|
|
|
|
|
|
// Test if the token indeed is a start tag, if not, move forward
|
|
|
|
// and test again.
|
|
|
|
while ($i < $size and $tokens[$i]->type != 'start') {
|
|
|
|
// pop a token index off the stack if we ended a node
|
|
|
|
if ($tokens[$i]->type == 'end') array_pop($stack);
|
|
|
|
$i++;
|
|
|
|
}
|
2006-07-24 01:54:25 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove implicit divs
|
|
|
|
array_shift($tokens);
|
|
|
|
array_pop($tokens);
|
|
|
|
|
|
|
|
return $tokens;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|