Class TagSpecUtils


  • public final class TagSpecUtils
    extends Object
    Tag spec utility methods.
    Author:
    nhant01, GeorgeLuo
    • Field Detail

      • AMP_IDENTIFIERS

        public static final List AMP_IDENTIFIERS
        List identifiers for AMP format.
      • AMP4ADS_IDENTIFIERS

        public static final List AMP4ADS_IDENTIFIERS
        List identifiers for AMP4ADS format.
      • AMP4EMAIL_IDENTIFIERS

        public static final List AMP4EMAIL_IDENTIFIERS
        List identifiers for AMP4EMAIL format.
      • ACTIONS_IDENTIFIERS

        public static final List ACTIONS_IDENTIFIERS
        List identifiers for ACTIONS format.
    • Method Detail

      • getTagSpecUrl

        public static String getTagSpecUrl​(@Nonnull
                                           dev.amp.validator.ValidatorProtos.TagSpec tagSpec)
        For creating error URLs, we either find the specUrl in the tag spec or fall back to the extension spec URL if available.
        Parameters:
        tagSpec - TagSpec instance from the validator.protoscii file.
        Returns:
        returns the tag spec URL.
      • getTagSpecName

        public static String getTagSpecName​(@Nonnull
                                            dev.amp.validator.ValidatorProtos.TagSpec tagSpec)
        For creating error messages, we either find the specName in the tag spec or fall back to the tagName.
        Parameters:
        tagSpec - TagSpec instance from the validator.protoscii file.
        Returns:
        return the tag spec name.
      • getTagDescriptiveName

        public static String getTagDescriptiveName​(@Nonnull
                                                   dev.amp.validator.ValidatorProtos.TagSpec tagSpec)
        For creating error messages, we either find the descriptiveName in the tag spec or fall back to the tagName.
        Parameters:
        tagSpec - TagSpec instance from the validator.protoscii file.
        Returns:
        return the descriptive tag spec name.
      • shouldRecordTagspecValidated

        public static RecordValidated shouldRecordTagspecValidated​(@Nonnull
                                                                   dev.amp.validator.ValidatorProtos.TagSpec tag,
                                                                   int tagSpecId,
                                                                   @Nonnull
                                                                   Map<Object,​Boolean> tagSpecIdsToTrack)
        We only track (that is, add them to Context.RecordTagspecValidated) validated tagspecs as necessary. That is, if it's needed for document scope validation: - Mandatory tags - Unique tags - Tags (identified by their TagSpecName() that are required by other tags.
        Parameters:
        tag - the TagSpec.
        tagSpecId - the tag spec id.
        tagSpecIdsToTrack - a map of tag spec id to boolean.
        Returns:
        returns a record validated enum value.
      • validateParentTag

        public static void validateParentTag​(@Nonnull
                                             ParsedTagSpec parsedTagSpec,
                                             @Nonnull
                                             Context context,
                                             @Nonnull
                                             dev.amp.validator.ValidatorProtos.ValidationResult.Builder validationResult)
                                      throws TagValidationException
        Validates whether the parent tag satisfied the spec (e.g., some tags can only appear in head).
        Parameters:
        parsedTagSpec - the parsed tag spec.
        context - the Context object.
        validationResult - validation result.
        Throws:
        TagValidationException - the TagValidationException.
      • validateAncestorTags

        public static void validateAncestorTags​(@Nonnull
                                                ParsedTagSpec parsedTagSpec,
                                                @Nonnull
                                                Context context,
                                                @Nonnull
                                                dev.amp.validator.ValidatorProtos.ValidationResult.Builder validationResult)
        Validates if the tag ancestors satisfied the spec.
        Parameters:
        parsedTagSpec - the parsed tag spec.
        context - the context.
        validationResult - validation result.
      • parseLayout

        public static dev.amp.validator.ValidatorProtos.AmpLayout.Layout parseLayout​(String layout)
        Parameters:
        layout - layout.
        Returns:
        amp.validator.AmpLayout.Layout.
      • calculateHeight

        public static CssLength calculateHeight​(@Nonnull
                                                dev.amp.validator.ValidatorProtos.AmpLayout spec,
                                                @Nonnull
                                                dev.amp.validator.ValidatorProtos.AmpLayout.Layout inputLayout,
                                                @Nonnull
                                                CssLength inputHeight)
        Calculates the effective height from input layout and input height.
        Parameters:
        spec - the amp layout spec.
        inputLayout - input layout.
        inputHeight - css length input height.
        Returns:
        returns the css length instance.
      • calculateLayout

        public static dev.amp.validator.ValidatorProtos.AmpLayout.Layout calculateLayout​(@Nonnull
                                                                                         dev.amp.validator.ValidatorProtos.AmpLayout.Layout inputLayout,
                                                                                         @Nonnull
                                                                                         CssLength width,
                                                                                         @Nonnull
                                                                                         CssLength height,
                                                                                         String sizesAttr,
                                                                                         String heightsAttr)
        Calculates the layout; this depends on the width / height calculation above. It happens last because web designers often make fixed-sized mocks first and then the layout determines how things will change for different viewports / devices / etc.
        Parameters:
        inputLayout - input layout.
        width - widht.
        height - height.
        sizesAttr - sizes attribute.
        heightsAttr - heights attribute.
        Returns:
        return the layout depends on the width/height.
      • validateSsrLayout

        public static void validateSsrLayout​(@Nonnull
                                             dev.amp.validator.ValidatorProtos.TagSpec spec,
                                             @Nonnull
                                             ParsedHtmlTag encounteredTag,
                                             @Nonnull
                                             dev.amp.validator.ValidatorProtos.AmpLayout.Layout inputLayout,
                                             @Nonnull
                                             CssLength inputWidth,
                                             @Nonnull
                                             CssLength inputHeight,
                                             String sizesAttr,
                                             String heightsAttr,
                                             @Nonnull
                                             Context context,
                                             @Nonnull
                                             dev.amp.validator.ValidatorProtos.ValidationResult.Builder result)
        Helper method for validateLayout. Validates the server-side rendering related attributes for the given layout.
        Parameters:
        spec - the tag spec.
        encounteredTag - the encounted tag.
        inputLayout - input layout.
        inputWidth - input width.
        inputHeight - input height.
        sizesAttr - sizes attribute.
        heightsAttr - heights attribute.
        context - the context.
        result - validation result.
      • isLayoutSizeDefined

        public static boolean isLayoutSizeDefined​(@Nonnull
                                                  dev.amp.validator.ValidatorProtos.AmpLayout.Layout layout)
        Parameters:
        layout - AMP layout.
        Returns:
        returns true if layout size is matched.
      • getLayoutClass

        public static String getLayoutClass​(@Nonnull
                                            dev.amp.validator.ValidatorProtos.AmpLayout.Layout layout)
        Parameters:
        layout - AMP layout.
        Returns:
        returns the layout class.
      • getLayoutSizeDefinedClass

        public static String getLayoutSizeDefinedClass()
        Returns:
        returns the layout size defined class.
      • getLayoutAwaitingSizeClass

        public static String getLayoutAwaitingSizeClass()
        Returns:
        the layout awaiting size
      • isLayoutAwaitingSize

        public static boolean isLayoutAwaitingSize​(@Nonnull
                                                   dev.amp.validator.ValidatorProtos.AmpLayout.Layout layout)
        Parameters:
        layout - the layout to check
        Returns:
        true iff it is a LayoutAwaitingSize
      • calculateWidthForTag

        public static CssLength calculateWidthForTag​(@Nonnull
                                                     dev.amp.validator.ValidatorProtos.AmpLayout.Layout inputLayout,
                                                     @Nonnull
                                                     CssLength inputWidth,
                                                     @Nonnull
                                                     String tagName)
        Calculates the effective width from the input layout, input width and tag. For certain tags it uses explicit dimensions.
        Parameters:
        inputLayout - input layout.
        inputWidth - input width.
        tagName - the tag name.
        Returns:
        returns the css length instance.
      • calculateHeightForTag

        public static CssLength calculateHeightForTag​(@Nonnull
                                                      dev.amp.validator.ValidatorProtos.AmpLayout.Layout inputLayout,
                                                      @Nonnull
                                                      CssLength inputHeight,
                                                      @Nonnull
                                                      String tagName)
        Calculates the effective height from the input layout, input height and tag. For certain tags it uses explicit dimensions.
        Parameters:
        inputLayout - input layout.
        inputHeight - input height.
        tagName - the tag name.
        Returns:
        returns the css length instance.
      • calculateWidth

        public static CssLength calculateWidth​(@Nonnull
                                               dev.amp.validator.ValidatorProtos.AmpLayout spec,
                                               @Nonnull
                                               dev.amp.validator.ValidatorProtos.AmpLayout.Layout inputLayout,
                                               @Nonnull
                                               CssLength inputWidth)
        Calculates the effective width from the input layout and input width. This involves considering that some elements, such as amp-audio and amp-pixel, have natural dimensions (browser or implementation-specific defaults for width / height).
        Parameters:
        spec - AMP layout spec.
        inputLayout - input layout.
        inputWidth - input width.
        Returns:
        return the css length instance.
      • validateDescendantTags

        public static void validateDescendantTags​(@Nonnull
                                                  ParsedHtmlTag encounteredTag,
                                                  @Nonnull
                                                  ParsedTagSpec parsedTagSpec,
                                                  @Nonnull
                                                  Context context,
                                                  @Nonnull
                                                  dev.amp.validator.ValidatorProtos.ValidationResult.Builder validationResult)
        Validates that this tag is an allowed descendant tag type. Registers new descendent constraints if they are set.
        Parameters:
        encounteredTag - encountered tag.
        parsedTagSpec - the parsed tag spec.
        context - the context.
        validationResult - validation result.
      • validateNoSiblingsAllowedTags

        public static void validateNoSiblingsAllowedTags​(@Nonnull
                                                         ParsedTagSpec parsedTagSpec,
                                                         @Nonnull
                                                         Context context,
                                                         @Nonnull
                                                         dev.amp.validator.ValidatorProtos.ValidationResult.Builder validationResult)
                                                  throws TagValidationException
        Validates if the 'no siblings allowed' rule exists.
        Parameters:
        parsedTagSpec - the parsed tag spec.
        context - the context.
        validationResult - validation result.
        Throws:
        TagValidationException - the TagValidationException.
      • validateLastChildTags

        public static void validateLastChildTags​(@Nonnull
                                                 Context context,
                                                 @Nonnull
                                                 dev.amp.validator.ValidatorProtos.ValidationResult.Builder validationResult)
                                          throws TagValidationException
        Validates if the 'last child' rule exists.
        Parameters:
        context - the context.
        validationResult - validation result.
        Throws:
        TagValidationException - the TagValidationException.
      • validateRequiredExtensions

        public static void validateRequiredExtensions​(@Nonnull
                                                      ParsedTagSpec parsedTagSpec,
                                                      @Nonnull
                                                      Context context,
                                                      @Nonnull
                                                      dev.amp.validator.ValidatorProtos.ValidationResult.Builder validationResult)
        If this tag requires an extension and we have processed all extensions, report an error if that extension has not been loaded.
        Parameters:
        parsedTagSpec - the parsed tag spec.
        context - the context.
        validationResult - validation result.
      • validateUniqueness

        public static void validateUniqueness​(@Nonnull
                                              ParsedTagSpec parsedTagSpec,
                                              @Nonnull
                                              Context context,
                                              @Nonnull
                                              dev.amp.validator.ValidatorProtos.ValidationResult.Builder validationResult)
        Check for duplicates of tags that should be unique, reporting errors for the second instance of each unique tag.
        Parameters:
        parsedTagSpec - the parsed tag spec.
        context - the context.
        validationResult - validation result.