annotate annotator_files/lib/range.js @ 3:6356e78ccf5c

new version contains Annotator JS files to be used with FilesystemSite.
author casties
date Thu, 05 Apr 2012 19:37:27 +0200
parents
children 6979313586cf
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
1 var Range;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
2
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
3 Range = {};
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
4
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
5 Range.sniff = function(r) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
6 if (r.commonAncestorContainer != null) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
7 return new Range.BrowserRange(r);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
8 } else if (typeof r.start === "string") {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
9 return new Range.SerializedRange(r);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
10 } else if (r.start && typeof r.start === "object") {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
11 return new Range.NormalizedRange(r);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
12 } else {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
13 console.error(_t("Could not sniff range type"));
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
14 return false;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
15 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
16 };
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
17
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
18 Range.BrowserRange = (function() {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
19
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
20 function BrowserRange(obj) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
21 this.commonAncestorContainer = obj.commonAncestorContainer;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
22 this.startContainer = obj.startContainer;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
23 this.startOffset = obj.startOffset;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
24 this.endContainer = obj.endContainer;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
25 this.endOffset = obj.endOffset;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
26 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
27
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
28 BrowserRange.prototype.normalize = function(root) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
29 var it, node, nr, offset, p, r, _i, _len, _ref;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
30 if (this.tainted) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
31 console.error(_t("You may only call normalize() once on a BrowserRange!"));
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
32 return false;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
33 } else {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
34 this.tainted = true;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
35 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
36 r = {};
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
37 nr = {};
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
38 _ref = ['start', 'end'];
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
39 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
40 p = _ref[_i];
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
41 node = this[p + 'Container'];
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
42 offset = this[p + 'Offset'];
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
43 if (!((node != null) && (offset != null))) return false;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
44 if (node.nodeType === 1) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
45 it = node.childNodes[offset];
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
46 node = it || node.childNodes[offset - 1];
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
47 if (node.nodeType === 1 && !node.firstChild) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
48 it = null;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
49 node = node.previousSibling;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
50 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
51 while (node.nodeType !== 3) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
52 node = node.firstChild;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
53 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
54 offset = it ? 0 : node.nodeValue.length;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
55 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
56 r[p] = node;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
57 r[p + 'Offset'] = offset;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
58 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
59 nr.start = r.startOffset > 0 ? r.start.splitText(r.startOffset) : r.start;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
60 if (r.start === r.end) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
61 if ((r.endOffset - r.startOffset) < nr.start.nodeValue.length) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
62 nr.start.splitText(r.endOffset - r.startOffset);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
63 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
64 nr.end = nr.start;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
65 } else {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
66 if (r.endOffset < r.end.nodeValue.length) r.end.splitText(r.endOffset);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
67 nr.end = r.end;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
68 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
69 nr.commonAncestor = this.commonAncestorContainer;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
70 while (nr.commonAncestor.nodeType !== 1) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
71 nr.commonAncestor = nr.commonAncestor.parentNode;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
72 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
73 return new Range.NormalizedRange(nr);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
74 };
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
75
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
76 BrowserRange.prototype.serialize = function(root, ignoreSelector) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
77 return this.normalize(root).serialize(root, ignoreSelector);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
78 };
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
79
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
80 return BrowserRange;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
81
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
82 })();
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
83
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
84 Range.NormalizedRange = (function() {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
85
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
86 function NormalizedRange(obj) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
87 this.commonAncestor = obj.commonAncestor;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
88 this.start = obj.start;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
89 this.end = obj.end;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
90 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
91
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
92 NormalizedRange.prototype.normalize = function(root) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
93 return this;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
94 };
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
95
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
96 NormalizedRange.prototype.limit = function(bounds) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
97 var nodes, parent, startParents, _i, _len, _ref;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
98 nodes = $.grep(this.textNodes(), function(node) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
99 return node.parentNode === bounds || $.contains(bounds, node.parentNode);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
100 });
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
101 if (!nodes.length) return null;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
102 this.start = nodes[0];
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
103 this.end = nodes[nodes.length - 1];
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
104 startParents = $(this.start).parents();
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
105 _ref = $(this.end).parents();
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
106 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
107 parent = _ref[_i];
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
108 if (startParents.index(parent) !== -1) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
109 this.commonAncestor = parent;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
110 break;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
111 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
112 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
113 return this;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
114 };
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
115
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
116 NormalizedRange.prototype.serialize = function(root, ignoreSelector) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
117 var end, serialization, start;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
118 serialization = function(node, isEnd) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
119 var n, nodes, offset, origParent, textNodes, xpath, _i, _len;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
120 if (ignoreSelector) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
121 origParent = $(node).parents(":not(" + ignoreSelector + ")").eq(0);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
122 } else {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
123 origParent = $(node).parent();
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
124 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
125 xpath = origParent.xpath(root)[0];
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
126 textNodes = origParent.textNodes();
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
127 nodes = textNodes.slice(0, textNodes.index(node));
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
128 offset = 0;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
129 for (_i = 0, _len = nodes.length; _i < _len; _i++) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
130 n = nodes[_i];
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
131 offset += n.nodeValue.length;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
132 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
133 if (isEnd) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
134 return [xpath, offset + node.nodeValue.length];
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
135 } else {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
136 return [xpath, offset];
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
137 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
138 };
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
139 start = serialization(this.start);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
140 end = serialization(this.end, true);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
141 return new Range.SerializedRange({
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
142 start: start[0],
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
143 end: end[0],
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
144 startOffset: start[1],
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
145 endOffset: end[1]
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
146 });
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
147 };
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
148
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
149 NormalizedRange.prototype.text = function() {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
150 var node;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
151 return ((function() {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
152 var _i, _len, _ref, _results;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
153 _ref = this.textNodes();
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
154 _results = [];
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
155 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
156 node = _ref[_i];
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
157 _results.push(node.nodeValue);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
158 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
159 return _results;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
160 }).call(this)).join('');
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
161 };
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
162
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
163 NormalizedRange.prototype.textNodes = function() {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
164 var end, start, textNodes, _ref;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
165 textNodes = $(this.commonAncestor).textNodes();
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
166 _ref = [textNodes.index(this.start), textNodes.index(this.end)], start = _ref[0], end = _ref[1];
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
167 return $.makeArray(textNodes.slice(start, end + 1 || 9e9));
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
168 };
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
169
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
170 NormalizedRange.prototype.toRange = function() {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
171 var range;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
172 range = document.createRange();
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
173 range.setStartBefore(this.start);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
174 range.setEndAfter(this.end);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
175 return range;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
176 };
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
177
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
178 return NormalizedRange;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
179
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
180 })();
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
181
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
182 Range.SerializedRange = (function() {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
183
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
184 function SerializedRange(obj) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
185 this.start = obj.start;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
186 this.startOffset = obj.startOffset;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
187 this.end = obj.end;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
188 this.endOffset = obj.endOffset;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
189 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
190
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
191 SerializedRange.prototype._nodeFromXPath = function(xpath) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
192 var customResolver, evaluateXPath, namespace, node, segment;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
193 evaluateXPath = function(xp, nsResolver) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
194 if (nsResolver == null) nsResolver = null;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
195 return document.evaluate(xp, document, nsResolver, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
196 };
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
197 if (!$.isXMLDoc(document.documentElement)) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
198 return evaluateXPath(xpath);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
199 } else {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
200 customResolver = document.createNSResolver(document.ownerDocument === null ? document.documentElement : document.ownerDocument.documentElement);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
201 node = evaluateXPath(xpath, customResolver);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
202 if (!node) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
203 xpath = ((function() {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
204 var _i, _len, _ref, _results;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
205 _ref = xpath.split('/');
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
206 _results = [];
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
207 for (_i = 0, _len = _ref.length; _i < _len; _i++) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
208 segment = _ref[_i];
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
209 if (segment && segment.indexOf(':') === -1) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
210 _results.push(segment.replace(/^([a-z]+)/, 'xhtml:$1'));
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
211 } else {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
212 _results.push(segment);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
213 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
214 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
215 return _results;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
216 })()).join('/');
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
217 namespace = document.lookupNamespaceURI(null);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
218 customResolver = function(ns) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
219 if (ns === 'xhtml') {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
220 return namespace;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
221 } else {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
222 return document.documentElement.getAttribute('xmlns:' + ns);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
223 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
224 };
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
225 node = evaluateXPath(xpath, customResolver);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
226 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
227 return node;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
228 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
229 };
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
230
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
231 SerializedRange.prototype.normalize = function(root) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
232 var cacXPath, common, endAncestry, i, length, p, parentXPath, range, startAncestry, tn, _i, _j, _len, _len2, _ref, _ref2, _ref3;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
233 parentXPath = $(root).xpath()[0];
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
234 startAncestry = this.start.split("/");
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
235 endAncestry = this.end.split("/");
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
236 common = [];
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
237 range = {};
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
238 for (i = 0, _ref = startAncestry.length; 0 <= _ref ? i < _ref : i > _ref; 0 <= _ref ? i++ : i--) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
239 if (startAncestry[i] === endAncestry[i]) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
240 common.push(startAncestry[i]);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
241 } else {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
242 break;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
243 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
244 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
245 cacXPath = parentXPath + common.join("/");
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
246 range.commonAncestorContainer = this._nodeFromXPath(cacXPath);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
247 if (!range.commonAncestorContainer) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
248 console.error(_t("Error deserializing range: can't find XPath '") + cacXPath + _t("'. Is this the right document?"));
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
249 return null;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
250 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
251 _ref2 = ['start', 'end'];
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
252 for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
253 p = _ref2[_i];
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
254 length = 0;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
255 _ref3 = $(this._nodeFromXPath(parentXPath + this[p])).textNodes();
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
256 for (_j = 0, _len2 = _ref3.length; _j < _len2; _j++) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
257 tn = _ref3[_j];
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
258 if (length + tn.nodeValue.length >= this[p + 'Offset']) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
259 range[p + 'Container'] = tn;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
260 range[p + 'Offset'] = this[p + 'Offset'] - length;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
261 break;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
262 } else {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
263 length += tn.nodeValue.length;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
264 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
265 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
266 }
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
267 return new Range.BrowserRange(range).normalize(root);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
268 };
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
269
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
270 SerializedRange.prototype.serialize = function(root, ignoreSelector) {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
271 return this.normalize(root).serialize(root, ignoreSelector);
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
272 };
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
273
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
274 SerializedRange.prototype.toObject = function() {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
275 return {
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
276 start: this.start,
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
277 startOffset: this.startOffset,
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
278 end: this.end,
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
279 endOffset: this.endOffset
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
280 };
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
281 };
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
282
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
283 return SerializedRange;
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
284
6356e78ccf5c new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff changeset
285 })();