E-book viewer: Fix an error when opening some books with highlights that span inline text formatting

Fixes #1954726 [epub file no longer opens](https://bugs.launchpad.net/calibre/+bug/1954726) [epub file no longer opens](https://bugs.launchpad.net/calibre/+bug/1954726)

normalize() modifies the text nodes, and can potentially result in a
previously selected node no longer having a parent causing wrap() to
throw an exception. In any case normalizing once after all nodes have
been wrapped is faster.
This commit is contained in:
Kovid Goyal 2021-12-14 12:30:00 +05:30
parent 27b2f3a92a
commit 07f72d2d94
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -99,11 +99,6 @@ def wrap_range(r, wrapper):
except: except:
wrapper.appendChild(r.extractContents()) wrapper.appendChild(r.extractContents())
r.insertNode(wrapper) r.insertNode(wrapper)
# remove any empty text nodes created by surroundContents() on either
# side of the wrapper. This happens for instance on Chrome when
# wrapping all text inside <i>some text</i>
r.commonAncestorContainer.normalize()
def create_wrapper_function(wrapper_elem, r, intersecting_wrappers, process_wrapper, all_wrappers): def create_wrapper_function(wrapper_elem, r, intersecting_wrappers, process_wrapper, all_wrappers):
start_node = r.startContainer start_node = r.startContainer
@ -159,6 +154,10 @@ def wrap_text_in_range(styler, r, class_to_add_to_last, process_wrapper):
all_wrappers = v'[]' all_wrappers = v'[]'
wrap_node = create_wrapper_function(wrapper_elem, r, intersecting_wrappers, process_wrapper, all_wrappers) wrap_node = create_wrapper_function(wrapper_elem, r, intersecting_wrappers, process_wrapper, all_wrappers)
text_nodes_in_range(r).map(wrap_node) text_nodes_in_range(r).map(wrap_node)
# remove any empty text nodes created by surroundContents() on either
# side of the wrapper. This happens for instance on Chrome when
# wrapping all text inside <i>some text</i>
r.commonAncestorContainer.normalize()
crw = wrapper_elem.dataset.calibreRangeWrapper crw = wrapper_elem.dataset.calibreRangeWrapper
v'delete intersecting_wrappers[crw]' v'delete intersecting_wrappers[crw]'
if class_to_add_to_last and all_wrappers.length: if class_to_add_to_last and all_wrappers.length: