From ec28b592132f468f283519fb1dcb252d63eddd9d Mon Sep 17 00:00:00 2001 From: Romain Deltour Date: Fri, 12 Nov 2021 22:39:26 +0100 Subject: [PATCH] feat: check that item URLs have no fragment Manifest `item` URLs that have a fragment are now reported as `OPF-091` (new check). Fix #1250 --- .../epubcheck/messages/DefaultSeverities.java | 1 + .../com/adobe/epubcheck/messages/MessageId.java | 1 + .../com/adobe/epubcheck/opf/OPFChecker30.java | 7 +++++-- .../epubcheck/messages/MessageBundle.properties | 3 ++- .../item-href-with-fragment-error.opf | 16 ++++++++++++++++ .../resources/epub3/package-document.feature | 7 ++++++- 6 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 src/test/resources/epub3/files/package-document/item-href-with-fragment-error.opf diff --git a/src/main/java/com/adobe/epubcheck/messages/DefaultSeverities.java b/src/main/java/com/adobe/epubcheck/messages/DefaultSeverities.java index 9bf07b253..68b419eb4 100644 --- a/src/main/java/com/adobe/epubcheck/messages/DefaultSeverities.java +++ b/src/main/java/com/adobe/epubcheck/messages/DefaultSeverities.java @@ -272,6 +272,7 @@ private void initialize() severities.put(MessageId.OPF_088, Severity.USAGE); severities.put(MessageId.OPF_089, Severity.ERROR); severities.put(MessageId.OPF_090, Severity.USAGE); + severities.put(MessageId.OPF_091, Severity.ERROR); // PKG severities.put(MessageId.PKG_001, Severity.WARNING); diff --git a/src/main/java/com/adobe/epubcheck/messages/MessageId.java b/src/main/java/com/adobe/epubcheck/messages/MessageId.java index dfbcb7a26..0d5003eda 100644 --- a/src/main/java/com/adobe/epubcheck/messages/MessageId.java +++ b/src/main/java/com/adobe/epubcheck/messages/MessageId.java @@ -266,6 +266,7 @@ public enum MessageId implements Comparable OPF_088("OPF-088"), OPF_089("OPF-089"), OPF_090("OPF-090"), + OPF_091("OPF-091"), // Messages relating to the entire package PKG_001("PKG-001"), diff --git a/src/main/java/com/adobe/epubcheck/opf/OPFChecker30.java b/src/main/java/com/adobe/epubcheck/opf/OPFChecker30.java index 8f7f27e10..4fe53e4ee 100644 --- a/src/main/java/com/adobe/epubcheck/opf/OPFChecker30.java +++ b/src/main/java/com/adobe/epubcheck/opf/OPFChecker30.java @@ -106,9 +106,12 @@ protected void checkItem(OPFItem item, OPFHandler opfHandler) EPUBLocation.create(path, item.getLineNumber(), item.getColumnNumber()), preferredMimeType, mimeType); } - + if (!PathUtil.isRemote(item.getPath()) && PathUtil.getFragment(item.getPath()) != null) { + report.message(MessageId.OPF_091, + EPUBLocation.create(path, item.getLineNumber(), item.getColumnNumber())); + } if ("application/xhtml+xml".equals(mimeType) - && !"xhtml".equals(Files.getFileExtension(item.getPath()))) + && !"xhtml".equals(Files.getFileExtension(PathUtil.removeFragment(item.getPath())))) { report.message(MessageId.HTM_014a, EPUBLocation.create(path, item.getLineNumber(), item.getColumnNumber()), item.getPath()); diff --git a/src/main/resources/com/adobe/epubcheck/messages/MessageBundle.properties b/src/main/resources/com/adobe/epubcheck/messages/MessageBundle.properties index 72bb09d3a..c43aa9f42 100644 --- a/src/main/resources/com/adobe/epubcheck/messages/MessageBundle.properties +++ b/src/main/resources/com/adobe/epubcheck/messages/MessageBundle.properties @@ -280,7 +280,8 @@ OPF_086b_SUG.warning=the "notice" property OPF_087=epub:type value "%1$s" is not allowed on documents of type "%2$s". OPF_088=Unrecognized epub:type value "%1$s". OPF_089=The "alternate" link rel keyword cannot be paired with other keywords. -OPF_090=It is encouraged to use MIME media type "%1$s" instead of "%2$s". +OPF_090=It is encouraged to use MIME media type "%1$s" instead of "%2$s". +OPF_091=The item href URL must not have a fragment identifier. #Package PKG_001=Validating the EPUB against version %1$s but detected version %2$s. diff --git a/src/test/resources/epub3/files/package-document/item-href-with-fragment-error.opf b/src/test/resources/epub3/files/package-document/item-href-with-fragment-error.opf new file mode 100644 index 000000000..7fff3c398 --- /dev/null +++ b/src/test/resources/epub3/files/package-document/item-href-with-fragment-error.opf @@ -0,0 +1,16 @@ + + + + xxx + Title + en + 2019-01-01T12:00:00Z + + + + + + + + diff --git a/src/test/resources/epub3/package-document.feature b/src/test/resources/epub3/package-document.feature index 310ebacd5..296e107fe 100644 --- a/src/test/resources/epub3/package-document.feature +++ b/src/test/resources/epub3/package-document.feature @@ -238,11 +238,16 @@ Feature: EPUB 3 ▸ Packages ▸ Package Document Checks And the message contains 'missing required attribute "media-type"' And no other errors or warnings are reported - Scenario: item paths should not contain spaces + Scenario: item URLs should not contain spaces When checking file 'item-href-contains-spaces-warning.opf' Then warning PKG-010 is reported And no other errors or warnings are reported + Scenario: item URLs must not have a fragment identifier + When checking file 'item-href-with-fragment-error.opf' + Then error OPF-091 is reported + And no other errors or warnings are reported + Scenario: two manifest items cannot represent the same resource When checking file 'item-duplicate-resource-error.opf' Then error OPF-074 is reported