2006-07-24 01:54:25 +00:00
|
|
|
<?php
|
|
|
|
|
2006-08-03 01:37:28 +00:00
|
|
|
/**
|
|
|
|
* Takes a well formed list of tokens and fixes their nesting.
|
2008-12-06 07:28:20 +00:00
|
|
|
*
|
2006-08-03 01:37:28 +00:00
|
|
|
* 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.
|
2008-12-06 07:28:20 +00:00
|
|
|
*
|
2013-10-21 05:18:59 +00:00
|
|
|
* The first major objective of this strategy is to iterate through all
|
|
|
|
* the nodes 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).
|
2008-12-06 07:28:20 +00:00
|
|
|
*
|
2006-08-03 01:37:28 +00:00
|
|
|
* 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).
|
2008-12-06 07:28:20 +00:00
|
|
|
*
|
2006-08-03 01:37:28 +00:00
|
|
|
* @note Whether or not unrecognized children are silently dropped or
|
|
|
|
* translated into text depends on the child definitions.
|
2008-12-06 07:28:20 +00:00
|
|
|
*
|
2013-10-21 05:18:59 +00:00
|
|
|
* @todo Enable nodes to be bubbled out of the structure. This is
|
|
|
|
* easier with our new algorithm.
|
2006-08-03 01:37:28 +00:00
|
|
|
*/
|
2006-07-31 03:04:57 +00:00
|
|
|
|
2006-07-24 01:54:25 +00:00
|
|
|
class HTMLPurifier_Strategy_FixNesting extends HTMLPurifier_Strategy
|
|
|
|
{
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
/**
|
|
|
|
* @param HTMLPurifier_Token[] $tokens
|
|
|
|
* @param HTMLPurifier_Config $config
|
|
|
|
* @param HTMLPurifier_Context $context
|
|
|
|
* @return array|HTMLPurifier_Token[]
|
|
|
|
*/
|
|
|
|
public function execute($tokens, $config, $context)
|
|
|
|
{
|
2013-10-21 05:18:59 +00:00
|
|
|
|
2006-08-07 20:28:12 +00:00
|
|
|
//####################################################################//
|
|
|
|
// Pre-processing
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-10-21 05:18:59 +00:00
|
|
|
// O(n) pass to convert to a tree, so that we can efficiently
|
|
|
|
// refer to substrings
|
|
|
|
$top_node = HTMLPurifier_Arborize::arborize($tokens, $config, $context);
|
2013-10-20 22:05:11 +00:00
|
|
|
|
2006-08-31 20:33:07 +00:00
|
|
|
// get a copy of the HTML definition
|
|
|
|
$definition = $config->getHTMLDefinition();
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-02-17 23:47:38 +00:00
|
|
|
$excludes_enabled = !$config->get('Core.DisableExcludes');
|
|
|
|
|
2007-05-27 23:12:17 +00:00
|
|
|
// setup the context variable 'IsInline', for chameleon processing
|
|
|
|
// is 'false' when we are not inline, 'true' when it must always
|
|
|
|
// be inline, and an integer when it is inline for a certain
|
|
|
|
// branch of the document tree
|
2007-05-22 00:47:03 +00:00
|
|
|
$is_inline = $definition->info_parent_def->descendants_are_inline;
|
2007-02-04 03:53:57 +00:00
|
|
|
$context->register('IsInline', $is_inline);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-06-26 23:43:28 +00:00
|
|
|
// setup error collector
|
|
|
|
$e =& $context->get('ErrorCollector', true);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-08-07 20:28:12 +00:00
|
|
|
//####################################################################//
|
|
|
|
// Loop initialization
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-08-03 01:18:57 +00:00
|
|
|
// stack that contains all elements that are excluded
|
2008-12-06 07:28:20 +00:00
|
|
|
// it is organized by parent elements, similar to $stack,
|
2007-05-27 23:12:17 +00:00
|
|
|
// but it is only populated when an element with exclusions is
|
|
|
|
// processed, i.e. there won't be empty exclusions.
|
2013-10-21 05:18:59 +00:00
|
|
|
$exclude_stack = array($definition->info_parent_def->excludes);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2007-06-26 23:43:28 +00:00
|
|
|
// variable that contains the start token while we are processing
|
|
|
|
// nodes. This enables error reporting to do its job
|
2013-10-21 05:18:59 +00:00
|
|
|
$node = $top_node;
|
|
|
|
// dummy token
|
|
|
|
list($token, $d) = $node->toTokenPair();
|
|
|
|
$context->register('CurrentNode', $node);
|
|
|
|
$context->register('CurrentToken', $token);
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-08-07 20:28:12 +00:00
|
|
|
//####################################################################//
|
|
|
|
// Loop
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-10-21 05:18:59 +00:00
|
|
|
// We need to implement a post-order traversal iteratively, to
|
|
|
|
// avoid running into stack space limits. This is pretty tricky
|
|
|
|
// to reason about, so we just manually stack-ify the recursive
|
|
|
|
// variant:
|
|
|
|
//
|
|
|
|
// function f($node) {
|
|
|
|
// foreach ($node->children as $child) {
|
|
|
|
// f($child);
|
|
|
|
// }
|
|
|
|
// validate($node);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// Thus, we will represent a stack frame as array($node,
|
|
|
|
// $is_inline, stack of children)
|
|
|
|
// e.g. array_reverse($node->children) - already processed
|
|
|
|
// children.
|
|
|
|
|
|
|
|
$parent_def = $definition->info_parent_def;
|
|
|
|
$stack = array(
|
|
|
|
array($top_node,
|
|
|
|
$parent_def->descendants_are_inline,
|
|
|
|
$parent_def->excludes, // exclusions
|
|
|
|
0)
|
|
|
|
);
|
|
|
|
|
|
|
|
while (!empty($stack)) {
|
|
|
|
list($node, $is_inline, $excludes, $ix) = array_pop($stack);
|
|
|
|
// recursive call
|
|
|
|
$go = false;
|
|
|
|
$def = empty($stack) ? $definition->info_parent_def : $definition->info[$node->name];
|
|
|
|
while (isset($node->children[$ix])) {
|
|
|
|
$child = $node->children[$ix++];
|
|
|
|
if ($child instanceof HTMLPurifier_Node_Element) {
|
|
|
|
$go = true;
|
|
|
|
$stack[] = array($node, $is_inline, $excludes, $ix);
|
|
|
|
$stack[] = array($child,
|
|
|
|
// ToDo: I don't think it matters if it's def or
|
|
|
|
// child_def, but double check this...
|
|
|
|
$is_inline || $def->descendants_are_inline,
|
|
|
|
empty($def->excludes) ? $excludes
|
|
|
|
: array_merge($excludes, $def->excludes),
|
|
|
|
0);
|
|
|
|
break;
|
2006-11-23 13:51:19 +00:00
|
|
|
}
|
2013-10-21 05:18:59 +00:00
|
|
|
};
|
|
|
|
if ($go) continue;
|
|
|
|
list($token, $d) = $node->toTokenPair();
|
|
|
|
// base case
|
|
|
|
if ($excludes_enabled && isset($excludes[$node->name])) {
|
|
|
|
$node->dead = true;
|
|
|
|
if ($e) $e->send(E_ERROR, 'Strategy_FixNesting: Node excluded');
|
2006-08-03 01:03:23 +00:00
|
|
|
} else {
|
2013-10-21 05:18:59 +00:00
|
|
|
// XXX I suppose it would be slightly more efficient to
|
|
|
|
// avoid the allocation here and have children
|
|
|
|
// strategies handle it
|
|
|
|
$children = array();
|
|
|
|
foreach ($node->children as $child) {
|
|
|
|
if (!$child->dead) $children[] = $child;
|
2007-02-04 03:53:57 +00:00
|
|
|
}
|
2013-10-21 05:18:59 +00:00
|
|
|
$result = $def->child->validateChildren($children, $config, $context);
|
|
|
|
if ($result === true) {
|
|
|
|
// nop
|
|
|
|
$node->children = $children;
|
|
|
|
} elseif ($result === false) {
|
|
|
|
$node->dead = true;
|
|
|
|
if ($e) $e->send(E_ERROR, 'Strategy_FixNesting: Node removed');
|
2006-12-06 22:38:25 +00:00
|
|
|
} else {
|
2013-10-21 05:18:59 +00:00
|
|
|
$node->children = $result;
|
|
|
|
if ($e) {
|
|
|
|
// XXX This will miss mutations of internal nodes. Perhaps defer to the child validators
|
|
|
|
if (empty($result) && !empty($children)) {
|
|
|
|
$e->send(E_ERROR, 'Strategy_FixNesting: Node contents removed');
|
|
|
|
} else if ($result != $children) {
|
|
|
|
$e->send(E_WARNING, 'Strategy_FixNesting: Node reorganized');
|
|
|
|
}
|
2007-06-26 23:43:28 +00:00
|
|
|
}
|
|
|
|
}
|
2006-07-31 03:04:57 +00:00
|
|
|
}
|
2006-07-24 01:54:25 +00:00
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-08-07 20:28:12 +00:00
|
|
|
//####################################################################//
|
|
|
|
// Post-processing
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-10-01 20:47:07 +00:00
|
|
|
// remove context variables
|
2007-02-04 03:53:57 +00:00
|
|
|
$context->destroy('IsInline');
|
2013-10-21 05:18:59 +00:00
|
|
|
$context->destroy('CurrentNode');
|
2007-06-26 23:43:28 +00:00
|
|
|
$context->destroy('CurrentToken');
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2006-08-07 20:28:12 +00:00
|
|
|
//####################################################################//
|
|
|
|
// Return
|
2013-10-21 05:18:59 +00:00
|
|
|
|
|
|
|
return HTMLPurifier_Arborize::flatten($node, $config, $context);
|
2006-07-24 01:54:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-06 09:24:59 +00:00
|
|
|
// vim: et sw=4 sts=4
|