child.tagName is guaranteed to be uppercase.

So just uppercase tag at the top level when it is first passed in to the function.

I notice that you're using this without a 'g.' prefix. I like that myself, and it would be fine with me if you dropped the g. stuff everywhere, especially now that the code is so much simpler.