From 747b08689eafb3f5063c5346fe58f7b707a57a22 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 21 Sep 2021 22:30:44 +0530 Subject: [PATCH] E-book viewer: Fix jumping to highlights in text that occurs after a line break and newline character not working in paged mode. Fixes #1944433 [Viewer: Highlight jumps to wrong page](https://bugs.launchpad.net/calibre/+bug/1944433) Works around a bug in Chromium where the bounding rect for a range containing just the newline is zero size at top-left corner. --- src/pyj/read_book/cfi.pyj | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/pyj/read_book/cfi.pyj b/src/pyj/read_book/cfi.pyj index c73d79e223..13500cebd6 100644 --- a/src/pyj/read_book/cfi.pyj +++ b/src/pyj/read_book/cfi.pyj @@ -632,6 +632,13 @@ def decoded_range_to_document_position(decoded): # Character offset # Get the bounding rect of the range, in (real) viewport space rect = decoded.range.getBoundingClientRect() + inserted_node = None + if not rect.width and not rect.height and not rect.left and not rect.right: + # this happens is range is a text node containing a newline after a + #
+ inserted_node = document.createTextNode('\xa0') + decoded.range.insertNode(inserted_node) + rect = decoded.range.getBoundingClientRect() # Now, get the viewport-space position (vs_pos) we want to scroll to # This is a little complicated. @@ -656,6 +663,8 @@ def decoded_range_to_document_position(decoded): else: block_vs_pos = scroll_viewport.rect_block_start(rect) + if inserted_node: + inserted_node.parentNode.removeChild(inserted_node) # Now, we need to convert these to document X and Y coordinates. return scroll_viewport.viewport_to_document_inline_block(inline_vs_pos, block_vs_pos, decoded.node.ownerDocument)