1
0
Fork 0
mirror of https://github.com/futurepress/epub.js.git synced 2025-10-04 15:09:16 +02:00

Added basic tests and started docs

This commit is contained in:
Fred Chasen 2013-07-21 21:05:19 -07:00
parent 660d570c02
commit b8a8ca9d3b
12 changed files with 2970 additions and 14 deletions

View file

@ -501,6 +501,8 @@ EPUBJS.Book.prototype.displayChapter = function(chap, end){
cfi,
pos;
if(!this.render) return false;
if(_.isNumber(chap)){
pos = chap;
}else{
@ -552,7 +554,11 @@ EPUBJS.Book.prototype.displayChapter = function(chap, end){
}
EPUBJS.Book.prototype.nextPage = function(){
var next = this.render.nextPage();
var next;
if(!this.render) return;
next = this.render.nextPage();
if(!next){
return this.nextChapter();
@ -560,7 +566,11 @@ EPUBJS.Book.prototype.nextPage = function(){
}
EPUBJS.Book.prototype.prevPage = function() {
var prev = this.render.prevPage();
var prev;
if(!this.render) return;
prev = this.render.prevPage();
if(!prev){
return this.prevChapter();
@ -587,6 +597,8 @@ EPUBJS.Book.prototype.goto = function(url){
spinePos = this.spineIndexByURL[absoluteURL],
book;
if(!this.render) return;
//-- If link fragment only stay on current chapter
if(!chapter){
spinePos = this.chapter.spinePos;
@ -670,7 +682,7 @@ EPUBJS.Book.prototype.setStyle = function(style, val, prefixed) {
if(this.render) this.render.setStyle(style, val, prefixed);
}
EPUBJS.Book.prototype.removeStyle = function(style, val, prefixed) {
EPUBJS.Book.prototype.removeStyle = function(style) {
if(this.render) this.render.removeStyle(style);
delete this.settings.styles[style];
@ -1967,7 +1979,7 @@ EPUBJS.Renderer.prototype.nextPage = function(){
this.currentLocationCfi = this.getPageCfi();
this.book.trigger("book:pageChanged", this.currentLocationCfi);
this.book.trigger("renderer:pageChanged", this.currentLocationCfi);
return this.chapterPos;
@ -1986,7 +1998,7 @@ EPUBJS.Renderer.prototype.prevPage = function(){
this.currentLocationCfi = this.getPageCfi();
this.book.trigger("book:pageChanged", this.currentLocationCfi);
this.book.trigger("renderer:pageChanged", this.currentLocationCfi);
return this.chapterPos;
}else{

4
build/epub.min.js vendored

File diff suppressed because one or more lines are too long

4
demo/js/epub.min.js vendored

File diff suppressed because one or more lines are too long

187
documentation/README.md Normal file
View file

@ -0,0 +1,187 @@
# EPUB.JS Documentation
## Getting Started
Grab the code here:
```html
<div onclick="Book.prevPage();"></div>
<div id="area"></div>
<div onclick="Book.nextPage();"></div>
<script>
var Book = ePub("url/to/book/", { restore: true });
Book.renderTo("area");
</script>
```
## Methods
#### ePub(bookPath, options)
Creates a new EPUBJS.Book()
bookPath is a optional convience method
that will start loading the book at that given path
```javascript
var Book = ePub("url/to/book/"); // With default options
```
```javascript
var Book = ePub({ restore: true });
Book.open("url/to/book/"); // Books can be opened later
```
Options:
```javascript
{
bookPath : null,
version: 1, // Changing will cause stored Book information to be reloaded
restore: false, // Skips parsing epub contents, loading from localstorage instead
storage: false, // true (auto) or false (none) | override: 'ram', 'websqldatabase', 'indexeddb', 'filesystem'
spreads: true, // Displays two columns
fixedLayout : false, //-- Will turn off pagination
styles : {}, // Styles to be applied to epub
width : false,
height: false,
}
```
Intially you'll probably just want to turn on restore.
The width and height will be set to the containing element's dimensions.
```javascript
var Book = ePub("url/to/book/", { restore: true });
```
The following examples will refer to this ePub variable as book.
#### Book.open(bookPath)
Will open and parse a book at the given path.
```javascript
var Book = ePub({ restore: true });
Book.open("url/to/book/"); // Books can be opened later
```
Books can be compressed epub.
See section X for additional information about handling these
```javascript
Book.open("url/to/book.epub");
```
#### Book.renderTo(element)
Appends the iframe that will contain the rendered book to a element.
Returns a promise with the render object after the first chapter has been loaded
```javascript
var Book = ePub("url/to/book/", { restore: true });
var $el = document.getElementById("div-id");
Book.renderTo($el);
```
renderTo can take a element id as a string.
```javascript
var Book = ePub("url/to/book/");
Book.renderTo("div-id");
```
#### Book.nextPage() / Book.prevPage()
Changes the page the book is on.
If on the first or last page of a chapter, the next chapter will be loaded.
```html
<div onclick="Book.prevPage();"></div>
<div onclick="Book.nextPage();"></div>
```
if the book has not been rendered yet, page changes will have no effect.
#### Book.displayChapter(chap, end)
Loads book chapter at a given spine position or epub CFI string.
Returns a promise with the render after the given chapter has been loaded.
```javascript
Book.displayChapter('/6/4[chap01ref]!/4[body01]/10');
```
Setting End to true will advance to the last page of the chapter.
```javascript
Book.displayChapter(3, true);
```
#### Book.goto(url)
Loads book chapter that has the given url
Returns a promise with the render after the given chapter has been loaded
```javascript
var skip = Book.goto("chapter_001.xhtml");
skip.then(function(){
console.log("On Chapter 1");
})
```
This is often used to create a table of contents, with links to specific chapters.
#### Book.setStyle(style, val, prefixed)
Adds style to be attached to the body element rendered book.
One common use is increasing font-size.
```javascript
Book.setStyle("font-size", "1.2em");
```
#### Book.removeStyle(style)
Removes a style from the rendered book
#### Book.destroy()
Remove the appended iframe and cleans up event listeners.
### Promises
#### Book.getMetadata()
```javascript
Book.getMetadata().then(function(meta){
document.title = meta.bookTitle+" "+meta.creator;
});
```
#### Book.getToc()
```javascript
Book.getToc().then(function(toc){
console.log(toc);
});
```
## Events
book:ready
book:stored
book:online
book:offline
renderer:resized
renderer:pageChanged
renderer:chapterDisplayed
renderer:chapterUnloaded

View file

View file

@ -436,6 +436,8 @@ EPUBJS.Book.prototype.displayChapter = function(chap, end){
cfi,
pos;
if(!this.render) return false;
if(_.isNumber(chap)){
pos = chap;
}else{
@ -487,7 +489,11 @@ EPUBJS.Book.prototype.displayChapter = function(chap, end){
}
EPUBJS.Book.prototype.nextPage = function(){
var next = this.render.nextPage();
var next;
if(!this.render) return;
next = this.render.nextPage();
if(!next){
return this.nextChapter();
@ -495,7 +501,11 @@ EPUBJS.Book.prototype.nextPage = function(){
}
EPUBJS.Book.prototype.prevPage = function() {
var prev = this.render.prevPage();
var prev;
if(!this.render) return;
prev = this.render.prevPage();
if(!prev){
return this.prevChapter();
@ -522,6 +532,8 @@ EPUBJS.Book.prototype.goto = function(url){
spinePos = this.spineIndexByURL[absoluteURL],
book;
if(!this.render) return;
//-- If link fragment only stay on current chapter
if(!chapter){
spinePos = this.chapter.spinePos;
@ -605,7 +617,7 @@ EPUBJS.Book.prototype.setStyle = function(style, val, prefixed) {
if(this.render) this.render.setStyle(style, val, prefixed);
}
EPUBJS.Book.prototype.removeStyle = function(style, val, prefixed) {
EPUBJS.Book.prototype.removeStyle = function(style) {
if(this.render) this.render.removeStyle(style);
delete this.settings.styles[style];

View file

@ -367,7 +367,7 @@ EPUBJS.Renderer.prototype.nextPage = function(){
this.currentLocationCfi = this.getPageCfi();
this.book.trigger("book:pageChanged", this.currentLocationCfi);
this.book.trigger("renderer:pageChanged", this.currentLocationCfi);
return this.chapterPos;
@ -386,7 +386,7 @@ EPUBJS.Renderer.prototype.prevPage = function(){
this.currentLocationCfi = this.getPageCfi();
this.book.trigger("book:pageChanged", this.currentLocationCfi);
this.book.trigger("renderer:pageChanged", this.currentLocationCfi);
return this.chapterPos;
}else{

142
tests/core.js Normal file
View file

@ -0,0 +1,142 @@
module('Create');
test("Using ePub(/path/to/epub/)", 1, function() {
var Book = ePub("../demo/moby-dick/");
equal( Book.settings.bookPath, "../demo/moby-dick/", "bookPath is passed to new EPUBJS.Book" );
});
test("Using ePub({ bookPath: '/path/to/epub/' })", 1, function() {
var Book = ePub({ bookPath : "../demo/moby-dick/" });
equal( Book.settings.bookPath, "../demo/moby-dick/", "bookPath is passed to new EPUBJS.Book" );
});
test("Using EPUBJS.Book({ bookPath: '/path/to/epub/' })", 1, function() {
var Book = new EPUBJS.Book({ bookPath : "../demo/moby-dick/" });
equal( Book.settings.bookPath, "../demo/moby-dick/", "bookPath is passed to new EPUBJS.Book" );
});
module('Open');
asyncTest("Get book URL from bookPath", 1, function() {
var Book = ePub();
var opended = Book.open('/demo/moby-dick/');
opended.then(function(){
equal( Book.bookUrl, location.origin + "/demo/moby-dick/", "bookUrl is correctly resolved" );
start();
});
});
asyncTest("Get book URL from ../bookPath", 1, function() {
var Book = ePub();
var opended = Book.open('../demo/moby-dick/');
opended.then(function(){
equal( Book.bookUrl, location.origin + "/demo/moby-dick/", "bookUrl with ../ is correctly resolved" );
start();
});
});
asyncTest("Get book URL from Compressed Epub", 2, function() {
var Book = ePub();
var opended = Book.open('/demo/moby-dick.epub');
opended.then(function(){
equal( Book.contained, true, "Book is contained");
equal( Book.bookUrl, "", "bookUrl from compressed epub should be empty string" );
start();
});
});
module('Contents');
asyncTest("Get Contents from Uncompressed Epub", 5, function() {
var Book = ePub('../demo/moby-dick/');
Book.getMetadata().then(function(meta){
equal( meta.bookTitle, "Moby-Dick", "bookTitle should be set");
equal( meta.creator, "Herman Melville", "creator should be set");
});
Book.getToc().then(function(toc){
equal( toc.length, 140, "All TOC items have loaded");
});
Book.ready.all.then(function(){
ok( true, "Book is all ready" );
equal( Book.cover, Book.settings.contentsPath + "images/9780316000000.jpg", "Cover url is set");
start();
});
});
asyncTest("Get Contents from Compressed Epub", 5, function() {
var Book = ePub('../demo/moby-dick.epub');
Book.getMetadata().then(function(meta){
equal( meta.bookTitle, "Moby-Dick", "bookTitle should be set");
equal( meta.creator, "Herman Melville", "creator should be set");
});
Book.getToc().then(function(toc){
equal( toc.length, 140, "All TOC items have loaded");
});
Book.ready.all.then(function(){
ok( true, "Book is all ready" );
equal( Book.cover, Book.settings.contentsPath + "images/9780316000000.jpg", "Cover url is set");
start();
});
});
asyncTest("Get Contents from Restored Epub", 7, function() {
var BookFirstLoad = ePub('../demo/moby-dick/', { restore: true, reload: true });
equal( BookFirstLoad.settings.restore, true, "Book settings are being restored");
BookFirstLoad.ready.all.then(function(){
BookFirstLoad.destroy();
var Book = ePub('../demo/moby-dick/', { restore: true });
equal( Book.settings.contentsPath, location.origin + "/demo/moby-dick/OPS/", "contentsPath was restored");
Book.getMetadata().then(function(meta){
equal( meta.bookTitle, "Moby-Dick", "bookTitle should be set");
equal( meta.creator, "Herman Melville", "creator should be set");
});
Book.getToc().then(function(toc){
equal( toc.length, 140, "All TOC items have loaded");
});
Book.ready.all.then(function(){
ok( true, "Book is all ready" );
equal( Book.cover, Book.settings.contentsPath + "images/9780316000000.jpg", "Cover url is set");
start();
});
});
});

28
tests/index.html Normal file
View file

@ -0,0 +1,28 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>EPUB.js QUnit Tests</title>
<link rel="stylesheet" href="qunit/qunit.css">
<script src="../libs/jquery/jquery-1.9.0.min.js"></script>
<script src="qunit/qunit.js"></script>
<script src="../demo/js/libs/zip.min.js"></script>
<script src="../build/epub.min.js"></script>
<script>
EPUBJS.filePath = "../demo/js/libs/";
EPUBJS.cssPath = "css/";
</script>
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<script src="core.js"></script>
<script src="render.js"></script>
</body>
</html>

244
tests/qunit/qunit.css Normal file
View file

@ -0,0 +1,244 @@
/**
* QUnit v1.12.0 - A JavaScript Unit Testing Framework
*
* http://qunitjs.com
*
* Copyright 2012 jQuery Foundation and other contributors
* Released under the MIT license.
* http://jquery.org/license
*/
/** Font Family and Sizes */
#qunit-tests, #qunit-header, #qunit-banner, #qunit-testrunner-toolbar, #qunit-userAgent, #qunit-testresult {
font-family: "Helvetica Neue Light", "HelveticaNeue-Light", "Helvetica Neue", Calibri, Helvetica, Arial, sans-serif;
}
#qunit-testrunner-toolbar, #qunit-userAgent, #qunit-testresult, #qunit-tests li { font-size: small; }
#qunit-tests { font-size: smaller; }
/** Resets */
#qunit-tests, #qunit-header, #qunit-banner, #qunit-userAgent, #qunit-testresult, #qunit-modulefilter {
margin: 0;
padding: 0;
}
/** Header */
#qunit-header {
padding: 0.5em 0 0.5em 1em;
color: #8699a4;
background-color: #0d3349;
font-size: 1.5em;
line-height: 1em;
font-weight: normal;
border-radius: 5px 5px 0 0;
-moz-border-radius: 5px 5px 0 0;
-webkit-border-top-right-radius: 5px;
-webkit-border-top-left-radius: 5px;
}
#qunit-header a {
text-decoration: none;
color: #c2ccd1;
}
#qunit-header a:hover,
#qunit-header a:focus {
color: #fff;
}
#qunit-testrunner-toolbar label {
display: inline-block;
padding: 0 .5em 0 .1em;
}
#qunit-banner {
height: 5px;
}
#qunit-testrunner-toolbar {
padding: 0.5em 0 0.5em 2em;
color: #5E740B;
background-color: #eee;
overflow: hidden;
}
#qunit-userAgent {
padding: 0.5em 0 0.5em 2.5em;
background-color: #2b81af;
color: #fff;
text-shadow: rgba(0, 0, 0, 0.5) 2px 2px 1px;
}
#qunit-modulefilter-container {
float: right;
}
/** Tests: Pass/Fail */
#qunit-tests {
list-style-position: inside;
}
#qunit-tests li {
padding: 0.4em 0.5em 0.4em 2.5em;
border-bottom: 1px solid #fff;
list-style-position: inside;
}
#qunit-tests.hidepass li.pass, #qunit-tests.hidepass li.running {
display: none;
}
#qunit-tests li strong {
cursor: pointer;
}
#qunit-tests li a {
padding: 0.5em;
color: #c2ccd1;
text-decoration: none;
}
#qunit-tests li a:hover,
#qunit-tests li a:focus {
color: #000;
}
#qunit-tests li .runtime {
float: right;
font-size: smaller;
}
.qunit-assert-list {
margin-top: 0.5em;
padding: 0.5em;
background-color: #fff;
border-radius: 5px;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
}
.qunit-collapsed {
display: none;
}
#qunit-tests table {
border-collapse: collapse;
margin-top: .2em;
}
#qunit-tests th {
text-align: right;
vertical-align: top;
padding: 0 .5em 0 0;
}
#qunit-tests td {
vertical-align: top;
}
#qunit-tests pre {
margin: 0;
white-space: pre-wrap;
word-wrap: break-word;
}
#qunit-tests del {
background-color: #e0f2be;
color: #374e0c;
text-decoration: none;
}
#qunit-tests ins {
background-color: #ffcaca;
color: #500;
text-decoration: none;
}
/*** Test Counts */
#qunit-tests b.counts { color: black; }
#qunit-tests b.passed { color: #5E740B; }
#qunit-tests b.failed { color: #710909; }
#qunit-tests li li {
padding: 5px;
background-color: #fff;
border-bottom: none;
list-style-position: inside;
}
/*** Passing Styles */
#qunit-tests li li.pass {
color: #3c510c;
background-color: #fff;
border-left: 10px solid #C6E746;
}
#qunit-tests .pass { color: #528CE0; background-color: #D2E0E6; }
#qunit-tests .pass .test-name { color: #366097; }
#qunit-tests .pass .test-actual,
#qunit-tests .pass .test-expected { color: #999999; }
#qunit-banner.qunit-pass { background-color: #C6E746; }
/*** Failing Styles */
#qunit-tests li li.fail {
color: #710909;
background-color: #fff;
border-left: 10px solid #EE5757;
white-space: pre;
}
#qunit-tests > li:last-child {
border-radius: 0 0 5px 5px;
-moz-border-radius: 0 0 5px 5px;
-webkit-border-bottom-right-radius: 5px;
-webkit-border-bottom-left-radius: 5px;
}
#qunit-tests .fail { color: #000000; background-color: #EE5757; }
#qunit-tests .fail .test-name,
#qunit-tests .fail .module-name { color: #000000; }
#qunit-tests .fail .test-actual { color: #EE5757; }
#qunit-tests .fail .test-expected { color: green; }
#qunit-banner.qunit-fail { background-color: #EE5757; }
/** Result */
#qunit-testresult {
padding: 0.5em 0.5em 0.5em 2.5em;
color: #2b81af;
background-color: #D2E0E6;
border-bottom: 1px solid white;
}
#qunit-testresult .module-name {
font-weight: bold;
}
/** Fixture */
#qunit-fixture {
position: absolute;
top: -10000px;
left: -10000px;
width: 1000px;
height: 1000px;
}

2212
tests/qunit/qunit.js Normal file

File diff suppressed because it is too large Load diff

119
tests/render.js Normal file
View file

@ -0,0 +1,119 @@
module('Render');
asyncTest("renderTo element on page", 1, function() {
var Book = ePub('../demo/moby-dick/');
var render = Book.renderTo("qunit-fixture");
var result = function(){
equal( $( "iframe", "#qunit-fixture" ).length, 1, "iframe added successfully" );
start();
};
render.then(result, result);
});
asyncTest("Fit to given width and height", 3, function() {
var Book = ePub('../demo/moby-dick/', { width: 400, height: 600 });
var render = Book.renderTo("qunit-fixture");
var result = function(){
var $iframe = $( "iframe", "#qunit-fixture" ),
$body;
equal( $iframe.length, 1, "iframe added successfully" );
equal( $iframe.width(), 400, "iframe had correct width" );
equal( $iframe.height(), 600, "iframe has correct height" );
start();
};
render.then(result);
});
asyncTest("Go to chapter 1 and advance to next page", 4, function() {
var Book = ePub('../demo/moby-dick/', { width: 400, height: 600 });
var render = Book.renderTo("qunit-fixture");
var result = function(){
var $iframe = $( "iframe", "#qunit-fixture" ),
$body;
equal( $iframe.length, 1, "iframe added successfully" );
start();
$body = $iframe.contents().find("body");
equal( $body.scrollLeft(), 0, "on page 1");
stop();
Book.goto("chapter_001.xhtml").then(function(){
start();
$body = $iframe.contents().find("body");
Book.nextPage();
equal( $body.scrollLeft(), 450, "on page 2");
Book.nextPage();
equal( $body.scrollLeft(), 900, "on page 3");
});
};
render.then(result);
});
asyncTest("Display end of chapter 20 and go to prev page", 3, function() {
var Book = ePub('../demo/moby-dick/', { width: 400, height: 600 });
var render = Book.renderTo("qunit-fixture");
var result = function(){
var $iframe = $( "iframe", "#qunit-fixture" ),
$body;
equal( $iframe.length, 1, "iframe added successfully" );
Book.displayChapter(20, true).then(function(){
start();
$body = $iframe.contents().find("body");
Book.prevPage();
equal( $body.scrollLeft(), 1350, "on last page");
Book.prevPage();
equal( $body.scrollLeft(), 900, "on second to last page ");
});
};
render.then(result);
});
// asyncTest("Add styles to book", 3, function() {
// });