2006-07-24 01:54:25 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
require_once 'HTMLPurifier/Strategy.php';
|
2006-08-14 21:22:49 +00:00
|
|
|
require_once 'HTMLPurifier/HTMLDefinition.php';
|
2006-07-24 01:54:25 +00:00
|
|
|
|
2006-08-03 01:37:28 +00:00
|
|
|
/**
|
|
|
|
* Takes a well formed list of tokens and fixes their nesting.
|
|
|
|
*
|
|
|
|
* HTML elements dictate which elements are allowed to be their children,
|
|
|
|
* for example, you can't have a p tag in a span tag. Other elements have
|
|
|
|
* much more rigorous definitions: tables, for instance, require a specific
|
|
|
|
* order for their elements. There are also constraints not expressible by
|
|
|
|
* document type definitions, such as the chameleon nature of ins/del
|
|
|
|
* tags and global child exclusions.
|
|
|
|
*
|
|
|
|
* The first major objective of this strategy is to iterate through all the
|
|
|
|
* nodes (not tokens) of the list of tokens and determine whether or not
|
|
|
|
* their children conform to the element's definition. If they do not, the
|
|
|
|
* child definition may optionally supply an amended list of elements that
|
|
|
|
* is valid or require that the entire node be deleted (and the previous
|
|
|
|
* node rescanned).
|
|
|
|
*
|
|
|
|
* The second objective is to ensure that explicitly excluded elements of
|
|
|
|
* an element do not appear in its children. Code that accomplishes this
|
|
|
|
* task is pervasive through the strategy, though the two are distinct tasks
|
|
|
|
* and could, theoretically, be seperated (although it's not recommended).
|
|
|
|
*
|
|
|
|
* @note Whether or not unrecognized children are silently dropped or
|
|
|
|
* translated into text depends on the child definitions.
|
|
|
|
*
|
|
|
|
* @todo Enable nodes to be bubbled out of the structure.
|
|
|
|
*/
|
2006-07-31 03:04:57 +00:00
|
|
|
|
2006-07-24 01:54:25 +00:00
|
|
|
class HTMLPurifier_Strategy_FixNesting extends HTMLPurifier_Strategy
|
|
|
|
{
|
|
|
|
|
2006-10-01 20:47:07 +00:00
|
|
|
function execute($tokens, $config, &$context) {
|
2006-08-07 20:28:12 +00:00
|
|
|
//####################################################################//
|
|
|
|
// Pre-processing
|
|
|
|
|
2006-08-31 20:33:07 +00:00
|
|
|
// get a copy of the HTML definition
|
|
|
|
$definition = $config->getHTMLDefinition();
|
|
|
|
|
2006-08-07 20:28:12 +00:00
|
|
|
// insert implicit "parent" node, will be removed at end.
|
|
|
|
// ! we might want to move this to configuration
|
|
|
|
// DEFINITION CALL
|
2006-08-31 20:33:07 +00:00
|
|
|
$parent_name = $definition->info_parent;
|
2006-07-30 22:57:54 +00:00
|
|
|
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-10-01 20:47:07 +00:00
|
|
|
// setup the context variables
|
|
|
|
$parent_type = 'unknown'; // reference var that we alter
|
|
|
|
$context->register('ParentType', $parent_type);
|
|
|
|
|
2006-08-07 20:28:12 +00:00
|
|
|
//####################################################################//
|
|
|
|
// Loop initialization
|
|
|
|
|
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-08-03 01:18:57 +00:00
|
|
|
// stack that contains all elements that are excluded
|
2006-08-07 20:28:12 +00:00
|
|
|
// same structure as $stack, but it is only populated when an element
|
|
|
|
// with exclusions is processed, i.e. there won't be empty exclusions.
|
2006-08-03 01:18:57 +00:00
|
|
|
$exclude_stack = array();
|
|
|
|
|
2006-08-07 20:28:12 +00:00
|
|
|
//####################################################################//
|
|
|
|
// Loop
|
|
|
|
|
|
|
|
// iterate through all start nodes. Determining the start node
|
|
|
|
// is complicated so it has been omitted from the loop construct
|
2006-07-24 01:54:25 +00:00
|
|
|
for ($i = 0, $size = count($tokens) ; $i < $size; ) {
|
|
|
|
|
2006-08-07 20:28:12 +00:00
|
|
|
//################################################################//
|
|
|
|
// Gather information on children
|
|
|
|
|
|
|
|
// child token accumulator
|
2006-07-24 01:54:25 +00:00
|
|
|
$child_tokens = array();
|
|
|
|
|
2006-08-07 20:28:12 +00:00
|
|
|
// scroll to the end of this node, report number, and collect
|
|
|
|
// all children
|
2006-07-24 01:54:25 +00:00
|
|
|
for ($j = $i, $depth = 0; ; $j++) {
|
|
|
|
if ($tokens[$j]->type == 'start') {
|
|
|
|
$depth++;
|
2006-08-07 20:28:12 +00:00
|
|
|
// skip token assignment on first iteration, this is the
|
|
|
|
// token we currently are on
|
2006-07-24 01:54:25 +00:00
|
|
|
if ($depth == 1) continue;
|
|
|
|
} elseif ($tokens[$j]->type == 'end') {
|
|
|
|
$depth--;
|
2006-08-07 20:28:12 +00:00
|
|
|
// skip token assignment on last iteration, this is the
|
|
|
|
// end token of the token we're currently on
|
2006-07-24 01:54:25 +00:00
|
|
|
if ($depth == 0) break;
|
|
|
|
}
|
|
|
|
$child_tokens[] = $tokens[$j];
|
|
|
|
}
|
|
|
|
|
|
|
|
// $i is index of start token
|
|
|
|
// $j is index of end token
|
|
|
|
|
2006-08-07 20:28:12 +00:00
|
|
|
//################################################################//
|
|
|
|
// Gather information on parent
|
|
|
|
|
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;
|
2006-08-31 20:33:07 +00:00
|
|
|
$parent_def = $definition->info[$parent_name];
|
2006-08-03 01:03:23 +00:00
|
|
|
} else {
|
2006-08-07 20:28:12 +00:00
|
|
|
// unknown info, it won't be used anyway
|
2006-08-03 01:03:23 +00:00
|
|
|
$parent_index = $parent_name = $parent_def = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// calculate context
|
|
|
|
if (isset($parent_def)) {
|
2006-10-01 20:47:07 +00:00
|
|
|
$parent_type = $parent_def->type;
|
2006-08-03 01:03:23 +00:00
|
|
|
} else {
|
2006-08-07 20:28:12 +00:00
|
|
|
// generally found in specialized elements like UL
|
2006-10-01 20:47:07 +00:00
|
|
|
$parent_type = 'unknown';
|
2006-08-03 01:03:23 +00:00
|
|
|
}
|
2006-07-30 19:11:18 +00:00
|
|
|
|
2006-08-07 20:28:12 +00:00
|
|
|
//################################################################//
|
|
|
|
// Determine whether element is explicitly excluded SGML-style
|
|
|
|
|
|
|
|
// determine whether or not element is excluded by checking all
|
|
|
|
// parent exclusions. The array should not be very large, two
|
|
|
|
// elements at most.
|
2006-08-03 01:18:57 +00:00
|
|
|
$excluded = false;
|
|
|
|
if (!empty($exclude_stack)) {
|
|
|
|
foreach ($exclude_stack as $lookup) {
|
|
|
|
if (isset($lookup[$tokens[$i]->name])) {
|
|
|
|
$excluded = true;
|
2006-08-07 20:28:12 +00:00
|
|
|
// no need to continue processing
|
2006-08-03 01:18:57 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-07-30 19:11:18 +00:00
|
|
|
|
2006-08-07 20:28:12 +00:00
|
|
|
//################################################################//
|
|
|
|
// Perform child validation
|
|
|
|
|
2006-08-03 01:18:57 +00:00
|
|
|
if ($excluded) {
|
2006-08-07 20:28:12 +00:00
|
|
|
// there is an exclusion, remove the entire node
|
2006-08-03 01:18:57 +00:00
|
|
|
$result = false;
|
|
|
|
} else {
|
|
|
|
// DEFINITION CALL
|
2006-08-31 20:33:07 +00:00
|
|
|
$def = $definition->info[$tokens[$i]->name];
|
2006-08-03 01:18:57 +00:00
|
|
|
$child_def = $def->child;
|
|
|
|
|
|
|
|
// have DTD child def validate children
|
2006-08-14 02:46:34 +00:00
|
|
|
$result = $child_def->validateChildren(
|
2006-10-01 20:47:07 +00:00
|
|
|
$child_tokens, $config, $context);
|
2006-08-03 01:18:57 +00:00
|
|
|
|
|
|
|
// determine whether or not this element has any exclusions
|
|
|
|
$excludes = $def->excludes;
|
|
|
|
}
|
2006-07-24 01:54:25 +00:00
|
|
|
|
2006-08-07 20:28:12 +00:00
|
|
|
// $result is now a bool or array
|
|
|
|
|
|
|
|
//################################################################//
|
|
|
|
// Process result by interpreting $result
|
|
|
|
|
2006-07-24 01:54:25 +00:00
|
|
|
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-08-03 01:18:57 +00:00
|
|
|
// register exclusions if there are any
|
|
|
|
if (!empty($excludes)) $exclude_stack[] = $excludes;
|
|
|
|
|
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-08-07 20:28:12 +00:00
|
|
|
// remove entire node
|
2006-07-24 01:54:25 +00:00
|
|
|
|
2006-08-07 20:28:12 +00:00
|
|
|
// calculate length of inner tokens and current tokens
|
2006-07-24 01:54:25 +00:00
|
|
|
$length = $j - $i + 1;
|
|
|
|
|
2006-08-07 20:28:12 +00:00
|
|
|
// perform removal
|
2006-07-24 01:54:25 +00:00
|
|
|
array_splice($tokens, $i, $length);
|
|
|
|
|
2006-08-07 20:28:12 +00:00
|
|
|
// update size
|
2006-07-24 01:54:25 +00:00
|
|
|
$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-09-15 01:52:22 +00:00
|
|
|
array_pop($stack);
|
2006-07-31 03:04:57 +00:00
|
|
|
}
|
2006-07-24 01:54:25 +00:00
|
|
|
|
2006-08-07 20:28:12 +00:00
|
|
|
// PROJECTED OPTIMIZATION: Process all children elements before
|
|
|
|
// reprocessing parent node.
|
|
|
|
|
2006-07-24 01:54:25 +00:00
|
|
|
} else {
|
2006-08-07 20:28:12 +00:00
|
|
|
// replace node with $result
|
2006-07-24 01:54:25 +00:00
|
|
|
|
2006-08-07 20:28:12 +00:00
|
|
|
// calculate length of inner tokens
|
2006-07-24 01:54:25 +00:00
|
|
|
$length = $j - $i - 1;
|
|
|
|
|
2006-08-07 20:28:12 +00:00
|
|
|
// perform replacement
|
2006-07-24 01:54:25 +00:00
|
|
|
array_splice($tokens, $i + 1, $length, $result);
|
|
|
|
|
2006-08-07 20:28:12 +00:00
|
|
|
// update size
|
2006-07-24 01:54:25 +00:00
|
|
|
$size -= $length;
|
|
|
|
$size += count($result);
|
|
|
|
|
2006-07-31 03:04:57 +00:00
|
|
|
// register start token as a parental node start
|
|
|
|
$stack[] = $i;
|
|
|
|
|
2006-08-03 01:18:57 +00:00
|
|
|
// register exclusions if there are any
|
|
|
|
if (!empty($excludes)) $exclude_stack[] = $excludes;
|
|
|
|
|
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-08-07 20:28:12 +00:00
|
|
|
//################################################################//
|
|
|
|
// Scroll to next start node
|
|
|
|
|
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') {
|
2006-08-03 01:18:57 +00:00
|
|
|
if ($tokens[$i]->type == 'end') {
|
|
|
|
// pop a token index off the stack if we ended a node
|
|
|
|
array_pop($stack);
|
|
|
|
// pop an exclusion lookup off exclusion stack if
|
|
|
|
// we ended node and that node had exclusions
|
2006-08-31 20:33:07 +00:00
|
|
|
if ($definition->info[$tokens[$i]->name]->excludes) {
|
2006-08-03 01:18:57 +00:00
|
|
|
array_pop($exclude_stack);
|
|
|
|
}
|
|
|
|
}
|
2006-07-31 03:04:57 +00:00
|
|
|
$i++;
|
|
|
|
}
|
2006-07-24 01:54:25 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-08-07 20:28:12 +00:00
|
|
|
//####################################################################//
|
|
|
|
// Post-processing
|
|
|
|
|
|
|
|
// remove implicit parent tokens at the beginning and end
|
2006-07-24 01:54:25 +00:00
|
|
|
array_shift($tokens);
|
|
|
|
array_pop($tokens);
|
|
|
|
|
2006-10-01 20:47:07 +00:00
|
|
|
// remove context variables
|
|
|
|
$context->destroy('ParentType');
|
|
|
|
|
2006-08-07 20:28:12 +00:00
|
|
|
//####################################################################//
|
|
|
|
// Return
|
|
|
|
|
2006-07-24 01:54:25 +00:00
|
|
|
return $tokens;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-09-15 01:52:22 +00:00
|
|
|
?>
|