mirror of
https://github.com/futurepress/epub.js.git
synced 2025-10-05 15:32:55 +02:00
Allow Cors withCredentials, decode paths for zips, update uri parsing
This commit is contained in:
parent
7649612ec5
commit
f1c4b8c917
14 changed files with 357 additions and 193 deletions
164
build/epub.js
164
build/epub.js
|
@ -1711,7 +1711,7 @@ global.RSVP = requireModule('rsvp');
|
|||
'use strict';
|
||||
|
||||
var EPUBJS = EPUBJS || {};
|
||||
EPUBJS.VERSION = "0.1.8";
|
||||
EPUBJS.VERSION = "0.1.9";
|
||||
|
||||
EPUBJS.plugins = EPUBJS.plugins || {};
|
||||
|
||||
|
@ -1794,6 +1794,7 @@ EPUBJS.Book = function(options){
|
|||
restore: false,
|
||||
reload : false,
|
||||
goto : false,
|
||||
withCredentials: false,
|
||||
styles : {}
|
||||
});
|
||||
|
||||
|
@ -2050,21 +2051,21 @@ EPUBJS.Book.prototype.loadXml = function(url){
|
|||
} else if(this.settings.contained) {
|
||||
return this.zip.getXml(url, this.settings.encoding);
|
||||
}else{
|
||||
return EPUBJS.core.request(url, 'xml');
|
||||
return EPUBJS.core.request(url, 'xml', this.settings.withCredentials);
|
||||
}
|
||||
};
|
||||
|
||||
//-- Turns a url into a absolute url
|
||||
EPUBJS.Book.prototype.urlFrom = function(bookPath){
|
||||
var absolute = bookPath.search("://") != -1,
|
||||
fromRoot = bookPath[0] == "/",
|
||||
var uri = EPUBJS.core.uri(bookPath),
|
||||
absolute = uri.protocol,
|
||||
fromRoot = uri.path[0] == "/",
|
||||
location = window.location,
|
||||
//-- Get URL orgin, try for native or combine
|
||||
origin = location.origin || location.protocol + "//" + location.host,
|
||||
baseTag = document.getElementsByTagName('base'),
|
||||
base;
|
||||
|
||||
// if(bookPath[bookPath.length - 1] != "/") bookPath += "/";
|
||||
|
||||
//-- Check is Base tag is set
|
||||
|
||||
|
@ -2073,33 +2074,18 @@ EPUBJS.Book.prototype.urlFrom = function(bookPath){
|
|||
}
|
||||
|
||||
//-- 1. Check if url is absolute
|
||||
if(absolute){
|
||||
return bookPath;
|
||||
if(uri.protocol){
|
||||
return uri.origin + uri.path;
|
||||
}
|
||||
|
||||
//-- 2. Check if url starts with /, add base url
|
||||
if(!absolute && fromRoot){
|
||||
if(base) {
|
||||
return base + bookPath;
|
||||
} else {
|
||||
return origin + bookPath;
|
||||
}
|
||||
return (base || origin) + uri.path;
|
||||
}
|
||||
|
||||
//-- 3. Or find full path to url and add that
|
||||
if(!absolute && !fromRoot){
|
||||
|
||||
//-- go back
|
||||
if(bookPath.slice(0, 3) == "../"){
|
||||
return EPUBJS.core.resolveUrl(base || location.pathname, bookPath);
|
||||
}
|
||||
|
||||
if(base) {
|
||||
return base + bookPath;
|
||||
} else {
|
||||
return origin + EPUBJS.core.folder(location.pathname) + bookPath;
|
||||
}
|
||||
|
||||
return EPUBJS.core.resolveUrl(base || location.pathname, uri.path);
|
||||
}
|
||||
|
||||
};
|
||||
|
@ -2122,8 +2108,9 @@ EPUBJS.Book.prototype.unarchive = function(bookPath){
|
|||
|
||||
//-- Checks if url has a .epub or .zip extension
|
||||
EPUBJS.Book.prototype.isContained = function(bookUrl){
|
||||
var dot = bookUrl.lastIndexOf('.'),
|
||||
ext = bookUrl.slice(dot+1);
|
||||
var uri = EPUBJS.core.uri(bookUrl),
|
||||
dot = uri.filename.lastIndexOf('.'),
|
||||
ext = uri.filename.slice(dot+1);
|
||||
|
||||
if(ext && (ext == "epub" || ext == "zip")){
|
||||
return true;
|
||||
|
@ -2687,7 +2674,7 @@ EPUBJS.core.getEls = function(classes) {
|
|||
return document.getElementsByClassName(classes);
|
||||
};
|
||||
|
||||
EPUBJS.core.request = function(url, type) {
|
||||
EPUBJS.core.request = function(url, type, withCredentials) {
|
||||
var supportsURL = window.URL;
|
||||
var BLOB_RESPONSE = supportsURL ? "blob" : "arraybuffer";
|
||||
|
||||
|
@ -2705,8 +2692,10 @@ EPUBJS.core.request = function(url, type) {
|
|||
value: function xmlHttpRequestOverrideMimeType(mimeType) {}
|
||||
});
|
||||
}
|
||||
|
||||
xhr.open("GET", url);
|
||||
if(withCredentials) {
|
||||
xhr.withCredentials = true;
|
||||
}
|
||||
xhr.open("GET", url, true);
|
||||
xhr.onreadystatechange = handler;
|
||||
|
||||
if(type == 'blob'){
|
||||
|
@ -2749,7 +2738,10 @@ EPUBJS.core.request = function(url, type) {
|
|||
|
||||
deferred.resolve(r);
|
||||
} else {
|
||||
deferred.reject(this);
|
||||
deferred.reject({
|
||||
message : this.response,
|
||||
stack : new Error().stack
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2781,10 +2773,12 @@ EPUBJS.core.uri = function(url){
|
|||
origin : '',
|
||||
directory : '',
|
||||
base : '',
|
||||
filename : ''
|
||||
filename : '',
|
||||
href : url
|
||||
},
|
||||
doubleSlash = url.indexOf('://'),
|
||||
search = url.indexOf('?'),
|
||||
withoutProtocol,
|
||||
firstSlash;
|
||||
|
||||
if(search != -1) {
|
||||
|
@ -2794,18 +2788,21 @@ EPUBJS.core.uri = function(url){
|
|||
|
||||
if(doubleSlash != -1) {
|
||||
uri.protocol = url.slice(0, doubleSlash);
|
||||
uri.path = url.slice(doubleSlash+3);
|
||||
firstSlash = uri.path.indexOf('/');
|
||||
withoutProtocol = url.slice(doubleSlash+3);
|
||||
firstSlash = withoutProtocol.indexOf('/');
|
||||
|
||||
if(firstSlash === -1) {
|
||||
uri.host = uri.path;
|
||||
uri.path = "";
|
||||
} else {
|
||||
uri.host = uri.path.slice(0, firstSlash);
|
||||
uri.host = withoutProtocol.slice(0, firstSlash);
|
||||
uri.path = withoutProtocol.slice(firstSlash);
|
||||
}
|
||||
|
||||
|
||||
uri.origin = uri.protocol + "://" + uri.host;
|
||||
|
||||
uri.directory = EPUBJS.core.folder(uri.path.replace(uri.host, ''));
|
||||
uri.directory = EPUBJS.core.folder(uri.path);
|
||||
|
||||
uri.base = uri.origin + uri.directory;
|
||||
// return origin;
|
||||
|
@ -3109,7 +3106,7 @@ EPUBJS.EpubCFI.prototype.getElement = function(cfi, _doc) {
|
|||
|
||||
// sections.shift(); //-- html
|
||||
|
||||
while(sections.length > 0) {
|
||||
while(sections && sections.length > 0) {
|
||||
|
||||
part = sections.shift();
|
||||
|
||||
|
@ -3219,11 +3216,23 @@ EPUBJS.Parser = function(baseUrl){
|
|||
};
|
||||
|
||||
EPUBJS.Parser.prototype.container = function(containerXml){
|
||||
|
||||
//-- <rootfile full-path="OPS/package.opf" media-type="application/oebps-package+xml"/>
|
||||
var rootfile = containerXml.querySelector("rootfile"),
|
||||
fullpath = rootfile.getAttribute('full-path'),
|
||||
folder = EPUBJS.core.folder(fullpath),
|
||||
var rootfile, fullpath, folder, encoding;
|
||||
|
||||
if(!containerXml) {
|
||||
console.error("Container File Not Found");
|
||||
return;
|
||||
}
|
||||
|
||||
rootfile = containerXml.querySelector("rootfile");
|
||||
|
||||
if(!rootfile) {
|
||||
console.error("No RootFile Found");
|
||||
return;
|
||||
}
|
||||
|
||||
fullpath = rootfile.getAttribute('full-path');
|
||||
folder = EPUBJS.core.uri(fullpath).directory;
|
||||
encoding = containerXml.xmlEncoding;
|
||||
|
||||
//-- Now that we have the path we can parse the contents
|
||||
|
@ -3235,29 +3244,66 @@ EPUBJS.Parser.prototype.container = function(containerXml){
|
|||
};
|
||||
|
||||
EPUBJS.Parser.prototype.identifier = function(packageXml){
|
||||
var metadataNode = packageXml.querySelector("metadata");
|
||||
var metadataNode;
|
||||
|
||||
if(!packageXml) {
|
||||
console.error("Package File Not Found");
|
||||
return;
|
||||
}
|
||||
|
||||
metadataNode = packageXml.querySelector("metadata");
|
||||
|
||||
if(!metadataNode) {
|
||||
console.error("No Metadata Found");
|
||||
return;
|
||||
}
|
||||
|
||||
return this.getElementText(metadataNode, "identifier");
|
||||
};
|
||||
|
||||
EPUBJS.Parser.prototype.packageContents = function(packageXml, baseUrl){
|
||||
var parse = this;
|
||||
var metadataNode, manifestNode, spineNode;
|
||||
var manifest, navPath, tocPath, coverPath;
|
||||
var spineNodeIndex;
|
||||
var spine;
|
||||
var spineIndexByURL;
|
||||
|
||||
if(baseUrl) this.baseUrl = baseUrl;
|
||||
|
||||
var metadataNode = packageXml.querySelector("metadata"),
|
||||
manifestNode = packageXml.querySelector("manifest"),
|
||||
spineNode = packageXml.querySelector("spine");
|
||||
if(!packageXml) {
|
||||
console.error("Package File Not Found");
|
||||
return;
|
||||
}
|
||||
|
||||
var manifest = parse.manifest(manifestNode),
|
||||
navPath = parse.findNavPath(manifestNode),
|
||||
tocPath = parse.findTocPath(manifestNode),
|
||||
metadataNode = packageXml.querySelector("metadata");
|
||||
if(!metadataNode) {
|
||||
console.error("No Metadata Found");
|
||||
return;
|
||||
}
|
||||
|
||||
manifestNode = packageXml.querySelector("manifest");
|
||||
if(!metadataNode) {
|
||||
console.error("No Manifest Found");
|
||||
return;
|
||||
}
|
||||
|
||||
spineNode = packageXml.querySelector("spine");
|
||||
if(!metadataNode) {
|
||||
console.error("No Spine Found");
|
||||
return;
|
||||
}
|
||||
|
||||
manifest = parse.manifest(manifestNode);
|
||||
navPath = parse.findNavPath(manifestNode);
|
||||
tocPath = parse.findTocPath(manifestNode);
|
||||
coverPath = parse.findCoverPath(manifestNode);
|
||||
|
||||
var spineNodeIndex = Array.prototype.indexOf.call(spineNode.parentNode.childNodes, spineNode);
|
||||
spineNodeIndex = Array.prototype.indexOf.call(spineNode.parentNode.childNodes, spineNode);
|
||||
|
||||
var spine = parse.spine(spineNode, manifest);
|
||||
spine = parse.spine(spineNode, manifest);
|
||||
|
||||
var spineIndexByURL = {};
|
||||
spineIndexByURL = {};
|
||||
spine.forEach(function(item){
|
||||
spineIndexByURL[item.href] = item.index;
|
||||
});
|
||||
|
@ -4328,7 +4374,12 @@ EPUBJS.replace.links = function(_store, full, done, link){
|
|||
|
||||
//-- Handle replacing urls in CSS
|
||||
if(link.getAttribute("rel") === "stylesheet") {
|
||||
EPUBJS.replace.stylesheets(_store, full).then(done);
|
||||
EPUBJS.replace.stylesheets(_store, full).then(function(url, full){
|
||||
// done
|
||||
setTimeout(function(){
|
||||
done(url, full);
|
||||
}, 5); //-- Allow for css to apply before displaying chapter
|
||||
});
|
||||
}else{
|
||||
_store.getUrl(full).then(done);
|
||||
}
|
||||
|
@ -4440,10 +4491,14 @@ EPUBJS.Unarchiver.prototype.getXml = function(url, encoding){
|
|||
EPUBJS.Unarchiver.prototype.getUrl = function(url, mime){
|
||||
var unarchiver = this;
|
||||
var deferred = new RSVP.defer();
|
||||
var entry = this.zipFs.find(url);
|
||||
var decodededUrl = window.decodeURIComponent(url);
|
||||
var entry = this.zipFs.find(decodededUrl);
|
||||
var _URL = window.URL || window.webkitURL || window.mozURL;
|
||||
|
||||
if(!entry) console.error("File not found:", url);
|
||||
if(!entry) {
|
||||
console.error("File not found in the epub:", url);
|
||||
return;
|
||||
}
|
||||
|
||||
if(url in this.urlCache) {
|
||||
deferred.resolve(this.urlCache[url]);
|
||||
|
@ -4462,7 +4517,8 @@ EPUBJS.Unarchiver.prototype.getUrl = function(url, mime){
|
|||
EPUBJS.Unarchiver.prototype.getText = function(url, encoding){
|
||||
var unarchiver = this;
|
||||
var deferred = new RSVP.defer();
|
||||
var entry = this.zipFs.find(url);
|
||||
var decodededUrl = window.decodeURIComponent(url);
|
||||
var entry = this.zipFs.find(decodededUrl);
|
||||
var _URL = window.URL || window.webkitURL || window.mozURL;
|
||||
|
||||
if(!entry) console.error(url);
|
||||
|
|
4
build/epub.min.js
vendored
4
build/epub.min.js
vendored
File diff suppressed because one or more lines are too long
|
@ -1710,7 +1710,7 @@ global.RSVP = requireModule('rsvp');
|
|||
'use strict';
|
||||
|
||||
var EPUBJS = EPUBJS || {};
|
||||
EPUBJS.VERSION = "0.1.8";
|
||||
EPUBJS.VERSION = "0.1.9";
|
||||
|
||||
EPUBJS.plugins = EPUBJS.plugins || {};
|
||||
|
||||
|
@ -1793,6 +1793,7 @@ EPUBJS.Book = function(options){
|
|||
restore: false,
|
||||
reload : false,
|
||||
goto : false,
|
||||
withCredentials: false,
|
||||
styles : {}
|
||||
});
|
||||
|
||||
|
@ -2049,21 +2050,21 @@ EPUBJS.Book.prototype.loadXml = function(url){
|
|||
} else if(this.settings.contained) {
|
||||
return this.zip.getXml(url, this.settings.encoding);
|
||||
}else{
|
||||
return EPUBJS.core.request(url, 'xml');
|
||||
return EPUBJS.core.request(url, 'xml', this.settings.withCredentials);
|
||||
}
|
||||
};
|
||||
|
||||
//-- Turns a url into a absolute url
|
||||
EPUBJS.Book.prototype.urlFrom = function(bookPath){
|
||||
var absolute = bookPath.search("://") != -1,
|
||||
fromRoot = bookPath[0] == "/",
|
||||
var uri = EPUBJS.core.uri(bookPath),
|
||||
absolute = uri.protocol,
|
||||
fromRoot = uri.path[0] == "/",
|
||||
location = window.location,
|
||||
//-- Get URL orgin, try for native or combine
|
||||
origin = location.origin || location.protocol + "//" + location.host,
|
||||
baseTag = document.getElementsByTagName('base'),
|
||||
base;
|
||||
|
||||
// if(bookPath[bookPath.length - 1] != "/") bookPath += "/";
|
||||
|
||||
//-- Check is Base tag is set
|
||||
|
||||
|
@ -2072,33 +2073,18 @@ EPUBJS.Book.prototype.urlFrom = function(bookPath){
|
|||
}
|
||||
|
||||
//-- 1. Check if url is absolute
|
||||
if(absolute){
|
||||
return bookPath;
|
||||
if(uri.protocol){
|
||||
return uri.origin + uri.path;
|
||||
}
|
||||
|
||||
//-- 2. Check if url starts with /, add base url
|
||||
if(!absolute && fromRoot){
|
||||
if(base) {
|
||||
return base + bookPath;
|
||||
} else {
|
||||
return origin + bookPath;
|
||||
}
|
||||
return (base || origin) + uri.path;
|
||||
}
|
||||
|
||||
//-- 3. Or find full path to url and add that
|
||||
if(!absolute && !fromRoot){
|
||||
|
||||
//-- go back
|
||||
if(bookPath.slice(0, 3) == "../"){
|
||||
return EPUBJS.core.resolveUrl(base || location.pathname, bookPath);
|
||||
}
|
||||
|
||||
if(base) {
|
||||
return base + bookPath;
|
||||
} else {
|
||||
return origin + EPUBJS.core.folder(location.pathname) + bookPath;
|
||||
}
|
||||
|
||||
return EPUBJS.core.resolveUrl(base || location.pathname, uri.path);
|
||||
}
|
||||
|
||||
};
|
||||
|
@ -2121,8 +2107,9 @@ EPUBJS.Book.prototype.unarchive = function(bookPath){
|
|||
|
||||
//-- Checks if url has a .epub or .zip extension
|
||||
EPUBJS.Book.prototype.isContained = function(bookUrl){
|
||||
var dot = bookUrl.lastIndexOf('.'),
|
||||
ext = bookUrl.slice(dot+1);
|
||||
var uri = EPUBJS.core.uri(bookUrl),
|
||||
dot = uri.filename.lastIndexOf('.'),
|
||||
ext = uri.filename.slice(dot+1);
|
||||
|
||||
if(ext && (ext == "epub" || ext == "zip")){
|
||||
return true;
|
||||
|
@ -2686,7 +2673,7 @@ EPUBJS.core.getEls = function(classes) {
|
|||
return document.getElementsByClassName(classes);
|
||||
};
|
||||
|
||||
EPUBJS.core.request = function(url, type) {
|
||||
EPUBJS.core.request = function(url, type, withCredentials) {
|
||||
var supportsURL = window.URL;
|
||||
var BLOB_RESPONSE = supportsURL ? "blob" : "arraybuffer";
|
||||
|
||||
|
@ -2704,8 +2691,10 @@ EPUBJS.core.request = function(url, type) {
|
|||
value: function xmlHttpRequestOverrideMimeType(mimeType) {}
|
||||
});
|
||||
}
|
||||
|
||||
xhr.open("GET", url);
|
||||
if(withCredentials) {
|
||||
xhr.withCredentials = true;
|
||||
}
|
||||
xhr.open("GET", url, true);
|
||||
xhr.onreadystatechange = handler;
|
||||
|
||||
if(type == 'blob'){
|
||||
|
@ -2748,7 +2737,10 @@ EPUBJS.core.request = function(url, type) {
|
|||
|
||||
deferred.resolve(r);
|
||||
} else {
|
||||
deferred.reject(this);
|
||||
deferred.reject({
|
||||
message : this.response,
|
||||
stack : new Error().stack
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2780,10 +2772,12 @@ EPUBJS.core.uri = function(url){
|
|||
origin : '',
|
||||
directory : '',
|
||||
base : '',
|
||||
filename : ''
|
||||
filename : '',
|
||||
href : url
|
||||
},
|
||||
doubleSlash = url.indexOf('://'),
|
||||
search = url.indexOf('?'),
|
||||
withoutProtocol,
|
||||
firstSlash;
|
||||
|
||||
if(search != -1) {
|
||||
|
@ -2793,18 +2787,21 @@ EPUBJS.core.uri = function(url){
|
|||
|
||||
if(doubleSlash != -1) {
|
||||
uri.protocol = url.slice(0, doubleSlash);
|
||||
uri.path = url.slice(doubleSlash+3);
|
||||
firstSlash = uri.path.indexOf('/');
|
||||
withoutProtocol = url.slice(doubleSlash+3);
|
||||
firstSlash = withoutProtocol.indexOf('/');
|
||||
|
||||
if(firstSlash === -1) {
|
||||
uri.host = uri.path;
|
||||
uri.path = "";
|
||||
} else {
|
||||
uri.host = uri.path.slice(0, firstSlash);
|
||||
uri.host = withoutProtocol.slice(0, firstSlash);
|
||||
uri.path = withoutProtocol.slice(firstSlash);
|
||||
}
|
||||
|
||||
|
||||
uri.origin = uri.protocol + "://" + uri.host;
|
||||
|
||||
uri.directory = EPUBJS.core.folder(uri.path.replace(uri.host, ''));
|
||||
uri.directory = EPUBJS.core.folder(uri.path);
|
||||
|
||||
uri.base = uri.origin + uri.directory;
|
||||
// return origin;
|
||||
|
@ -3108,7 +3105,7 @@ EPUBJS.EpubCFI.prototype.getElement = function(cfi, _doc) {
|
|||
|
||||
// sections.shift(); //-- html
|
||||
|
||||
while(sections.length > 0) {
|
||||
while(sections && sections.length > 0) {
|
||||
|
||||
part = sections.shift();
|
||||
|
||||
|
@ -3218,11 +3215,23 @@ EPUBJS.Parser = function(baseUrl){
|
|||
};
|
||||
|
||||
EPUBJS.Parser.prototype.container = function(containerXml){
|
||||
|
||||
//-- <rootfile full-path="OPS/package.opf" media-type="application/oebps-package+xml"/>
|
||||
var rootfile = containerXml.querySelector("rootfile"),
|
||||
fullpath = rootfile.getAttribute('full-path'),
|
||||
folder = EPUBJS.core.folder(fullpath),
|
||||
var rootfile, fullpath, folder, encoding;
|
||||
|
||||
if(!containerXml) {
|
||||
console.error("Container File Not Found");
|
||||
return;
|
||||
}
|
||||
|
||||
rootfile = containerXml.querySelector("rootfile");
|
||||
|
||||
if(!rootfile) {
|
||||
console.error("No RootFile Found");
|
||||
return;
|
||||
}
|
||||
|
||||
fullpath = rootfile.getAttribute('full-path');
|
||||
folder = EPUBJS.core.uri(fullpath).directory;
|
||||
encoding = containerXml.xmlEncoding;
|
||||
|
||||
//-- Now that we have the path we can parse the contents
|
||||
|
@ -3234,29 +3243,66 @@ EPUBJS.Parser.prototype.container = function(containerXml){
|
|||
};
|
||||
|
||||
EPUBJS.Parser.prototype.identifier = function(packageXml){
|
||||
var metadataNode = packageXml.querySelector("metadata");
|
||||
var metadataNode;
|
||||
|
||||
if(!packageXml) {
|
||||
console.error("Package File Not Found");
|
||||
return;
|
||||
}
|
||||
|
||||
metadataNode = packageXml.querySelector("metadata");
|
||||
|
||||
if(!metadataNode) {
|
||||
console.error("No Metadata Found");
|
||||
return;
|
||||
}
|
||||
|
||||
return this.getElementText(metadataNode, "identifier");
|
||||
};
|
||||
|
||||
EPUBJS.Parser.prototype.packageContents = function(packageXml, baseUrl){
|
||||
var parse = this;
|
||||
var metadataNode, manifestNode, spineNode;
|
||||
var manifest, navPath, tocPath, coverPath;
|
||||
var spineNodeIndex;
|
||||
var spine;
|
||||
var spineIndexByURL;
|
||||
|
||||
if(baseUrl) this.baseUrl = baseUrl;
|
||||
|
||||
var metadataNode = packageXml.querySelector("metadata"),
|
||||
manifestNode = packageXml.querySelector("manifest"),
|
||||
spineNode = packageXml.querySelector("spine");
|
||||
if(!packageXml) {
|
||||
console.error("Package File Not Found");
|
||||
return;
|
||||
}
|
||||
|
||||
var manifest = parse.manifest(manifestNode),
|
||||
navPath = parse.findNavPath(manifestNode),
|
||||
tocPath = parse.findTocPath(manifestNode),
|
||||
metadataNode = packageXml.querySelector("metadata");
|
||||
if(!metadataNode) {
|
||||
console.error("No Metadata Found");
|
||||
return;
|
||||
}
|
||||
|
||||
manifestNode = packageXml.querySelector("manifest");
|
||||
if(!metadataNode) {
|
||||
console.error("No Manifest Found");
|
||||
return;
|
||||
}
|
||||
|
||||
spineNode = packageXml.querySelector("spine");
|
||||
if(!metadataNode) {
|
||||
console.error("No Spine Found");
|
||||
return;
|
||||
}
|
||||
|
||||
manifest = parse.manifest(manifestNode);
|
||||
navPath = parse.findNavPath(manifestNode);
|
||||
tocPath = parse.findTocPath(manifestNode);
|
||||
coverPath = parse.findCoverPath(manifestNode);
|
||||
|
||||
var spineNodeIndex = Array.prototype.indexOf.call(spineNode.parentNode.childNodes, spineNode);
|
||||
spineNodeIndex = Array.prototype.indexOf.call(spineNode.parentNode.childNodes, spineNode);
|
||||
|
||||
var spine = parse.spine(spineNode, manifest);
|
||||
spine = parse.spine(spineNode, manifest);
|
||||
|
||||
var spineIndexByURL = {};
|
||||
spineIndexByURL = {};
|
||||
spine.forEach(function(item){
|
||||
spineIndexByURL[item.href] = item.index;
|
||||
});
|
||||
|
@ -4327,7 +4373,12 @@ EPUBJS.replace.links = function(_store, full, done, link){
|
|||
|
||||
//-- Handle replacing urls in CSS
|
||||
if(link.getAttribute("rel") === "stylesheet") {
|
||||
EPUBJS.replace.stylesheets(_store, full).then(done);
|
||||
EPUBJS.replace.stylesheets(_store, full).then(function(url, full){
|
||||
// done
|
||||
setTimeout(function(){
|
||||
done(url, full);
|
||||
}, 5); //-- Allow for css to apply before displaying chapter
|
||||
});
|
||||
}else{
|
||||
_store.getUrl(full).then(done);
|
||||
}
|
||||
|
@ -4439,10 +4490,14 @@ EPUBJS.Unarchiver.prototype.getXml = function(url, encoding){
|
|||
EPUBJS.Unarchiver.prototype.getUrl = function(url, mime){
|
||||
var unarchiver = this;
|
||||
var deferred = new RSVP.defer();
|
||||
var entry = this.zipFs.find(url);
|
||||
var decodededUrl = window.decodeURIComponent(url);
|
||||
var entry = this.zipFs.find(decodededUrl);
|
||||
var _URL = window.URL || window.webkitURL || window.mozURL;
|
||||
|
||||
if(!entry) console.error("File not found:", url);
|
||||
if(!entry) {
|
||||
console.error("File not found in the epub:", url);
|
||||
return;
|
||||
}
|
||||
|
||||
if(url in this.urlCache) {
|
||||
deferred.resolve(this.urlCache[url]);
|
||||
|
@ -4461,7 +4516,8 @@ EPUBJS.Unarchiver.prototype.getUrl = function(url, mime){
|
|||
EPUBJS.Unarchiver.prototype.getText = function(url, encoding){
|
||||
var unarchiver = this;
|
||||
var deferred = new RSVP.defer();
|
||||
var entry = this.zipFs.find(url);
|
||||
var decodededUrl = window.decodeURIComponent(url);
|
||||
var entry = this.zipFs.find(decodededUrl);
|
||||
var _URL = window.URL || window.webkitURL || window.mozURL;
|
||||
|
||||
if(!entry) console.error(url);
|
||||
|
|
|
@ -21,13 +21,11 @@
|
|||
|
||||
document.onreadystatechange = function () {
|
||||
if (document.readyState == "complete") {
|
||||
EPUBJS.VERSION = "0.1.7";
|
||||
|
||||
EPUBJS.filePath = "js/libs/";
|
||||
EPUBJS.cssPath = "css/";
|
||||
// fileStorage.filePath = EPUBJS.filePath;
|
||||
|
||||
window.Reader = ePubReader("moby-dick.epub", { reload: true });
|
||||
window.Reader = ePubReader("moby-dick/", { reload: true });
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -21,8 +21,6 @@
|
|||
|
||||
document.onreadystatechange = function () {
|
||||
if (document.readyState == "complete") {
|
||||
EPUBJS.VERSION = "0.1.7";
|
||||
|
||||
EPUBJS.filePath = "js/libs/";
|
||||
EPUBJS.cssPath = "css/";
|
||||
// fileStorage.filePath = EPUBJS.filePath;
|
||||
|
|
4
demo/js/epub.min.js
vendored
4
demo/js/epub.min.js
vendored
File diff suppressed because one or more lines are too long
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "EpubJS",
|
||||
"version": "0.1.8",
|
||||
"version": "0.1.9",
|
||||
"repository": "https://github.com/futurepress/epub.js",
|
||||
"devDependencies": {
|
||||
"grunt": "~0.4.1",
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
'use strict';
|
||||
|
||||
var EPUBJS = EPUBJS || {};
|
||||
EPUBJS.VERSION = "0.1.8";
|
||||
EPUBJS.VERSION = "0.1.9";
|
||||
|
||||
EPUBJS.plugins = EPUBJS.plugins || {};
|
||||
|
||||
|
|
37
src/book.js
37
src/book.js
|
@ -20,6 +20,7 @@ EPUBJS.Book = function(options){
|
|||
restore: false,
|
||||
reload : false,
|
||||
goto : false,
|
||||
withCredentials: false,
|
||||
styles : {}
|
||||
});
|
||||
|
||||
|
@ -276,21 +277,21 @@ EPUBJS.Book.prototype.loadXml = function(url){
|
|||
} else if(this.settings.contained) {
|
||||
return this.zip.getXml(url, this.settings.encoding);
|
||||
}else{
|
||||
return EPUBJS.core.request(url, 'xml');
|
||||
return EPUBJS.core.request(url, 'xml', this.settings.withCredentials);
|
||||
}
|
||||
};
|
||||
|
||||
//-- Turns a url into a absolute url
|
||||
EPUBJS.Book.prototype.urlFrom = function(bookPath){
|
||||
var absolute = bookPath.search("://") != -1,
|
||||
fromRoot = bookPath[0] == "/",
|
||||
var uri = EPUBJS.core.uri(bookPath),
|
||||
absolute = uri.protocol,
|
||||
fromRoot = uri.path[0] == "/",
|
||||
location = window.location,
|
||||
//-- Get URL orgin, try for native or combine
|
||||
origin = location.origin || location.protocol + "//" + location.host,
|
||||
baseTag = document.getElementsByTagName('base'),
|
||||
base;
|
||||
|
||||
// if(bookPath[bookPath.length - 1] != "/") bookPath += "/";
|
||||
|
||||
//-- Check is Base tag is set
|
||||
|
||||
|
@ -299,33 +300,18 @@ EPUBJS.Book.prototype.urlFrom = function(bookPath){
|
|||
}
|
||||
|
||||
//-- 1. Check if url is absolute
|
||||
if(absolute){
|
||||
return bookPath;
|
||||
if(uri.protocol){
|
||||
return uri.origin + uri.path;
|
||||
}
|
||||
|
||||
//-- 2. Check if url starts with /, add base url
|
||||
if(!absolute && fromRoot){
|
||||
if(base) {
|
||||
return base + bookPath;
|
||||
} else {
|
||||
return origin + bookPath;
|
||||
}
|
||||
return (base || origin) + uri.path;
|
||||
}
|
||||
|
||||
//-- 3. Or find full path to url and add that
|
||||
if(!absolute && !fromRoot){
|
||||
|
||||
//-- go back
|
||||
if(bookPath.slice(0, 3) == "../"){
|
||||
return EPUBJS.core.resolveUrl(base || location.pathname, bookPath);
|
||||
}
|
||||
|
||||
if(base) {
|
||||
return base + bookPath;
|
||||
} else {
|
||||
return origin + EPUBJS.core.folder(location.pathname) + bookPath;
|
||||
}
|
||||
|
||||
return EPUBJS.core.resolveUrl(base || location.pathname, uri.path);
|
||||
}
|
||||
|
||||
};
|
||||
|
@ -348,8 +334,9 @@ EPUBJS.Book.prototype.unarchive = function(bookPath){
|
|||
|
||||
//-- Checks if url has a .epub or .zip extension
|
||||
EPUBJS.Book.prototype.isContained = function(bookUrl){
|
||||
var dot = bookUrl.lastIndexOf('.'),
|
||||
ext = bookUrl.slice(dot+1);
|
||||
var uri = EPUBJS.core.uri(bookUrl),
|
||||
dot = uri.filename.lastIndexOf('.'),
|
||||
ext = uri.filename.slice(dot+1);
|
||||
|
||||
if(ext && (ext == "epub" || ext == "zip")){
|
||||
return true;
|
||||
|
|
28
src/core.js
28
src/core.js
|
@ -11,7 +11,7 @@ EPUBJS.core.getEls = function(classes) {
|
|||
return document.getElementsByClassName(classes);
|
||||
};
|
||||
|
||||
EPUBJS.core.request = function(url, type) {
|
||||
EPUBJS.core.request = function(url, type, withCredentials) {
|
||||
var supportsURL = window.URL;
|
||||
var BLOB_RESPONSE = supportsURL ? "blob" : "arraybuffer";
|
||||
|
||||
|
@ -29,8 +29,10 @@ EPUBJS.core.request = function(url, type) {
|
|||
value: function xmlHttpRequestOverrideMimeType(mimeType) {}
|
||||
});
|
||||
}
|
||||
|
||||
xhr.open("GET", url);
|
||||
if(withCredentials) {
|
||||
xhr.withCredentials = true;
|
||||
}
|
||||
xhr.open("GET", url, true);
|
||||
xhr.onreadystatechange = handler;
|
||||
|
||||
if(type == 'blob'){
|
||||
|
@ -73,7 +75,10 @@ EPUBJS.core.request = function(url, type) {
|
|||
|
||||
deferred.resolve(r);
|
||||
} else {
|
||||
deferred.reject(this);
|
||||
deferred.reject({
|
||||
message : this.response,
|
||||
stack : new Error().stack
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -105,10 +110,12 @@ EPUBJS.core.uri = function(url){
|
|||
origin : '',
|
||||
directory : '',
|
||||
base : '',
|
||||
filename : ''
|
||||
filename : '',
|
||||
href : url
|
||||
},
|
||||
doubleSlash = url.indexOf('://'),
|
||||
search = url.indexOf('?'),
|
||||
withoutProtocol,
|
||||
firstSlash;
|
||||
|
||||
if(search != -1) {
|
||||
|
@ -118,18 +125,21 @@ EPUBJS.core.uri = function(url){
|
|||
|
||||
if(doubleSlash != -1) {
|
||||
uri.protocol = url.slice(0, doubleSlash);
|
||||
uri.path = url.slice(doubleSlash+3);
|
||||
firstSlash = uri.path.indexOf('/');
|
||||
withoutProtocol = url.slice(doubleSlash+3);
|
||||
firstSlash = withoutProtocol.indexOf('/');
|
||||
|
||||
if(firstSlash === -1) {
|
||||
uri.host = uri.path;
|
||||
uri.path = "";
|
||||
} else {
|
||||
uri.host = uri.path.slice(0, firstSlash);
|
||||
uri.host = withoutProtocol.slice(0, firstSlash);
|
||||
uri.path = withoutProtocol.slice(firstSlash);
|
||||
}
|
||||
|
||||
|
||||
uri.origin = uri.protocol + "://" + uri.host;
|
||||
|
||||
uri.directory = EPUBJS.core.folder(uri.path.replace(uri.host, ''));
|
||||
uri.directory = EPUBJS.core.folder(uri.path);
|
||||
|
||||
uri.base = uri.origin + uri.directory;
|
||||
// return origin;
|
||||
|
|
|
@ -151,7 +151,7 @@ EPUBJS.EpubCFI.prototype.getElement = function(cfi, _doc) {
|
|||
|
||||
// sections.shift(); //-- html
|
||||
|
||||
while(sections.length > 0) {
|
||||
while(sections && sections.length > 0) {
|
||||
|
||||
part = sections.shift();
|
||||
|
||||
|
|
|
@ -3,11 +3,23 @@ EPUBJS.Parser = function(baseUrl){
|
|||
};
|
||||
|
||||
EPUBJS.Parser.prototype.container = function(containerXml){
|
||||
|
||||
//-- <rootfile full-path="OPS/package.opf" media-type="application/oebps-package+xml"/>
|
||||
var rootfile = containerXml.querySelector("rootfile"),
|
||||
fullpath = rootfile.getAttribute('full-path'),
|
||||
folder = EPUBJS.core.folder(fullpath),
|
||||
var rootfile, fullpath, folder, encoding;
|
||||
|
||||
if(!containerXml) {
|
||||
console.error("Container File Not Found");
|
||||
return;
|
||||
}
|
||||
|
||||
rootfile = containerXml.querySelector("rootfile");
|
||||
|
||||
if(!rootfile) {
|
||||
console.error("No RootFile Found");
|
||||
return;
|
||||
}
|
||||
|
||||
fullpath = rootfile.getAttribute('full-path');
|
||||
folder = EPUBJS.core.uri(fullpath).directory;
|
||||
encoding = containerXml.xmlEncoding;
|
||||
|
||||
//-- Now that we have the path we can parse the contents
|
||||
|
@ -19,29 +31,66 @@ EPUBJS.Parser.prototype.container = function(containerXml){
|
|||
};
|
||||
|
||||
EPUBJS.Parser.prototype.identifier = function(packageXml){
|
||||
var metadataNode = packageXml.querySelector("metadata");
|
||||
var metadataNode;
|
||||
|
||||
if(!packageXml) {
|
||||
console.error("Package File Not Found");
|
||||
return;
|
||||
}
|
||||
|
||||
metadataNode = packageXml.querySelector("metadata");
|
||||
|
||||
if(!metadataNode) {
|
||||
console.error("No Metadata Found");
|
||||
return;
|
||||
}
|
||||
|
||||
return this.getElementText(metadataNode, "identifier");
|
||||
};
|
||||
|
||||
EPUBJS.Parser.prototype.packageContents = function(packageXml, baseUrl){
|
||||
var parse = this;
|
||||
var metadataNode, manifestNode, spineNode;
|
||||
var manifest, navPath, tocPath, coverPath;
|
||||
var spineNodeIndex;
|
||||
var spine;
|
||||
var spineIndexByURL;
|
||||
|
||||
if(baseUrl) this.baseUrl = baseUrl;
|
||||
|
||||
var metadataNode = packageXml.querySelector("metadata"),
|
||||
manifestNode = packageXml.querySelector("manifest"),
|
||||
spineNode = packageXml.querySelector("spine");
|
||||
if(!packageXml) {
|
||||
console.error("Package File Not Found");
|
||||
return;
|
||||
}
|
||||
|
||||
var manifest = parse.manifest(manifestNode),
|
||||
navPath = parse.findNavPath(manifestNode),
|
||||
tocPath = parse.findTocPath(manifestNode),
|
||||
metadataNode = packageXml.querySelector("metadata");
|
||||
if(!metadataNode) {
|
||||
console.error("No Metadata Found");
|
||||
return;
|
||||
}
|
||||
|
||||
manifestNode = packageXml.querySelector("manifest");
|
||||
if(!metadataNode) {
|
||||
console.error("No Manifest Found");
|
||||
return;
|
||||
}
|
||||
|
||||
spineNode = packageXml.querySelector("spine");
|
||||
if(!metadataNode) {
|
||||
console.error("No Spine Found");
|
||||
return;
|
||||
}
|
||||
|
||||
manifest = parse.manifest(manifestNode);
|
||||
navPath = parse.findNavPath(manifestNode);
|
||||
tocPath = parse.findTocPath(manifestNode);
|
||||
coverPath = parse.findCoverPath(manifestNode);
|
||||
|
||||
var spineNodeIndex = Array.prototype.indexOf.call(spineNode.parentNode.childNodes, spineNode);
|
||||
spineNodeIndex = Array.prototype.indexOf.call(spineNode.parentNode.childNodes, spineNode);
|
||||
|
||||
var spine = parse.spine(spineNode, manifest);
|
||||
spine = parse.spine(spineNode, manifest);
|
||||
|
||||
var spineIndexByURL = {};
|
||||
spineIndexByURL = {};
|
||||
spine.forEach(function(item){
|
||||
spineIndexByURL[item.href] = item.index;
|
||||
});
|
||||
|
|
|
@ -34,7 +34,12 @@ EPUBJS.replace.links = function(_store, full, done, link){
|
|||
|
||||
//-- Handle replacing urls in CSS
|
||||
if(link.getAttribute("rel") === "stylesheet") {
|
||||
EPUBJS.replace.stylesheets(_store, full).then(done);
|
||||
EPUBJS.replace.stylesheets(_store, full).then(function(url, full){
|
||||
// done
|
||||
setTimeout(function(){
|
||||
done(url, full);
|
||||
}, 5); //-- Allow for css to apply before displaying chapter
|
||||
});
|
||||
}else{
|
||||
_store.getUrl(full).then(done);
|
||||
}
|
||||
|
|
|
@ -49,10 +49,14 @@ EPUBJS.Unarchiver.prototype.getXml = function(url, encoding){
|
|||
EPUBJS.Unarchiver.prototype.getUrl = function(url, mime){
|
||||
var unarchiver = this;
|
||||
var deferred = new RSVP.defer();
|
||||
var entry = this.zipFs.find(url);
|
||||
var decodededUrl = window.decodeURIComponent(url);
|
||||
var entry = this.zipFs.find(decodededUrl);
|
||||
var _URL = window.URL || window.webkitURL || window.mozURL;
|
||||
|
||||
if(!entry) console.error("File not found:", url);
|
||||
if(!entry) {
|
||||
console.error("File not found in the epub:", url);
|
||||
return;
|
||||
}
|
||||
|
||||
if(url in this.urlCache) {
|
||||
deferred.resolve(this.urlCache[url]);
|
||||
|
@ -71,7 +75,8 @@ EPUBJS.Unarchiver.prototype.getUrl = function(url, mime){
|
|||
EPUBJS.Unarchiver.prototype.getText = function(url, encoding){
|
||||
var unarchiver = this;
|
||||
var deferred = new RSVP.defer();
|
||||
var entry = this.zipFs.find(url);
|
||||
var decodededUrl = window.decodeURIComponent(url);
|
||||
var entry = this.zipFs.find(decodededUrl);
|
||||
var _URL = window.URL || window.webkitURL || window.mozURL;
|
||||
|
||||
if(!entry) console.error(url);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue