From e33e5ede29af1a46417e2134e1b88930e9ffc213 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 6 Jan 2012 15:53:39 +0530 Subject: [PATCH] EPUB CFI: Workaround webkit not reporting the boundingclientrect for an empty span that follows lots of whitespace correctly --- src/calibre/ebooks/oeb/display/cfi.coffee | 13 +++++++++---- .../ebooks/oeb/display/test-cfi/cfi-test.coffee | 2 +- src/calibre/ebooks/oeb/display/test-cfi/index.html | 3 +++ 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/calibre/ebooks/oeb/display/cfi.coffee b/src/calibre/ebooks/oeb/display/cfi.coffee index bffa0f1ae4..924241baa4 100644 --- a/src/calibre/ebooks/oeb/display/cfi.coffee +++ b/src/calibre/ebooks/oeb/display/cfi.coffee @@ -482,7 +482,9 @@ class CanonicalFragmentIdentifier this.set_current_time(point.node, point.time) if point.range != null + # Character offset r = point.range + [so, eo, sc, ec] = [r.startOffset, r.endOffset, r.startContainer, r.endContainer] node = r.startContainer ndoc = node.ownerDocument nwin = ndoc.defaultView @@ -491,16 +493,19 @@ class CanonicalFragmentIdentifier r.surroundContents(span) span.scrollIntoView() fn = -> - rect = span.getBoundingClientRect() - x = (point.a*rect.left + (1-point.a)*rect.right) - y = (rect.top + rect.bottom)/2 - [x, y] = viewport_to_document(x, y, ndoc) p = span.parentNode for node in span.childNodes span.removeChild(node) p.insertBefore(node, span) p.removeChild(span) p.normalize() + r.setStart(sc, so) + r.setEnd(ec, eo) + + rect = r.getClientRects()[0] + x = (point.a*rect.left + (1-point.a)*rect.right) + y = (rect.top + rect.bottom)/2 + [x, y] = viewport_to_document(x, y, ndoc) if callback callback(x, y) else diff --git a/src/calibre/ebooks/oeb/display/test-cfi/cfi-test.coffee b/src/calibre/ebooks/oeb/display/test-cfi/cfi-test.coffee index 5b71acbe0c..b9b2b7f5d6 100644 --- a/src/calibre/ebooks/oeb/display/test-cfi/cfi-test.coffee +++ b/src/calibre/ebooks/oeb/display/test-cfi/cfi-test.coffee @@ -66,8 +66,8 @@ window.onload = -> base = document.getElementById('first-h1').innerHTML document.title = base + ": " + window.current_cfi fn = () -> + show_cfi() window_ypos(ypos) - setTimeout(show_cfi, 1) setTimeout(fn, 100) null diff --git a/src/calibre/ebooks/oeb/display/test-cfi/index.html b/src/calibre/ebooks/oeb/display/test-cfi/index.html index 0ff6e3ef2f..24f2499629 100644 --- a/src/calibre/ebooks/oeb/display/test-cfi/index.html +++ b/src/calibre/ebooks/oeb/display/test-cfi/index.html @@ -96,6 +96,9 @@ demoralized by the charms of pleasure of the moment, so blinded by desire, that they cannot foresee

+

Lots of collapsed whitespace

+

Try clicking the A character after the colon: A suffix

+

Lots of nested tags

A bunch of nested and sibling tags, all mixed together. Click all