Node.data trims start/end whitespace

Emmanouil Batsis manos_lists at geekologue.com
Tue May 24 21:43:58 BST 2005


It seems node.data for nodeTypes Node.TEXT_NODE nodeType and 
Node.CDATA_SECTION_NODE missbehaves; it does not return whitespace when that 
is at the start or end of the given node text value. For example the 
following function [2] will return "hfyrhy5hyfgfdsgf" instead of "hfyrhy 5h 
yfgfdsgf" if fed with the XML tree below. Can someone please confirm whether 
this is a bug for me to submit?

[1] <root><element>hfyrhy <![CDATA[5h ]]>yf</element>gfdsgf</root>

// [2]

Sarissa.getText = function(oNode, deep){
    var s = "";
    var nodes = oNode.childNodes;
    for(var i=0; i < nodes.length; i++){
        var node = nodes[i];
        var nodeType = node.nodeType;
        if(nodeType == Node.TEXT_NODE || nodeType == Node.CDATA_SECTION_NODE){
            alert("node.data: '"+node.data+"'");
            s += node.data;
        }
        else if(deep == true
                    && (nodeType == Node.ELEMENT_NODE
                        || nodeType == Node.DOCUMENT_NODE
                        || nodeType == Node.DOCUMENT_FRAGMENT_NODE)){
            s += Sarissa.getText(node, true);
        };
    };
    return s;
};




More information about the kfm-devel mailing list