Class 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 Detail

      • TagStack

        public TagStack()
        Constructor.
    • Method Detail

      • countDocCssBytes

        public boolean countDocCssBytes()
                                 throws TagValidationException
        Returns 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.
      • tellParentNoSiblingsAllowed

        public void tellParentNoSiblingsAllowed​(@Nonnull
                                                String tagName,
                                                @Nonnull
                                                Locator lineCol)
                                         throws TagValidationException
        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).
        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.
      • 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 TagValidationException
        Tells 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 TagValidationException
        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.
        Parameters:
        encounteredTag - encountered tag.
        context - the Context object.
        result - validation result.
        Throws:
        TagValidationException - the TagValidationException.
      • isScriptTypeJsonChild

        public boolean isScriptTypeJsonChild()
                                      throws TagValidationException
        Returns 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 TagValidationException
        Returns 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 TagValidationException
        Upon 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 TagValidationException
        Given 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 TagValidationException
        Update 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 TagValidationException
        The 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.
      • 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.
      • 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.