When creating and using id assertions in CFI check that the ID is unique

This commit is contained in:
Kovid Goyal 2023-04-19 23:02:17 +05:30
parent b63ca39143
commit d153d026e9
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -238,7 +238,14 @@ def encode(doc, node, offset, tail):
index -= 1
# Add id assertions for robustness where possible
id = node.id
idspec = ('[' + escape_for_cfi(id) + ']') if id else ''
idspec = ''
if id:
try:
multiples = document.querySelectorAll('#' + id)
except:
multiples = None
if multiples and multiples.length < 2:
idspec = ('[' + escape_for_cfi(id) + ']')
cfi = '/' + index + idspec + cfi
node = p
@ -268,7 +275,12 @@ def node_for_path_step(parent, target, assertion):
if assertion:
q = document.getElementById(assertion)
if q:
return q
try:
multiples = document.querySelectorAll('#' + assertion)
except:
multiples = None
if multiples and multiples.length < 2:
return q
is_element = target % 2 == 0
target //= 2
if is_element and target > 0: