mirror of
https://github.com/ezyang/htmlpurifier.git
synced 2025-03-23 14:27:02 +00:00
Profusely comment FixNesting.
git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@183 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
parent
7e2693fdea
commit
9cec089f97
@ -42,31 +42,53 @@ class HTMLPurifier_Strategy_FixNesting extends HTMLPurifier_Strategy
|
|||||||
|
|
||||||
function execute($tokens) {
|
function execute($tokens) {
|
||||||
|
|
||||||
// insert implicit "parent" node, will be removed at end
|
//####################################################################//
|
||||||
|
// Pre-processing
|
||||||
|
|
||||||
|
// insert implicit "parent" node, will be removed at end.
|
||||||
|
// ! we might want to move this to configuration
|
||||||
|
// DEFINITION CALL
|
||||||
$parent_name = $this->definition->info_parent;
|
$parent_name = $this->definition->info_parent;
|
||||||
array_unshift($tokens, new HTMLPurifier_Token_Start($parent_name));
|
array_unshift($tokens, new HTMLPurifier_Token_Start($parent_name));
|
||||||
$tokens[] = new HTMLPurifier_Token_End($parent_name);
|
$tokens[] = new HTMLPurifier_Token_End($parent_name);
|
||||||
|
|
||||||
|
//####################################################################//
|
||||||
|
// Loop initialization
|
||||||
|
|
||||||
// stack that contains the indexes of all parents,
|
// stack that contains the indexes of all parents,
|
||||||
// $stack[count($stack)-1] being the current parent
|
// $stack[count($stack)-1] being the current parent
|
||||||
$stack = array();
|
$stack = array();
|
||||||
|
|
||||||
// stack that contains all elements that are excluded
|
// stack that contains all elements that are excluded
|
||||||
|
// same structure as $stack, but it is only populated when an element
|
||||||
|
// with exclusions is processed, i.e. there won't be empty exclusions.
|
||||||
$exclude_stack = array();
|
$exclude_stack = array();
|
||||||
|
|
||||||
|
//####################################################################//
|
||||||
|
// Loop
|
||||||
|
|
||||||
|
// iterate through all start nodes. Determining the start node
|
||||||
|
// is complicated so it has been omitted from the loop construct
|
||||||
for ($i = 0, $size = count($tokens) ; $i < $size; ) {
|
for ($i = 0, $size = count($tokens) ; $i < $size; ) {
|
||||||
|
|
||||||
|
//################################################################//
|
||||||
|
// Gather information on children
|
||||||
|
|
||||||
|
// child token accumulator
|
||||||
$child_tokens = array();
|
$child_tokens = array();
|
||||||
|
|
||||||
// scroll to the end of this node, and report number
|
// scroll to the end of this node, report number, and collect
|
||||||
|
// all children
|
||||||
for ($j = $i, $depth = 0; ; $j++) {
|
for ($j = $i, $depth = 0; ; $j++) {
|
||||||
if ($tokens[$j]->type == 'start') {
|
if ($tokens[$j]->type == 'start') {
|
||||||
$depth++;
|
$depth++;
|
||||||
// skip token assignment on first iteration
|
// skip token assignment on first iteration, this is the
|
||||||
|
// token we currently are on
|
||||||
if ($depth == 1) continue;
|
if ($depth == 1) continue;
|
||||||
} elseif ($tokens[$j]->type == 'end') {
|
} elseif ($tokens[$j]->type == 'end') {
|
||||||
$depth--;
|
$depth--;
|
||||||
// skip token assignment on last iteration
|
// skip token assignment on last iteration, this is the
|
||||||
|
// end token of the token we're currently on
|
||||||
if ($depth == 0) break;
|
if ($depth == 0) break;
|
||||||
}
|
}
|
||||||
$child_tokens[] = $tokens[$j];
|
$child_tokens[] = $tokens[$j];
|
||||||
@ -75,12 +97,16 @@ class HTMLPurifier_Strategy_FixNesting extends HTMLPurifier_Strategy
|
|||||||
// $i is index of start token
|
// $i is index of start token
|
||||||
// $j is index of end token
|
// $j is index of end token
|
||||||
|
|
||||||
|
//################################################################//
|
||||||
|
// Gather information on parent
|
||||||
|
|
||||||
// calculate parent information
|
// calculate parent information
|
||||||
if ($count = count($stack)) {
|
if ($count = count($stack)) {
|
||||||
$parent_index = $stack[$count-1];
|
$parent_index = $stack[$count-1];
|
||||||
$parent_name = $tokens[$parent_index]->name;
|
$parent_name = $tokens[$parent_index]->name;
|
||||||
$parent_def = $this->definition->info[$parent_name];
|
$parent_def = $this->definition->info[$parent_name];
|
||||||
} else {
|
} else {
|
||||||
|
// unknown info, it won't be used anyway
|
||||||
$parent_index = $parent_name = $parent_def = null;
|
$parent_index = $parent_name = $parent_def = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -88,26 +114,36 @@ class HTMLPurifier_Strategy_FixNesting extends HTMLPurifier_Strategy
|
|||||||
if (isset($parent_def)) {
|
if (isset($parent_def)) {
|
||||||
$context = $parent_def->type;
|
$context = $parent_def->type;
|
||||||
} else {
|
} else {
|
||||||
|
// generally found in specialized elements like UL
|
||||||
$context = 'unknown';
|
$context = 'unknown';
|
||||||
}
|
}
|
||||||
|
|
||||||
// determine whether or not element is excluded
|
//################################################################//
|
||||||
|
// 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.
|
||||||
$excluded = false;
|
$excluded = false;
|
||||||
if (!empty($exclude_stack)) {
|
if (!empty($exclude_stack)) {
|
||||||
foreach ($exclude_stack as $lookup) {
|
foreach ($exclude_stack as $lookup) {
|
||||||
if (isset($lookup[$tokens[$i]->name])) {
|
if (isset($lookup[$tokens[$i]->name])) {
|
||||||
$excluded = true;
|
$excluded = true;
|
||||||
|
// no need to continue processing
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//################################################################//
|
||||||
|
// Perform child validation
|
||||||
|
|
||||||
if ($excluded) {
|
if ($excluded) {
|
||||||
|
// there is an exclusion, remove the entire node
|
||||||
$result = false;
|
$result = false;
|
||||||
} else {
|
} else {
|
||||||
// DEFINITION CALL
|
// DEFINITION CALL
|
||||||
$def = $this->definition->info[$tokens[$i]->name];
|
$def = $this->definition->info[$tokens[$i]->name];
|
||||||
|
|
||||||
$child_def = $def->child;
|
$child_def = $def->child;
|
||||||
|
|
||||||
// have DTD child def validate children
|
// have DTD child def validate children
|
||||||
@ -117,9 +153,12 @@ class HTMLPurifier_Strategy_FixNesting extends HTMLPurifier_Strategy
|
|||||||
$excludes = $def->excludes;
|
$excludes = $def->excludes;
|
||||||
}
|
}
|
||||||
|
|
||||||
// process result
|
// $result is now a bool or array
|
||||||
|
|
||||||
|
//################################################################//
|
||||||
|
// Process result by interpreting $result
|
||||||
|
|
||||||
if ($result === true) {
|
if ($result === true) {
|
||||||
|
|
||||||
// leave the node as is
|
// leave the node as is
|
||||||
|
|
||||||
// register start token as a parental node start
|
// register start token as a parental node start
|
||||||
@ -132,13 +171,15 @@ class HTMLPurifier_Strategy_FixNesting extends HTMLPurifier_Strategy
|
|||||||
$i++;
|
$i++;
|
||||||
|
|
||||||
} elseif($result === false) {
|
} elseif($result === false) {
|
||||||
|
// remove entire node
|
||||||
|
|
||||||
|
// calculate length of inner tokens and current tokens
|
||||||
$length = $j - $i + 1;
|
$length = $j - $i + 1;
|
||||||
|
|
||||||
// remove entire node
|
// perform removal
|
||||||
array_splice($tokens, $i, $length);
|
array_splice($tokens, $i, $length);
|
||||||
|
|
||||||
// change size
|
// update size
|
||||||
$size -= $length;
|
$size -= $length;
|
||||||
|
|
||||||
// there is no start token to register,
|
// there is no start token to register,
|
||||||
@ -150,14 +191,19 @@ class HTMLPurifier_Strategy_FixNesting extends HTMLPurifier_Strategy
|
|||||||
$i = $parent_index;
|
$i = $parent_index;
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
// PROJECTED OPTIMIZATION: Process all children elements before
|
||||||
|
// reprocessing parent node.
|
||||||
|
|
||||||
|
} else {
|
||||||
|
// replace node with $result
|
||||||
|
|
||||||
|
// calculate length of inner tokens
|
||||||
$length = $j - $i - 1;
|
$length = $j - $i - 1;
|
||||||
|
|
||||||
// replace node with $result
|
// perform replacement
|
||||||
array_splice($tokens, $i + 1, $length, $result);
|
array_splice($tokens, $i + 1, $length, $result);
|
||||||
|
|
||||||
// change size
|
// update size
|
||||||
$size -= $length;
|
$size -= $length;
|
||||||
$size += count($result);
|
$size += count($result);
|
||||||
|
|
||||||
@ -172,6 +218,9 @@ class HTMLPurifier_Strategy_FixNesting extends HTMLPurifier_Strategy
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//################################################################//
|
||||||
|
// Scroll to next start node
|
||||||
|
|
||||||
// We assume, at this point, that $i is the index of the token
|
// We assume, at this point, that $i is the index of the token
|
||||||
// that is the first possible new start point for a node.
|
// that is the first possible new start point for a node.
|
||||||
|
|
||||||
@ -192,10 +241,16 @@ class HTMLPurifier_Strategy_FixNesting extends HTMLPurifier_Strategy
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// remove implicit divs
|
//####################################################################//
|
||||||
|
// Post-processing
|
||||||
|
|
||||||
|
// remove implicit parent tokens at the beginning and end
|
||||||
array_shift($tokens);
|
array_shift($tokens);
|
||||||
array_pop($tokens);
|
array_pop($tokens);
|
||||||
|
|
||||||
|
//####################################################################//
|
||||||
|
// Return
|
||||||
|
|
||||||
return $tokens;
|
return $tokens;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user