Firefox のnode.childNodes には、テキストノードが含まれる件

http://oshiete.goo.ne.jp/qa/8007184.html

で、Firefox の、node.childNodes を、テキストノードを返さないように上書きしようと思ったらできなかった。きっと何か代わりのものがあるかもしれない。
なので名前を変えた。

Element.prototype.__defineGetter__ ('childElements', function () {
  var node = this.firstChild, result = [];
  for (; node ; node = node.nextSibling)
    if (node.nodeType == Node.ELEMENT_NODE)
      result.push (node);
  return result;
});

そういえば、
node.nextElementSibling
node.previousElementSibling
とか使えたのにしばらく応用してない。というか存在を忘れてた。


ずいぶん前に見たコードを探してみた。のちの何かの為にここにも置いておこう!
もちろん、私がこういうのを書けるはずもない。

(function (proto, getElement) {
    proto.__defineGetter__ ('firstElementChild', function () {
        return getElement.call (this, 'firstChild', 'nextSibling');
    });
    
    proto.__defineGetter__ ('lastElementChild', function () {
        return getElement.call (this, 'lastChild', 'previousSibling');
    });
    
    proto.__defineGetter__ ('previousElementSibling', function () {
        return getElement.call (this, 'previousSibling');
    });
    
    proto.__defineGetter__ ('nextElementSibling', function () {
        return getElement.call (this, 'nextSibling');
    });
    
    proto.__defineGetter__ ('childElementCount', function () {
        for (var count = 0, n = this.firstElementChild; n; n = n.nextElementSibling, count++);
        return count;
    });
} )(
    Element.prototype,
    
    function (start, direction) {
        for (var node = this[start]; node; node = node[direction || start]) {
            if (node.nodeType == Node.ELEMENT_NODE) {
                break;
            }
        }
        return node;
    }
);