Edit book: Check book: Mark empty id attributes in the OPF as errors. See #1852318 (Private bug)

This commit is contained in:
Kovid Goyal 2019-11-15 09:10:06 +05:30
parent 028241eb0e
commit 72e8b008bf
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -23,6 +23,14 @@ class MissingSection(BaseError):
'The <%s> section is required in the OPF file. You have to create one.') % section_name) 'The <%s> section is required in the OPF file. You have to create one.') % section_name)
class EmptyID(BaseError):
def __init__(self, name, lnum):
BaseError.__init__(self, _('Empty id attributes are invalid'), name, lnum)
self.HELP = xml(_(
'Empty ID attributes are invalid in OPF files.'))
class IncorrectIdref(BaseError): class IncorrectIdref(BaseError):
def __init__(self, name, idref, lnum): def __init__(self, name, idref, lnum):
@ -292,6 +300,10 @@ def check_opf(container):
errors.append(MissingSection(container.opf_name, tag)) errors.append(MissingSection(container.opf_name, tag))
all_ids = set(container.opf_xpath('//*/@id')) all_ids = set(container.opf_xpath('//*/@id'))
if '' in all_ids:
for empty_id_tag in container.opf_xpath('//*[@id=""]'):
errors.append(EmptyID(container.opf_name, empty_id_tag.sourceline))
all_ids.discard('')
for elem in container.opf_xpath('//*[@idref]'): for elem in container.opf_xpath('//*[@idref]'):
if elem.get('idref') not in all_ids: if elem.get('idref') not in all_ids:
errors.append(IncorrectIdref(container.opf_name, elem.get('idref'), elem.sourceline)) errors.append(IncorrectIdref(container.opf_name, elem.get('idref'), elem.sourceline))