From 07f72d2d9491cb53e2848ced6b71a876a9a8ce46 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 14 Dec 2021 12:30:00 +0530 Subject: [PATCH] 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. --- src/pyj/range_utils.pyj | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/pyj/range_utils.pyj b/src/pyj/range_utils.pyj index 15b6a2821c..bae04ef2bd 100644 --- a/src/pyj/range_utils.pyj +++ b/src/pyj/range_utils.pyj @@ -99,11 +99,6 @@ def wrap_range(r, wrapper): except: wrapper.appendChild(r.extractContents()) 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 some text - r.commonAncestorContainer.normalize() - def create_wrapper_function(wrapper_elem, r, intersecting_wrappers, process_wrapper, all_wrappers): 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'[]' wrap_node = create_wrapper_function(wrapper_elem, r, intersecting_wrappers, process_wrapper, all_wrappers) 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 some text + r.commonAncestorContainer.normalize() crw = wrapper_elem.dataset.calibreRangeWrapper v'delete intersecting_wrappers[crw]' if class_to_add_to_last and all_wrappers.length: