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:
Kovid Goyal 2020-08-20 09:21:27 +05:30
parent 11d00b59ea
commit d1a5850619
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -104,6 +104,8 @@ def create_wrapper_function(wrapper_elem, r, intersecting_wrappers, process_wrap
current_range = (node.ownerDocument or document).createRange()
current_wrapper = wrapper_elem.cloneNode()
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):
current_range.setStart(node, start_offset)
start_node = current_wrapper
@ -116,6 +118,10 @@ def create_wrapper_function(wrapper_elem, r, intersecting_wrappers, process_wrap
if crw:
intersecting_wrappers[crw] = True
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:
process_wrapper(current_wrapper)
all_wrappers.push(current_wrapper)