Package dev.amp.validator.utils
Class CssSpecUtils
- java.lang.Object
-
- dev.amp.validator.utils.CssSpecUtils
-
public final class CssSpecUtils extends Object
Methods to handle Css Spec processing.- Author:
- nhant01, GeorgeLuo
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classCssSpecUtils.BlockTypeEnum describing how to parse the rules inside a CSS AT Rule.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static StringallowedDeclarationsString(dev.amp.validator.ValidatorProtos.CssSpec cssSpec)Returns a string of the allowed Declarations.static booleanasciiMatch(com.steadystate.css.parser.Token token, String str)static voidextractImportantDeclarations(Stylesheet stylesheet, List<Declaration> important)Extracts the declarations marked `!important` within within the provided stylesheet, emitting them into `important`.static voidextractUrls(Stylesheet stylesheet, List<ParsedCssUrl> parsedUrls, List<ErrorToken> errors)Extracts the URLs within the provided stylesheet, emitting them into parsedUrls and errors into errors.static voidextractUrlsFromDeclaration(Declaration declaration, List<ParsedCssUrl> parsedUrls, List<ErrorToken> errors)Same as the stylesheet variant above, but operates on a single declaration at a time.static booleanisDeclarationValid(dev.amp.validator.ValidatorProtos.CssSpec cssSpec, String declarationName)Returns true if the given Declaration is considered valid.static StylesheetparseAStylesheet(List<com.steadystate.css.parser.Token> tokenList, Map<String,CssSpecUtils.BlockType> atRuleSpec, CssSpecUtils.BlockType defaultSpec, List<ErrorToken> errors)Returns a Stylesheet object with nested parse_css.Rules.static List<Declaration>parseInlineStyle(List<com.steadystate.css.parser.Token> tokenList, List<ErrorToken> errors)Parse inline style content into Declaration objects.static intparseUrlFunction(List<com.steadystate.css.parser.Token> tokens, int tokenIdx, ParsedCssUrl parsed)Parses a CSS function token named 'url', including the string and closing paren.static voidparseUrlToken(List<com.steadystate.css.parser.Token> tokens, int tokenIdx, ParsedCssUrl parsed)Parses a CSS URL token; typically takes the form "url(http://foo)".static StringstripMinMax(String prefixedString)Strips 'min-' or 'max-' from the start of a media feature identifier, if present.static StringstripVendorPrefix(String prefixedString)Strips vendor prefixes from identifiers, e.g.static voidvalidateAmp4AdsCss(Stylesheet styleSheet, List<ErrorToken> errors)validate a css document against amp4ads specsstatic voidvalidateAttrCss(ParsedAttrSpec parsedAttrSpec, Context context, dev.amp.validator.ValidatorProtos.TagSpec tagSpec, String attrName, String attrValue, ValidateTagResult result)Helper method for ValidateAttributes.static voidvalidateKeyframesCss(Stylesheet styleSheet, List<ErrorToken> errors)validate the keyframes of css content
-
-
-
Method Detail
-
parseAStylesheet
public static Stylesheet parseAStylesheet(@Nonnull List<com.steadystate.css.parser.Token> tokenList, @Nonnull Map<String,CssSpecUtils.BlockType> atRuleSpec, @Nonnull CssSpecUtils.BlockType defaultSpec, @Nonnull List<ErrorToken> errors) throws CssValidationException
Returns a Stylesheet object with nested parse_css.Rules.The top level Rules in a Stylesheet are always a series of QualifiedRule's or AtRule's.
- Parameters:
tokenList- the css content as token listatRuleSpec- block type rules for all CSS AT rules this canonicalizer should handle.defaultSpec- default block type for types not found in atRuleSpec.errors- output array for the errors.- Returns:
- a stylesheet object model
- Throws:
CssValidationException- css validation exception
-
extractUrls
public static void extractUrls(@Nonnull Stylesheet stylesheet, @Nonnull List<ParsedCssUrl> parsedUrls, @Nonnull List<ErrorToken> errors) throws CssValidationExceptionExtracts the URLs within the provided stylesheet, emitting them into parsedUrls and errors into errors.- Parameters:
stylesheet- to parse urls fromparsedUrls- urls found in stylesheeterrors- collection of tokens to populate- Throws:
CssValidationException- css validation exception
-
extractUrlsFromDeclaration
public static void extractUrlsFromDeclaration(@Nonnull Declaration declaration, @Nonnull List<ParsedCssUrl> parsedUrls, @Nonnull List<ErrorToken> errors) throws CssValidationExceptionSame as the stylesheet variant above, but operates on a single declaration at a time. Usedful when operating on parsed style attributes.- Parameters:
declaration- the decl to parseparsedUrls- collection of urlserrors- reference- Throws:
CssValidationException- CssValidationException
-
extractImportantDeclarations
public static void extractImportantDeclarations(@Nonnull Stylesheet stylesheet, @Nonnull List<Declaration> important) throws CssValidationExceptionExtracts the declarations marked `!important` within within the provided stylesheet, emitting them into `important`.- Parameters:
stylesheet- to walk throughimportant- list to populate- Throws:
CssValidationException- css validation exception
-
stripVendorPrefix
public static String stripVendorPrefix(@Nonnull String prefixedString)
Strips vendor prefixes from identifiers, e.g. property names or names of at rules. E.g., "-moz-keyframes" to "keyframes".- Parameters:
prefixedString- string with prefix- Returns:
- input string less the prefix component
-
stripMinMax
public static String stripMinMax(@Nonnull String prefixedString)
Strips 'min-' or 'max-' from the start of a media feature identifier, if present. E.g., "min-width" to "width".- Parameters:
prefixedString- the string with a prefix- Returns:
- the prefix-stripped string
-
asciiMatch
public static boolean asciiMatch(@Nonnull com.steadystate.css.parser.Token token, @Nonnull String str)- Parameters:
token- value to matchstr- to match against- Returns:
- true iff ascii value of token and string match
-
validateKeyframesCss
public static void validateKeyframesCss(@Nonnull Stylesheet styleSheet, @Nonnull List<ErrorToken> errors) throws CssValidationExceptionvalidate the keyframes of css content- Parameters:
styleSheet- to validateerrors- generated from css parsing- Throws:
CssValidationException- css validation exception
-
validateAmp4AdsCss
public static void validateAmp4AdsCss(@Nonnull Stylesheet styleSheet, @Nonnull List<ErrorToken> errors) throws CssValidationExceptionvalidate a css document against amp4ads specs- Parameters:
styleSheet- to validateerrors- generated from css parsing- Throws:
CssValidationException- css validation exception
-
isDeclarationValid
public static boolean isDeclarationValid(@Nonnull dev.amp.validator.ValidatorProtos.CssSpec cssSpec, @Nonnull String declarationName)Returns true if the given Declaration is considered valid.- Parameters:
cssSpec- the css spec of interest.declarationName- the declaration to query for in the css spec.- Returns:
- true iff the declaration is found in scc spec's allowed declarations
-
allowedDeclarationsString
public static String allowedDeclarationsString(@Nonnull dev.amp.validator.ValidatorProtos.CssSpec cssSpec)
Returns a string of the allowed Declarations.- Parameters:
cssSpec- of interest- Returns:
- a string representation of allowed declarations
-
parseUrlToken
public static void parseUrlToken(@Nonnull List<com.steadystate.css.parser.Token> tokens, int tokenIdx, @Nonnull ParsedCssUrl parsed) throws CssValidationExceptionParses a CSS URL token; typically takes the form "url(http://foo)". Preconditions: tokens[token_idx] is a URL token and token_idx + 1 is in range.- Parameters:
tokens- to parsetokenIdx- starting index from tokensparsed- ParsedCssUrl to populate- Throws:
CssValidationException- css validation exception
-
parseUrlFunction
public static int parseUrlFunction(@Nonnull List<com.steadystate.css.parser.Token> tokens, int tokenIdx, @Nonnull ParsedCssUrl parsed) throws CssValidationExceptionParses a CSS function token named 'url', including the string and closing paren. Typically takes the form "url('http://foo')". Returns the token_idx past the closing paren, or -1 if parsing fails. Preconditions: tokens[token_idx] is a URL token and tokens[token_idx].StringValue() == "url"- Parameters:
tokens- to validatetokenIdx- index to start fromparsed- ParsedCssUrl object to populate- Returns:
- the token_idx past the closing paren, or -1 if parsing fails.
- Throws:
CssValidationException- css validation exception
-
parseInlineStyle
public static List<Declaration> parseInlineStyle(@Nonnull List<com.steadystate.css.parser.Token> tokenList, @Nonnull List<ErrorToken> errors) throws CssValidationException
Parse inline style content into Declaration objects.- Parameters:
tokenList- the css content in a list of tokens.errors- output array for the errors.- Returns:
- Returns a array of Declaration objects.
- Throws:
CssValidationException- Css Validation Exception
-
validateAttrCss
public static void validateAttrCss(@Nonnull ParsedAttrSpec parsedAttrSpec, @Nonnull Context context, @Nonnull dev.amp.validator.ValidatorProtos.TagSpec tagSpec, @Nonnull String attrName, @Nonnull String attrValue, @Nonnull ValidateTagResult result) throws IOException, CssValidationExceptionHelper method for ValidateAttributes.- Parameters:
parsedAttrSpec- parsed attribute speccontext- the contexttagSpec- tag specattrName- attribute nameattrValue- attribute valueresult- validate tag result- Throws:
IOException- for css tokenizeCssValidationException- css validation exception
-
-