From 5f0440364ab5b42cb635846c669b182adb02641a Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 7 Apr 2020 09:14:15 +0530 Subject: [PATCH] Tests for encoding with range wrappers --- src/pyj/read_book/cfi.pyj | 4 ++++ src/pyj/read_book/test_cfi.pyj | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/pyj/read_book/cfi.pyj b/src/pyj/read_book/cfi.pyj index e909d6e3e4..f0b32832f5 100644 --- a/src/pyj/read_book/cfi.pyj +++ b/src/pyj/read_book/cfi.pyj @@ -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: diff --git a/src/pyj/read_book/test_cfi.pyj b/src/pyj/read_book/test_cfi.pyj index 3446823fbd..a60a8b15b7 100644 --- a/src/pyj/read_book/test_cfi.pyj +++ b/src/pyj/read_book/test_cfi.pyj @@ -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')