This commit is contained in:
Kovid Goyal 2020-07-20 15:05:39 +05:30
parent ad490e9cd6
commit 29215758c3
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 10 additions and 4 deletions

View File

@ -149,6 +149,8 @@ def text_length_in_range_wrapper(node):
def adjust_node_for_text_offset(node):
if node.parentNode and node.parentNode.dataset.calibreRangeWrapper:
node = node.parentNode
offset = 0
adjusted = False
while True:
@ -161,6 +163,10 @@ def adjust_node_for_text_offset(node):
offset += text_length_in_range_wrapper(p)
node = p
adjusted = True
if adjusted and node.nodeType is Node.ELEMENT_NODE and node.dataset.calibreRangeWrapper:
if not node.firstChild or node.firstChild.nodeType is not Node.TEXT_NODE:
node.insertBefore(document.createTextNode(''), node.firstChild or None)
node = node.firstChild
return node, offset, adjusted
@ -221,10 +227,7 @@ def encode(doc, node, offset, tail):
node = node.firstChild
if is_text_node(node):
offset = offset or 0
adjust_node = node
if node.parentNode and node.parentNode.dataset.calibreRangeWrapper:
adjust_node = node.parentNode
adjusted_node, additional_offset, adjusted = adjust_node_for_text_offset(adjust_node)
adjusted_node, additional_offset, adjusted = adjust_node_for_text_offset(node)
if adjusted:
node = adjusted_node
offset += additional_offset

View File

@ -95,3 +95,6 @@ def cfi_with_range_wrappers():
p.appendChild(E.span('abc', data_calibre_range_wrapper='7'))
with_wrapper = encode(document, p.firstChild.firstChild, 0)
assert_equal(without_wrapper, with_wrapper)
p.appendChild(document.createTextNode('def'))
after_wrapper = encode(document, p.lastChild, 1)
assert_equal(after_wrapper, f'{path_to_p}/1:4')