SitePoint Sponsor

User Tag List

Results 1 to 1 of 1
  1. #1
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Empty Text Nodes in Gecko

    Here's another couple of functions that I've come up with that help me with my DOM scripting. These functions were born when I had problems with object heirarchy doing funny things to my scripts in Gecko browsers. The problem was as follows. Let's use the following HTML snippet for my example
    Code:
    <div>
    <span>Hello</span>
    <span>World!</span>
    </div>
    To IE's DOM, this HTML looks like this
    Code:
    <DIV>
    -> <SPAN>
       -> #textNode(Hello)
    -> <SPAN>
       -> #textNode(World!)
    But, to Gecko's DOM, it looks like this
    Code:
    <DIV>
    -> #textNode(\n)
    -> <SPAN>
       -> #textNode(Hello)
    -> #textNode(\n)
    -> <SPAN>
       -> #textNode(World!)
    -> #textNode(\n)
    In otherwords, Gecko's DOM inteprets lines breaks in the HTML as 'empty' textNodes, that acutally contain a newline character. Now, it should be apparent that in the Gecko DOM, there are more children to the DIV than in the IE DOM (3 in this example), and that's where things get messed up. The following two functions will remove all 'emtpy' textNodes so that your references (i.e this.firstChild, or this.previousSibling) will behave properly (<-- subjective use of 'properly' here ) in Gecko. (I'm gonna use the PHP tags here for the nifty syntax coloring)
    PHP Code:
    // Inits parsing to remove empty textnodes for Gecko
    function initTNparse(o) {
        
    window.TNcount 1;
        while (
    window.TNcount != 0) {
            
    window.TNcount 0;
            
    TNparse(o);
            }
        }
    // Recursive funtions that removes the textnodes
    function TNparse(oNode) {
        var 
    j=0;
        while( 
    j<oNode.childNodes.length) {
            var 
    currNode oNode.childNodes[j];
            if (
    currNode.childNodes.length>0)
                
    TNparse(currNode);
            if (
    currNode.nodeType == && !currNode.nodeValue.search(/^\s+$/)) {
                
    oNode.removeChild(currNode);
                
    window.TNcount++;
                }
            
    j++;
            }
        }

    <
    body onLoad="initTNparse(document.body);"
    I know what you're thinking. Why the looping initializer function if the parsing function is recursive? Good question. I don't know either. For some reason I couldn't get TNparse() to properly recur in Gecko.

    If you don't want to remove all the empty textNodes from the entire document, then just pass the initTNparse() function a different object.

    Happy (DOM) scripting!
    Last edited by beetle; Sep 24, 2002 at 09:52.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev





Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •