Skip to content

Commit

Permalink
fix: OPF-018 was incorrectly reported with inline CSS
Browse files Browse the repository at this point in the history
`OPF-018` checks that the `remote-resources` property is not defined
if no remote resource was found.
Prior to this commit, it was incorrectly reported when an XHTML content
document carrying the property also contained inline CSS: the CSS checker
thought that the property was not required since no remote resource
was found in the CSS itself.
This commit fixes that bug by only checking `OPF-018` for standalone
CSS documents.

Fix #1335
  • Loading branch information
rdeltour committed Nov 28, 2022
1 parent 545b7f7 commit c3d767c
Show file tree
Hide file tree
Showing 13 changed files with 142 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/adobe/epubcheck/css/CSSChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class CSSChecker extends PublicationResourceChecker
private int line; // where css string occurs in host
private final boolean isStyleAttribute;

private enum Mode
enum Mode
{
FILE,
STRING
Expand Down Expand Up @@ -94,7 +94,7 @@ protected boolean checkContent()
try
{

CSSHandler handler = new CSSHandler(context);
CSSHandler handler = new CSSHandler(context, mode);
if (this.mode == Mode.STRING && this.line > -1)
{
handler.setStartingLineNumber(this.line);
Expand Down
29 changes: 18 additions & 11 deletions src/main/java/com/adobe/epubcheck/css/CSSHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import com.adobe.epubcheck.api.EPUBLocation;
import com.adobe.epubcheck.api.Report;
import com.adobe.epubcheck.css.CSSChecker.Mode;
import com.adobe.epubcheck.messages.MessageId;
import com.adobe.epubcheck.opf.OPFChecker;
import com.adobe.epubcheck.opf.OPFChecker30;
Expand All @@ -43,6 +44,7 @@ public class CSSHandler implements CssContentHandler, CssErrorHandler
final ValidationContext context;
final Report report;
final EPUBVersion version;
final Mode mode;
int startingLineNumber = 0; // append to line info from css parser
int startingColumnNumber = 0;
static final CharMatcher SPACE_AND_QUOTES = CharMatcher.anyOf(" \t\n\r\f\"'").precomputed();
Expand All @@ -64,11 +66,12 @@ public class CSSHandler implements CssContentHandler, CssErrorHandler
// properties the must be declared on the related OPF item
final Set<ITEM_PROPERTIES> detectedProperties = EnumSet.noneOf(ITEM_PROPERTIES.class);

public CSSHandler(ValidationContext context)
public CSSHandler(ValidationContext context, Mode mode)
{
this.context = context;
this.report = context.report;
this.version = context.version;
this.mode = mode;
this.urlChecker = new URLChecker(context);
}

Expand Down Expand Up @@ -447,17 +450,21 @@ protected void checkProperties()
EPUBLocation.of(context).at(startingLineNumber, startingColumnNumber),
PackageVocabs.ITEM_VOCAB.getName(property));
}

// Check that properties declared in the OPF item were found in the content
Set<ITEM_PROPERTIES> uncheckedProperties = Sets
.difference(declaredProperties, detectedProperties)
.copyInto(EnumSet.noneOf(ITEM_PROPERTIES.class));
if (uncheckedProperties.contains(ITEM_PROPERTIES.REMOTE_RESOURCES))
{
uncheckedProperties.remove(ITEM_PROPERTIES.REMOTE_RESOURCES);
report.message(MessageId.OPF_018,
EPUBLocation.of(context).at(startingLineNumber, startingColumnNumber));

if (mode == Mode.FILE) {
// Check that properties declared in the OPF item were found in the content
// We only check this for standalone CSS documents (not CSS inlined in HTML)
Set<ITEM_PROPERTIES> uncheckedProperties = Sets
.difference(declaredProperties, detectedProperties)
.copyInto(EnumSet.noneOf(ITEM_PROPERTIES.class));
if (uncheckedProperties.contains(ITEM_PROPERTIES.REMOTE_RESOURCES))
{
uncheckedProperties.remove(ITEM_PROPERTIES.REMOTE_RESOURCES);
report.message(MessageId.OPF_018,
EPUBLocation.of(context).at(startingLineNumber, startingColumnNumber));
}
}

}

public void setStartingLineNumber(int offset)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html xmlns:epub="http://www.idpf.org/2007/ops" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
lang="en">
<head>
<meta charset="utf-8" />
<title>Minimal EPUB</title>
<style>
@font-face {
font-family: "Open Sans";
src: url("https://example.org/font") format("woff");
}</style>
</head>
<body>
<h1>Loomings</h1>
<p>Call me Ishmael.</p>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xml:lang="en" lang="en">
<head>
<meta charset="utf-8"/>
<title>Minimal Nav</title>
</head>
<body>
<nav epub:type="toc">
<ol>
<li><a href="content_001.xhtml">content 001</a></li>
</ol>
</nav>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<package xmlns="http://www.idpf.org/2007/opf" version="3.0" xml:lang="en" unique-identifier="q">
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/">
<dc:title id="title">Minimal EPUB 3.0</dc:title>
<dc:language>en</dc:language>
<dc:identifier id="q">NOID</dc:identifier>
<meta property="dcterms:modified">2017-06-14T00:00:01Z</meta>
</metadata>
<manifest>
<item id="content_001" href="content_001.xhtml" media-type="application/xhtml+xml"/>
<item id="nav" href="nav.xhtml" media-type="application/xhtml+xml" properties="nav"/>
<item id="font" href="https://example.org/font" media-type="font/woff"/>
</manifest>
<spine>
<itemref idref="content_001" />
</spine>
</package>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8" ?>
<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
<rootfiles>
<rootfile full-path="EPUB/package.opf" media-type="application/oebps-package+xml"/>
</rootfiles>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
application/epub+zip
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html xmlns:epub="http://www.idpf.org/2007/ops" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
lang="en">
<head>
<meta charset="utf-8" />
<title>Minimal EPUB</title>
<style>
.red {
color: red;
}</style>
</head>
<body>
<h1>Loomings</h1>
<p>Call me Ishmael.</p>
<video src="https://example.org/video.mp4"></video>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xml:lang="en" lang="en">
<head>
<meta charset="utf-8"/>
<title>Minimal Nav</title>
</head>
<body>
<nav epub:type="toc">
<ol>
<li><a href="content_001.xhtml">content 001</a></li>
</ol>
</nav>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<package xmlns="http://www.idpf.org/2007/opf" version="3.0" xml:lang="en" unique-identifier="q">
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/">
<dc:title id="title">Minimal EPUB 3.0</dc:title>
<dc:language>en</dc:language>
<dc:identifier id="q">NOID</dc:identifier>
<meta property="dcterms:modified">2017-06-14T00:00:01Z</meta>
</metadata>
<manifest>
<item id="content_001" href="content_001.xhtml" media-type="application/xhtml+xml" properties="remote-resources"/>
<item id="nav" href="nav.xhtml" media-type="application/xhtml+xml" properties="nav"/>
<item id="video" href="https://example.org/video.mp4" media-type="video/mp4"/>
</manifest>
<spine>
<itemref idref="content_001" />
</spine>
</package>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8" ?>
<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
<rootfiles>
<rootfile full-path="EPUB/package.opf" media-type="application/oebps-package+xml"/>
</rootfiles>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
application/epub+zip
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,12 @@ Feature: EPUB 3 — Package document
Then error OPF-014 is reported
And no other errors or warnings are reported

@spec @xref:sec-item-resource-properties
Scenario: Report a missing `remote-resources` property when inline CSS has remote references
When checking EPUB 'package-remote-font-in-inline-css-missing-property-error'
Then error OPF-014 is reported
And no other errors or warnings are reported

@spec @xref:sec-item-resource-properties
Scenario: Report an SVG using remote fonts without the `remote-resource` property set in the package document
When checking EPUB 'package-remote-font-in-svg-missing-property-error'
Expand Down Expand Up @@ -570,6 +576,12 @@ Feature: EPUB 3 — Package document
Then error OPF-014 is reported
And no other errors or warnings are reported

@spec @xref:sec-item-resource-properties
Scenario: Verify that inline CSS does not trigger an unrequired `remote-resources` property error
When checking EPUB 'package-remote-resource-and-inline-css-valid'
Then no errors or warnings are reported


##### scripted

@spec @xref:sec-item-resource-properties
Expand Down

0 comments on commit c3d767c

Please sign in to comment.