Skip to content

Commit

Permalink
Skip navs without type in EPUB 3 navigation documents (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
vers-one authored Dec 16, 2024
1 parent 0320c50 commit 3bc152b
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ public class Epub3NavDocumentReaderTests
""";

private const string MINIMAL_NAV_FILE_WITH_H1_HEADER = """
<html xmlns="http://www.w3.org/1999/xhtml">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<body>
<nav>
<nav epub:type="toc">
<h1>Test header</h1>
<ol />
</nav>
Expand All @@ -61,9 +61,9 @@ public class Epub3NavDocumentReaderTests
""";

private const string MINIMAL_NAV_FILE_WITH_H2_HEADER = """
<html xmlns="http://www.w3.org/1999/xhtml">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<body>
<nav>
<nav epub:type="toc">
<h2>Test header</h2>
<ol />
</nav>
Expand All @@ -72,9 +72,9 @@ public class Epub3NavDocumentReaderTests
""";

private const string MINIMAL_NAV_FILE_WITH_H3_HEADER = """
<html xmlns="http://www.w3.org/1999/xhtml">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<body>
<nav>
<nav epub:type="toc">
<h3>Test header</h3>
<ol />
</nav>
Expand All @@ -83,9 +83,9 @@ public class Epub3NavDocumentReaderTests
""";

private const string MINIMAL_NAV_FILE_WITH_H4_HEADER = """
<html xmlns="http://www.w3.org/1999/xhtml">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<body>
<nav>
<nav epub:type="toc">
<h4>Test header</h4>
<ol />
</nav>
Expand All @@ -94,9 +94,9 @@ public class Epub3NavDocumentReaderTests
""";

private const string MINIMAL_NAV_FILE_WITH_H5_HEADER = """
<html xmlns="http://www.w3.org/1999/xhtml">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<body>
<nav>
<nav epub:type="toc">
<h5>Test header</h5>
<ol />
</nav>
Expand All @@ -105,9 +105,9 @@ public class Epub3NavDocumentReaderTests
""";

private const string MINIMAL_NAV_FILE_WITH_H6_HEADER = """
<html xmlns="http://www.w3.org/1999/xhtml">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<body>
<nav>
<nav epub:type="toc">
<h6>Test header</h6>
<ol />
</nav>
Expand All @@ -126,17 +126,17 @@ public class Epub3NavDocumentReaderTests
""";

private const string NAV_FILE_WITHOUT_TOP_OL_ELEMENT = """
<html xmlns="http://www.w3.org/1999/xhtml">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<body>
<nav />
<nav epub:type="toc" />
</body>
</html>
""";

private const string NAV_FILE_WITH_EMPTY_LI_ELEMENT = """
<html xmlns="http://www.w3.org/1999/xhtml">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<body>
<nav>
<nav epub:type="toc">
<ol>
<li />
</ol>
Expand All @@ -160,10 +160,10 @@ public class Epub3NavDocumentReaderTests
""";

private const string NAV_FILE_WITH_WITH_NON_TOP_LEVEL_NAV_ELEMENT = """
<html xmlns="http://www.w3.org/1999/xhtml">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<body>
<div>
<nav>
<nav epub:type="toc">
<h1>Test header</h1>
<ol />
</nav>
Expand All @@ -172,6 +172,28 @@ public class Epub3NavDocumentReaderTests
</html>
""";

private const string NAV_FILE_WITHOUT_TYPE_IN_NAV_ELEMENT = """
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<body>
<nav>
<ol>
<li>
<a href="chapter1.html">Chapter 1</a>
</li>
</ol>
</nav>
</body>
</html>
""";

private const string MINIMAL_NAV_FILE_WITHOUT_TYPE_IN_NAV_ELEMENT = """
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<body>
<nav />
</body>
</html>
""";

private static EpubPackage MinimalEpubPackageWithNav =>
new
(
Expand Down Expand Up @@ -290,7 +312,7 @@ public class Epub3NavDocumentReaderTests
{
new
(
type: null,
type: Epub3StructuralSemanticsProperty.TOC,
isHidden: false,
head: "Test header",
ol: new Epub3NavOl()
Expand Down Expand Up @@ -466,6 +488,18 @@ public async Task ReadEpub3NavDocumentAsyncWithNonTopLevelNavElementTest()
await TestSuccessfulReadOperation(NAV_FILE_WITH_WITH_NON_TOP_LEVEL_NAV_ELEMENT, MinimalEpub3NavDocumentWithHeader);
}

[Fact(DisplayName = "'nav' elements without 'type' attribute should be ignored")]
public async Task ReadEpub3NavDocumentAsyncWithNavElementWithoutTypeTest()
{
await TestSuccessfulReadOperation(NAV_FILE_WITHOUT_TYPE_IN_NAV_ELEMENT, MinimalEpub3NavDocument);
}

[Fact(DisplayName = "'nav' elements without 'type' attribute that don't conform to EPUB standard should not throw an exception")]
public async Task ReadEpub3NavDocumentAsyncWithNonEpubNavElementWithoutTypeTest()
{
await TestSuccessfulReadOperation(MINIMAL_NAV_FILE_WITHOUT_TYPE_IN_NAV_ELEMENT, MinimalEpub3NavDocument);
}

private static async Task TestSuccessfulReadOperation(string navFileContent, Epub3NavDocument expectedEpub3NavDocument, EpubReaderOptions? epubReaderOptions = null)
{
TestZipFile testZipFile = CreateTestZipFileWithNavFile(navFileContent);
Expand Down
13 changes: 10 additions & 3 deletions Source/VersOne.Epub/Readers/Epub3NavDocumentReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,11 @@ private static void ReadEpub3NavsWithinContainerElement(XElement containerElemen
{
if (childElement.GetLowerCaseLocalName() == "nav")
{
Epub3Nav epub3Nav = ReadEpub3Nav(childElement);
resultNavs.Add(epub3Nav);
Epub3Nav? epub3Nav = ReadEpub3Nav(childElement);
if (epub3Nav != null)
{
resultNavs.Add(epub3Nav);
}
}
else
{
Expand All @@ -69,7 +72,7 @@ private static void ReadEpub3NavsWithinContainerElement(XElement containerElemen
}
}

private static Epub3Nav ReadEpub3Nav(XElement navNode)
private static Epub3Nav? ReadEpub3Nav(XElement navNode)
{
Epub3StructuralSemanticsProperty? type = null;
bool isHidden = false;
Expand All @@ -88,6 +91,10 @@ private static Epub3Nav ReadEpub3Nav(XElement navNode)
break;
}
}
if (type == null)
{
return null;
}
foreach (XElement navChildNode in navNode.Elements())
{
switch (navChildNode.GetLowerCaseLocalName())
Expand Down

0 comments on commit 3bc152b

Please sign in to comment.