Tests for encoding with range wrappers

This commit is contained in:
Kovid Goyal 2020-04-07 09:14:15 +05:30
parent 77e5bea56f
commit 5f0440364a
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 23 additions and 0 deletions

View File

@ -213,6 +213,10 @@ def encode(doc, node, offset, tail):
if not node.firstChild:
node.appendChild(document.createTextNode(''))
node = node.firstChild
elif node.dataset.calibreRangeWrapper:
if not node.firstChild:
node.appendChild(document.createTextNode(''))
node = node.firstChild
if is_text_node(node):
offset = offset or 0
if node.parentNode and node.parentNode.dataset.calibreRangeWrapper:

View File

@ -51,3 +51,22 @@ def cfi_roundtripping():
assert_equal(decode(path_to_span), {'node': span})
assert_equal(encode(document, span.firstChild, 1), f'{path_to_span}/1:1')
assert_equal(decode(f'{path_to_span}/1:1'), {'node': span.firstChild, 'offset': 1})
@test
def cfi_with_range_wrappers():
document.body.appendChild(E.p('abc'))
p = document.body.firstChild
path_to_p = encode(document, p)
p.appendChild(E.span('def', data_calibre_range_wrapper='1'))
rw1 = p.lastChild
p.appendChild(document.createTextNode('123'))
assert_equal(encode(document, p.firstChild, 1), f'{path_to_p}/1:1')
assert_equal(encode(document, rw1), f'{path_to_p}/1:3')
assert_equal(encode(document, rw1.firstChild, 1), f'{path_to_p}/1:4')
assert_equal(encode(document, p.lastChild, 1), f'{path_to_p}/1:7')
p.appendChild(E.span('456', E.i('789'), data_calibre_range_wrapper='2'))
assert_equal(encode(document, p.lastChild.firstChild, 1), f'{path_to_p}/1:10')
itag = p.querySelector('i')
assert_equal(encode(document, itag), f'{path_to_p}/2')
assert_equal(encode(document, itag.firstChild, 2), f'{path_to_p}/2/1:2')