Fix incorrect mapping of CFI to position for CFI at the start of a tag whose contents are wrapped in an annotation span

This commit is contained in:
Kovid Goyal 2020-07-16 10:30:45 +05:30
parent 873d6080fd
commit abba1ef004
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -566,31 +566,35 @@ def point(cfi, doc): # {{{
else: else:
try_list = [{'start':0, 'end':0, 'a':0.5}, {'start':-1, 'end':0, 'a':0}, {'start':0, 'end':1, 'a':1}] try_list = [{'start':0, 'end':0, 'a':0.5}, {'start':-1, 'end':0, 'a':0}, {'start':0, 'end':1, 'a':1}]
a = None a = None
rects = None
node_len = node.nodeValue.length if node.nodeValue else 0 node_len = node.nodeValue.length if node.nodeValue else 0
offset = r.offset offset = r.offset
for v'var i = 0; i < 2; i++': if not offset:
# Try reducing the offset by 1 if we get no match as if it refers to the position after the range_.setStart(node, 0)
# last character we wont get a match with getClientRects range_.setEnd(node, 0)
offset = r.offset - i else:
if offset < 0: rects = v'[]'
offset = 0 for v'var i = 0; i < 2; i++':
k = 0 # Try reducing the offset by 1 if we get no match as if it refers to the position after the
while (not rects or not rects.length) and k < try_list.length: # last character we wont get a match with getClientRects
t = try_list[k] offset = r.offset - i
k += 1 if offset < 0:
start_offset = offset + t.start offset = 0
end_offset = offset + t.end k = 0
a = t.a while not rects?.length and k < try_list.length:
if start_offset < 0 or end_offset >= node_len: t = try_list[k]
continue k += 1
range_.setStart(node, start_offset) start_offset = offset + t.start
range_.setEnd(node, end_offset) end_offset = offset + t.end
rects = range_.getClientRects() a = t.a
if start_offset < 0 or end_offset >= node_len:
continue
range_.setStart(node, start_offset)
range_.setEnd(node, end_offset)
rects = range_.getClientRects()
if not rects or not rects.length: if not rects?.length:
print(str.format("Could not find caret position: rects: {} offset: {}", rects, r.offset)) print(str.format("Could not find caret position for {} : rects: {} offset: {}", cfi, rects, r.offset))
return None return None
else: else:
x, y = r.x, r.y x, y = r.x, r.y