Commit dcab98ab authored by csillag's avatar csillag

Added position-based anchoir rettachment strategy. Now we can handle DOM structure changes.

parent 2766e46d
/* /*
** Annotator 1.2.5-dev-4109850 ** Annotator 1.2.5-dev-f066e48
** https://github.com/okfn/annotator/ ** https://github.com/okfn/annotator/
** **
** Copyright 2012 Aron Carroll, Rufus Pollock, and Nick Stenning. ** Copyright 2012 Aron Carroll, Rufus Pollock, and Nick Stenning.
** Dual licensed under the MIT and GPLv3 licenses. ** Dual licensed under the MIT and GPLv3 licenses.
** https://github.com/okfn/annotator/blob/master/LICENSE ** https://github.com/okfn/annotator/blob/master/LICENSE
** **
** Built at: 2013-03-03 22:22:50Z ** Built at: 2013-03-03 22:52:56Z
*/ */
(function() { (function() {
...@@ -872,13 +872,12 @@ ...@@ -872,13 +872,12 @@
}; };
Annotator.prototype.findAnchorFromXPathRangeSelector = function(target) { Annotator.prototype.findAnchorFromXPathRangeSelector = function(target) {
var currentQuote, endOffset, nRange, quoteSelector, savedQuote, selector, startOffset; var currentQuote, endOffset, nRange, savedQuote, selector, startOffset;
selector = this.findSelector(target.selector, "xpath range"); selector = this.findSelector(target.selector, "xpath range");
if (selector != null) { if (selector == null) return null;
try { try {
nRange = this.getNormalizedRangeFromXPathRangeSelector(selector); nRange = this.getNormalizedRangeFromXPathRangeSelector(selector);
quoteSelector = this.findSelector(target.selector, "context+quote"); savedQuote = this.getQuoteForTarget(target);
savedQuote = quoteSelector != null ? quoteSelector.exact : void 0;
if (savedQuote != null) { if (savedQuote != null) {
startOffset = (this.domMapper.getInfoForNode(nRange.start)).start; startOffset = (this.domMapper.getInfoForNode(nRange.start)).start;
endOffset = (this.domMapper.getInfoForNode(nRange.end)).end; endOffset = (this.domMapper.getInfoForNode(nRange.end)).end;
...@@ -888,6 +887,8 @@ ...@@ -888,6 +887,8 @@
console.log("Saved quote is '" + savedQuote + "'."); console.log("Saved quote is '" + savedQuote + "'.");
console.log("Current quote is '" + currentQuote + "'."); console.log("Current quote is '" + currentQuote + "'.");
return null; return null;
} else {
} }
} else { } else {
...@@ -898,17 +899,39 @@ ...@@ -898,17 +899,39 @@
console.log("Could not apply XPath selector to current document. Structure must have changed."); console.log("Could not apply XPath selector to current document. Structure must have changed.");
return null; return null;
} else { } else {
console.log(exception.stack);
throw exception; throw exception;
} }
} }
} };
Annotator.prototype.findAnchorFromPositionSelector = function(target) {
var browserRange, currentQuote, mappings, savedQuote, selector;
selector = this.findSelector(target.selector, "position");
if (selector == null) return null;
savedQuote = this.getQuoteForTarget(target);
if (savedQuote != null) {
savedQuote = this.getQuoteForTarget(target);
currentQuote = this.domMapper.getContentForRange(selector.start, selector.end);
if (currentQuote !== savedQuote) {
console.log("Could not apply position selector to current document, because the quote has changed.");
console.log("Saved quote is '" + savedQuote + "'.");
console.log("Current quote is '" + currentQuote + "'.");
return null; return null;
} else {
}
} else {
}
mappings = this.domMapper.getMappingsForRange(selector.start, selector.end);
browserRange = new Range.BrowserRange(mappings.range);
return browserRange.normalize();
}; };
Annotator.prototype.findAnchor = function(target) { Annotator.prototype.findAnchor = function(target) {
var anchor; var anchor;
anchor = this.findAnchorFromXPathRangeSelector(target); anchor = this.findAnchorFromXPathRangeSelector(target);
anchor || (anchor = this.findAnchorFromPositionSelector(target));
return anchor; return anchor;
}; };
...@@ -923,6 +946,7 @@ ...@@ -923,6 +946,7 @@
_ref2 = annotation.target; _ref2 = annotation.target;
for (_k = 0, _len3 = _ref2.length; _k < _len3; _k++) { for (_k = 0, _len3 = _ref2.length; _k < _len3; _k++) {
t = _ref2[_k]; t = _ref2[_k];
try {
r = this.findAnchor(t); r = this.findAnchor(t);
if (r != null) { if (r != null) {
normedRanges.push(r); normedRanges.push(r);
...@@ -930,6 +954,11 @@ ...@@ -930,6 +954,11 @@
console.log("Could not find anchor for annotation target '" + t.id + "' (for annotation '" + annotation.id + "')."); console.log("Could not find anchor for annotation target '" + t.id + "' (for annotation '" + annotation.id + "').");
this.publish('findAnchorFail', [annotation, t]); this.publish('findAnchorFail', [annotation, t]);
} }
} catch (exception) {
if (exception.stack != null) console.log(exception.stack);
console.log(exception.message);
console.log(exception);
}
} }
annotation.currentQuote = []; annotation.currentQuote = [];
annotation.currentRanges = []; annotation.currentRanges = [];
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment