Skip to content

Commit

Permalink
update epubcfi from master
Browse files Browse the repository at this point in the history
  • Loading branch information
fchasen committed Dec 4, 2015
1 parent 645d64d commit c13f698
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 12 deletions.
40 changes: 36 additions & 4 deletions dist/epub.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/epub.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/epub.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/epub.min.js.map

Large diffs are not rendered by default.

38 changes: 35 additions & 3 deletions src/epubcfi.js
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,25 @@ EpubCFI.prototype.generateXpathFromSteps = function(steps) {
return xpath.join("/");
};

EpubCFI.prototype.generateQueryFromSteps = function(steps) {
var query = ["html"];

steps.forEach(function(step){
var position = step.index + 1;

if(step.id){
query.push("#" + step.id);
} else if(step.type === "text") {
// unsupported in querySelector
// query.push("text()[" + position + "]");
} else {
query.push("*:nth-child(" + position + ")");
}
});

return query.join(">");
};


EpubCFI.prototype.generateRangeFromCfi = function(cfi, _doc) {
var doc = _doc || document;
Expand All @@ -472,6 +491,8 @@ EpubCFI.prototype.generateRangeFromCfi = function(cfi, _doc) {
var xpath;
var startContainer;
var textLength;
var query;
var startContainerParent;

if(typeof cfi === 'string') {
cfi = this.parse(cfi);
Expand All @@ -483,11 +504,22 @@ EpubCFI.prototype.generateRangeFromCfi = function(cfi, _doc) {
return false;
}

xpath = this.generateXpathFromSteps(cfi.steps);

// Get the terminal step
lastStep = cfi.steps[cfi.steps.length-1];
startContainer = doc.evaluate(xpath, doc, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;

if(typeof document.evaluate != 'undefined') {
xpath = this.generateXpathFromSteps(cfi.steps);
startContainer = doc.evaluate(xpath, doc, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
} else {
// Get the query string
query = this.generateQueryFromSteps(cfi.steps);
// Find the containing element
startContainerParent = doc.querySelector(query);
// Find the text node within that element
if(startContainerParent && lastStep.type == "text") {
startContainer = startContainerParent.childNodes[lastStep.index];
}
}

if(!startContainer) {
return null;
Expand Down
2 changes: 1 addition & 1 deletion src/locations.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ Locations.prototype.cfiFromLocation = function(loc){
return cfi;
};

EPUBJS.Locations.prototype.cfiFromPercentage = function(value){
Locations.prototype.cfiFromPercentage = function(value){
var percentage = (value > 1) ? value / 100 : value; // Normalize value to 0-1
var loc = Math.ceil(this.total * percentage);

Expand Down

0 comments on commit c13f698

Please sign in to comment.