mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
When creating range wrappers, normalize parent nodes to ensure there are no empty text nodes, as these mess up CFI calculations
This commit is contained in:
parent
11d00b59ea
commit
d1a5850619
@ -104,6 +104,8 @@ def create_wrapper_function(wrapper_elem, r, intersecting_wrappers, process_wrap
|
|||||||
current_range = (node.ownerDocument or document).createRange()
|
current_range = (node.ownerDocument or document).createRange()
|
||||||
current_wrapper = wrapper_elem.cloneNode()
|
current_wrapper = wrapper_elem.cloneNode()
|
||||||
current_range.selectNodeContents(node)
|
current_range.selectNodeContents(node)
|
||||||
|
# adjust start and end in case the current node is one of the
|
||||||
|
# boundaries of the original range
|
||||||
if node.isSameNode(start_node):
|
if node.isSameNode(start_node):
|
||||||
current_range.setStart(node, start_offset)
|
current_range.setStart(node, start_offset)
|
||||||
start_node = current_wrapper
|
start_node = current_wrapper
|
||||||
@ -116,6 +118,10 @@ def create_wrapper_function(wrapper_elem, r, intersecting_wrappers, process_wrap
|
|||||||
if crw:
|
if crw:
|
||||||
intersecting_wrappers[crw] = True
|
intersecting_wrappers[crw] = True
|
||||||
current_range.surroundContents(current_wrapper)
|
current_range.surroundContents(current_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>
|
||||||
|
current_range.commonAncestorContainer.normalize()
|
||||||
if process_wrapper:
|
if process_wrapper:
|
||||||
process_wrapper(current_wrapper)
|
process_wrapper(current_wrapper)
|
||||||
all_wrappers.push(current_wrapper)
|
all_wrappers.push(current_wrapper)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user