fix(epub): remove refines when referenced elements are removed (#2048)
This commit is contained in:
@@ -224,6 +224,7 @@ public class EpubMetadataWriter implements MetadataWriter {
|
||||
addBookloreMetadata(metadataElement, opfDoc, metadata);
|
||||
cleanupCalibreArtifacts(metadataElement, opfDoc);
|
||||
organizeMetadataElements(metadataElement);
|
||||
removeInvalidMetaRefines(metadataElement, opfDoc);
|
||||
removeEmptyTextNodes(opfDoc);
|
||||
Transformer transformer = TransformerFactory.newInstance().newTransformer();
|
||||
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
|
||||
@@ -595,6 +596,41 @@ public class EpubMetadataWriter implements MetadataWriter {
|
||||
}
|
||||
}
|
||||
|
||||
private void removeInvalidMetaRefines(Element metadataElement, Document doc) {
|
||||
// In an ideal world we could set the `validating` flag or
|
||||
// otherwise set the `isId` attribute tag correctly for `id`
|
||||
// but because we cannot, we can't use `getElementById()` on
|
||||
// the document. With that in mind, we area going to read all
|
||||
// tags, check if they have an `id`, and store that in a `Set`
|
||||
Set<String> ids = new HashSet<>();
|
||||
|
||||
NodeList nodes = doc.getElementsByTagName("*");
|
||||
for (int i = 0; i < nodes.getLength(); i++) {
|
||||
var node = nodes.item(i);
|
||||
|
||||
if (node instanceof Element element) {
|
||||
var idAttribute = element.getAttribute("id");
|
||||
if (!idAttribute.isBlank()) {
|
||||
ids.add(idAttribute);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
NodeList metas = metadataElement.getElementsByTagNameNS("*", "meta");
|
||||
for (int i = metas.getLength() - 1; i >= 0; i--) {
|
||||
Element meta = (Element) metas.item(i);
|
||||
String refines = meta.getAttribute("refines");
|
||||
|
||||
if (refines.startsWith("#")) {
|
||||
String refinesId = refines.substring(1);
|
||||
|
||||
if (!ids.contains(refinesId)) {
|
||||
metadataElement.removeChild(meta);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void removeMetaByRefines(Element metadataElement, String refines) {
|
||||
NodeList metas = metadataElement.getElementsByTagNameNS("*", "meta");
|
||||
for (int i = metas.getLength() - 1; i >= 0; i--) {
|
||||
|
||||
+62
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user