Skip to content

Commit

Permalink
fix: allow images to point to SVG fragments
Browse files Browse the repository at this point in the history
Updates check `RSC-009`, which reports images defined as fragments:
- only report it if the image resource is not an SVG document.
- report it as a `WARNING` instead of an `ERROR`.
- improve the message to make it applicable to both HTML and SVG image
  elements.

Fix (part of) #987
  • Loading branch information
rdeltour committed Mar 10, 2019
1 parent f81d8b8 commit 18afa9d
Show file tree
Hide file tree
Showing 19 changed files with 167 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ private void initialize()
severities.put(MessageId.RSC_007, Severity.ERROR);
severities.put(MessageId.RSC_007w, Severity.WARNING);
severities.put(MessageId.RSC_008, Severity.ERROR);
severities.put(MessageId.RSC_009, Severity.ERROR);
severities.put(MessageId.RSC_009, Severity.WARNING);
severities.put(MessageId.RSC_010, Severity.ERROR);
severities.put(MessageId.RSC_011, Severity.ERROR);
severities.put(MessageId.RSC_012, Severity.ERROR);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/adobe/epubcheck/opf/XRefChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ else if (!undeclared.contains(ref.refResource)
}
break;
case IMAGE:
if (ref.fragment != null)
if (ref.fragment != null && !res.item.getMimeType().equals("image/svg+xml"))
{
report.message(MessageId.RSC_009, EPUBLocation.create(ref.source, ref.lineNumber,
ref.columnNumber, ref.refResource + "#" + ref.fragment));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ RSC_006b=Resource '%1$s' is located outside the EPUB Container; please check the
RSC_007=Referenced resource '%1$s' could not be found in the EPUB.
RSC_007w=Referenced resource '%1$s' could not be found in the EPUB.
RSC_008=Referenced resource '%1$s' is not declared in the OPF manifest.
RSC_009=A fragment identifier should not be used with an img src attribute.
RSC_009=A non-SVG image resource should not be defined with a fragment identifier.
RSC_010=Reference to non-standard resource type found.
RSC_011=Found a reference to a resource that is not a spine item.
RSC_012=Fragment identifier is not defined.
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/com/adobe/epubcheck/api/Epub30CheckExpandedTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,22 @@ public void testValidateEPUB30_svgSwitch()
testValidateDocument("valid/svg-switch/");
}

@Test
public void testValidateEPUB30_ImageFragmentIsSVG()
{
// tests that images can point to SVG fragments
testValidateDocument("valid/image-fragment-svg/");
}

@Test
public void testValidateEPUB30_ImageFragmentNotSVG()
{
// tests that non-SVG images defined as fragments are reported as WARNING
// (1 warning for an SVG 'image' element, 1 warning for an HTML 'img' element)
Collections.addAll(expectedWarnings, MessageId.RSC_009, MessageId.RSC_009);
testValidateDocument("invalid/image-fragment-non-svg/");
}

@Test
public void testValidateEPUB30_InvalidLinks()
{
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/com/adobe/epubcheck/ops/OPSCheckerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,13 @@ public void testValidateSVG_DuplicateIds()
Collections.addAll(expectedErrors, MessageId.RSC_005, MessageId.RSC_005);
testValidateDocument("svg/invalid/duplicate-ids.svg", "image/svg+xml", EPUBVersion.VERSION_3);
}

@Test
public void testValidateSVG_ImageHrefWithAFragment()
{
// tests that SVG 'image' elements can have an 'xlink:href' URL pointing to a fragment
testValidateDocument("svg/valid/svg-image-fragment.svg", "image/svg+xml", EPUBVersion.VERSION_3);
}


@Test
Expand Down
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="OPS/package.opf" media-type="application/oebps-package+xml"/>
</rootfiles>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xml:lang="en" lang="en">
<head>
<title>Minimal EPUB</title>
</head>
<body epub:type="bodymatter">
<h1>Loomings</h1>
<p>Call me Ishmael.</p>
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1">
<title>Test</title>
<desc>Image</desc>
<image xlink:href="image.jpg#id" width="100" height="100"/>
</svg>
<img src="image.jpg#id" alt="rect"/>
</body>
</html>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xml:lang="en" lang="en">
<head>
<title>Minimal Nav</title>
</head>
<body>
<nav epub:type="toc">
<ol>
<li><a href="content_001.xhtml">content 001</a></li>
</ol>
</nav>
<nav epub:type="landmarks">
<ol>
<li><a href="content_001.xhtml" epub:type="bodymatter">Start Reading</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="svg"/>
<item id="nav" href="nav.xhtml" media-type="application/xhtml+xml" properties="nav"/>
<item id="image" href="image.jpg" media-type="image/jpeg"/>
</manifest>
<spine>
<itemref idref="content_001" />
</spine>
</package>
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,6 @@
<?xml version="1.0" encoding="UTF-8" ?>
<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
<rootfiles>
<rootfile full-path="OPS/package.opf" media-type="application/oebps-package+xml"/>
</rootfiles>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xml:lang="en" lang="en">
<head>
<title>Minimal EPUB</title>
</head>
<body epub:type="bodymatter">
<h1>Loomings</h1>
<p>Call me Ishmael.</p>
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1">
<title>Test</title>
<desc>Image</desc>
<image xlink:href="images.svg#rect" width="100" height="100"/>
</svg>
<img src="images.svg#rect" alt="rect"/>
</body>
</html>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xml:lang="en" lang="en">
<head>
<title>Minimal Nav</title>
</head>
<body>
<nav epub:type="toc">
<ol>
<li><a href="content_001.xhtml">content 001</a></li>
</ol>
</nav>
<nav epub:type="landmarks">
<ol>
<li><a href="content_001.xhtml" epub:type="bodymatter">Start Reading</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="svg"/>
<item id="nav" href="nav.xhtml" media-type="application/xhtml+xml" properties="nav"/>
<item id="images" href="images.svg" media-type="image/svg+xml"/>
</manifest>
<spine>
<itemref idref="content_001" />
</spine>
</package>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
application/epub+zip
7 changes: 7 additions & 0 deletions src/test/resources/30/single/svg/valid/svg-image-fragment.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,8 @@
"suggestion" : null
}, {
"ID" : "RSC-009",
"severity" : "ERROR",
"message" : "A fragment identifier should not be used with an img src attribute.",
"severity" : "WARNING",
"message" : "A non-SVG image resource should not be defined with a fragment identifier.",
"additionalLocations" : 0,
"locations" : [ {
"path" : "OPS/links.xhtml",
Expand Down Expand Up @@ -323,8 +323,8 @@
"checkDate" : "02-05-2019 15:52:58",
"elapsedTime" : 74,
"nFatal" : 0,
"nError" : 7,
"nWarning" : 2,
"nError" : 6,
"nWarning" : 3,
"nUsage" : 6
},
"publication" : {
Expand Down

0 comments on commit 18afa9d

Please sign in to comment.