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:
parent
7547bce31e
commit
57df816060
6 changed files with 41 additions and 7 deletions
19
dist/epub.js
vendored
19
dist/epub.js
vendored
|
@ -5495,7 +5495,7 @@ EpubCFI.prototype.findParent = function(cfi, _doc) {
|
|||
element = children[part.index];
|
||||
}
|
||||
// Element can't be found
|
||||
if(typeof element === "undefined") {
|
||||
if(!element || typeof element === "undefined") {
|
||||
console.error("No Element For", part, cfi.str);
|
||||
return false;
|
||||
}
|
||||
|
@ -5582,6 +5582,23 @@ EpubCFI.prototype.generateCfiFromTextNode = function(anchor, offset, base) {
|
|||
var steps = this.pathTo(parent);
|
||||
var path = this.generatePathComponent(steps);
|
||||
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)+")";
|
||||
};
|
||||
|
||||
|
|
2
dist/epub.js.map
vendored
2
dist/epub.js.map
vendored
File diff suppressed because one or more lines are too long
|
@ -120,7 +120,7 @@
|
|||
<script>
|
||||
// Load the 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", {
|
||||
method: "paginate",
|
||||
width: "100%",
|
||||
|
|
|
@ -56,7 +56,7 @@ gulp.task('watch', function(cb) {
|
|||
bundle('epub.js', cb);
|
||||
});
|
||||
|
||||
gulp.task('serve', ["watch"], function() {
|
||||
gulp.task('serve', function() {
|
||||
server();
|
||||
});
|
||||
|
||||
|
|
|
@ -301,7 +301,7 @@ EpubCFI.prototype.findParent = function(cfi, _doc) {
|
|||
element = children[part.index];
|
||||
}
|
||||
// Element can't be found
|
||||
if(typeof element === "undefined") {
|
||||
if(!element || typeof element === "undefined") {
|
||||
console.error("No Element For", part, cfi.str);
|
||||
return false;
|
||||
}
|
||||
|
@ -388,6 +388,23 @@ EpubCFI.prototype.generateCfiFromTextNode = function(anchor, offset, base) {
|
|||
var steps = this.pathTo(parent);
|
||||
var path = this.generatePathComponent(steps);
|
||||
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)+")";
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue