Skip to content
This repository was archived by the owner on May 7, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions js/Readium.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@


define(['readium_shared_js/globals', 'text!version.json', 'jquery', 'underscore', 'readium_shared_js/views/reader_view', 'readium_js/epub-fetch/publication_fetcher',
'readium_js/epub-model/package_document_parser', 'readium_js/epub-fetch/iframe_zip_loader', 'readium_shared_js/views/iframe_loader'
'readium_js/epub-model/package_document_parser', 'readium_js/epub-fetch/iframe_zip_loader', 'readium_shared_js/views/iframe_loader', 'readium_shared_js/XmlParse'
],
function (Globals, versionText, $, _, ReaderView, PublicationFetcher,
PackageParser, IframeZipLoader, IframeLoader) {
PackageParser, IframeZipLoader, IframeLoader, XmlParse) {

var DEBUG_VERSION_GIT = false;

Expand Down Expand Up @@ -67,6 +67,8 @@ define(['readium_shared_js/globals', 'text!version.json', 'jquery', 'underscore'
contentDocumentHtml = contentDocumentHtml.replace(/<title>[\s]*<\/title>/g, '<title>TITLE</title>');
contentDocumentHtml = contentDocumentHtml.replace(/<title[\s]*\/>/g, '<title>TITLE</title>');

contentDocumentHtml = XmlParse.preprocess(contentDocumentHtml);

return contentDocumentHtml;
};

Expand Down
7 changes: 4 additions & 3 deletions js/epub-fetch/content_document_fetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// prior written permission.

define(
['jquery', 'underscore', 'URIjs', './discover_content_type'],
function ($, _, URI, ContentTypeDiscovery) {
['jquery', 'underscore', 'URIjs', './discover_content_type', 'readium_shared_js/XmlParse'],
function ($, _, URI, ContentTypeDiscovery, XmlParse) {


var ContentDocumentFetcher = function (publicationFetcher, spineItem, loadedDocumentUri, publicationResourcesCache, contentDocumentTextPreprocessor) {
Expand Down Expand Up @@ -44,7 +44,7 @@ define(

this.resolveInternalPackageResources = function (resolvedDocumentCallback, onerror) {

_contentDocumentDom = _publicationFetcher.markupParser.parseMarkup(_contentDocumentText, _srcMediaType);
_contentDocumentDom = XmlParse.fromString(_contentDocumentText, _srcMediaType);
setBaseUri(_contentDocumentDom, loadedDocumentUri);

var resolutionDeferreds = [];
Expand Down Expand Up @@ -331,6 +331,7 @@ define(
function resolveDocumentImages(resolutionDeferreds, onerror) {
resolveResourceElements('img', 'src', 'blob', resolutionDeferreds, onerror);
resolveResourceElements('image', 'xlink:href', 'blob', resolutionDeferreds, onerror);
resolveResourceElements('image', 'href', 'blob', resolutionDeferreds, onerror);
}

function resolveDocumentAudios(resolutionDeferreds, onerror) {
Expand Down
2 changes: 1 addition & 1 deletion js/epub-fetch/iframe_zip_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ define(['URIjs', 'readium_shared_js/views/iframe_loader', 'underscore', './disco

$.ajax({
url: path,
dataType: 'html',
dataType: 'text',
async: true,
success: function (result) {

Expand Down
33 changes: 0 additions & 33 deletions js/epub-fetch/markup_parser.js

This file was deleted.

55 changes: 44 additions & 11 deletions js/epub-fetch/publication_fetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,40 @@
// used to endorse or promote products derived from this software without specific
// prior written permission.

define(['jquery', 'URIjs', './markup_parser', './plain_resource_fetcher', './zip_resource_fetcher',
'./content_document_fetcher', './resource_cache', './encryption_handler', './discover_content_type', 'readium_shared_js/helpers'],
function ($, URI, MarkupParser, PlainResourceFetcher, ZipResourceFetcher, ContentDocumentFetcher,
ResourceCache, EncryptionHandler, ContentTypeDiscovery, Helpers) {
define(['jquery', 'URIjs', './plain_resource_fetcher', './zip_resource_fetcher',
'./content_document_fetcher', './resource_cache', './encryption_handler', './discover_content_type', 'readium_shared_js/helpers', 'readium_shared_js/XmlParse'],
function ($, URI, PlainResourceFetcher, ZipResourceFetcher, ContentDocumentFetcher,
ResourceCache, EncryptionHandler, ContentTypeDiscovery, Helpers, XmlParse) {

var PublicationFetcher = function(ebookURL, jsLibRoot, sourceWindow, cacheSizeEvictThreshold, contentDocumentTextPreprocessor, contentType) {

var self = this;

var _ebookURLOPF = undefined;
var ebookURLTrimmed = ebookURL;
try {
//.absoluteTo("http://readium.org/epub")
ebookURLTrimmed = new URI(ebookURLTrimmed).search('').hash('').toString();
} catch(err) {
console.error(err);
console.log(ebookURL);
}
if (/\.opf$/.test(ebookURLTrimmed)) {

var iSlash = ebookURLTrimmed.lastIndexOf("/");
if (iSlash >= 0) {
console.log("ebookURL is OPF: " + ebookURL);

ebookURL = ebookURLTrimmed.substr(0, iSlash+1);
console.log("ebookURL rebased: " + ebookURL);

_ebookURLOPF = ebookURLTrimmed.substr(iSlash+1, ebookURLTrimmed.length-1);
console.log("ebookURL OPF file (bypass META-INF/container.xml): " + _ebookURLOPF);
}
} else if (/\/META-INF\/container\.xml$/.test(ebookURLTrimmed)) {
ebookURL = ebookURL.substr(0, ebookURL.length-("/META-INF/container.xml".length)+1);
}

self.contentTypePackageReadStrategyMap = {
'application/oebps-package+xml': 'exploded',
'application/epub+zip': 'zipped',
Expand All @@ -38,8 +63,6 @@ define(['jquery', 'URIjs', './markup_parser', './plain_resource_fetcher', './zip
var _contentDocumentTextPreprocessor = contentDocumentTextPreprocessor;
var _contentType = contentType;

this.markupParser = new MarkupParser();

this.initialize = function(callback) {

var isEpubExploded = isExploded();
Expand Down Expand Up @@ -200,7 +223,7 @@ define(['jquery', 'URIjs', './markup_parser', './plain_resource_fetcher', './zip

this.getXmlFileDom = function (xmlFilePathRelativeToPackageRoot, callback, onerror) {
self.getFileContentsFromPackage(xmlFilePathRelativeToPackageRoot, function (xmlFileContents) {
var fileDom = self.markupParser.parseXml(xmlFileContents);
var fileDom = XmlParse.fromString(xmlFileContents, "text/xml");
callback(fileDom);
}, onerror);
};
Expand Down Expand Up @@ -230,7 +253,8 @@ define(['jquery', 'URIjs', './markup_parser', './plain_resource_fetcher', './zip
} else {
_packageDomInitializationDeferred = $.Deferred();
_packageDomInitializationDeferred.done(callback);
self.getPackageFullPath(function (packageFullPath) {

var func = function (packageFullPath) {

_packageFullPath = packageFullPath;
_packageDocumentAbsoluteUrl = _resourceFetcher.resolveURI(_packageFullPath);
Expand All @@ -248,7 +272,14 @@ define(['jquery', 'URIjs', './markup_parser', './plain_resource_fetcher', './zip
_packageDomInitializationDeferred.resolve(packageDom);
_packageDomInitializationDeferred = undefined;
})
}, onerror);
};

// We already know which OPF to open, let's bypass META-INF/container.xml rootFile discovery
if (_ebookURLOPF) {
func(_ebookURLOPF);
} else {
self.getPackageFullPath(func, onerror);
}
}
}
};
Expand All @@ -257,8 +288,10 @@ define(['jquery', 'URIjs', './markup_parser', './plain_resource_fetcher', './zip
// (starting with "/", already relative to the EPUB archive's base folder)
// then the returned value is relativeToPackagePath.
this.convertPathRelativeToPackageToRelativeToBase = function (relativeToPackagePath) {

return new URI(relativeToPackagePath).absoluteTo(_packageFullPath).toString();
var uriStr = new URI(relativeToPackagePath).absoluteTo(_packageFullPath).toString();
// Note that _packageFullPath is just be a relative path (to the root folder that contains ./META-INF/container.xml), and so is the returned path
//console.log(relativeToPackagePath + " /// " + _packageFullPath + " === " + uriStr);
return uriStr;
};

// Note that the relativeToPackagePath parameter can in fact be absolute
Expand Down
40 changes: 29 additions & 11 deletions js/epub-model/package_document.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
// used to endorse or promote products derived from this software without specific
// prior written permission.

define(['jquery', 'underscore', 'URIjs', 'readium_cfi_js'],
function ($, _, URI, epubCFI) {
define(['jquery', 'underscore', 'URIjs', 'readium_cfi_js', 'readium_shared_js/XmlParse'],
function ($, _, URI, epubCFI, XmlParse) {

// Description: This model provides an interface for navigating an EPUB's package document
var PackageDocument = function(packageDocumentURL, packageDocumentDOM, resourceFetcher, metadata, spine, manifest) {
Expand Down Expand Up @@ -86,7 +86,6 @@ define(['jquery', 'underscore', 'URIjs', 'readium_cfi_js'],
return metadata;
};


this.getTocItem = function(){

var item = manifest.getNavItem();
Expand All @@ -110,28 +109,48 @@ define(['jquery', 'underscore', 'URIjs', 'readium_cfi_js'],
return null;
};

this.getTocURI = function() {
var href = this.getToc();
if (href) {
var tocDocumentAbsoluteURL = new URI(href).absoluteTo(packageDocumentURL).toString();

return tocDocumentAbsoluteURL;
}

return null;
};

this.getTocText = function(callback) {
var toc = this.getToc();
if (!toc) {

var item = this.getTocItem();
if (!item) {
console.error("No TOC?!");
callback(undefined);
return;
}

var tocHref = item.href; //this.getToc();
//var tocContentType = item.media_type;

resourceFetcher.relativeToPackageFetchFileContents(toc, 'text', function (tocDocumentText) {
resourceFetcher.relativeToPackageFetchFileContents(tocHref, 'text', function (tocDocumentText) {
callback(tocDocumentText)
}, function (err) {
console.error('ERROR fetching TOC from [' + toc + ']:');
console.error('ERROR fetching TOC from [' + tocHref + ']:');
console.error(err);
callback(undefined);
});
};

this.getTocDom = function(callback) {

var that = this;
this.getTocText(function (tocText) {
if (typeof tocText === 'string') {
var tocDom = (new DOMParser()).parseFromString(tocText, "text/xml");

var item = that.getTocItem();
var tocHref = item.href; //this.getToc();
var tocContentType = item.media_type;

var tocDom = XmlParse.fromString(tocText, tocContentType);
callback(tocDom);
} else {
callback(undefined);
Expand All @@ -151,8 +170,7 @@ define(['jquery', 'underscore', 'URIjs', 'readium_cfi_js'],
$ncxOrderedList = getNcxOrderedList($("navMap", tocDom));
callback($ncxOrderedList[0]);
} else {
var packageDocumentAbsoluteURL = new URI(packageDocumentURL).absoluteTo(document.URL);
var tocDocumentAbsoluteURL = new URI(that.getToc()).absoluteTo(packageDocumentAbsoluteURL);
var tocDocumentAbsoluteURL = that.getTocURI();
// add a BASE tag to change the TOC document's baseURI.
var oldBaseTag = $(tocDom).remove('base');
var newBaseTag = $('<base></base>');
Expand Down
11 changes: 5 additions & 6 deletions js/epub-model/package_document_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
// used to endorse or promote products derived from this software without specific
// prior written permission.

define(['jquery', 'underscore', '../epub-fetch/markup_parser', 'URIjs', './package_document',
'./smil_document_parser', './metadata', './manifest', 'readium_cfi_js'],
function($, _, MarkupParser, URI, PackageDocument, SmilDocumentParser, Metadata,
Manifest, epubCFI) {
define(['jquery', 'underscore', 'URIjs', './package_document',
'./smil_document_parser', './metadata', './manifest', 'readium_cfi_js', 'readium_shared_js/XmlParse'],
function($, _, URI, PackageDocument, SmilDocumentParser, Metadata,
Manifest, epubCFI, XmlParse) {

// `PackageDocumentParser` is used to parse the xml of an epub package
// document and build a javascript object. The constructor accepts an
Expand Down Expand Up @@ -95,8 +95,7 @@ define(['jquery', 'underscore', '../epub-fetch/markup_parser', 'URIjs', './packa

publicationFetcher.relativeToPackageFetchFileContents(pathToIBooksSpecificXml, 'text', function (ibookPropText) {
if(ibookPropText) {
var parser = new MarkupParser();
var propModel = parser.parseXml(ibookPropText);
var propModel = XmlParse.fromString(ibookPropText, "text/xml");
var fixLayoutProp = $("option[name=fixed-layout]", propModel)[0];
if(fixLayoutProp) {
var fixLayoutVal = $(fixLayoutProp).text();
Expand Down
19 changes: 17 additions & 2 deletions js/epub-model/smil_document_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,27 @@ define(['jquery', 'underscore'], function ($, _) {
var propParse = property.split(':');
var destProperty = propParse[propParse.length - 1];

var attr = undefined;
if (propParse.length == 1) { // no XML namespace
attr = fromNode.getAttribute(property);

} else { // XML namespace, such as epub:type

// Note: no need to use Helpers.getEpubTypeRoleAttributeValue() here, as SMIL / XML guarantees epub:type, unlike XHTML vs. HTML EPUB content documents
if (fromNode.getAttributeNS && propParse[0] == "epub") {
attr = fromNode.getAttributeNS('http://www.idpf.org/2007/ops', destProperty);
}
if (!attr) {
attr = fromNode.getAttribute(property) || fromNode.getAttribute(destProperty);
}
}

if (destProperty === "type") {
destProperty = "epubtype";
}

if (fromNode.getAttribute(property) != undefined) {
toItem[destProperty] = fromNode.getAttribute(property);
if (attr) {
toItem[destProperty] = attr;
} else if (isRequired) {
if (defaultValue !== undefined) {
toItem[destProperty] = defaultValue;
Expand Down