Search in sources :

Example 1 with Node

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

the class SoyTreeUtils method visitAllNodes.

/**
 * Runs the visitor on all nodes (including {@link ExprNode expr nodes}) reachable from the given
 * node. The order of visiting is breadth first.
 *
 * <p>If the visitor return {@code false} from {@link NodeVisitor#exec(Node)} we will short
 * circuit visiting.
 */
public static void visitAllNodes(Node node, NodeVisitor<? super Node, VisitDirective> visitor) {
    ArrayDeque<Node> queue = new ArrayDeque<>();
    queue.add(node);
    Node current;
    while ((current = queue.poll()) != null) {
        switch(visitor.exec(current)) {
            case ABORT:
                return;
            case CONTINUE:
                if (current instanceof ParentNode<?>) {
                    queue.addAll(((ParentNode<?>) current).getChildren());
                }
                if (current instanceof ExprHolderNode) {
                    queue.addAll(((ExprHolderNode) current).getExprList());
                }
                continue;
            case SKIP_CHILDREN:
                continue;
            default:
                throw new AssertionError();
        }
    }
}
Also used : ParentNode(com.google.template.soy.basetree.ParentNode) 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) ExprHolderNode(com.google.template.soy.soytree.SoyNode.ExprHolderNode) ArrayDeque(java.util.ArrayDeque)

Example 2 with Node

use of com.google.template.soy.basetree.Node 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 3 with Node

use of com.google.template.soy.basetree.Node 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)3 NodeVisitor (com.google.template.soy.basetree.NodeVisitor)2 ParentNode (com.google.template.soy.basetree.ParentNode)2 ExprNode (com.google.template.soy.exprtree.ExprNode)2 ExprRootNode (com.google.template.soy.exprtree.ExprRootNode)2 ExprHolderNode (com.google.template.soy.soytree.SoyNode.ExprHolderNode)2 ParentSoyNode (com.google.template.soy.soytree.SoyNode.ParentSoyNode)2 AbstractNodeVisitor (com.google.template.soy.basetree.AbstractNodeVisitor)1 MsgPlaceholderInitialNode (com.google.template.soy.soytree.SoyNode.MsgPlaceholderInitialNode)1 ArrayDeque (java.util.ArrayDeque)1 Nullable (javax.annotation.Nullable)1