Skip to content

Commit

Permalink
feat: support new rules related to FXL SVG sizing
Browse files Browse the repository at this point in the history
This change implements the new rules related to expressing the initial
containing block dimensions for SVG documents in Fixed Layout EPUBs.

New messages:

- `HTM-054` (`WARNING`): when the size is given by `width`/`height`
  attributes, but the recommended `viewBox` attribute is missing
- `HTM-055` (`WARNING`): when the values of the `height` and `width`
  attributes are not specified in pixels, and `viewBox` is not present

Changed message:

- `HTM-048` (`ERROR`): now says that either `viewBox` or `height` and
  `width` must be present

Bug fix:

- Only the outer SVG element is checked (previously, all the `svg`
  elements where checked).

Fix #902.

Note: the checking logic may need to be tweaked depending on the outcome
of issue w3c/epub-specs#1236
  • Loading branch information
GrayWolfMT authored and rdeltour committed Feb 23, 2019
1 parent ac2f1bd commit 17f5eee
Show file tree
Hide file tree
Showing 41 changed files with 299 additions and 734 deletions.
38 changes: 34 additions & 4 deletions src/main/java/com/adobe/epubcheck/ctc/EpubSVGCheck.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.adobe.epubcheck.ctc;

import java.util.Hashtable;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;

import org.w3c.dom.Document;
Expand Down Expand Up @@ -116,19 +117,48 @@ void checkSvgDoc(String svgDocEntry)
}
}

// FIXME this RegEX is a bit too naïve for CSS syntax rules
// e.g. escape values and comments are theoretically allowed too
private static Pattern PIXEL_LENGTH_REGEX = Pattern.compile("\\d+(px)?");

void checkViewBox(String svgDocEntry, Document doc)
{
NodeList n = doc.getElementsByTagNameNS(svgNS, "svg");
for (int i = 0; i < n.getLength(); i++)
if (n.getLength() > 0)
{
Element svgElement = (Element) n.item(i);
Element svgElement = (Element) n.item(0);
String viewport = svgElement.getAttributeNS(svgNS, "viewBox");
if (viewport == null || viewport.length() == 0)
boolean viewportFound = (viewport != null && viewport.trim().length() > 0);
String height = svgElement.getAttributeNS(svgNS, "height");
boolean heightFound = (height != null && height.trim().length() > 0);
boolean isHeightInPixel = heightFound && PIXEL_LENGTH_REGEX.matcher(height.trim()).matches();
String width = svgElement.getAttributeNS(svgNS, "width");
boolean widthFound = (width != null && width.trim().length() > 0);
boolean isWidthInPixel = widthFound && PIXEL_LENGTH_REGEX.matcher(width.trim()).matches();
if (!viewportFound)
{
report.message(MessageId.HTM_048, EPUBLocation.create(svgDocEntry, XmlDocParser.getElementLineNumber(svgElement), XmlDocParser.getElementColumnNumber(svgElement)));
if (!heightFound || !widthFound)
{
report.message(MessageId.HTM_048,
EPUBLocation.create(svgDocEntry, XmlDocParser.getElementLineNumber(svgElement),
XmlDocParser.getElementColumnNumber(svgElement)));
}
else
{
report.message(MessageId.HTM_054,
EPUBLocation.create(svgDocEntry, XmlDocParser.getElementLineNumber(svgElement),
XmlDocParser.getElementColumnNumber(svgElement)));
if (!isHeightInPixel || !isWidthInPixel)
{
report.message(MessageId.HTM_055,
EPUBLocation.create(svgDocEntry, XmlDocParser.getElementLineNumber(svgElement),
XmlDocParser.getElementColumnNumber(svgElement)));
}
}
}
}
}

