fix(epub): remove refines when referenced elements are removed (#2048)

This commit is contained in:
James Ward
2026-07-27 20:46:30 -04:00
committed by GitHub
parent 3376d3302f
commit 9dd8bcc0f7
2 changed files with 98 additions and 0 deletions
@@ -117,6 +117,28 @@ class EpubMetadataWriterTest {
}
}
}
@Test
@DisplayName("Should remove extraneous refines in EPUB3")
void writeMetadata_removesMissingRefines() throws Exception {
String opfContent = """
<?xml version="1.0" encoding="UTF-8"?>
<package xmlns="http://www.idpf.org/2007/opf" version="3.0">
<other id="example">Example</other>
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/">
<meta refines="#example" property="identifier-type">wizard</meta>
<meta refines="#example2" property="source-of">magic</meta>
</metadata>
</package>""";
File epubFile = createEpubWithOpf(opfContent, "test-epub3-role-" + System.nanoTime() + ".epub");
writer.saveMetadataToFile(epubFile, metadata, null, new MetadataClearFlags());
String content = readOpfContent(epubFile);
assertThat(content).doesNotContain("refines=\"#example2\"");
assertThat(content).contains("refines=\"#example\"");
}
}
@Nested
@@ -461,6 +483,46 @@ class EpubMetadataWriterTest {
assertThat(content).contains("property=\"title-type\"");
assertThat(content).contains(">subtitle<");
}
@Test
@DisplayName("Should remove refinement when subtitle is removed")
void epub3_shouldRemoveRefinementWhenRemovingSubtitle() throws Exception {
String opfContent = """
<?xml version="1.0" encoding="UTF-8"?>
<package xmlns="http://www.idpf.org/2007/opf" version="3.0">
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/">
<dc:title id="#subtitle-12b45ebf">Subtitle</dc:title>
<opf:meta xmlns:opf="http://www.idpf.org/2007/opf" property="title-type" refines="#subtitle-12b45ebf">subtitle</opf:meta>
</metadata>
</package>""";
metadata.setSubtitle(null);
File epubFile = createEpubWithOpf(opfContent, "test-epub3-subtitle-" + System.nanoTime() + ".epub");
writer.saveMetadataToFile(epubFile, metadata, null, new MetadataClearFlags());
String content = readOpfContent(epubFile);
assertThat(content).doesNotContain(">subtitle</opf:meta>");
}
@Test
@DisplayName("Should do nothing when no subtitle and subtitle is removed")
void epub3_shouldSaveNoSubtitleWhenAlreadyNoSubtitle() throws Exception {
String opfContent = """
<?xml version="1.0" encoding="UTF-8"?>
<package xmlns="http://www.idpf.org/2007/opf" version="3.0">
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/">
</metadata>
</package>""";
metadata.setSubtitle(null);
File epubFile = createEpubWithOpf(opfContent, "test-epub3-subtitle-" + System.nanoTime() + ".epub");
writer.saveMetadataToFile(epubFile, metadata, null, new MetadataClearFlags());
String content = readOpfContent(epubFile);
assertThat(content).doesNotContain(">subtitle</opf:meta>");
}
}
@Nested