Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hasChildNodes() of attribute nodes return different results for Chrome and Firefox

Check this jsfiddle separately in Chrome and Firefox: http://jsfiddle.net/9aE2p/1/

Also pasting the same code here:

var xmlStr = '<?xml version="1.0" encoding="UTF-8"?><abc abc_attr="abc_attr_value"><abc_child abc_child_attr="abc_child_attr_value1"/><abc_child abc_child_attr="abc_child_attr_value2"/></abc>';

var parser = new DOMParser();
var xmlDoc = parser.parseFromString(xmlStr, "text/xml");

var path = 'abc/@abc_attr';

var nodes = xmlDoc.evaluate(path, xmlDoc, null, XPathResult.ANY_TYPE, null);

var result = nodes.iterateNext();

while (result) {
    var textContent = '<BR>result.textContent: "' + result.textContent + '"';
    var nodeType = '<BR>result.nodeType: "' + result.nodeType + '"';
    var resultHasChildren = '<BR>result.hasChildNodes(): ' + result.hasChildNodes();

    document.write(nodeType);
    document.write(textContent);
    document.write(resultHasChildren);

    result = nodes.iterateNext();
}

What I am noticing is that hasChildNodes() returns false for Firefox and true for Chrome.

If a nodeType is an attribute node, then in Chrome it has a child node which has the actual value. But in Firefox, it doesn't have any child node and the value is stored inside attribute node itself.

I am curious to know is there is any documentation on this subtle difference?

I already checked the following documents but couldn't find any such specifics:

https://developer.mozilla.org/en-US/docs/DOM/Node.hasChildNodes

http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-810594187

like image 791
bits Avatar asked Nov 21 '12 18:11

bits


1 Answers

As just posted in my comment, I believe this has to do with changes to the way attributes are implemented in DOM4 in contrast to previous version.

  • Link to Attr interface documentation on Mozilla Developer Network, MDN

In previous versions the Attr interface extended Node. This was changed so you cannot use Node methods anymore. However, the name and value properties still exist.

like image 151
FK82 Avatar answered Oct 31 '22 15:10

FK82