EPUB CFI: Workaround webkit not reporting the boundingclientrect for an empty span that follows lots of whitespace correctly

This commit is contained in:
Kovid Goyal 2012-01-06 15:53:39 +05:30
parent 3eb93282f6
commit e33e5ede29
3 changed files with 13 additions and 5 deletions

View File

@ -482,7 +482,9 @@ class CanonicalFragmentIdentifier
this.set_current_time(point.node, point.time) this.set_current_time(point.node, point.time)
if point.range != null if point.range != null
# Character offset
r = point.range r = point.range
[so, eo, sc, ec] = [r.startOffset, r.endOffset, r.startContainer, r.endContainer]
node = r.startContainer node = r.startContainer
ndoc = node.ownerDocument ndoc = node.ownerDocument
nwin = ndoc.defaultView nwin = ndoc.defaultView
@ -491,16 +493,19 @@ class CanonicalFragmentIdentifier
r.surroundContents(span) r.surroundContents(span)
span.scrollIntoView() span.scrollIntoView()
fn = -> 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 p = span.parentNode
for node in span.childNodes for node in span.childNodes
span.removeChild(node) span.removeChild(node)
p.insertBefore(node, span) p.insertBefore(node, span)
p.removeChild(span) p.removeChild(span)
p.normalize() 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 if callback
callback(x, y) callback(x, y)
else else

View File

@ -66,8 +66,8 @@ window.onload = ->
base = document.getElementById('first-h1').innerHTML base = document.getElementById('first-h1').innerHTML
document.title = base + ": " + window.current_cfi document.title = base + ": " + window.current_cfi
fn = () -> fn = () ->
show_cfi()
window_ypos(ypos) window_ypos(ypos)
setTimeout(show_cfi, 1)
setTimeout(fn, 100) setTimeout(fn, 100)
null null

View File

@ -96,6 +96,9 @@
demoralized by the charms of pleasure of the moment, so blinded demoralized by the charms of pleasure of the moment, so blinded
by desire, that they cannot foresee</p> by desire, that they cannot foresee</p>
<h2>Lots of collapsed whitespace</h2>
<p>Try clicking the A character after the colon: A suffix</p>
<h2>Lots of nested tags</h2> <h2>Lots of nested tags</h2>
<p>A <span>bunch of <span>nested<span> and</span> <span>sibling</span> <p>A <span>bunch of <span>nested<span> and</span> <span>sibling</span>
tags, all </span> mixed together</span>. <span>Click all</span> tags, all </span> mixed together</span>. <span>Click all</span>