From d153d026e9460d56c1a66ec6532be23414919f88 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 19 Apr 2023 23:02:17 +0530 Subject: [PATCH] When creating and using id assertions in CFI check that the ID is unique --- src/pyj/read_book/cfi.pyj | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/pyj/read_book/cfi.pyj b/src/pyj/read_book/cfi.pyj index 41f4ec6ccd..2253eca0ee 100644 --- a/src/pyj/read_book/cfi.pyj +++ b/src/pyj/read_book/cfi.pyj @@ -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: