3.2.0

Question on reusing code of the forum

Hello Alex!

Some time ago there was a forum posting where the code for the selectNodes and selectSingleNode emulation was posted by you.

Is this snippet copyrighted or where is it derived from?

The Forum message was:

http://www.activewidgets.com/javascript.forum.4524.20/new-bug-with-firefox-1.html

if (window.XPathEvaluator) {

    var xpath = new XPathEvaluator();

    var element = Element.prototype;
    var attr = Attr.prototype;
    var doc = Document.prototype;

    delete element.text;
    delete attr.text;

    element.selectNodes = function (path) {
        var result = xpath.evaluate(path, this, this.ownerDocument._ns, 7, null);
        var i, nodes = [];
        for (i=0; i<result.snapshotLength; i++) {
            nodes[i] = result.snapshotItem(i);
            nodes[i].text = nodes[i].firstChild ? nodes[i].firstChild.nodeValue : "";
        }
        return nodes;
    };

    element.selectSingleNode = function (path) {
       var node = xpath.evaluate(path, this, this.ownerDocument._ns, 9, null).singleNodeValue;
       dump('element.selectSingleNode(path): ' + path + '\n');
       if ( node != null ) node.text = node.firstChild ? node.firstChild.nodeValue : "";
       return node;
    };

    doc.selectSingleNode = function (path) {
        dump('doc.selectSingleNode(path): ' + path + '\n');
        var node = xpath.evaluate(path, this, this._ns, 9, null).singleNodeValue;
        if ( node != null ) node.text = node.firstChild ? node.firstChild.nodeValue : "";
        return node;
    };

    doc.selectNodes = function (path) {
        var result = xpath.evaluate(path, this, this._ns, 7, null);
        var i, nodes = [];
        for (i=0; i<result.snapshotLength; i++) {
            nodes[i] = result.snapshotItem(i);
            nodes[i].text = nodes[i].firstChild ? nodes[i].firstChild.nodeValue : "";
        }
        return nodes;
    };

}

Can I use the code for something else (project which is published under the LGPL)?

Pleas give me a short feedback.

Thank you.

Best Regards...
Dietrich
November 30,
Dietrich,

this is the original code which was released as part of ActiveWidgets 1.0 under GPL and ActiveWidgets commercial license. So you can use it freely under GPL terms (but not LGPL).
Alex (ActiveWidgets)
November 30,
Thank you for answering.

Just for your interrest: Here is a code part which uses nearly the same code and claims to be copyright by Microsoft:

http://research.microsoft.com/users/som/blog/AtlasCompat.htm

function selectNodes(doc,path,c){
    var xpath =new XPathEvaluator();
    var result =xpath.evaluate(path,doc.documentElement,null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null);
    var nodeList =new Array();
    for(var i =0;i <result.snapshotLength;i++){
      nodeList.add(result.snapshotItem(i));
    }
    return nodeList;
  }
  function selectSingleNode(doc,path,c){
    path +='[1]';
    var nodes =selectNodes(doc,path,c);
    if (nodes.length !=0){
      return nodes[0];
    }
    else {
      return null;
    }
  }
  w.XMLDocument.prototype.selectNodes =function(path){
    return selectNodes(this,path,this);
  }
  w.XMLDocument.prototype.selectSingleNode =function(path){
    return selectSingleNode(this,path,null);
  }


And here is my code part which I want to give to a lgpl project. Maybe you rethink allowing me to use the code?


if (window.XPathEvaluator) {

  var xpath = new XPathEvaluator();

  if (!Element.prototype.selectSingleNode) {
    Element.prototype.selectSingleNode = function (path) {
      return xpath.evaluate(path, this, this.ownerDocument._ns, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
    };
  }
  
  if (!Element.prototype.selectNodes) {
    Element.prototype.selectNodes = function (path) {
      var result = xpath.evaluate(path, this, this.ownerDocument._ns, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
      var i, nodes = [];
  
      for (i=0; i<result.snapshotLength; i++) {
        nodes[i] = result.snapshotItem(i);
      };
  
      return nodes;
    };
  }
  
  if (!Document.prototype.selectSingleNode) {
    Document.prototype.selectSingleNode = function (path) {
      return xpath.evaluate(path, this, this._ns, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
    };
  }
  
  if (!Document.prototype.selectNodes) {
    Document.prototype.selectNodes = function (path) {
      var result = xpath.evaluate(path, this, this._ns, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
      var i, nodes = [];

      for (i=0; i<result.snapshotLength; i++) {
        nodes[i] = result.snapshotItem(i);
      };
      
      return nodes;
    };
  }

  Element.prototype.__defineGetter__('text',
    function() {
      var text = '';
        for (var i=0; i<this.childNodes.length; i++) {
          text += this.childNodes[i].text;
        };
        return text;
    }
  );
  
  Element.prototype.__lookupGetter__('text');

  Attr.prototype.__defineGetter__('text', function(){return this.nodeValue});
  Attr.prototype.__lookupGetter__('text');

  Text.prototype.__defineGetter__('text', function(){return this.nodeValue});
  Text.prototype.__lookupGetter__('text');
  
}





Dietrich
November 30,
Dietrich,

this is very common functionality so I am not surprised there are many similar implementations. I just wanted to say that AW code is the original one (not copied from somewhere).

In your case - I think this functionality is very simple and can be re-implemented in 10 minutes. However copying parts of AW code into LGPL project is NOT ok.
Alex (ActiveWidgets)
November 30,
OK.

So to clarify it, please just say yes or no to the last code block.

Thank you...
Dietrich
November 30,
Yes.
Alex (ActiveWidgets)
November 30,

This topic is archived.

See also:


Back to support forum