| 1 |
# vim:fileencoding=utf-8 |
| 2 |
# License: GPL v3 Copyright: 2020, Kovid Goyal <kovid at kovidgoyal.net> |
| 3 |
from __python__ import bound_methods, hash_literals |
| 4 |
|
| 5 |
from elementmaker import E |
| 6 |
|
| 7 |
from read_book.cfi import encode, decode, escape_for_cfi, unescape_from_cfi, cfi_for_selection |
| 8 |
from testing import assert_equal, test |
| 9 |
|
| 10 |
|
| 11 |
@test |
| 12 |
def cfi_escaping(): |
| 13 |
t = 'a^!,1' |
| 14 |
assert_equal(t, unescape_from_cfi(escape_for_cfi(t))) |
| 15 |
|
| 16 |
|
| 17 |
@test |
| 18 |
def cfi_roundtripping(): |
| 19 |
idc = 0 |
| 20 |
|
| 21 |
def nid(): |
| 22 |
nonlocal idc |
| 23 |
idc += 1 |
| 24 |
return idc + '' |
| 25 |
|
| 26 |
document.body.appendChild(E.p('abc')) |
| 27 |
p = document.body.firstChild |
| 28 |
path_to_p = '/2/4/2' |
| 29 |
assert_equal(encode(document, p), path_to_p) |
| 30 |
assert_equal(decode(path_to_p), {'node': p}) |
| 31 |
|
| 32 |
assert_equal(encode(document, p.firstChild), f'{path_to_p}/1:0') |
| 33 |
assert_equal(decode(f'{path_to_p}/1:0'), {'node': p.firstChild, 'offset': 0}) |
| 34 |
|
| 35 |
assert_equal(encode(document, p.firstChild, 1), f'{path_to_p}/1:1') |
| 36 |
assert_equal(decode(f'{path_to_p}/1:1'), {'node': p.firstChild, 'offset': 1}) |
| 37 |
|
| 38 |
p.appendChild(document.createTextNode('def')) |
| 39 |
assert_equal(encode(document, p.firstChild, 5), f'{path_to_p}/1:5') |
| 40 |
assert_equal(p.childNodes.length, 2) |
| 41 |
assert_equal(encode(document, p.lastChild, 1), f'{path_to_p}/1:4') |
| 42 |
assert_equal(decode(f'{path_to_p}/1:5'), {'node': p.lastChild, 'offset': 2}) |
| 43 |
assert_equal(decode(f'{path_to_p}/1:1'), {'node': p.firstChild, 'offset': 1}) |
| 44 |
|
| 45 |
p.appendChild(E.span('123', id=nid())) |
| 46 |
span = p.lastChild |
| 47 |
path_to_span = f'{path_to_p}/2[{span.id}]' |
| 48 |
p.appendChild(document.createTextNode('456')) |
| 49 |
assert_equal(encode(document, p.lastChild, 1), f'{path_to_p}/1:7') |
| 50 |
assert_equal(decode(f'{path_to_p}/1:7'), {'node': p.lastChild, 'offset': 1}) |
| 51 |
assert_equal(encode(document, span), path_to_span) |
| 52 |
assert_equal(decode(path_to_span), {'node': span}) |
| 53 |
assert_equal(encode(document, span.firstChild, 1), f'{path_to_span}/1:1') |
| 54 |
assert_equal(decode(f'{path_to_span}/1:1'), {'node': span.firstChild, 'offset': 1}) |
| 55 |
|
| 56 |
|
| 57 |
@test |
| 58 |
def cfi_with_range_wrappers(): |
| 59 |
document.body.appendChild(E.p('abc', E.span('def', data_calibre_range_wrapper='1'), '123')) |
| 60 |
p = document.body.firstChild |
| 61 |
path_to_p = encode(document, p) |
| 62 |
rw1 = p.querySelector('span') |
| 63 |
assert_equal(encode(document, p.firstChild, 1), f'{path_to_p}/1:1') |
| 64 |
assert_equal(decode(f'{path_to_p}/1:1'), {'node': p.firstChild, 'offset': 1}) |
| 65 |
assert_equal(encode(document, rw1), f'{path_to_p}/1:3') |
| 66 |
assert_equal(decode(f'{path_to_p}/1:3'), {'node': p.firstChild, 'offset': 3}) |
| 67 |
assert_equal(encode(document, rw1.firstChild, 1), f'{path_to_p}/1:4') |
| 68 |
assert_equal(decode(f'{path_to_p}/1:4'), {'node': rw1.firstChild, 'offset': 1}) |
| 69 |
assert_equal(encode(document, p.lastChild, 1), f'{path_to_p}/1:7') |
| 70 |
assert_equal(decode(f'{path_to_p}/1:7'), {'node': p.lastChild, 'offset': 1}) |
| 71 |
|
| 72 |
p.appendChild(E.span('456', E.i('789'), data_calibre_range_wrapper='2')) |
| 73 |
itag = p.querySelector('i') |
| 74 |
assert_equal(encode(document, p.lastChild.firstChild, 1), f'{path_to_p}/1:10') |
| 75 |
assert_equal(decode(f'{path_to_p}/1:10'), {'node': p.lastChild.firstChild, 'offset': 1}) |
| 76 |
assert_equal(encode(document, itag), f'{path_to_p}/2') |
| 77 |
assert_equal(decode(f'{path_to_p}/2'), {'node': itag}) |
| 78 |
assert_equal(encode(document, itag.firstChild, 2), f'{path_to_p}/2/1:2') |
| 79 |
assert_equal(decode(f'{path_to_p}/2/1:2'), {'node': itag.firstChild, 'offset': 2}) |
| 80 |
|
| 81 |
document.body.appendChild(E.p('abc')) |
| 82 |
p = document.body.lastChild |
| 83 |
path_to_p = encode(document, p) |
| 84 |
p.appendChild(document.createTextNode('def')) |
| 85 |
assert_equal(decode(f'{path_to_p}/1:2'), {'node': p.firstChild, 'offset': 2}) |
| 86 |
assert_equal(decode(f'{path_to_p}/3:2'), {'node': p.lastChild, 'offset': 2}) |
| 87 |
|
| 88 |
document.body.appendChild(E.p('abc')) |
| 89 |
p = document.body.lastChild |
| 90 |
path_to_p = encode(document, p) |
| 91 |
without_wrapper = encode(document, p.firstChild, 0) |
| 92 |
assert_equal(without_wrapper, f'{path_to_p}/1:0') |
| 93 |
p.removeChild(p.firstChild) |
| 94 |
p.appendChild(E.span('abc', data_calibre_range_wrapper='7')) |
| 95 |
with_wrapper = encode(document, p.firstChild.firstChild, 0) |
| 96 |
assert_equal(without_wrapper, with_wrapper) |
| 97 |
p.appendChild(document.createTextNode('def')) |
| 98 |
after_wrapper = encode(document, p.lastChild, 1) |
| 99 |
assert_equal(after_wrapper, f'{path_to_p}/1:4') |
| 100 |
|
| 101 |
document.body.appendChild( |
| 102 |
E.p(E.span('abc', data_calibre_range_wrapper='8'), 'def', E.span('123', data_calibre_range_wrapper='9'), '456')) |
| 103 |
p = document.body.lastChild |
| 104 |
path_to_p = encode(document, p) |
| 105 |
rw = p.querySelectorAll('span')[-1] |
| 106 |
assert_equal(decode(f'{path_to_p}/1:0'), {'node': p.firstChild.firstChild, 'offset': 0}) |
| 107 |
assert_equal(decode(f'{path_to_p}/1:5'), {'node': p.firstChild.nextSibling, 'offset': 2}) |
| 108 |
assert_equal(decode(f'{path_to_p}/1:7'), {'node': rw.firstChild, 'offset': 1}) |
| 109 |
assert_equal(decode(f'{path_to_p}/1:11'), {'node': rw.nextSibling, 'offset': 2}) |
| 110 |
|
| 111 |
document.body.appendChild(E.p( |
| 112 |
E.span('Labor is', data_calibre_range_wrapper='10'), |
| 113 |
E.em(E.span('not the source', data_calibre_range_wrapper='10')), |
| 114 |
E.span(' of all wealth.', data_calibre_range_wrapper='10'), |
| 115 |
' ', |
| 116 |
E.em('Nature'), |
| 117 |
' is just as much. The above', |
| 118 |
)) |
| 119 |
p = document.body.lastChild |
| 120 |
path_to_p = encode(document, p) |
| 121 |
r = document.createRange() |
| 122 |
em = p.querySelectorAll('em')[-1] |
| 123 |
r.setStart(em.firstChild, 0) |
| 124 |
r.setEnd(p.lastChild, 3) |
| 125 |
assert_equal(r.toString(), 'Nature is') |
| 126 |
bounds = cfi_for_selection(r) |
| 127 |
s = decode(bounds.start) |
| 128 |
r.setStart(s.node, s.offset) |
| 129 |
e = decode(bounds.end) |
| 130 |
r.setEnd(e.node, e.offset) |
| 131 |
assert_equal(r.toString(), 'Nature is') |
| 132 |
assert_equal(decode(bounds.end), {'node': p.lastChild, 'offset': 3}) |