Package dev.amp.validator.utils
Class TagSpecUtils
- java.lang.Object
-
- dev.amp.validator.utils.TagSpecUtils
-
public final class TagSpecUtils extends Object
Tag spec utility methods.- Author:
- nhant01, GeorgeLuo
-
-
Field Summary
Fields Modifier and Type Field Description static ListACTIONS_IDENTIFIERSList identifiers for ACTIONS format.static ListAMP_IDENTIFIERSList identifiers for AMP format.static ListAMP4ADS_IDENTIFIERSList identifiers for AMP4ADS format.static ListAMP4EMAIL_IDENTIFIERSList identifiers for AMP4EMAIL format.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static CssLengthcalculateHeight(dev.amp.validator.ValidatorProtos.AmpLayout spec, dev.amp.validator.ValidatorProtos.AmpLayout.Layout inputLayout, CssLength inputHeight)Calculates the effective height from input layout and input height.static CssLengthcalculateHeightForTag(dev.amp.validator.ValidatorProtos.AmpLayout.Layout inputLayout, CssLength inputHeight, String tagName)Calculates the effective height from the input layout, input height and tag.static dev.amp.validator.ValidatorProtos.AmpLayout.LayoutcalculateLayout(dev.amp.validator.ValidatorProtos.AmpLayout.Layout inputLayout, CssLength width, CssLength height, String sizesAttr, String heightsAttr)Calculates the layout; this depends on the width / height calculation above.static CssLengthcalculateWidth(dev.amp.validator.ValidatorProtos.AmpLayout spec, dev.amp.validator.ValidatorProtos.AmpLayout.Layout inputLayout, CssLength inputWidth)Calculates the effective width from the input layout and input width.static CssLengthcalculateWidthForTag(dev.amp.validator.ValidatorProtos.AmpLayout.Layout inputLayout, CssLength inputWidth, String tagName)Calculates the effective width from the input layout, input width and tag.static StringgetLayoutAwaitingSizeClass()static StringgetLayoutClass(dev.amp.validator.ValidatorProtos.AmpLayout.Layout layout)static StringgetLayoutSizeDefinedClass()static StringgetTagDescriptiveName(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.static StringgetTagSpecName(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.static StringgetTagSpecUrl(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.static booleanisLayoutAwaitingSize(dev.amp.validator.ValidatorProtos.AmpLayout.Layout layout)static booleanisLayoutSizeDefined(dev.amp.validator.ValidatorProtos.AmpLayout.Layout layout)static dev.amp.validator.ValidatorProtos.AmpLayout.LayoutparseLayout(String layout)static RecordValidatedshouldRecordTagspecValidated(dev.amp.validator.ValidatorProtos.TagSpec tag, int tagSpecId, Map<Object,Boolean> tagSpecIdsToTrack)We only track (that is, add them to Context.RecordTagspecValidated) validated tagspecs as necessary.static voidvalidateAncestorTags(ParsedTagSpec parsedTagSpec, Context context, dev.amp.validator.ValidatorProtos.ValidationResult.Builder validationResult)Validates if the tag ancestors satisfied the spec.static voidvalidateDescendantTags(ParsedHtmlTag encounteredTag, ParsedTagSpec parsedTagSpec, Context context, dev.amp.validator.ValidatorProtos.ValidationResult.Builder validationResult)Validates that this tag is an allowed descendant tag type.static voidvalidateLastChildTags(Context context, dev.amp.validator.ValidatorProtos.ValidationResult.Builder validationResult)Validates if the 'last child' rule exists.static voidvalidateNoSiblingsAllowedTags(ParsedTagSpec parsedTagSpec, Context context, dev.amp.validator.ValidatorProtos.ValidationResult.Builder validationResult)Validates if the 'no siblings allowed' rule exists.static voidvalidateParentTag(ParsedTagSpec parsedTagSpec, Context context, dev.amp.validator.ValidatorProtos.ValidationResult.Builder validationResult)Validates whether the parent tag satisfied the spec (e.g., some tags can only appear in head).static voidvalidateRequiredExtensions(ParsedTagSpec parsedTagSpec, Context context, 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.static voidvalidateSsrLayout(dev.amp.validator.ValidatorProtos.TagSpec spec, ParsedHtmlTag encounteredTag, dev.amp.validator.ValidatorProtos.AmpLayout.Layout inputLayout, CssLength inputWidth, CssLength inputHeight, String sizesAttr, String heightsAttr, Context context, dev.amp.validator.ValidatorProtos.ValidationResult.Builder result)Helper method for validateLayout.static ValidateTagResultvalidateTag(Context context, ParsedHtmlTag encounteredTag, ParsedTagSpec bestMatchReferencePoint)Validates the provided |tagName| with respect to the tag specifications in the validator's rules, returning a ValidationResult with errors for this tag and a PASS or FAIL status.static ValidateTagResultvalidateTagAgainstSpec(ParsedTagSpec parsedTagSpec, ParsedTagSpec bestMatchReferencePoint, Context context, ParsedHtmlTag encounteredTag)Validates the provided |tagName| with respect to a single tag specification.static voidvalidateUniqueness(ParsedTagSpec parsedTagSpec, Context context, 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.
-
-
-
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.
-
validateTag
public static ValidateTagResult validateTag(@Nonnull Context context, @Nonnull ParsedHtmlTag encounteredTag, ParsedTagSpec bestMatchReferencePoint) throws TagValidationException, ValidatorException, IOException, CssValidationException
Validates the provided |tagName| with respect to the tag specifications in the validator's rules, returning a ValidationResult with errors for this tag and a PASS or FAIL status. At least one specification must validate, or the result will have status FAIL. Also passes back a reference to the tag spec which matched, if a match was found.- Parameters:
context- the contextencounteredTag- the encountered tagbestMatchReferencePoint- the best match reference point- Returns:
- returns ValidateTagResult.
- Throws:
TagValidationException- tag validation exception.ValidatorException- tag validation exception.IOException- the IOException.CssValidationException- Css Validation Exception.
-
validateTagAgainstSpec
public static ValidateTagResult validateTagAgainstSpec(@Nonnull ParsedTagSpec parsedTagSpec, ParsedTagSpec bestMatchReferencePoint, @Nonnull Context context, @Nonnull ParsedHtmlTag encounteredTag) throws TagValidationException, IOException, CssValidationException
Validates the provided |tagName| with respect to a single tag specification.- Parameters:
parsedTagSpec- parsed tag spec.bestMatchReferencePoint- best match reference point.context- the context object.encounteredTag- the encountered tag.- Returns:
- returns the validation result.
- Throws:
TagValidationException- the TagValidationException.IOException- IO ExceptionCssValidationException- Css validation exception.
-
validateParentTag
public static void validateParentTag(@Nonnull ParsedTagSpec parsedTagSpec, @Nonnull Context context, @Nonnull dev.amp.validator.ValidatorProtos.ValidationResult.Builder validationResult) throws TagValidationExceptionValidates 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 TagValidationExceptionValidates 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 TagValidationExceptionValidates 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.
-
-