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();
}
}
}
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;
}
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;
}
Aggregations