in reply to tracking where I am in a tree structure

I think what you are going to want to do is to make the $withclickiness variable no longer optional in descend_htmltree, since you are checking it in htmltree_to_html you really dont need to anyway as it will already be set or 0.

Also, I think you actually meant to be passing that along your recursive call to descend_htmltree anyway, otherwise it would only do it for the top node.

Then once this is all in place, add a third variable, which will be the node-id you are looking for. Each time you pass the $node_id to the next recursive call, it will get appended with the $node_counter, which is just the depth in the current child group. This should then produce the node_id you are looking for.

Here is some code, I could not test is as you supplied no test data to test against. But it should work, let me know if you have any problems with it.

sub descend_htmltree {   my $node = shift;   my $withclickiness = shift; my $node_id = shift; my $node_counter = 0;   foreach my $tmpnode (@{$node}) { $node_counter++     if(ref($tmpnode) eq 'HASH') {       my $nodeid = "${node_id}.$node_counter"; # Magic code to generat +e node's position in tree       $htmloutput .= "<div style='border: thin solid #bbbbbb' onDblCli +ck=\"alert('you clicked $nodeid')\">" if($withclickiness);       $htmloutput .= "<$tmpnode->{tag}";       foreach(keys %{$tmpnode}) {         $htmloutput .= " $_=\"$tmpnode->{$_}\"" if($_ ne 'tag' && $_ n +e 'content');       }       $htmloutput .= ">";       descend_htmltree($tmpnode->{content}, $withclickiness, $current_ +node_id);       $htmloutput .= "</$tmpnode->{tag}>";       $htmloutput .= "</div>" if($withclickiness);     } else {       $htmloutput .= "$tmpnode";     }   } }
One another note, I am not 100% sure your HTML, in particular the onClick part of the DIV tag will work properly. The browser may see the entire contents of the DIV (it and all its subtrees) as all part of the same HTML container.

-stvn

Replies are listed 'Best First'.
Re^2: tracking where I am in a tree structure
by agaffney (Beadle) on Jul 22, 2004 at 22:07 UTC
    That was the type of thing I was trying to put together in my head, but for some reason it just wouldn't come together. That code seems to work perfectly, although, I haven't tested it more than 1 level deep, yet.

    As for the DIV problem, how would you suggest fixing that?
      That was the type of thing I was trying to put together in my head, but for some reason it just wouldn't come together.

      Recursive solutions always are harder to wrap your head around, they tend require more "faith" than iterative solutions, and that is not something most programmers are usually accustomed too (all relgious meanings aside of course).

      As for the DIV problem, how would you suggest fixing that?

      Well not having seen your output I am not sure, but I would suspect you could try putting the 'onClick' handler in the tag you are writing, rather than wrapping that tag. Although this may have the same effect when you are dealing with things like TABLE, TR, UL tags, since they too are container tags which sometimes "enclose" their descendents. The other option, and I am not sure if this would be appropriate or not, is to only wrap your leaf nodes with the "onClick" since they are likely to be text nodes and the like (although things like BR and HR will fall into that category too). Hard to really say without either having some test data, or knowing more about what you want the UI to do in the end.

      -stvn
        The purpose of this whole thing is to build an online HTML editor with simple support for TABLEs, Ps, SPANs, Bs, As, and a few pre-defined CSS styles.

        After the HTML is parsed, it is regenerated from the tree for viewing in a preview window. I have the DIVs because I want a way to highlight the various elements of the page. When the user clicks (or double clicks) inside one of these DIVs, it will refresh the page with another parameter ($nodeid) that tells which element in the page they selected.

        I am doing all of this because my boss (uses Frontpage and loves the <BLINK> tag) insists on being able to edit content on our new HTML::Mason based site.
        Recursive solutions always are harder to wrap your head around, they tend require more "faith" than iterative solutions, and that is not something most programmers are usually accustomed too (all relgious meanings aside of course).

        Piffle. Recursion is neither magical nor obscure, especially when dealing with recursive data structures (like parse trees).

        --
        F o x t r o t U n i f o r m
        Found a typo in this node? /msg me
        % man 3 strfry