Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse <nav> elements nested in other HTML elements in EPUB 3 navigation documents #113

Merged
merged 1 commit into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,19 @@ public class Epub3NavDocumentReaderTests
</html>
""";

private const string NAV_FILE_WITH_WITH_NON_TOP_LEVEL_NAV_ELEMENT = """
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<div>
<nav>
<h1>Test header</h1>
<ol />
</nav>
</div>
</body>
</html>
""";

private static EpubPackage MinimalEpubPackageWithNav =>
new
(
Expand Down Expand Up @@ -447,6 +460,12 @@ public async Task ReadEpub3NavDocumentAsyncWithEscapedAHrefTest()
await TestSuccessfulReadOperation(NAV_FILE_WITH_ESCAPED_HREF_IN_A_ELEMENT, expectedEpub3NavDocument);
}

[Fact(DisplayName = "Reading a NAV file with non-top-level 'nav' element should succeed")]
public async Task ReadEpub3NavDocumentAsyncWithNonTopLevelNavElementTest()
{
await TestSuccessfulReadOperation(NAV_FILE_WITH_WITH_NON_TOP_LEVEL_NAV_ELEMENT, MinimalEpub3NavDocumentWithHeader);
}

private static async Task TestSuccessfulReadOperation(string navFileContent, Epub3NavDocument expectedEpub3NavDocument, EpubReaderOptions? epubReaderOptions = null)
{
TestZipFile testZipFile = CreateTestZipFileWithNavFile(navFileContent);
Expand Down
20 changes: 16 additions & 4 deletions Source/VersOne.Epub/Readers/Epub3NavDocumentReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,24 @@ public Epub3NavDocumentReader(EpubReaderOptions? epubReaderOptions = null)
XElement htmlNode = navDocument.Element(xhtmlNamespace + "html") ?? throw new Epub3NavException("EPUB parsing error: navigation file does not contain html element.");
XElement bodyNode = htmlNode.Element(xhtmlNamespace + "body") ?? throw new Epub3NavException("EPUB parsing error: navigation file does not contain body element.");
List<Epub3Nav> navs = new();
foreach (XElement navNode in bodyNode.Elements(xhtmlNamespace + "nav"))
ReadEpub3NavsWithinContainerElement(bodyNode, navs);
return new(navFileEntryPath, navs);
}

private static void ReadEpub3NavsWithinContainerElement(XElement containerElement, List<Epub3Nav> resultNavs)
{
foreach (XElement childElement in containerElement.Elements())
{
Epub3Nav epub3Nav = ReadEpub3Nav(navNode);
navs.Add(epub3Nav);
if (childElement.GetLowerCaseLocalName() == "nav")
{
Epub3Nav epub3Nav = ReadEpub3Nav(childElement);
resultNavs.Add(epub3Nav);
}
else
{
ReadEpub3NavsWithinContainerElement(childElement, resultNavs);
}
}
return new(navFileEntryPath, navs);
}

private static Epub3Nav ReadEpub3Nav(XElement navNode)
Expand Down
Loading