Skip to content

Commit f7b5dd9

Browse files
committed
fix: incorrect ID refs in toc nav caused a NullPointerException
When the `toc` nav of the navigation document contained links pointing to non-existing IDs, checking that links are in reading order caused a NullPointerException. This commit fixes that by using a more defensive programming logic. Fix #1516
1 parent c15e4ea commit f7b5dd9

File tree

8 files changed

+75
-17
lines changed

8 files changed

+75
-17
lines changed

src/main/java/org/w3c/epubcheck/core/references/ResourceReferencesChecker.java

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -456,25 +456,27 @@ private void checkReadingOrder(Queue<Reference> references)
456456
// check that the fragment is in document order
457457
URLFragment fragment = URLFragment.parse(ref.url, res.getMimeType());
458458
int targetAnchorPosition = resourceRegistry.getIDPosition(fragment.getId(), res);
459-
if (targetAnchorPosition < lastAnchorPosition)
460-
{
461-
String orderContext = LocalizedMessages.getInstance(locale).getSuggestion(
462-
MessageId.NAV_011,
463-
"document");
464-
if (ref.type == Reference.Type.OVERLAY_TEXT_LINK)
465-
{
466-
report.message(MessageId.MED_015, ref.location, container.relativize(ref.url),
467-
orderContext);
468-
}
469-
else
459+
if (targetAnchorPosition > -1) {
460+
if (targetAnchorPosition < lastAnchorPosition)
470461
{
471-
report.message(MessageId.NAV_011, ref.location,
472-
(ref.type == Reference.Type.NAV_TOC_LINK) ? "toc" : "page-list",
473-
container.relativize(ref.url),
474-
orderContext);
462+
String orderContext = LocalizedMessages.getInstance(locale).getSuggestion(
463+
MessageId.NAV_011,
464+
"document");
465+
if (ref.type == Reference.Type.OVERLAY_TEXT_LINK)
466+
{
467+
report.message(MessageId.MED_015, ref.location, container.relativize(ref.url),
468+
orderContext);
469+
}
470+
else
471+
{
472+
report.message(MessageId.NAV_011, ref.location,
473+
(ref.type == Reference.Type.NAV_TOC_LINK) ? "toc" : "page-list",
474+
container.relativize(ref.url),
475+
orderContext);
476+
}
475477
}
478+
lastAnchorPosition = targetAnchorPosition;
476479
}
477-
lastAnchorPosition = targetAnchorPosition;
478480
}
479481
}
480482

src/main/java/org/w3c/epubcheck/core/references/ResourceRegistry.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.adobe.epubcheck.opf.OPFItem;
1212
import com.google.common.base.Preconditions;
1313
import com.google.common.collect.HashBasedTable;
14+
import com.google.common.collect.ImmutableList;
1415
import com.google.common.collect.Table;
1516

1617
import io.mola.galimatias.URL;
@@ -54,7 +55,8 @@ public int getIDPosition(String id, Resource resource)
5455
{
5556
Preconditions.checkArgument(resource != null);
5657
if (id == null || id.trim().isEmpty()) return 0;
57-
int position = ids.get(resource.getURL()).indexOf(id);
58+
int position = Optional.ofNullable(ids.get(resource.getURL())).orElse(ImmutableList.of())
59+
.indexOf(id);
5860
return (position == -1) ? -1 : position + 1;
5961
}
6062

src/test/resources/epub3/06-content-document/content-document-xhtml.feature

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,11 @@ Feature: EPUB 3 — Content Documents — XHTML
263263
Then error RSC-012 is reported
264264
And no errors or warnings are reported
265265

266+
Scenario: Report a hyperlink to a missing identifier in the Nav Doc
267+
When checking EPUB 'content-xhtml-link-to-missing-id-in-nav-doc-error'
268+
Then error RSC-012 is reported
269+
And no errors or warnings are reported
270+
266271
Scenario: Report a hyperlink to a mising identifier in another document
267272
When checking EPUB 'content-xhtml-link-to-missing-id-xref-error'
268273
Then error RSC-012 is reported
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html xmlns:epub="http://www.idpf.org/2007/ops" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
3+
<head>
4+
<meta charset="utf-8"/>
5+
<title>Minimal EPUB</title>
6+
</head>
7+
<body>
8+
<h1>Loomings</h1>
9+
<p>Call me Ishmael.</p>
10+
</body>
11+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xml:lang="en" lang="en">
3+
<head>
4+
<meta charset="utf-8"/>
5+
<title>Minimal Nav</title>
6+
</head>
7+
<body>
8+
<nav epub:type="toc">
9+
<ol>
10+
<li><a href="content_001.xhtml">content 001</a></li>
11+
<li><a href="content_001.xhtml#missing">content 002</a></li>
12+
</ol>
13+
</nav>
14+
</body>
15+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<package xmlns="http://www.idpf.org/2007/opf" version="3.0" xml:lang="en" unique-identifier="q">
3+
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/">
4+
<dc:title id="title">Minimal EPUB 3.0</dc:title>
5+
<dc:language>en</dc:language>
6+
<dc:identifier id="q">NOID</dc:identifier>
7+
<meta property="dcterms:modified">2017-06-14T00:00:01Z</meta>
8+
</metadata>
9+
<manifest>
10+
<item id="content_001" href="content_001.xhtml" media-type="application/xhtml+xml"/>
11+
<item id="nav" href="nav.xhtml" media-type="application/xhtml+xml" properties="nav"/>
12+
</manifest>
13+
<spine>
14+
<itemref idref="content_001" />
15+
</spine>
16+
</package>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
3+
<rootfiles>
4+
<rootfile full-path="EPUB/package.opf" media-type="application/oebps-package+xml"/>
5+
</rootfiles>
6+
</container>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
application/epub+zip

0 commit comments

Comments
 (0)