Skip to content

Commit

Permalink
feat: check the formal viewport meta tag syntax
Browse files Browse the repository at this point in the history
- `HTM-047` is now reported for invalid `viewport` values
- new `HTM-056` is reported when a `viewport` value has no `width` or
  `height`
- new `HTM-057` is reported when a viewport width or height value is
  invalid (i.e. not a positive number or a well-known kewyword)

Also:

- the `isASCIIWhitespace` static utility method is extraced to a public
  `InfraUtil` class in a new `org.w3c.epubcheck.util.infra` package
- the viewport parser is tested as a standalone class in its own feature
  file. However, for easier lookup, this file is however located in the
  `epub3/F-viewport-meta-tag` directory, as if it was a functional test.
- several additional functional tests are added to the `epub3/08-layout`
  directory, for viewport metadata checks.

Fixes #1214, fixes #1347
  • Loading branch information
rdeltour committed Nov 14, 2022
1 parent 39f97a4 commit 01a7758
Show file tree
Hide file tree
Showing 55 changed files with 901 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ private void initialize()
severities.put(MessageId.HTM_053, Severity.INFO);
severities.put(MessageId.HTM_054, Severity.ERROR);
severities.put(MessageId.HTM_055, Severity.WARNING);
severities.put(MessageId.HTM_056, Severity.ERROR);
severities.put(MessageId.HTM_057, Severity.ERROR);

// 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 @@ -131,6 +131,8 @@ public enum MessageId implements Comparable<MessageId>
HTM_053("HTM_053"),
HTM_054("HTM_054"),
HTM_055("HTM_055"),
HTM_056("HTM_056"),
HTM_057("HTM_057"),

// Messages associated with media (images, audio and video)
MED_001("MED-001"),
Expand Down
52 changes: 46 additions & 6 deletions src/main/java/com/adobe/epubcheck/ops/OPSHandler30.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand All @@ -11,6 +12,9 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.w3c.epubcheck.util.microsyntax.ViewportMeta;
import org.w3c.epubcheck.util.microsyntax.ViewportMeta.ParseError;

import com.adobe.epubcheck.api.EPUBLocation;
import com.adobe.epubcheck.api.EPUBProfile;
import com.adobe.epubcheck.messages.MessageId;
Expand Down Expand Up @@ -701,19 +705,55 @@ protected void processMeta()
if (EpubConstants.HtmlNamespaceUri.equals(e.getNamespace()))
{
String name = e.getAttribute("name");
if ("viewport".equals(name))
if ("viewport".equals(Strings.nullToEmpty(name).trim()))
{
// Mark the viewport as seen
// (used when checking the existence of viewport metadata)
hasViewport = true;
// For a fixed-layout document:
// check that the vieport declares both height and width
// For a fixed-layout documents:
if (context.opfItem.isPresent() && context.opfItem.get().isFixedLayout())
{
String contentAttribute = e.getAttribute("content");
if (contentAttribute == null || !contentAttribute.contains("width=")
|| !contentAttribute.contains("height="))

// parse viewport metadata
List<ViewportMeta.ParseError> syntaxErrors = new LinkedList<>();
ViewportMeta viewport = ViewportMeta.parse(contentAttribute,
new ViewportMeta.ErrorHandler()
{
@Override
public void error(ParseError error, int position)
{
syntaxErrors.add(error);
}
});
if (!syntaxErrors.isEmpty())
{
// report any syntax error
report.message(MessageId.HTM_047, location(), contentAttribute);
}
else
{
report.message(MessageId.HTM_047, location());
// check that viewport metadata has a valid width value
if (!viewport.hasProperty("width"))
{
report.message(MessageId.HTM_056, location(), "width");
}
else if (!ViewportMeta.isValidWidth(viewport.getValue("width")))
{
report.message(MessageId.HTM_057, location(), "width");
}

// check that viewport metadata has a valid height value
if (!viewport.hasProperty("height"))
{
report.message(MessageId.HTM_056, location(), "height");
}
else if (!ViewportMeta.isValidHeight(viewport.getValue("height")))
{
report.message(MessageId.HTM_057, location(), "height");
}
}

}
}
}
Expand Down
15 changes: 5 additions & 10 deletions src/main/java/com/adobe/epubcheck/util/SourceSet.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.adobe.epubcheck.util;

import static org.w3c.epubcheck.util.infra.InfraUtil.isASCIIWhitespace;

import java.nio.CharBuffer;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Pattern;

import org.w3c.epubcheck.util.infra.InfraUtil;

import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
Expand Down Expand Up @@ -162,7 +166,7 @@ public SourceSet parse(CharSequence srcset)
{
case SPLIT:
assert (url.length() == 0);
if (isASCIIWhitespace(c))
if (InfraUtil.isASCIIWhitespace(c))
{
// skip whitespace
}
Expand Down Expand Up @@ -271,15 +275,6 @@ else if (c == '(')
return builder.build();
}

/**
* if a character is https://infra.spec.whatwg.org/#ascii-whitespace U+0009 TAB,
* U+000A LF, U+000C FF, U+000D CR, or U+0020 SPACE.
*/
private static boolean isASCIIWhitespace(char c)
{
return c == ' ' || c == '\t' || c == '\f' || c == '\n' || c == '\r';
}

/**
* Remove trailing commas and return the count of commas removed
*/
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/org/w3c/epubcheck/util/infra/InfraUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.w3c.epubcheck.util.infra;

public final class InfraUtil
{

private InfraUtil()
{
// static utility class
}

/**
* if a character is https://infra.spec.whatwg.org/#ascii-whitespace U+0009
* TAB, U+000A LF, U+000C FF, U+000D CR, or U+0020 SPACE.
*/
public static boolean isASCIIWhitespace(char c)
{
return c == ' ' || c == '\t' || c == '\f' || c == '\n' || c == '\r';
}
}
Loading

0 comments on commit 01a7758

Please sign in to comment.