Changeset 4:6979313586cf in OKFNAnnotator (for Zope) for annotator_files/lib/range.js


Ignore:
Timestamp:
Aug 27, 2012, 5:05:38 PM (12 years ago)
Author:
casties
Branch:
default
Message:

new version of annotator.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • annotator_files/lib/range.js

    r3 r4  
    1 var Range;
     1// Generated by CoffeeScript 1.3.3
     2var Range,
     3  __hasProp = {}.hasOwnProperty,
     4  __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
    25
    36Range = {};
     
    1619};
    1720
     21Range.nodeFromXPath = function(xpath, root) {
     22  var customResolver, evaluateXPath, namespace, node, segment;
     23  if (root == null) {
     24    root = document;
     25  }
     26  evaluateXPath = function(xp, nsResolver) {
     27    if (nsResolver == null) {
     28      nsResolver = null;
     29    }
     30    return document.evaluate('.' + xp, root, nsResolver, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
     31  };
     32  if (!$.isXMLDoc(document.documentElement)) {
     33    return evaluateXPath(xpath);
     34  } else {
     35    customResolver = document.createNSResolver(document.ownerDocument === null ? document.documentElement : document.ownerDocument.documentElement);
     36    node = evaluateXPath(xpath, customResolver);
     37    if (!node) {
     38      xpath = ((function() {
     39        var _i, _len, _ref, _results;
     40        _ref = xpath.split('/');
     41        _results = [];
     42        for (_i = 0, _len = _ref.length; _i < _len; _i++) {
     43          segment = _ref[_i];
     44          if (segment && segment.indexOf(':') === -1) {
     45            _results.push(segment.replace(/^([a-z]+)/, 'xhtml:$1'));
     46          } else {
     47            _results.push(segment);
     48          }
     49        }
     50        return _results;
     51      })()).join('/');
     52      namespace = document.lookupNamespaceURI(null);
     53      customResolver = function(ns) {
     54        if (ns === 'xhtml') {
     55          return namespace;
     56        } else {
     57          return document.documentElement.getAttribute('xmlns:' + ns);
     58        }
     59      };
     60      node = evaluateXPath(xpath, customResolver);
     61    }
     62    return node;
     63  }
     64};
     65
     66Range.RangeError = (function(_super) {
     67
     68  __extends(RangeError, _super);
     69
     70  function RangeError(type, message, parent) {
     71    this.type = type;
     72    this.message = message;
     73    this.parent = parent != null ? parent : null;
     74    RangeError.__super__.constructor.call(this, this.message);
     75  }
     76
     77  return RangeError;
     78
     79})(Error);
     80
    1881Range.BrowserRange = (function() {
    1982
     
    41104      node = this[p + 'Container'];
    42105      offset = this[p + 'Offset'];
    43       if (!((node != null) && (offset != null))) return false;
     106      if (!((node != null) && (offset != null))) {
     107        return false;
     108      }
    44109      if (node.nodeType === 1) {
    45110        it = node.childNodes[offset];
     
    64129      nr.end = nr.start;
    65130    } else {
    66       if (r.endOffset < r.end.nodeValue.length) r.end.splitText(r.endOffset);
     131      if (r.endOffset < r.end.nodeValue.length) {
     132        r.end.splitText(r.endOffset);
     133      }
    67134      nr.end = r.end;
    68135    }
     
    99166      return node.parentNode === bounds || $.contains(bounds, node.parentNode);
    100167    });
    101     if (!nodes.length) return null;
     168    if (!nodes.length) {
     169      return null;
     170    }
    102171    this.start = nodes[0];
    103172    this.end = nodes[nodes.length - 1];
     
    189258  }
    190259
    191   SerializedRange.prototype._nodeFromXPath = function(xpath) {
    192     var customResolver, evaluateXPath, namespace, node, segment;
    193     evaluateXPath = function(xp, nsResolver) {
    194       if (nsResolver == null) nsResolver = null;
    195       return document.evaluate(xp, document, nsResolver, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
    196     };
    197     if (!$.isXMLDoc(document.documentElement)) {
    198       return evaluateXPath(xpath);
    199     } else {
    200       customResolver = document.createNSResolver(document.ownerDocument === null ? document.documentElement : document.ownerDocument.documentElement);
    201       node = evaluateXPath(xpath, customResolver);
     260  SerializedRange.prototype.normalize = function(root) {
     261    var contains, length, node, p, range, tn, _i, _j, _len, _len1, _ref, _ref1;
     262    range = {};
     263    _ref = ['start', 'end'];
     264    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
     265      p = _ref[_i];
     266      try {
     267        node = Range.nodeFromXPath(this[p], root);
     268      } catch (e) {
     269        throw new Range.RangeError(p, ("Error while finding " + p + " node: " + this[p] + ": ") + e, e);
     270      }
    202271      if (!node) {
    203         xpath = ((function() {
    204           var _i, _len, _ref, _results;
    205           _ref = xpath.split('/');
    206           _results = [];
    207           for (_i = 0, _len = _ref.length; _i < _len; _i++) {
    208             segment = _ref[_i];
    209             if (segment && segment.indexOf(':') === -1) {
    210               _results.push(segment.replace(/^([a-z]+)/, 'xhtml:$1'));
    211             } else {
    212               _results.push(segment);
    213             }
    214           }
    215           return _results;
    216         })()).join('/');
    217         namespace = document.lookupNamespaceURI(null);
    218         customResolver = function(ns) {
    219           if (ns === 'xhtml') {
    220             return namespace;
    221           } else {
    222             return document.documentElement.getAttribute('xmlns:' + ns);
    223           }
    224         };
    225         node = evaluateXPath(xpath, customResolver);
    226       }
    227       return node;
    228     }
    229   };
    230 
    231   SerializedRange.prototype.normalize = function(root) {
    232     var cacXPath, common, endAncestry, i, length, p, parentXPath, range, startAncestry, tn, _i, _j, _len, _len2, _ref, _ref2, _ref3;
    233     parentXPath = $(root).xpath()[0];
    234     startAncestry = this.start.split("/");
    235     endAncestry = this.end.split("/");
    236     common = [];
    237     range = {};
    238     for (i = 0, _ref = startAncestry.length; 0 <= _ref ? i < _ref : i > _ref; 0 <= _ref ? i++ : i--) {
    239       if (startAncestry[i] === endAncestry[i]) {
    240         common.push(startAncestry[i]);
    241       } else {
    242         break;
    243       }
    244     }
    245     cacXPath = parentXPath + common.join("/");
    246     range.commonAncestorContainer = this._nodeFromXPath(cacXPath);
    247     if (!range.commonAncestorContainer) {
    248       console.error(_t("Error deserializing range: can't find XPath '") + cacXPath + _t("'. Is this the right document?"));
    249       return null;
    250     }
    251     _ref2 = ['start', 'end'];
    252     for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
    253       p = _ref2[_i];
     272        throw new Range.RangeError(p, "Couldn't find " + p + " node: " + this[p]);
     273      }
    254274      length = 0;
    255       _ref3 = $(this._nodeFromXPath(parentXPath + this[p])).textNodes();
    256       for (_j = 0, _len2 = _ref3.length; _j < _len2; _j++) {
    257         tn = _ref3[_j];
     275      _ref1 = $(node).textNodes();
     276      for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
     277        tn = _ref1[_j];
    258278        if (length + tn.nodeValue.length >= this[p + 'Offset']) {
    259279          range[p + 'Container'] = tn;
     
    264284        }
    265285      }
    266     }
     286      if (!(range[p + 'Offset'] != null)) {
     287        throw new Range.RangeError("" + p + "offset", "Couldn't find offset " + this[p + 'Offset'] + " in element " + this[p]);
     288      }
     289    }
     290    contains = !(document.compareDocumentPosition != null) ? function(a, b) {
     291      return a.contains(b);
     292    } : function(a, b) {
     293      return a.compareDocumentPosition(b) & 16;
     294    };
     295    $(range.startContainer).parents().reverse().each(function() {
     296      if (contains(this, range.endContainer)) {
     297        range.commonAncestorContainer = this;
     298        return false;
     299      }
     300    });
    267301    return new Range.BrowserRange(range).normalize(root);
    268302  };
Note: See TracChangeset for help on using the changeset viewer.