1
0
Fork 0
mirror of https://github.com/futurepress/epub.js.git synced 2025-10-04 15:09:16 +02:00

Ignore a node when finding the cfi path

This commit is contained in:
fchasen 2015-12-09 23:12:20 -05:00
parent 7547bce31e
commit 57df816060
6 changed files with 41 additions and 7 deletions

19
dist/epub.js vendored
View file

@ -5495,7 +5495,7 @@ EpubCFI.prototype.findParent = function(cfi, _doc) {
element = children[part.index]; element = children[part.index];
} }
// Element can't be found // Element can't be found
if(typeof element === "undefined") { if(!element || typeof element === "undefined") {
console.error("No Element For", part, cfi.str); console.error("No Element For", part, cfi.str);
return false; return false;
} }
@ -5582,6 +5582,23 @@ EpubCFI.prototype.generateCfiFromTextNode = function(anchor, offset, base) {
var steps = this.pathTo(parent); var steps = this.pathTo(parent);
var path = this.generatePathComponent(steps); var path = this.generatePathComponent(steps);
var index = 1 + (2 * Array.prototype.indexOf.call(parent.childNodes, anchor)); var index = 1 + (2 * Array.prototype.indexOf.call(parent.childNodes, anchor));
var ignoreClass = 'annotator-hl';
var needsIgnoring = parent.classList.contains(ignoreClass);
var sibling = parent.previousSibling;
if (!needsIgnoring) {
parent = parent.parentNode;
if (sibling.nodeType === Node.TEXT_NODE) {
// If the previous sibling is a text node, join the offset
offset = sibling.textContent.length + offset
index = 1 + (2 * Array.prototype.indexOf.call(parent.childNodes, sibling));
} else {
// Otherwise just ignore the node by getting the path to its parent
index = 1 + (2 * Array.prototype.indexOf.call(parent.childNodes, anchor.parentNode));
}
}
return "epubcfi(" + base + "!" + path + "/"+index+":"+(offset || 0)+")"; return "epubcfi(" + base + "!" + path + "/"+index+":"+(offset || 0)+")";
}; };

2
dist/epub.js.map vendored

File diff suppressed because one or more lines are too long

View file

@ -120,7 +120,7 @@
<script> <script>
// Load the opf // Load the opf
// var book = ePub("https://s3.amazonaws.com/moby-dick/OPS/package.opf"); // var book = ePub("https://s3.amazonaws.com/moby-dick/OPS/package.opf");
var book = ePub("../books/alice.epub"); var book = ePub("../books/moby-dick.epub");
var rendition = book.renderTo("viewer", { var rendition = book.renderTo("viewer", {
method: "paginate", method: "paginate",
width: "100%", width: "100%",

View file

@ -56,7 +56,7 @@ gulp.task('watch', function(cb) {
bundle('epub.js', cb); bundle('epub.js', cb);
}); });
gulp.task('serve', ["watch"], function() { gulp.task('serve', function() {
server(); server();
}); });

View file

@ -301,7 +301,7 @@ EpubCFI.prototype.findParent = function(cfi, _doc) {
element = children[part.index]; element = children[part.index];
} }
// Element can't be found // Element can't be found
if(typeof element === "undefined") { if(!element || typeof element === "undefined") {
console.error("No Element For", part, cfi.str); console.error("No Element For", part, cfi.str);
return false; return false;
} }
@ -388,6 +388,23 @@ EpubCFI.prototype.generateCfiFromTextNode = function(anchor, offset, base) {
var steps = this.pathTo(parent); var steps = this.pathTo(parent);
var path = this.generatePathComponent(steps); var path = this.generatePathComponent(steps);
var index = 1 + (2 * Array.prototype.indexOf.call(parent.childNodes, anchor)); var index = 1 + (2 * Array.prototype.indexOf.call(parent.childNodes, anchor));
var ignoreClass = 'annotator-hl';
var needsIgnoring = parent.classList.contains(ignoreClass);
var sibling = parent.previousSibling;
if (!needsIgnoring) {
parent = parent.parentNode;
if (sibling.nodeType === Node.TEXT_NODE) {
// If the previous sibling is a text node, join the offset
offset = sibling.textContent.length + offset
index = 1 + (2 * Array.prototype.indexOf.call(parent.childNodes, sibling));
} else {
// Otherwise just ignore the node by getting the path to its parent
index = 1 + (2 * Array.prototype.indexOf.call(parent.childNodes, anchor.parentNode));
}
}
return "epubcfi(" + base + "!" + path + "/"+index+":"+(offset || 0)+")"; return "epubcfi(" + base + "!" + path + "/"+index+":"+(offset || 0)+")";
}; };