CSS TreeView adapter – too much recursion

Late in 2006 a semi-skunk works projects was released called CSS Control Adapter Toolkit. The idea was simple enough, the controls shipped in ASP.NET produced some dreadful HTML so by cunningly re-using the framework intended to support "non-standard HTML" devices you can reroute controls to use their CSS friendly equivalents. One such control given the CSS friendly treatment was the TreeView control. To support all the various clients side features of the Adapter toolkit it ships a JavaScript file that you should included in your sites. My colleague made the switch on our site and after a few teething problems the CSS version of our tree views seemed to be working…guess what is coming. We started to get reports that it wasn’t working on FireFox, the whole page seemed to lock up and wouldn’t respond to mouse clicks. I investigated the issue and soon realised that the JavaScript engine in Firefox was complaining about too much recursion. Normally I wouldn’t have been too surprised to see recursion in something processing a tree but in this case I knew that the tree wasn’t very deep at all, at most it would be six branches, surely that isn’t enough to send the recursion stack into melt down? I looked at the JavaScript shipped and spotted the problem (source file). When you process a tree the typical algorithm is to process all the peer nodes one at a time and for each node you pass the child nodes into the same function, hence the recursion. However for a tree six deep it should only ever have six contexts in the stack. However, this code was passing the next sibling back into the function therefore if you have 20 siblings then your stack was at least 20 deep and that’s before you consider the number children and their children, etc. So very quickly you can see that the stack could overflow. As elegant as it may seem to pass the sibling back into the function is not the right thing to do! I rewrote the code to simply enumerate over the siblings and only pass their children back into the function, unsurprisingly the engine managed to cope with six calls. If anyone has this problem and wants the code than I can post it, although it really is very simple fix.

3 thoughts on “CSS TreeView adapter – too much recursion

  1. IonutC March 1, 2011 / 6:30 am

    Hi, I encountered the same problem with the tree, and I thought I solved the problem, but I got another one, related to the expand-collapse.
    I really think I didn’t solve the recursion issue right, so can you help me with this? Can you show the way you solved the issue regarding the recursion?

    Thanks, Ionut

    • pauliom March 1, 2011 / 8:58 am

      Hi, i’m not in the office at the moment, but will try and grab the code for you sometime in the next 24hrs. Watch this space.

Leave a Reply to IonutC Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s