Skip to content

Commit

Permalink
lint: Add x-021, <figure> with no @id attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
acabal committed Jul 9, 2024
1 parent 81bfc4f commit 1207259
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
6 changes: 6 additions & 0 deletions se/se_epub_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@
"x-018", "Unused [xhtml]id[/] attribute."
"x-019", "Unexpected value of [attr]id[/] attribute. Expected: [attr]{unexpected_id[1]}[/]."
"x-020", "Link to [path][link=file://{local_css_path}]se.css[/][/] in [xhtml]<head>[/], but not an SE boilerplate file."
"x-021", "[xhtml]<figure>[/] element with no [attr]id[/] attribute."
TYPOS
"y-001", "Possible typo: doubled [text]a/the/and/of/or/as/if[/]."
Expand Down Expand Up @@ -2854,6 +2855,11 @@ def _lint_xhtml_xhtml_checks(filename: Path, dom: se.easy_xml.EasyXmlTree, file_
if nodes:
messages.append(LintMessage("x-020", f"Link to [path][link=file://{local_css_path}]se.css[/][/] in [xhtml]<head>[/], but not an SE boilerplate file.", se.MESSAGE_TYPE_ERROR, filename, [node.to_tag_string() for node in nodes]))

# Check that all `<img>` elements have an `@id` attribute. If the `<img>` has a parent `<figure>`, then the `<figure>` should have the `@id`.
nodes = dom.xpath("/html/body//figure[not(@id)]")
if nodes:
messages.append(LintMessage("x-021", "[xhtml]<figure>[/] element with no [attr]id[/] attribute.", se.MESSAGE_TYPE_ERROR, filename, [node.to_string() for node in nodes]))

return messages

def _lint_xhtml_typo_checks(filename: Path, dom: se.easy_xml.EasyXmlTree, file_contents: str, special_file: Optional[str]) -> list:
Expand Down
7 changes: 7 additions & 0 deletions tests/lint/typos/y-003/golden/y-003-out.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
x-021 [Error] chapter-1.xhtml `<figure>` element with no `id` attribute.
<figure>
<figcaption>

<p>Look ma, no picture</p>
</figcaption>
</figure>
y-003 [Manual Review] chapter-1.xhtml Possible typo: paragraph missing ending
punctuation.
<p>This one should error</p>
Empty file.
20 changes: 20 additions & 0 deletions tests/lint/xhtml/x-021/in/chapter-1.xhtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" epub:prefix="z3998: http://www.daisy.org/z3998/2012/vocab/structure/, se: https://standardebooks.org/vocab/1.0" xml:lang="en-GB">
<head>
<title>I</title>
<link href="../css/core.css" rel="stylesheet" type="text/css"/>
<link href="../css/local.css" rel="stylesheet" type="text/css"/>
</head>
<body epub:type="bodymatter z3998:fiction">
<section id="chapter-1" epub:type="chapter">
<!-- Match, no @id attribute. -->
<figure>
<img alt="A portrait illustration of a young woman." src="test.png" epub:type="z3998:illustration se:image.color-depth.black-on-transparent se:image.style.realistic"/>
</figure>
<!-- Don't match, has an @id attribute. -->
<figure id="illustration-1">
<img alt="A portrait illustration of a young woman." src="test.png" epub:type="z3998:illustration se:image.color-depth.black-on-transparent se:image.style.realistic"/>
</figure>
</section>
</body>
</html>

0 comments on commit 1207259

Please sign in to comment.