Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: check the formal viewport meta tag syntax #1400

Merged
merged 1 commit into from
Dec 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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