Class CssSpecUtils


  • public final class CssSpecUtils
    extends Object
    Methods to handle Css Spec processing.
    Author:
    nhant01, GeorgeLuo
    • 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 list
        atRuleSpec - 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 CssValidationException
        Extracts the URLs within the provided stylesheet, emitting them into parsedUrls and errors into errors.
        Parameters:
        stylesheet - to parse urls from
        parsedUrls - urls found in stylesheet
        errors - 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 CssValidationException
        Same 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 parse
        parsedUrls - collection of urls
        errors - reference
        Throws:
        CssValidationException - CssValidationException
      • extractImportantDeclarations

        public static void extractImportantDeclarations​(@Nonnull
                                                        Stylesheet stylesheet,
                                                        @Nonnull
                                                        List<Declaration> important)
                                                 throws CssValidationException
        Extracts the declarations marked `!important` within within the provided stylesheet, emitting them into `important`.
        Parameters:
        stylesheet - to walk through
        important - 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 match
        str - to match against
        Returns:
        true iff ascii value of token and string match
      • 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 CssValidationException
        Parses 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 parse
        tokenIdx - starting index from tokens
        parsed - 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 CssValidationException
        Parses 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 validate
        tokenIdx - index to start from
        parsed - 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,
                                           CssValidationException
        Helper method for ValidateAttributes.
        Parameters:
        parsedAttrSpec - parsed attribute spec
        context - the context
        tagSpec - tag spec
        attrName - attribute name
        attrValue - attribute value
        result - validate tag result
        Throws:
        IOException - for css tokenize
        CssValidationException - css validation exception