Avoid error when selecting a non-existent range wrapper

This commit is contained in:
Kovid Goyal 2020-07-20 09:48:14 +05:30
parent 60e8ddf968
commit 1f124726c2
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 12 additions and 8 deletions

View File

@ -60,12 +60,17 @@ def unwrap_all_crw():
def select_crw(crw):
nodes = document.querySelectorAll(f'span[data-calibre-range-wrapper="{crw}"]')
if nodes and nodes.length:
r = document.createRange()
r.setStart(nodes[0].firstChild, 0)
r.setEnd(nodes[-1].lastChild, nodes[-1].lastChild.nodeValue.length)
sel = window.getSelection()
sel.removeAllRanges()
sel.addRange(r)
return True
else:
print(f'range-wrapper: {crw} does not exist')
return False
def create_wrapper_function(wrapper_elem, r, intersecting_wrappers, process_wrapper):

View File

@ -730,8 +730,7 @@ class IframeBoss:
set_selection_style(data.style)
elif data.type is 'edit-highlight':
crw_ = {v: k for k, v in Object.entries(annot_id_uuid_map)}[data.uuid]
if crw_:
select_crw(crw_)
if crw_ and select_crw(crw_):
self.ensure_selection_visible()
self.initiate_creation_of_annotation(data.uuid)
elif data.type is 'remove-highlight':