From 3e6bcb7a0f9df7c0f88207965d364c7ae2db948e Mon Sep 17 00:00:00 2001 From: "Edward Z. Yang" Date: Wed, 19 Jul 2006 00:21:21 +0000 Subject: [PATCH] Implemented good behavior structure for fixNesting. Goes into infinite loop if bad stuff is passed. Remove dud test and note which tests need to be added. Also, we're only running one test at a time to ease debugging. git-svn-id: http://htmlpurifier.org/svnroot/html_purifier/trunk@57 48356398-32a2-884e-a903-53898d9a118a --- PureHTMLDefinition.php | 41 ++++++++++++++++++++++++++++++++---- tests/PureHTMLDefinition.php | 19 +++++++---------- 2 files changed, 45 insertions(+), 15 deletions(-) diff --git a/PureHTMLDefinition.php b/PureHTMLDefinition.php index 08769ba7..db4bfc77 100644 --- a/PureHTMLDefinition.php +++ b/PureHTMLDefinition.php @@ -326,15 +326,48 @@ class PureHTMLDefinition function fixNesting($tokens) { if (empty($this->info)) $this->loadData(); - /*$to_next_node = 0; // defines how much further to scroll to get - // to next node. + // insert implicit "parent" node, will be removed at end + array_unshift($tokens, new MF_StartTag('div')); + $tokens[] = new MF_EndTag('div'); - for ($i = 0, $size = count($tokens) ; $i < $size; $i += $to_next_node) { + 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++; + if ($depth == 1) continue; + } elseif ($tokens[$j]->type == 'end') { + $depth--; + if ($depth == 0) break; + } + $child_tokens[] = $tokens[$j]; } - }*/ + + // have DTD child def validate children + $element_def = $this->info[$tokens[$i]->name]; + $result = $element_def->child_def->validateChildren($child_tokens); + + // process result + if ($result === true) { + + // leave the nodes as is, scroll to next node + $i++; + while ($i < $size and $tokens[$i]->type != 'start') { + $i++; + } + + } + + } + + // remove implicit divs + array_shift($tokens); + array_pop($tokens); + + return $tokens; } diff --git a/tests/PureHTMLDefinition.php b/tests/PureHTMLDefinition.php index 76340d98..a66ae83e 100644 --- a/tests/PureHTMLDefinition.php +++ b/tests/PureHTMLDefinition.php @@ -413,17 +413,14 @@ class Test_PureHTMLDefinition extends UnitTestCase new MF_EndTag('b'), ); - // illegal in - $inputs[3] = array( - new MF_StartTag('a'), - - new MF_EndTag('a') - ); - $expect[3] = array( - new MF_StartTag('a'), - - new MF_EndTag('a') - ); + // need test of empty set that's required, resulting in removal of node + + // need test of cascading removal (if possible) + + // ! cover all child element conditions + + // execute only one test at a time: + $inputs = array( $inputs[0] ); foreach ($inputs as $i => $input) { $result = $this->def->fixNesting($input);