From fe8edc77c14beaa01e09fb0c989a3f94bd13cdd0 Mon Sep 17 00:00:00 2001 From: Fred Chasen Date: Wed, 23 Jul 2014 17:02:22 -0400 Subject: [PATCH] Updated URL parsing to handle remote book paths --- examples/basic-dev.html | 2 +- lib/epubjs/book.js | 11 ++++++++--- test/tests/epub.js | 30 +++++++++++++++++++++++++----- 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/examples/basic-dev.html b/examples/basic-dev.html index aa6225f..7df78fc 100644 --- a/examples/basic-dev.html +++ b/examples/basic-dev.html @@ -79,7 +79,7 @@ var currentSection; var currentSectionIndex = 7; - var book = ePub("../books/moby-dick/OPS/package.opf"); + var book = ePub("https://s3.amazonaws.com/moby-dick/OPS/package.opf"); book.loaded.navigation.then(function(nav){ var toc = nav.get(); var $select = document.getElementById("toc"), diff --git a/lib/epubjs/book.js b/lib/epubjs/book.js index a68efc9..848df8d 100644 --- a/lib/epubjs/book.js +++ b/lib/epubjs/book.js @@ -3,7 +3,7 @@ EPUBJS.Book = function(_url){ this.opening = new RSVP.defer(); this.opened = this.opening.promise; - this.baseUrl = undefined; + this.url = undefined; this.spine = undefined; @@ -59,9 +59,14 @@ EPUBJS.Book.prototype.open = function(_url){ // Find path to the Container if(uri.extension === "opf") { // Direct link to package, no container - this.packageUrl = uri.path; + this.packageUrl = uri.href; this.containerUrl = ''; - this.url = uri.directory; + + if(uri.origin) { + this.url = uri.origin + "/" + uri.directory; + } else { + this.url = uri.directory; + } epubPackage = this.request(this.packageUrl); diff --git a/test/tests/epub.js b/test/tests/epub.js index 27ad249..da3d72f 100644 --- a/test/tests/epub.js +++ b/test/tests/epub.js @@ -30,6 +30,26 @@ asyncTest("Open using ePub(/path/to/epub/package.opf)", 1, function() { }); +asyncTest("Open Remote ePub", 1, function() { + + var book = ePub("https://s3.amazonaws.com/moby-dick/"); + book.opened.then(function(){ + equal( book.packageUrl, "https://s3.amazonaws.com/moby-dick/OPS/package.opf", "packageUrl is set" ); + start(); + }); + +}); + +asyncTest("Open Remote ePub from Package", 1, function() { + + var book = ePub("https://s3.amazonaws.com/moby-dick/OPS/package.opf"); + book.opened.then(function(){ + equal( book.packageUrl, "https://s3.amazonaws.com/moby-dick/OPS/package.opf", "packageUrl is set" ); + start(); + }); + +}); + asyncTest("Find Epub Package", 1, function() { var book = ePub("../books/moby-dick/OPS/package.opf"); @@ -126,8 +146,8 @@ asyncTest("Find Item by Href", 2, function() { var book = ePub("../books/moby-dick/OPS/package.opf"); book.opened.then(function(){ var section = book.spine.get("chapter_001.xhtml"); - equal( section.href, "epigraph_001.xhtml", "chap 1 spine item href found" ); - equal( section.cfiBase, "/6/12[xepigraph_001]", "chap 1 spine item cfi found" ); + equal( section.href, "chapter_001.xhtml", "chap 1 spine item href found" ); + equal( section.cfiBase, "/6/14[xchapter_001]", "chap 1 spine item cfi found" ); start(); }); @@ -139,8 +159,8 @@ asyncTest("Find Item by ID", 2, function() { var book = ePub("../books/moby-dick/OPS/package.opf"); book.opened.then(function(){ var section = book.spine.get("#xchapter_050"); - equal( section.href, "chapter_049.xhtml", "chap 50 spine item href found" ); - equal( section.cfiBase, "/6/110[xchapter_049]", "chap 50 spine item cfi found" ); + equal( section.href, "chapter_050.xhtml", "chap 50 spine item href found" ); + equal( section.cfiBase, "/6/112[xchapter_050]", "chap 50 spine item cfi found" ); start(); }); @@ -153,7 +173,7 @@ asyncTest("Render Spine Item", 1, function() { book.opened.then(function(){ var section = book.spine.get("#xchapter_050"); section.render().then(function(content){ - equal( content.substring(303, 334), "

Chapter 49. The Hyena.

", "Chapter text rendered as string" ); + equal( content.substring(303, 355), "

Chapter 50. Ahab’s Boat and Crew. Fedallah.

", "Chapter text rendered as string" ); }); start();