's and a stack for 's
//
//
//
for($i=1;$i<$total;$i++) {
//echo "Checking TAG $i\n";
if (!isset($res[$i]->tag)) {
continue;
}
$tag = strtoupper($res[$i]->tag);
if ($tag{0} != '/') { // it's not a close tag..
if (!isset($stack[$tag])) {
$npos = $stack[$tag]['pos'] = 0;
} else {
$npos = ++$stack[$tag]['pos'];
}
$stack[$tag][$npos] = $i;
continue;
}
//echo "GOT END TAG: {$res[$i]->tag}\n";
$tag = substr($tag,1);
if (!isset($stack[$tag]['pos'])) {
continue; // unmatched
}
$npos = $stack[$tag]['pos'];
if (!isset($stack[$tag][$npos])) {
// stack is empty!!!
continue;
}
// alias closer to opener..
$this->tokens[$stack[$tag][$npos]]->close = &$this->tokens[$i];
$stack[$tag]['pos']--;
// take it off the stack so no one else uses it!!!
unset($stack[$tag][$npos]);
if ($stack[$tag]['pos'] < 0) {
// too many closes - just ignore it..
$stack[$tag]['pos'] = 0;
}
continue;
// new entry on stack..
}
// create a dummy close for the end
$i = $total;
$this->tokens[$i] = new HTML_Template_Flexy_Token;
$this->tokens[$i]->id = $total;
$this->tokens[0]->close = &$this->tokens[$i];
// now is it possible to connect children...
// now we need to GLOBALIZE!! -
}
/**
* Build the child array for each element.
* RECURSIVE FUNCTION!!!!
*
* does not move tokens, just aliases the child nodes into the token array.
*
* @param int id of node to add children to.
*
* @access public
*/
function buildChildren($id)
{
$base = &$this->tokens[$id];
$base->children = array();
$start = $base->id +1;
$end = $base->close->id;
for ($i=$start; $i<$end; $i++) {
//echo "{$base->id}:{$base->tag} ADDING {$i}{$_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i]->tag}
";
//if ($base->id == 1176) {
// echo "";print_r($_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i]);
// }
$base->children[] = &$this->tokens[$i];
if (isset($this->tokens[$i]->close)) {
// if the close id is greater than my id - ignore it! -
if ($this->tokens[$i]->close->id > $end) {
continue;
}
$this->buildChildren($i);
$i = $this->tokens[$i]->close->id;
}
}
}
/**
* Locates Flexy:startchildren etc. if it is used.
* and returns the base of the tree. (eg. otherwise token[0].
*
* @return HTML_Template_Flexy_Token (base of tree.)
* @access public
*/
function returnStart() {
foreach(array_keys($this->tokens) as $i) {
switch(true) {
case isset($this->tokens[$i]->ucAttributes['FLEXYSTART']):
case isset($this->tokens[$i]->ucAttributes['FLEXY:START']):
$this->tokens[$i]->removeAttribute('FLEXY:START');
$this->tokens[$i]->removeAttribute('FLEXYSTART');
return $this->tokens[$i];
case isset($this->tokens[$i]->ucAttributes['FLEXYSTARTCHILDREN']):
case isset($this->tokens[$i]->ucAttributes['FLEXY:STARTCHILDREN']):
$this->tokens[0]->children = $this->tokens[$i]->children;
return $this->tokens[0];
}
}
return $this->tokens[0];
}
}