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.
This commit is contained in:
Kovid Goyal 2021-09-21 22:30:44 +05:30
parent dc2866315b
commit 747b08689e
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -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
# <br>
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)