diff --git a/src/calibre/ebooks/oeb/display/cfi.coffee b/src/calibre/ebooks/oeb/display/cfi.coffee index 2a15f03b0d..5020174664 100644 --- a/src/calibre/ebooks/oeb/display/cfi.coffee +++ b/src/calibre/ebooks/oeb/display/cfi.coffee @@ -6,6 +6,10 @@ Released under the GPLv3 License Based on code originally written by Peter Sorotkin (http://code.google.com/p/epub-revision/source/browse/trunk/src/samples/cfi/epubcfi.js) + Improvements with respect to that code: + 1. Works on all browsers (WebKit, Firefox and IE >= 8) + 2. Works if the point is after the last text character in an element + 3. Works for elements that are scrollable (i.e. have their own scrollbars) To check if this script is compatible with the current browser, call window.cfi.is_compatible() it will throw an exception if not compatible. @@ -470,7 +474,12 @@ class CanonicalFragmentIdentifier x = (point.a*rect.left + (1-point.a)*rect.right) y = (rect.top + rect.bottom)/2 [x, y] = viewport_to_document(x, y, ndoc) - span.outerHTML = span.innerHTML + tn = if span.firstChild then span.firstChild.nodeValue else '' + tn = ndoc.createTextNode(tn) + p = span.parentNode + p.insertBefore(tn, span) + p.removeChild(span) + p.normalize() if callback callback(x, y) else diff --git a/src/calibre/ebooks/oeb/display/test/cfi-test.coffee b/src/calibre/ebooks/oeb/display/test/cfi-test.coffee index 427a74c6e9..ab82c32df4 100644 --- a/src/calibre/ebooks/oeb/display/test/cfi-test.coffee +++ b/src/calibre/ebooks/oeb/display/test/cfi-test.coffee @@ -13,47 +13,42 @@ log = (error) -> else if process?.stdout?.write process.stdout.write(error + '\n') -viewport_top = (node) -> - $(node).offset().top - window.pageYOffset - -viewport_left = (node) -> - $(node).offset().left - window.pageXOffset - -show_cfi = (dont_seek) -> +show_cfi = () -> if window.current_cfi fn = (x, y) -> - ms = $("#marker") - ms.css('visibility', 'visible') - # This strange sequence is needed to get it to work in Chrome - # when called from the onload handler - ms.offset({left:x-1, top:y-30}) - ms.offset() - ms.offset({left:x-1, top:y-30}) - + ms = document.getElementById("marker").style + ms.display = 'block' + ms.top = y - 30 + 'px' + ms.left = x - 1 + 'px' window.cfi.scroll_to(window.current_cfi, fn) null - mark_and_reload = (evt) -> - window.current_cfi = window.cfi.at(evt.clientX, evt.clientY) - if window.current_cfi - fn = () -> + # Remove image in case the click was on the image itself, we want the cfi to + # be on the underlying element + ms = document.getElementById("marker") + ms.parentNode.removeChild(ms) + + fn = () -> + window.current_cfi = window.cfi.at(evt.clientX, evt.clientY) + if window.current_cfi epubcfi = "#epubcfi(#{ window.current_cfi })" newloc = window.location.href.replace(/#.*$/, '') + epubcfi window.location.replace(newloc) document.getElementById('current-cfi').innerHTML = window.current_cfi window.location.reload() - setTimeout(fn, 1) + setTimeout(fn, 1) null window.onload = -> - window.onscroll = show_cfi - window.onresize = show_cfi + try + window.cfi.is_compatible() + catch error + alert(error) + return document.onclick = mark_and_reload - for iframe in document.getElementsByTagName("iframe") - iframe.contentWindow.onscroll = show_cfi r = location.hash.match(/#epubcfi\((.+)\)$/) if r window.current_cfi = r[1] diff --git a/src/calibre/ebooks/oeb/display/test/index.html b/src/calibre/ebooks/oeb/display/test/index.html index f43848c5e7..afeae055a4 100644 --- a/src/calibre/ebooks/oeb/display/test/index.html +++ b/src/calibre/ebooks/oeb/display/test/index.html @@ -3,7 +3,6 @@