diff --git a/package.json b/package.json index 7e9bd49..f525bf8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "epubjs", - "version": "0.3.52", + "version": "0.3.53", "description": "Parse and Render Epubs", "main": "lib/index.js", "module": "src/index.js", diff --git a/src/book.js b/src/book.js index c2e351d..4ebca38 100644 --- a/src/book.js +++ b/src/book.js @@ -46,7 +46,8 @@ class Book { requestCredentials: undefined, requestHeaders: undefined, encoding: undefined, - replacements: undefined + replacements: undefined, + canonical: undefined }); extend(this.settings, options); @@ -320,6 +321,27 @@ class Book { return resolved; } + /** + * Get a canonical link to a path + * @param {string} path + * @return {string} the canonical path string + */ + canonical(path) { + var url = path; + + if (!path) { + return ""; + } + + if (this.settings.canonical) { + url = this.settings.canonical(path); + } else { + url = this.resolve(path, true); + } + + return url; + } + /** * Determine the type of they input passed to open * @private @@ -369,13 +391,13 @@ class Book { unpack(opf) { this.package = opf; - this.spine.unpack(this.package, this.resolve.bind(this)); + this.spine.unpack(this.package, this.resolve.bind(this), this.canonical.bind(this)); this.resources = new Resources(this.package.manifest, { archive: this.archive, resolver: this.resolve.bind(this), request: this.request.bind(this), - replacements: this.settings.replacements || "base64" + replacements: this.settings.replacements || (this.archived ? "blobUrl" : "base64") }); this.loadNavigation(this.package).then(() => { diff --git a/src/managers/views/iframe.js b/src/managers/views/iframe.js index 635fcab..48aeea8 100644 --- a/src/managers/views/iframe.js +++ b/src/managers/views/iframe.js @@ -352,11 +352,11 @@ class IframeView { var link = this.document.querySelector("link[rel='canonical']"); if (link) { - link.setAttribute("href", this.section.url); + link.setAttribute("href", this.section.canonical); } else { link = this.document.createElement("link"); link.setAttribute("rel", "canonical"); - link.setAttribute("href", this.section.url); + link.setAttribute("href", this.section.canonical); this.document.querySelector("head").appendChild(link); } diff --git a/src/section.js b/src/section.js index efcadfa..a297cfa 100644 --- a/src/section.js +++ b/src/section.js @@ -18,6 +18,7 @@ class Section { this.index = item.index; this.href = item.href; this.url = item.url; + this.canonical = item.canonical; this.next = item.next; this.prev = item.prev; diff --git a/src/spine.js b/src/spine.js index 3636998..6f7291a 100644 --- a/src/spine.js +++ b/src/spine.js @@ -37,7 +37,7 @@ class Spine { * @param {Package} _package * @param {method} resolver URL resolver */ - unpack(_package, resolver) { + unpack(_package, resolver, canonical) { this.items = _package.spine; this.manifest = _package.manifest; @@ -54,11 +54,14 @@ class Spine { if (item.href) { item.url = resolver(item.href, true); + item.canonical = canonical(item.href); } if(manifestItem) { item.href = manifestItem.href; item.url = resolver(item.href, true); + item.canonical = canonical(item.href); + if(manifestItem.properties.length){ item.properties.push.apply(item.properties, manifestItem.properties); } diff --git a/src/utils/replacements.js b/src/utils/replacements.js index d6cc501..700fb75 100644 --- a/src/utils/replacements.js +++ b/src/utils/replacements.js @@ -5,13 +5,13 @@ import Path from "./path"; export function replaceBase(doc, section){ var base; var head; + var url = section.url; + var absolute = (url.indexOf("://") > -1); if(!doc){ return; } - // head = doc.querySelector("head"); - // base = head.querySelector("base"); head = qs(doc, "head"); base = qs(head, "base"); @@ -20,13 +20,18 @@ export function replaceBase(doc, section){ head.insertBefore(base, head.firstChild); } - base.setAttribute("href", section.url); + // Fix for Safari crashing if the url doesn't have an origin + if (!absolute && window && window.location) { + url = window.location.origin + url; + } + + base.setAttribute("href", url); } export function replaceCanonical(doc, section){ var head; var link; - var url = section.url; // window.location.origin + window.location.pathname + "?loc=" + encodeURIComponent(section.url); + var url = section.canonical; if(!doc){ return;