Search in sources :

Example 1 with NodeVisitor

use of com.google.template.soy.basetree.NodeVisitor in project closure-templates by google.

the class MsgHtmlTagNode method getFullTagText.

/**
 * This method calculates a string that can be used to tell if two tags that were turned into
 * placeholders are equivalent and thus could be turned into identical placeholders.
 *
 * <p>In theory we should use something like {@link ExprEquivalence} to see if two nodes would
 * render the same thing. However, for backwards compatibility we need to use a different
 * heuristic. The old code would simply detect if the node had a single child and use the {@link
 * SoyNode#toSourceString()} of it for the tag text. Due to how the children were constructed,
 * this would only happen if the tag was a single {@link RawTextNode}, e.g. {@code <foo
 * class=bar>}. Now that we are actually parsing the html tags the rules are more complex. We
 * should instead only use the {@link SoyNode#toSourceString()} if the only (transitive) children
 * are {@link RawTextNode}, {@link HtmlAttributeNode} or {@link HtmlAttributeValueNode}.
 */
@Nullable
private static String getFullTagText(HtmlTagNode openTagNode) {
    class Visitor implements NodeVisitor<Node, VisitDirective> {

        boolean isConstantContent = true;

        @Override
        public VisitDirective exec(Node node) {
            if (node instanceof RawTextNode || node instanceof HtmlAttributeNode || node instanceof HtmlAttributeValueNode || node instanceof HtmlOpenTagNode || node instanceof HtmlCloseTagNode) {
                return VisitDirective.CONTINUE;
            }
            isConstantContent = false;
            return VisitDirective.ABORT;
        }
    }
    Visitor visitor = new Visitor();
    SoyTreeUtils.visitAllNodes(openTagNode, visitor);
    if (visitor.isConstantContent) {
        // toSourceString is lame, but how this worked before
        return openTagNode.toSourceString();
    }
    return null;
}
Also used : NodeVisitor(com.google.template.soy.basetree.NodeVisitor) Node(com.google.template.soy.basetree.Node) MsgPlaceholderInitialNode(com.google.template.soy.soytree.SoyNode.MsgPlaceholderInitialNode) NodeVisitor(com.google.template.soy.basetree.NodeVisitor) Nullable(javax.annotation.Nullable)

Example 2 with NodeVisitor

use of com.google.template.soy.basetree.NodeVisitor in project closure-templates by google.

the class SoyTreeUtils method hasNodesOfType.

/**
 * Returns true if the given {@code node} contains any children of the given types.
 */
@SafeVarargs
public static boolean hasNodesOfType(Node node, final Class<? extends Node>... types) {
    class Visitor implements NodeVisitor<Node, VisitDirective> {

        boolean found;

        @Override
        public VisitDirective exec(Node node) {
            for (Class type : types) {
                if (type.isInstance(node)) {
                    found = true;
                    return VisitDirective.ABORT;
                }
            }
            return VisitDirective.CONTINUE;
        }
    }
    Visitor v = new Visitor();
    visitAllNodes(node, v);
    return v.found;
}
Also used : AbstractNodeVisitor(com.google.template.soy.basetree.AbstractNodeVisitor) NodeVisitor(com.google.template.soy.basetree.NodeVisitor) Node(com.google.template.soy.basetree.Node) ExprRootNode(com.google.template.soy.exprtree.ExprRootNode) ParentSoyNode(com.google.template.soy.soytree.SoyNode.ParentSoyNode) ExprHolderNode(com.google.template.soy.soytree.SoyNode.ExprHolderNode) ParentNode(com.google.template.soy.basetree.ParentNode) ExprNode(com.google.template.soy.exprtree.ExprNode) AbstractNodeVisitor(com.google.template.soy.basetree.AbstractNodeVisitor) NodeVisitor(com.google.template.soy.basetree.NodeVisitor)

Aggregations

Node (com.google.template.soy.basetree.Node)2 NodeVisitor (com.google.template.soy.basetree.NodeVisitor)2 AbstractNodeVisitor (com.google.template.soy.basetree.AbstractNodeVisitor)1 ParentNode (com.google.template.soy.basetree.ParentNode)1 ExprNode (com.google.template.soy.exprtree.ExprNode)1 ExprRootNode (com.google.template.soy.exprtree.ExprRootNode)1 ExprHolderNode (com.google.template.soy.soytree.SoyNode.ExprHolderNode)1 MsgPlaceholderInitialNode (com.google.template.soy.soytree.SoyNode.MsgPlaceholderInitialNode)1 ParentSoyNode (com.google.template.soy.soytree.SoyNode.ParentSoyNode)1 Nullable (javax.annotation.Nullable)1