Package dev.amp.validator
Class TagStack
- java.lang.Object
-
- dev.amp.validator.TagStack
-
public class TagStack extends Object
This class keeps track of the tag names and ChildTagMatchers as we enter / exit tags in the document. Closing tags is tricky: - We assume that all end tags are optional and we close, that is, pop off tags our stack, lazily as we encounter parent closing tags. This part differs slightly from the behavior per spec: instead of closing an option tag when a following option tag is seen, we close it when the parent closing tag (in practice select) is encountered.- Author:
- nhant01, GeorgeLuo
-
-
Constructor Summary
Constructors Constructor Description TagStack()Constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description List<DescendantConstraints>allowedDescendantsList()Returns the list of DescendantConstraints.TagStackEntryback()Alias to the last element on the tag stack.CdataMatchercdataMatcher()Returns the cdata matcher for the tag currently on the stack.booleancountDocCssBytes()Returns true if this within lt;style amp-custom gt;.voidexitTag(Context context, dev.amp.validator.ValidatorProtos.ValidationResult.Builder result)Upon exiting a tag, validation for the current child tag matcher is triggered, e.g.booleanhasAncestor(String ancestor)Returns true if the current tag has ancestor with the given tag name or specName.booleanhasAncestorMarker(dev.amp.validator.ValidatorProtos.AncestorMarker.Marker query)Returns true if the current tag has an ancestor which set the given marker.booleanisScriptTypeJsonChild()Returns true if this within script type=application/json.voidmatchChildTagName(ParsedHtmlTag encounteredTag, Context context, dev.amp.validator.ValidatorProtos.ValidationResult.Builder result)This method is called as we're visiting a tag; so the matcher we need here is the one provided/specified for the tag parent.intparentChildCount()The number of children that have been discovered up to now by traversing the stack.booleanparentHasChildWithLastChildRule()booleanparentHasChildWithNoSiblingRule()LocatorparentLastChildErrorLineCol()Returns the Locator of the tag that set the 'last child' rule.StringparentLastChildTagName()StringparentLastChildUrl()LocatorparentOnlyChildErrorLineCol()Returns the parent only child error line col.StringparentOnlyChildTagName()ReferencePointMatcherparentReferencePointMatcher()Returns the parent stack entry reference point matcher.TagStackEntryparentStackEntry()The parent of the current stack entry.StringparentTagName()The name of the parent of the current tag.StringparentTagSpecName()The spec_name of the parent of the current tag if one exists, otherwise the tag_name.voidsetCdataMatcher(CdataMatcher matcher)Sets the cdata matcher for the tag currently on the stack.voidsetChildTagMatcher(ChildTagMatcher matcher)Sets the child tag matcher for the tag currently on the stack.voidsetDescendantConstraintList(ParsedTagSpec parsedTagSpec, ParsedValidatorRules parsedRules)Setting the descendant constraint list.voidsetReferencePointMatcher(ReferencePointMatcher matcher)Sets the reference point matcher for the tag currently on the stack.voidtellParentImTheLastChild(String tagName, String url, Locator lineCol)Tells the parent of the current stack entry that its last child must be me (the current stack entry).voidtellParentNoSiblingsAllowed(String tagName, Locator lineCol)Tells the parent of the current stack entry that it can only have 1 child and that child must be me (the current stack entry).voidupdateFromTagResults(ParsedHtmlTag encounteredTag, ValidateTagResult referencePointResult, ValidateTagResult tagResult, ParsedValidatorRules parsedRules, Locator lineCol)Update tagstack state after validating an encountered tag.voidupdateStackEntryFromTagResult(ValidateTagResult result, ParsedValidatorRules parsedRules, Locator lineCol)Given a ValidateTagResult, update the tag stack entry at the top of the tag stack to add any constraints from the spec.
-
-
-
Method Detail
-
countDocCssBytes
public boolean countDocCssBytes() throws TagValidationExceptionReturns true if this within lt;style amp-custom gt;.- Returns:
- returns true if this within lt;style amp-custom gt; Else false.
- Throws:
TagValidationException- the TagValidationException.
-
parentStackEntry
public TagStackEntry parentStackEntry() throws TagValidationException
The parent of the current stack entry.- Returns:
- TagStackEntry the TagStackEntry.
- Throws:
TagValidationException- the TagValidationException.
-
back
public TagStackEntry back() throws TagValidationException
Alias to the last element on the tag stack.- Returns:
- TagStackEntry the TagStackEntry.
- Throws:
TagValidationException- the TagValidationException.
-
parentReferencePointMatcher
public ReferencePointMatcher parentReferencePointMatcher() throws TagValidationException
Returns the parent stack entry reference point matcher.- Returns:
- returns the parent stack entry reference point matcher.
- Throws:
TagValidationException- the TagValidationException.
-
tellParentNoSiblingsAllowed
public void tellParentNoSiblingsAllowed(@Nonnull String tagName, @Nonnull Locator lineCol) throws TagValidationExceptionTells the parent of the current stack entry that it can only have 1 child and that child must be me (the current stack entry).- Parameters:
tagName- The current stack entry's tag name.lineCol- pair of line/col.- Throws:
TagValidationException- the TagValidationException.
-
parentOnlyChildErrorLineCol
public Locator parentOnlyChildErrorLineCol() throws TagValidationException
Returns the parent only child error line col.- Returns:
- returns the Locator of the tag that set the rule.
- Throws:
TagValidationException- the TagValidationException.
-
parentOnlyChildTagName
public String parentOnlyChildTagName() throws TagValidationException
- Returns:
- returns the name of the tag that set the 'no siblings allowed' rule.
- Throws:
TagValidationException- the TagValidationException.
-
parentHasChildWithNoSiblingRule
public boolean parentHasChildWithNoSiblingRule() throws TagValidationException- Returns:
- returns true if this tag's parent has a child with 'no siblings allowed' rule. Else false.
- Throws:
TagValidationException- the TagValidationException.
-
parentLastChildErrorLineCol
public Locator parentLastChildErrorLineCol() throws TagValidationException
Returns the Locator of the tag that set the 'last child' rule.- Returns:
- The Locator of the tag that set the 'last child' rule.
- Throws:
TagValidationException- the TagValidationException.
-
allowedDescendantsList
public List<DescendantConstraints> allowedDescendantsList()
Returns the list of DescendantConstraints.- Returns:
- returns the list of DescendantConstraints.
-
tellParentImTheLastChild
public void tellParentImTheLastChild(@Nonnull String tagName, @Nonnull String url, @Nonnull Locator lineCol) throws TagValidationExceptionTells the parent of the current stack entry that its last child must be me (the current stack entry).- Parameters:
tagName- The current stack entry's tag name.url- The current stack entry's spec url.lineCol- a pair line/col.- Throws:
TagValidationException- the TagValidationException.
-
matchChildTagName
public void matchChildTagName(@Nonnull ParsedHtmlTag encounteredTag, @Nonnull Context context, @Nonnull dev.amp.validator.ValidatorProtos.ValidationResult.Builder result) throws TagValidationExceptionThis method is called as we're visiting a tag; so the matcher we need here is the one provided/specified for the tag parent.- Parameters:
encounteredTag- encountered tag.context- the Context object.result- validation result.- Throws:
TagValidationException- the TagValidationException.
-
isScriptTypeJsonChild
public boolean isScriptTypeJsonChild() throws TagValidationExceptionReturns true if this within script type=application/json. Else false.- Returns:
- returns true if this within script type=application/json. Else false.
- Throws:
TagValidationException- the TagValidationException.
-
hasAncestor
public boolean hasAncestor(@Nonnull String ancestor)Returns true if the current tag has ancestor with the given tag name or specName.- Parameters:
ancestor- ancestor tag.- Returns:
- returns true if the current tag has ancestor with the given tag name or specName.
-
hasAncestorMarker
public boolean hasAncestorMarker(@Nonnull dev.amp.validator.ValidatorProtos.AncestorMarker.Marker query) throws TagValidationExceptionReturns true if the current tag has an ancestor which set the given marker.- Parameters:
query- the ancestor marker.- Returns:
- returns true if the current tag has an ancestor which set the given marker.
- Throws:
TagValidationException- the TagValidationException.
-
exitTag
public void exitTag(@Nonnull Context context, @Nonnull dev.amp.validator.ValidatorProtos.ValidationResult.Builder result) throws TagValidationExceptionUpon exiting a tag, validation for the current child tag matcher is triggered, e.g. for checking that the tag had some specified number of children.- Parameters:
context- the context.result- the ValidationResult.- Throws:
TagValidationException- the TagValidationException.
-
updateStackEntryFromTagResult
public void updateStackEntryFromTagResult(@Nonnull ValidateTagResult result, @Nonnull ParsedValidatorRules parsedRules, @Nonnull Locator lineCol) throws TagValidationExceptionGiven a ValidateTagResult, update the tag stack entry at the top of the tag stack to add any constraints from the spec.- Parameters:
result- the ValidateTagResult.parsedRules- the ParsedValidatorRules.lineCol- a pair of line/col.- Throws:
TagValidationException- the TagValidationException.
-
updateFromTagResults
public void updateFromTagResults(@Nonnull ParsedHtmlTag encounteredTag, @Nonnull ValidateTagResult referencePointResult, @Nonnull ValidateTagResult tagResult, @Nonnull ParsedValidatorRules parsedRules, @Nonnull Locator lineCol) throws TagValidationExceptionUpdate tagstack state after validating an encountered tag. Called with the best matching specs, even if not a match.- Parameters:
encounteredTag- the ParsedHtmlTag.referencePointResult- reference point result.tagResult- tag result.parsedRules- parsed rules.lineCol- a pair line/col.- Throws:
TagValidationException- the TagValidationException.
-
parentTagSpecName
public String parentTagSpecName() throws TagValidationException
The spec_name of the parent of the current tag if one exists, otherwise the tag_name.- Returns:
- The spec_name of the parent of the current tag, or tag_name
- Throws:
TagValidationException- the TagValidationException.
-
parentChildCount
public int parentChildCount() throws TagValidationExceptionThe number of children that have been discovered up to now by traversing the stack.- Returns:
- returns parent stack's num children.
- Throws:
TagValidationException- the TagValidationException.
-
setReferencePointMatcher
public void setReferencePointMatcher(ReferencePointMatcher matcher) throws TagValidationException
Sets the reference point matcher for the tag currently on the stack.- Parameters:
matcher- reference point matcher.- Throws:
TagValidationException- the TagValidationException.
-
setChildTagMatcher
public void setChildTagMatcher(ChildTagMatcher matcher) throws TagValidationException
Sets the child tag matcher for the tag currently on the stack.- Parameters:
matcher- child tag matcher.- Throws:
TagValidationException- the TagValidationException.
-
setCdataMatcher
public void setCdataMatcher(CdataMatcher matcher) throws TagValidationException
Sets the cdata matcher for the tag currently on the stack.- Parameters:
matcher- Cdata matcher.- Throws:
TagValidationException- the TagValidationException.
-
cdataMatcher
public CdataMatcher cdataMatcher() throws TagValidationException
Returns the cdata matcher for the tag currently on the stack. If there is no cdata matcher, returns null.- Returns:
- returns the CdataMatcher for the tag currently on the stack.
- Throws:
TagValidationException- the TagValidationException.
-
parentTagName
public String parentTagName() throws TagValidationException
The name of the parent of the current tag.- Returns:
- returns the parent stack entry tag name.
- Throws:
TagValidationException- the TagValidationException.
-
parentLastChildTagName
public String parentLastChildTagName() throws TagValidationException
- Returns:
- returns the name of the tag with the 'last child' rule.
- Throws:
TagValidationException- the TagValidationException.
-
parentLastChildUrl
public String parentLastChildUrl() throws TagValidationException
- Returns:
- returns the spec url of the last child.
- Throws:
TagValidationException- the TagValidationException.
-
parentHasChildWithLastChildRule
public boolean parentHasChildWithLastChildRule() throws TagValidationException- Returns:
- returns true if this tag's parent has a child with 'last child' rule. Else false.
- Throws:
TagValidationException- the TagValidationException.
-
setDescendantConstraintList
public void setDescendantConstraintList(@Nonnull ParsedTagSpec parsedTagSpec, @Nonnull ParsedValidatorRules parsedRules) throws TagValidationExceptionSetting the descendant constraint list.- Parameters:
parsedTagSpec- the parsed tag spec.parsedRules- the parsed rules.- Throws:
TagValidationException- the TagValidationException.
-
-