void checkImageXlinkHrefInline(String svgDocEntry, Document doc)
{
NodeList n = doc.getElementsByTagNameNS(svgNS, "image");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ private void initialize()
severities.put(MessageId.HTM_051, Severity.WARNING);
severities.put(MessageId.HTM_052, Severity.ERROR);
severities.put(MessageId.HTM_053, Severity.INFO);
severities.put(MessageId.HTM_054, Severity.WARNING);
severities.put(MessageId.HTM_055, Severity.WARNING);

// Media
severities.put(MessageId.MED_001, Severity.ERROR);
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/adobe/epubcheck/messages/MessageId.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ public enum MessageId implements Comparable<MessageId>
HTM_051("HTM-051"),
HTM_052("HTM-052"),
HTM_053("HTM_053"),
HTM_054("HTM-054"),
HTM_055("HTM-055"),

// Messages associated with media (images, audio and video)
MED_001("MED-001"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,16 @@ HTM_046=Fixed format item has no viewport defined.
HTM_046_SUG=A viewport declaration is required for fixed format items.
HTM_047=Html viewport is missing height and/or width.
HTM_047_SUG=The viewport declaration must declare both width and height.
HTM_048=SVG ViewBox is missing on fixed format document.
HTM_048_SUG=A viewBox declaration is required for fixed format documents.
HTM_048=SVG ViewBox and width and height are missing on fixed format document.
HTM_048_SUG=A viewBox declaration or width and height are required for fixed format documents.
HTM_049=Html element does not have an xmlns set to 'http://www.w3.org/1999/xhtml'.
HTM_049_SUG=Add xmlns="http://www.w3.org/1999/xhtml" to the html element.
HTM_050=Found epub:type="pagebreak" attribute in content document.
HTM_051=Found Microdata semantic enrichments but no RDFa. EDUPUB recommends using RDFa Lite.
HTM_052=The property 'region-based' is only allowed on nav elements in Data Navigation Documents.
HTM_053=Found an external file link (file://) in file: '%1$s'.
HTM_054=The use of a 'viewBox' attribute is recommended on SVG elements.
HTM_055=When no `viewBox` attribute is present, the values of the 'height' and 'width' attributes should be specified in pixels.

#media
MED_001=Video poster must have core media image type.
Expand Down
23 changes: 21 additions & 2 deletions src/test/java/com/adobe/epubcheck/api/Epub30CheckExpandedTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -674,11 +674,31 @@ public void testFont_NonCoreMediaType() {
public void testFXL_WithSVG() {
testValidateDocument("valid/fxl-svg/");
}

@Test
public void testFXL_WithSVG_InnerSVG() {
// tests that the ICB-defining rules are only checked on the outer svg element
testValidateDocument("valid/fxl-svg-no-viewbox-on-inner-svg");
}

@Test
public void testFXL_WithSVG_NoViewbox() {
expectedErrors.add(MessageId.HTM_048);
testValidateDocument("invalid/fxl-svg-noviewbox/");
testValidateDocument("invalid/fxl-svg-no-viewbox-no-heightwidth");
}

@Test
public void testFXL_WithSVG_NoViewbox_WidthHeight(){
//Testing for Width/Height but no viewbox - re: Issue 902
Collections.addAll(expectedWarnings, MessageId.HTM_054);
testValidateDocument("invalid/fxl-svg-no-viewbox");
}

@Test
public void testFXL_WithSVG_NoViewbox_WidthHeightInPercent(){
//Testing for Width/Height but no viewbox - re: Issue 902
Collections.addAll(expectedWarnings, MessageId.HTM_054, MessageId.HTM_055);
testValidateDocument("invalid/fxl-svg-no-viewbox-widthheight-in-percent");
}

@Test
Expand Down Expand Up @@ -1116,5 +1136,4 @@ public void testBaseURI()
{
testValidateDocument("valid/base-uri");
}

}
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 @@ -3,7 +3,6 @@
xmlns:epub="http://www.idpf.org/2007/ops">
<head>
<title>Nav Doc</title>
<link type="text/css" rel="stylesheet" href="style.css" />
<meta name="viewport" content="width=1200, height=600" />
</head>
<body>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?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>
<meta property="rendition:layout">pre-paginated</meta>
</metadata>
<manifest>
<item id="nav" href="nav.xhtml" media-type="application/xhtml+xml" properties="nav"/>
<item id="cover" href="cover.svg" media-type="image/svg+xml"/>
</manifest>
<spine>
<itemref idref="nav" linear="no"/>
<itemref idref="cover"/>
</spine>
</package>
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,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="la" lang="la"
xmlns:epub="http://www.idpf.org/2007/ops">
<head>
<title>Nav Doc</title>
<meta name="viewport" content="width=1200, height=600" />
</head>
<body>
<h2>Table of Contents</h2>
<nav epub:type="toc">
<ol>
<li><a href="cover.svg">Cover</a></li>
</ol>
</nav>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?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>
<meta property="rendition:layout">pre-paginated</meta>
</metadata>
<manifest>
<item id="nav" href="nav.xhtml" media-type="application/xhtml+xml" properties="nav"/>
<item id="cover" href="cover.svg" media-type="image/svg+xml"/>
</manifest>
<spine>
<itemref idref="nav" linear="no"/>
<itemref idref="cover"/>
</spine>
</package>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<container xmlns="urn:oasis:names:tc:opendocument:xmlns:container" version="1.0">
<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
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,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="la" lang="la"
xmlns:epub="http://www.idpf.org/2007/ops">
<head>
<title>Nav Doc</title>
<meta name="viewport" content="width=1200, height=600" />
</head>
<body>
<h2>Table of Contents</h2>
<nav epub:type="toc">
<ol>
<li><a href="cover.svg">Cover</a></li>
</ol>
</nav>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?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>
<meta property="rendition:layout">pre-paginated</meta>
</metadata>
<manifest>
<item id="nav" href="nav.xhtml" media-type="application/xhtml+xml" properties="nav"/>
<item id="cover" href="cover.svg" media-type="image/svg+xml"/>
</manifest>
<spine>
<itemref idref="nav" linear="no"/>
<itemref idref="cover"/>
</spine>
</package>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<container xmlns="urn:oasis:names:tc:opendocument:xmlns:container" version="1.0">
<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
Loading

0 comments on commit 17f5eee

Please sign in to comment.