diff --git a/examples/hypothesis-spreads.html b/examples/hypothesis-spreads.html index 4286eaf..66a63eb 100644 --- a/examples/hypothesis-spreads.html +++ b/examples/hypothesis-spreads.html @@ -218,7 +218,7 @@ menu
- + g chevron_left diff --git a/package.json b/package.json index bf6691d..381b5e5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "epubjs", - "version": "0.3.49", + "version": "0.3.50", "description": "Parse and Render Epubs", "main": "lib/index.js", "module": "src/index.js", diff --git a/src/contents.js b/src/contents.js index f6dd60b..2c9f819 100644 --- a/src/contents.js +++ b/src/contents.js @@ -8,6 +8,11 @@ import { Pane, Highlight, Underline } from "marks-pane"; // Dom events to listen for const EVENTS = ["keydown", "keyup", "keypressed", "mouseup", "mousedown", "click", "touchend", "touchstart"]; +const isWebkit = /AppleWebKit/.test(navigator.userAgent); + +const ELEMENT_NODE = 1; +const TEXT_NODE = 3; + class Contents { constructor(doc, content, cfiBase, sectionIndex) { // Blank Cfi for Parsing @@ -455,12 +460,26 @@ class Contents { } else { // Webkit does not handle collapsed range bounds correctly // https://bugs.webkit.org/show_bug.cgi?id=138949 - if (range.collapsed) { - position = range.getClientRects()[0]; + + // Construct a new non-collapsed range + if (isWebkit) { + let container = range.startContainer; + let newRange = new Range(); + + if (container.nodeType === ELEMENT_NODE) { + position = container.getBoundingClientRect(); + } else if (container.length + 2 < range.startOffset) { + newRange.setStart(container, range.startOffset); + newRange.setEnd(container, range.startOffset + 2); + position = newRange.getBoundingClientRect(); + } else { + newRange.setStart(container, range.startOffset - 2); + newRange.setEnd(container, range.startOffset); + position = newRange.getBoundingClientRect(); + } } else { position = range.getBoundingClientRect(); } - } } diff --git a/src/managers/default/index.js b/src/managers/default/index.js index f318d44..9232d3e 100644 --- a/src/managers/default/index.js +++ b/src/managers/default/index.js @@ -124,11 +124,14 @@ class DefaultViewManager { this.removeEventListeners(); - this.views.each(function(view){ - if (view) { - view.destroy(); - } - }); + if (this.views) { + this.views.each(function(view){ + if (view) { + view.destroy(); + } + }); + } + this.stage.destroy(); @@ -295,7 +298,6 @@ class DefaultViewManager { distX = this.container.scrollWidth - this.layout.delta; } } - this.scrollTo(distX, distY, true); } @@ -760,6 +762,9 @@ class DefaultViewManager { getContents(){ var contents = []; + if (!this.views) { + return contents; + } this.views.each(function(view){ const viewContents = view && view.contents; if (viewContents) {