1
0
Fork 0
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:
Fred Chasen 2014-01-13 21:14:03 -08:00
parent 7649612ec5
commit f1c4b8c917
14 changed files with 357 additions and 193 deletions

View file

@ -1711,7 +1711,7 @@ global.RSVP = requireModule('rsvp');
'use strict'; 'use strict';
var EPUBJS = EPUBJS || {}; var EPUBJS = EPUBJS || {};
EPUBJS.VERSION = "0.1.8"; EPUBJS.VERSION = "0.1.9";
EPUBJS.plugins = EPUBJS.plugins || {}; EPUBJS.plugins = EPUBJS.plugins || {};
@ -1794,6 +1794,7 @@ EPUBJS.Book = function(options){
restore: false, restore: false,
reload : false, reload : false,
goto : false, goto : false,
withCredentials: false,
styles : {} styles : {}
}); });
@ -2050,21 +2051,21 @@ EPUBJS.Book.prototype.loadXml = function(url){
} else if(this.settings.contained) { } else if(this.settings.contained) {
return this.zip.getXml(url, this.settings.encoding); return this.zip.getXml(url, this.settings.encoding);
}else{ }else{
return EPUBJS.core.request(url, 'xml'); return EPUBJS.core.request(url, 'xml', this.settings.withCredentials);
} }
}; };
//-- Turns a url into a absolute url //-- Turns a url into a absolute url
EPUBJS.Book.prototype.urlFrom = function(bookPath){ EPUBJS.Book.prototype.urlFrom = function(bookPath){
var absolute = bookPath.search("://") != -1, var uri = EPUBJS.core.uri(bookPath),
fromRoot = bookPath[0] == "/", absolute = uri.protocol,
fromRoot = uri.path[0] == "/",
location = window.location, location = window.location,
//-- Get URL orgin, try for native or combine //-- Get URL orgin, try for native or combine
origin = location.origin || location.protocol + "//" + location.host, origin = location.origin || location.protocol + "//" + location.host,
baseTag = document.getElementsByTagName('base'), baseTag = document.getElementsByTagName('base'),
base; base;
// if(bookPath[bookPath.length - 1] != "/") bookPath += "/";
//-- Check is Base tag is set //-- Check is Base tag is set
@ -2073,33 +2074,18 @@ EPUBJS.Book.prototype.urlFrom = function(bookPath){
} }
//-- 1. Check if url is absolute //-- 1. Check if url is absolute
if(absolute){ if(uri.protocol){
return bookPath; return uri.origin + uri.path;
} }
//-- 2. Check if url starts with /, add base url //-- 2. Check if url starts with /, add base url
if(!absolute && fromRoot){ if(!absolute && fromRoot){
if(base) { return (base || origin) + uri.path;
return base + bookPath;
} else {
return origin + bookPath;
}
} }
//-- 3. Or find full path to url and add that //-- 3. Or find full path to url and add that
if(!absolute && !fromRoot){ if(!absolute && !fromRoot){
return EPUBJS.core.resolveUrl(base || location.pathname, uri.path);
//-- 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;
}
} }
}; };
@ -2122,8 +2108,9 @@ EPUBJS.Book.prototype.unarchive = function(bookPath){
//-- Checks if url has a .epub or .zip extension //-- Checks if url has a .epub or .zip extension
EPUBJS.Book.prototype.isContained = function(bookUrl){ EPUBJS.Book.prototype.isContained = function(bookUrl){
var dot = bookUrl.lastIndexOf('.'), var uri = EPUBJS.core.uri(bookUrl),
ext = bookUrl.slice(dot+1); dot = uri.filename.lastIndexOf('.'),
ext = uri.filename.slice(dot+1);
if(ext && (ext == "epub" || ext == "zip")){ if(ext && (ext == "epub" || ext == "zip")){
return true; return true;
@ -2687,7 +2674,7 @@ EPUBJS.core.getEls = function(classes) {
return document.getElementsByClassName(classes); return document.getElementsByClassName(classes);
}; };
EPUBJS.core.request = function(url, type) { EPUBJS.core.request = function(url, type, withCredentials) {
var supportsURL = window.URL; var supportsURL = window.URL;
var BLOB_RESPONSE = supportsURL ? "blob" : "arraybuffer"; var BLOB_RESPONSE = supportsURL ? "blob" : "arraybuffer";
@ -2705,8 +2692,10 @@ EPUBJS.core.request = function(url, type) {
value: function xmlHttpRequestOverrideMimeType(mimeType) {} value: function xmlHttpRequestOverrideMimeType(mimeType) {}
}); });
} }
if(withCredentials) {
xhr.open("GET", url); xhr.withCredentials = true;
}
xhr.open("GET", url, true);
xhr.onreadystatechange = handler; xhr.onreadystatechange = handler;
if(type == 'blob'){ if(type == 'blob'){
@ -2749,7 +2738,10 @@ EPUBJS.core.request = function(url, type) {
deferred.resolve(r); deferred.resolve(r);
} else { } else {
deferred.reject(this); deferred.reject({
message : this.response,
stack : new Error().stack
});
} }
} }
} }
@ -2781,10 +2773,12 @@ EPUBJS.core.uri = function(url){
origin : '', origin : '',
directory : '', directory : '',
base : '', base : '',
filename : '' filename : '',
href : url
}, },
doubleSlash = url.indexOf('://'), doubleSlash = url.indexOf('://'),
search = url.indexOf('?'), search = url.indexOf('?'),
withoutProtocol,
firstSlash; firstSlash;
if(search != -1) { if(search != -1) {
@ -2794,18 +2788,21 @@ EPUBJS.core.uri = function(url){
if(doubleSlash != -1) { if(doubleSlash != -1) {
uri.protocol = url.slice(0, doubleSlash); uri.protocol = url.slice(0, doubleSlash);
uri.path = url.slice(doubleSlash+3); withoutProtocol = url.slice(doubleSlash+3);
firstSlash = uri.path.indexOf('/'); firstSlash = withoutProtocol.indexOf('/');
if(firstSlash === -1) { if(firstSlash === -1) {
uri.host = uri.path; uri.host = uri.path;
uri.path = "";
} else { } 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.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; uri.base = uri.origin + uri.directory;
// return origin; // return origin;
@ -3109,7 +3106,7 @@ EPUBJS.EpubCFI.prototype.getElement = function(cfi, _doc) {
// sections.shift(); //-- html // sections.shift(); //-- html
while(sections.length > 0) { while(sections && sections.length > 0) {
part = sections.shift(); part = sections.shift();
@ -3219,12 +3216,24 @@ EPUBJS.Parser = function(baseUrl){
}; };
EPUBJS.Parser.prototype.container = function(containerXml){ EPUBJS.Parser.prototype.container = function(containerXml){
//-- <rootfile full-path="OPS/package.opf" media-type="application/oebps-package+xml"/> //-- <rootfile full-path="OPS/package.opf" media-type="application/oebps-package+xml"/>
var rootfile = containerXml.querySelector("rootfile"), var rootfile, fullpath, folder, encoding;
fullpath = rootfile.getAttribute('full-path'),
folder = EPUBJS.core.folder(fullpath), if(!containerXml) {
encoding = containerXml.xmlEncoding; 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 //-- Now that we have the path we can parse the contents
return { return {
@ -3235,29 +3244,66 @@ EPUBJS.Parser.prototype.container = function(containerXml){
}; };
EPUBJS.Parser.prototype.identifier = function(packageXml){ 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"); return this.getElementText(metadataNode, "identifier");
}; };
EPUBJS.Parser.prototype.packageContents = function(packageXml, baseUrl){ EPUBJS.Parser.prototype.packageContents = function(packageXml, baseUrl){
var parse = this; var parse = this;
var metadataNode, manifestNode, spineNode;
var manifest, navPath, tocPath, coverPath;
var spineNodeIndex;
var spine;
var spineIndexByURL;
if(baseUrl) this.baseUrl = baseUrl; if(baseUrl) this.baseUrl = baseUrl;
var metadataNode = packageXml.querySelector("metadata"), if(!packageXml) {
manifestNode = packageXml.querySelector("manifest"), console.error("Package File Not Found");
spineNode = packageXml.querySelector("spine"); return;
}
var manifest = parse.manifest(manifestNode), metadataNode = packageXml.querySelector("metadata");
navPath = parse.findNavPath(manifestNode), if(!metadataNode) {
tocPath = parse.findTocPath(manifestNode), console.error("No Metadata Found");
coverPath = parse.findCoverPath(manifestNode); return;
}
var spineNodeIndex = Array.prototype.indexOf.call(spineNode.parentNode.childNodes, spineNode); manifestNode = packageXml.querySelector("manifest");
if(!metadataNode) {
console.error("No Manifest Found");
return;
}
var spine = parse.spine(spineNode, manifest); spineNode = packageXml.querySelector("spine");
if(!metadataNode) {
console.error("No Spine Found");
return;
}
var spineIndexByURL = {}; manifest = parse.manifest(manifestNode);
navPath = parse.findNavPath(manifestNode);
tocPath = parse.findTocPath(manifestNode);
coverPath = parse.findCoverPath(manifestNode);
spineNodeIndex = Array.prototype.indexOf.call(spineNode.parentNode.childNodes, spineNode);
spine = parse.spine(spineNode, manifest);
spineIndexByURL = {};
spine.forEach(function(item){ spine.forEach(function(item){
spineIndexByURL[item.href] = item.index; spineIndexByURL[item.href] = item.index;
}); });
@ -4328,7 +4374,12 @@ EPUBJS.replace.links = function(_store, full, done, link){
//-- Handle replacing urls in CSS //-- Handle replacing urls in CSS
if(link.getAttribute("rel") === "stylesheet") { 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{ }else{
_store.getUrl(full).then(done); _store.getUrl(full).then(done);
} }
@ -4440,10 +4491,14 @@ EPUBJS.Unarchiver.prototype.getXml = function(url, encoding){
EPUBJS.Unarchiver.prototype.getUrl = function(url, mime){ EPUBJS.Unarchiver.prototype.getUrl = function(url, mime){
var unarchiver = this; var unarchiver = this;
var deferred = new RSVP.defer(); 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; 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) { if(url in this.urlCache) {
deferred.resolve(this.urlCache[url]); deferred.resolve(this.urlCache[url]);
@ -4462,7 +4517,8 @@ EPUBJS.Unarchiver.prototype.getUrl = function(url, mime){
EPUBJS.Unarchiver.prototype.getText = function(url, encoding){ EPUBJS.Unarchiver.prototype.getText = function(url, encoding){
var unarchiver = this; var unarchiver = this;
var deferred = new RSVP.defer(); 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; var _URL = window.URL || window.webkitURL || window.mozURL;
if(!entry) console.error(url); if(!entry) console.error(url);

4
build/epub.min.js vendored

File diff suppressed because one or more lines are too long

View file

@ -1710,7 +1710,7 @@ global.RSVP = requireModule('rsvp');
'use strict'; 'use strict';
var EPUBJS = EPUBJS || {}; var EPUBJS = EPUBJS || {};
EPUBJS.VERSION = "0.1.8"; EPUBJS.VERSION = "0.1.9";
EPUBJS.plugins = EPUBJS.plugins || {}; EPUBJS.plugins = EPUBJS.plugins || {};
@ -1793,6 +1793,7 @@ EPUBJS.Book = function(options){
restore: false, restore: false,
reload : false, reload : false,
goto : false, goto : false,
withCredentials: false,
styles : {} styles : {}
}); });
@ -2049,21 +2050,21 @@ EPUBJS.Book.prototype.loadXml = function(url){
} else if(this.settings.contained) { } else if(this.settings.contained) {
return this.zip.getXml(url, this.settings.encoding); return this.zip.getXml(url, this.settings.encoding);
}else{ }else{
return EPUBJS.core.request(url, 'xml'); return EPUBJS.core.request(url, 'xml', this.settings.withCredentials);
} }
}; };
//-- Turns a url into a absolute url //-- Turns a url into a absolute url
EPUBJS.Book.prototype.urlFrom = function(bookPath){ EPUBJS.Book.prototype.urlFrom = function(bookPath){
var absolute = bookPath.search("://") != -1, var uri = EPUBJS.core.uri(bookPath),
fromRoot = bookPath[0] == "/", absolute = uri.protocol,
fromRoot = uri.path[0] == "/",
location = window.location, location = window.location,
//-- Get URL orgin, try for native or combine //-- Get URL orgin, try for native or combine
origin = location.origin || location.protocol + "//" + location.host, origin = location.origin || location.protocol + "//" + location.host,
baseTag = document.getElementsByTagName('base'), baseTag = document.getElementsByTagName('base'),
base; base;
// if(bookPath[bookPath.length - 1] != "/") bookPath += "/";
//-- Check is Base tag is set //-- Check is Base tag is set
@ -2072,33 +2073,18 @@ EPUBJS.Book.prototype.urlFrom = function(bookPath){
} }
//-- 1. Check if url is absolute //-- 1. Check if url is absolute
if(absolute){ if(uri.protocol){
return bookPath; return uri.origin + uri.path;
} }
//-- 2. Check if url starts with /, add base url //-- 2. Check if url starts with /, add base url
if(!absolute && fromRoot){ if(!absolute && fromRoot){
if(base) { return (base || origin) + uri.path;
return base + bookPath;
} else {
return origin + bookPath;
}
} }
//-- 3. Or find full path to url and add that //-- 3. Or find full path to url and add that
if(!absolute && !fromRoot){ if(!absolute && !fromRoot){
return EPUBJS.core.resolveUrl(base || location.pathname, uri.path);
//-- 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;
}
} }
}; };
@ -2121,8 +2107,9 @@ EPUBJS.Book.prototype.unarchive = function(bookPath){
//-- Checks if url has a .epub or .zip extension //-- Checks if url has a .epub or .zip extension
EPUBJS.Book.prototype.isContained = function(bookUrl){ EPUBJS.Book.prototype.isContained = function(bookUrl){
var dot = bookUrl.lastIndexOf('.'), var uri = EPUBJS.core.uri(bookUrl),
ext = bookUrl.slice(dot+1); dot = uri.filename.lastIndexOf('.'),
ext = uri.filename.slice(dot+1);
if(ext && (ext == "epub" || ext == "zip")){ if(ext && (ext == "epub" || ext == "zip")){
return true; return true;
@ -2686,7 +2673,7 @@ EPUBJS.core.getEls = function(classes) {
return document.getElementsByClassName(classes); return document.getElementsByClassName(classes);
}; };
EPUBJS.core.request = function(url, type) { EPUBJS.core.request = function(url, type, withCredentials) {
var supportsURL = window.URL; var supportsURL = window.URL;
var BLOB_RESPONSE = supportsURL ? "blob" : "arraybuffer"; var BLOB_RESPONSE = supportsURL ? "blob" : "arraybuffer";
@ -2704,8 +2691,10 @@ EPUBJS.core.request = function(url, type) {
value: function xmlHttpRequestOverrideMimeType(mimeType) {} value: function xmlHttpRequestOverrideMimeType(mimeType) {}
}); });
} }
if(withCredentials) {
xhr.open("GET", url); xhr.withCredentials = true;
}
xhr.open("GET", url, true);
xhr.onreadystatechange = handler; xhr.onreadystatechange = handler;
if(type == 'blob'){ if(type == 'blob'){
@ -2748,7 +2737,10 @@ EPUBJS.core.request = function(url, type) {
deferred.resolve(r); deferred.resolve(r);
} else { } else {
deferred.reject(this); deferred.reject({
message : this.response,
stack : new Error().stack
});
} }
} }
} }
@ -2780,10 +2772,12 @@ EPUBJS.core.uri = function(url){
origin : '', origin : '',
directory : '', directory : '',
base : '', base : '',
filename : '' filename : '',
href : url
}, },
doubleSlash = url.indexOf('://'), doubleSlash = url.indexOf('://'),
search = url.indexOf('?'), search = url.indexOf('?'),
withoutProtocol,
firstSlash; firstSlash;
if(search != -1) { if(search != -1) {
@ -2793,18 +2787,21 @@ EPUBJS.core.uri = function(url){
if(doubleSlash != -1) { if(doubleSlash != -1) {
uri.protocol = url.slice(0, doubleSlash); uri.protocol = url.slice(0, doubleSlash);
uri.path = url.slice(doubleSlash+3); withoutProtocol = url.slice(doubleSlash+3);
firstSlash = uri.path.indexOf('/'); firstSlash = withoutProtocol.indexOf('/');
if(firstSlash === -1) { if(firstSlash === -1) {
uri.host = uri.path; uri.host = uri.path;
uri.path = "";
} else { } 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.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; uri.base = uri.origin + uri.directory;
// return origin; // return origin;
@ -3108,7 +3105,7 @@ EPUBJS.EpubCFI.prototype.getElement = function(cfi, _doc) {
// sections.shift(); //-- html // sections.shift(); //-- html
while(sections.length > 0) { while(sections && sections.length > 0) {
part = sections.shift(); part = sections.shift();
@ -3218,12 +3215,24 @@ EPUBJS.Parser = function(baseUrl){
}; };
EPUBJS.Parser.prototype.container = function(containerXml){ EPUBJS.Parser.prototype.container = function(containerXml){
//-- <rootfile full-path="OPS/package.opf" media-type="application/oebps-package+xml"/> //-- <rootfile full-path="OPS/package.opf" media-type="application/oebps-package+xml"/>
var rootfile = containerXml.querySelector("rootfile"), var rootfile, fullpath, folder, encoding;
fullpath = rootfile.getAttribute('full-path'),
folder = EPUBJS.core.folder(fullpath), if(!containerXml) {
encoding = containerXml.xmlEncoding; 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 //-- Now that we have the path we can parse the contents
return { return {
@ -3234,29 +3243,66 @@ EPUBJS.Parser.prototype.container = function(containerXml){
}; };
EPUBJS.Parser.prototype.identifier = function(packageXml){ 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"); return this.getElementText(metadataNode, "identifier");
}; };
EPUBJS.Parser.prototype.packageContents = function(packageXml, baseUrl){ EPUBJS.Parser.prototype.packageContents = function(packageXml, baseUrl){
var parse = this; var parse = this;
var metadataNode, manifestNode, spineNode;
var manifest, navPath, tocPath, coverPath;
var spineNodeIndex;
var spine;
var spineIndexByURL;
if(baseUrl) this.baseUrl = baseUrl; if(baseUrl) this.baseUrl = baseUrl;
var metadataNode = packageXml.querySelector("metadata"), if(!packageXml) {
manifestNode = packageXml.querySelector("manifest"), console.error("Package File Not Found");
spineNode = packageXml.querySelector("spine"); return;
}
var manifest = parse.manifest(manifestNode), metadataNode = packageXml.querySelector("metadata");
navPath = parse.findNavPath(manifestNode), if(!metadataNode) {
tocPath = parse.findTocPath(manifestNode), console.error("No Metadata Found");
coverPath = parse.findCoverPath(manifestNode); return;
}
var spineNodeIndex = Array.prototype.indexOf.call(spineNode.parentNode.childNodes, spineNode); manifestNode = packageXml.querySelector("manifest");
if(!metadataNode) {
console.error("No Manifest Found");
return;
}
var spine = parse.spine(spineNode, manifest); spineNode = packageXml.querySelector("spine");
if(!metadataNode) {
console.error("No Spine Found");
return;
}
var spineIndexByURL = {}; manifest = parse.manifest(manifestNode);
navPath = parse.findNavPath(manifestNode);
tocPath = parse.findTocPath(manifestNode);
coverPath = parse.findCoverPath(manifestNode);
spineNodeIndex = Array.prototype.indexOf.call(spineNode.parentNode.childNodes, spineNode);
spine = parse.spine(spineNode, manifest);
spineIndexByURL = {};
spine.forEach(function(item){ spine.forEach(function(item){
spineIndexByURL[item.href] = item.index; spineIndexByURL[item.href] = item.index;
}); });
@ -4327,7 +4373,12 @@ EPUBJS.replace.links = function(_store, full, done, link){
//-- Handle replacing urls in CSS //-- Handle replacing urls in CSS
if(link.getAttribute("rel") === "stylesheet") { 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{ }else{
_store.getUrl(full).then(done); _store.getUrl(full).then(done);
} }
@ -4439,10 +4490,14 @@ EPUBJS.Unarchiver.prototype.getXml = function(url, encoding){
EPUBJS.Unarchiver.prototype.getUrl = function(url, mime){ EPUBJS.Unarchiver.prototype.getUrl = function(url, mime){
var unarchiver = this; var unarchiver = this;
var deferred = new RSVP.defer(); 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; 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) { if(url in this.urlCache) {
deferred.resolve(this.urlCache[url]); deferred.resolve(this.urlCache[url]);
@ -4461,7 +4516,8 @@ EPUBJS.Unarchiver.prototype.getUrl = function(url, mime){
EPUBJS.Unarchiver.prototype.getText = function(url, encoding){ EPUBJS.Unarchiver.prototype.getText = function(url, encoding){
var unarchiver = this; var unarchiver = this;
var deferred = new RSVP.defer(); 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; var _URL = window.URL || window.webkitURL || window.mozURL;
if(!entry) console.error(url); if(!entry) console.error(url);

View file

@ -21,13 +21,11 @@
document.onreadystatechange = function () { document.onreadystatechange = function () {
if (document.readyState == "complete") { if (document.readyState == "complete") {
EPUBJS.VERSION = "0.1.7";
EPUBJS.filePath = "js/libs/"; EPUBJS.filePath = "js/libs/";
EPUBJS.cssPath = "css/"; EPUBJS.cssPath = "css/";
// fileStorage.filePath = EPUBJS.filePath; // fileStorage.filePath = EPUBJS.filePath;
window.Reader = ePubReader("moby-dick.epub", { reload: true }); window.Reader = ePubReader("moby-dick/", { reload: true });
} }
}; };

View file

@ -21,8 +21,6 @@
document.onreadystatechange = function () { document.onreadystatechange = function () {
if (document.readyState == "complete") { if (document.readyState == "complete") {
EPUBJS.VERSION = "0.1.7";
EPUBJS.filePath = "js/libs/"; EPUBJS.filePath = "js/libs/";
EPUBJS.cssPath = "css/"; EPUBJS.cssPath = "css/";
// fileStorage.filePath = EPUBJS.filePath; // fileStorage.filePath = EPUBJS.filePath;

4
demo/js/epub.min.js vendored

File diff suppressed because one or more lines are too long

View file

@ -1,6 +1,6 @@
{ {
"name": "EpubJS", "name": "EpubJS",
"version": "0.1.8", "version": "0.1.9",
"repository": "https://github.com/futurepress/epub.js", "repository": "https://github.com/futurepress/epub.js",
"devDependencies": { "devDependencies": {
"grunt": "~0.4.1", "grunt": "~0.4.1",

View file

@ -1,7 +1,7 @@
'use strict'; 'use strict';
var EPUBJS = EPUBJS || {}; var EPUBJS = EPUBJS || {};
EPUBJS.VERSION = "0.1.8"; EPUBJS.VERSION = "0.1.9";
EPUBJS.plugins = EPUBJS.plugins || {}; EPUBJS.plugins = EPUBJS.plugins || {};

View file

@ -20,6 +20,7 @@ EPUBJS.Book = function(options){
restore: false, restore: false,
reload : false, reload : false,
goto : false, goto : false,
withCredentials: false,
styles : {} styles : {}
}); });
@ -276,21 +277,21 @@ EPUBJS.Book.prototype.loadXml = function(url){
} else if(this.settings.contained) { } else if(this.settings.contained) {
return this.zip.getXml(url, this.settings.encoding); return this.zip.getXml(url, this.settings.encoding);
}else{ }else{
return EPUBJS.core.request(url, 'xml'); return EPUBJS.core.request(url, 'xml', this.settings.withCredentials);
} }
}; };
//-- Turns a url into a absolute url //-- Turns a url into a absolute url
EPUBJS.Book.prototype.urlFrom = function(bookPath){ EPUBJS.Book.prototype.urlFrom = function(bookPath){
var absolute = bookPath.search("://") != -1, var uri = EPUBJS.core.uri(bookPath),
fromRoot = bookPath[0] == "/", absolute = uri.protocol,
fromRoot = uri.path[0] == "/",
location = window.location, location = window.location,
//-- Get URL orgin, try for native or combine //-- Get URL orgin, try for native or combine
origin = location.origin || location.protocol + "//" + location.host, origin = location.origin || location.protocol + "//" + location.host,
baseTag = document.getElementsByTagName('base'), baseTag = document.getElementsByTagName('base'),
base; base;
// if(bookPath[bookPath.length - 1] != "/") bookPath += "/";
//-- Check is Base tag is set //-- Check is Base tag is set
@ -299,33 +300,18 @@ EPUBJS.Book.prototype.urlFrom = function(bookPath){
} }
//-- 1. Check if url is absolute //-- 1. Check if url is absolute
if(absolute){ if(uri.protocol){
return bookPath; return uri.origin + uri.path;
} }
//-- 2. Check if url starts with /, add base url //-- 2. Check if url starts with /, add base url
if(!absolute && fromRoot){ if(!absolute && fromRoot){
if(base) { return (base || origin) + uri.path;
return base + bookPath;
} else {
return origin + bookPath;
}
} }
//-- 3. Or find full path to url and add that //-- 3. Or find full path to url and add that
if(!absolute && !fromRoot){ if(!absolute && !fromRoot){
return EPUBJS.core.resolveUrl(base || location.pathname, uri.path);
//-- 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;
}
} }
}; };
@ -348,8 +334,9 @@ EPUBJS.Book.prototype.unarchive = function(bookPath){
//-- Checks if url has a .epub or .zip extension //-- Checks if url has a .epub or .zip extension
EPUBJS.Book.prototype.isContained = function(bookUrl){ EPUBJS.Book.prototype.isContained = function(bookUrl){
var dot = bookUrl.lastIndexOf('.'), var uri = EPUBJS.core.uri(bookUrl),
ext = bookUrl.slice(dot+1); dot = uri.filename.lastIndexOf('.'),
ext = uri.filename.slice(dot+1);
if(ext && (ext == "epub" || ext == "zip")){ if(ext && (ext == "epub" || ext == "zip")){
return true; return true;

View file

@ -11,7 +11,7 @@ EPUBJS.core.getEls = function(classes) {
return document.getElementsByClassName(classes); return document.getElementsByClassName(classes);
}; };
EPUBJS.core.request = function(url, type) { EPUBJS.core.request = function(url, type, withCredentials) {
var supportsURL = window.URL; var supportsURL = window.URL;
var BLOB_RESPONSE = supportsURL ? "blob" : "arraybuffer"; var BLOB_RESPONSE = supportsURL ? "blob" : "arraybuffer";
@ -29,8 +29,10 @@ EPUBJS.core.request = function(url, type) {
value: function xmlHttpRequestOverrideMimeType(mimeType) {} value: function xmlHttpRequestOverrideMimeType(mimeType) {}
}); });
} }
if(withCredentials) {
xhr.open("GET", url); xhr.withCredentials = true;
}
xhr.open("GET", url, true);
xhr.onreadystatechange = handler; xhr.onreadystatechange = handler;
if(type == 'blob'){ if(type == 'blob'){
@ -73,7 +75,10 @@ EPUBJS.core.request = function(url, type) {
deferred.resolve(r); deferred.resolve(r);
} else { } else {
deferred.reject(this); deferred.reject({
message : this.response,
stack : new Error().stack
});
} }
} }
} }
@ -105,10 +110,12 @@ EPUBJS.core.uri = function(url){
origin : '', origin : '',
directory : '', directory : '',
base : '', base : '',
filename : '' filename : '',
href : url
}, },
doubleSlash = url.indexOf('://'), doubleSlash = url.indexOf('://'),
search = url.indexOf('?'), search = url.indexOf('?'),
withoutProtocol,
firstSlash; firstSlash;
if(search != -1) { if(search != -1) {
@ -118,18 +125,21 @@ EPUBJS.core.uri = function(url){
if(doubleSlash != -1) { if(doubleSlash != -1) {
uri.protocol = url.slice(0, doubleSlash); uri.protocol = url.slice(0, doubleSlash);
uri.path = url.slice(doubleSlash+3); withoutProtocol = url.slice(doubleSlash+3);
firstSlash = uri.path.indexOf('/'); firstSlash = withoutProtocol.indexOf('/');
if(firstSlash === -1) { if(firstSlash === -1) {
uri.host = uri.path; uri.host = uri.path;
uri.path = "";
} else { } 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.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; uri.base = uri.origin + uri.directory;
// return origin; // return origin;

View file

@ -151,7 +151,7 @@ EPUBJS.EpubCFI.prototype.getElement = function(cfi, _doc) {
// sections.shift(); //-- html // sections.shift(); //-- html
while(sections.length > 0) { while(sections && sections.length > 0) {
part = sections.shift(); part = sections.shift();

View file

@ -3,12 +3,24 @@ EPUBJS.Parser = function(baseUrl){
}; };
EPUBJS.Parser.prototype.container = function(containerXml){ EPUBJS.Parser.prototype.container = function(containerXml){
//-- <rootfile full-path="OPS/package.opf" media-type="application/oebps-package+xml"/> //-- <rootfile full-path="OPS/package.opf" media-type="application/oebps-package+xml"/>
var rootfile = containerXml.querySelector("rootfile"), var rootfile, fullpath, folder, encoding;
fullpath = rootfile.getAttribute('full-path'),
folder = EPUBJS.core.folder(fullpath), if(!containerXml) {
encoding = containerXml.xmlEncoding; 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 //-- Now that we have the path we can parse the contents
return { return {
@ -19,29 +31,66 @@ EPUBJS.Parser.prototype.container = function(containerXml){
}; };
EPUBJS.Parser.prototype.identifier = function(packageXml){ 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"); return this.getElementText(metadataNode, "identifier");
}; };
EPUBJS.Parser.prototype.packageContents = function(packageXml, baseUrl){ EPUBJS.Parser.prototype.packageContents = function(packageXml, baseUrl){
var parse = this; var parse = this;
var metadataNode, manifestNode, spineNode;
var manifest, navPath, tocPath, coverPath;
var spineNodeIndex;
var spine;
var spineIndexByURL;
if(baseUrl) this.baseUrl = baseUrl; if(baseUrl) this.baseUrl = baseUrl;
var metadataNode = packageXml.querySelector("metadata"), if(!packageXml) {
manifestNode = packageXml.querySelector("manifest"), console.error("Package File Not Found");
spineNode = packageXml.querySelector("spine"); return;
}
var manifest = parse.manifest(manifestNode), metadataNode = packageXml.querySelector("metadata");
navPath = parse.findNavPath(manifestNode), if(!metadataNode) {
tocPath = parse.findTocPath(manifestNode), console.error("No Metadata Found");
coverPath = parse.findCoverPath(manifestNode); return;
}
var spineNodeIndex = Array.prototype.indexOf.call(spineNode.parentNode.childNodes, spineNode); manifestNode = packageXml.querySelector("manifest");
if(!metadataNode) {
console.error("No Manifest Found");
return;
}
var spine = parse.spine(spineNode, manifest); spineNode = packageXml.querySelector("spine");
if(!metadataNode) {
console.error("No Spine Found");
return;
}
var spineIndexByURL = {}; manifest = parse.manifest(manifestNode);
navPath = parse.findNavPath(manifestNode);
tocPath = parse.findTocPath(manifestNode);
coverPath = parse.findCoverPath(manifestNode);
spineNodeIndex = Array.prototype.indexOf.call(spineNode.parentNode.childNodes, spineNode);
spine = parse.spine(spineNode, manifest);
spineIndexByURL = {};
spine.forEach(function(item){ spine.forEach(function(item){
spineIndexByURL[item.href] = item.index; spineIndexByURL[item.href] = item.index;
}); });

View file

@ -34,7 +34,12 @@ EPUBJS.replace.links = function(_store, full, done, link){
//-- Handle replacing urls in CSS //-- Handle replacing urls in CSS
if(link.getAttribute("rel") === "stylesheet") { 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{ }else{
_store.getUrl(full).then(done); _store.getUrl(full).then(done);
} }

View file

@ -49,10 +49,14 @@ EPUBJS.Unarchiver.prototype.getXml = function(url, encoding){
EPUBJS.Unarchiver.prototype.getUrl = function(url, mime){ EPUBJS.Unarchiver.prototype.getUrl = function(url, mime){
var unarchiver = this; var unarchiver = this;
var deferred = new RSVP.defer(); 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; 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) { if(url in this.urlCache) {
deferred.resolve(this.urlCache[url]); deferred.resolve(this.urlCache[url]);
@ -71,7 +75,8 @@ EPUBJS.Unarchiver.prototype.getUrl = function(url, mime){
EPUBJS.Unarchiver.prototype.getText = function(url, encoding){ EPUBJS.Unarchiver.prototype.getText = function(url, encoding){
var unarchiver = this; var unarchiver = this;
var deferred = new RSVP.defer(); 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; var _URL = window.URL || window.webkitURL || window.mozURL;
if(!entry) console.error(url); if(!entry) console.error(url);