W3C home > Mailing lists > Public > www-dom@w3.org > January to March 1999

Re: "Empty" Text Nodes

From: <uche.ogbuji@fourthought.com>
Date: Fri, 26 Feb 1999 22:36:02 -0700
Message-Id: <199902270536.WAA00893@malatesta.local>
To: www-dom@w3.org
> On the other hand it's no big effort transforming a DOM tree with
> whitespace text nodes into another one without them ...

Correct.  In fact, 4DOM comes with a simple helper function: DOM.Ext.Strip, 
which does this using the Level 2 NodeIterators.  Here's the function, if 
anyone is interested.  It's in Python, but it uses the strict DOM API and so 
should be quite readable to a non-Pythoneer:

def Strip(start_node, skip_elements=["PRE", "STYLE", "SCRIPT"]):
    remove_list = []
    snit = start_node.createSelectiveTreeIterator(DOM.DOMIF.Node.TW_TEXT)
    curr_node = snit.nextNode()
    while curr_node:
        #first of all make sure it is not inside one of the skip_elements
        ancestor = curr_node
        while ancestor != start_node:
            if ancestor.getNodeType() == DOM.DOMIF.Node.ELEMENT_NODE:
                if ancestor.getNodeName() in skip_elements:
                    break
            ancestor = ancestor.getParentNode()
        else:
            curr_node.setData(string.strip(curr_node.getData()))
            if not len(curr_node.getData()):
                remove_list.append(curr_node)
        curr_node = snit.nextNode()
    for node_to_remove in remove_list:
        node_to_remove.getParentNode().removeChild(node_to_remove)
    return start_node


-- 
Uche Ogbuji
FourThought LLC, IT Consultants
uche.ogbuji@fourthought.com	(970)481-0805
Software engineering, project management, Intranets and Extranets
http://FourThought.com		http://OpenTechnology.org
Received on Saturday, 27 February 1999 00:32:42 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Friday, 22 June 2012 06:13:46 GMT