Search in sources :

Example 11 with PrintNode

use of com.google.template.soy.soytree.PrintNode in project closure-templates by google.

the class SoyExprForPySubject method translatesTo.

/**
 * Asserts the subject translates to the expected PyExpr including verification of the exact
 * PyExpr class (e.g. {@code PyStringExpr.class}).
 *
 * @param expectedPyExpr the expected result of translation
 * @param expectedClass the expected class of the resulting PyExpr
 */
public void translatesTo(PyExpr expectedPyExpr, Class<? extends PyExpr> expectedClass) {
    SoyFileSetNode soyTree = SoyFileSetParserBuilder.forTemplateContents(untypedTemplateBodyForExpression(getSubject())).options(opts).parse().fileSet();
    PrintNode node = (PrintNode) SharedTestUtils.getNode(soyTree, 0);
    ExprNode exprNode = node.getExpr();
    PyExpr actualPyExpr = new TranslateToPyExprVisitor(localVarExprs, ErrorReporter.exploding()).exec(exprNode);
    assertThat(actualPyExpr.getText()).isEqualTo(expectedPyExpr.getText());
    assertThat(actualPyExpr.getPrecedence()).isEqualTo(expectedPyExpr.getPrecedence());
    if (expectedClass != null) {
        assertThat(actualPyExpr.getClass()).isEqualTo(expectedClass);
    }
}
Also used : ExprNode(com.google.template.soy.exprtree.ExprNode) PyExpr(com.google.template.soy.pysrc.restricted.PyExpr) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) PrintNode(com.google.template.soy.soytree.PrintNode)

Example 12 with PrintNode

use of com.google.template.soy.soytree.PrintNode in project closure-templates by google.

the class PerformDeprecatedNonContextualAutoescapeVisitorTest method testAutoescapeOnSimple.

@Test
public void testAutoescapeOnSimple() throws Exception {
    String testPrintTags = "{'<br>'}";
    SoyFileSetNode soyTree = parseTestPrintTagsHelper(testPrintTags);
    List<PrintNode> printNodes = SoyTreeUtils.getAllNodesOfType(soyTree, PrintNode.class);
    // Before.
    assertThat(printNodes.get(0).getChildren()).isEmpty();
    performAutoescape(soyTree);
    // After.
    assertThat(printNodes.get(0).getChildren()).hasSize(1);
    assertThat(printNodes.get(0).getChild(0).getName()).isEqualTo(EscapeHtmlDirective.NAME);
}
Also used : SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) PrintNode(com.google.template.soy.soytree.PrintNode) Test(org.junit.Test)

Example 13 with PrintNode

use of com.google.template.soy.soytree.PrintNode in project closure-templates by google.

the class PerformDeprecatedNonContextualAutoescapeVisitorTest method testAutoescapeOnWithOtherDirectives.

@Test
public void testAutoescapeOnWithOtherDirectives() throws Exception {
    String testPrintTags = "{'<br>' |truncate:5}";
    SoyFileSetNode soyTree = parseTestPrintTagsHelper(testPrintTags);
    List<PrintNode> printNodes = SoyTreeUtils.getAllNodesOfType(soyTree, PrintNode.class);
    // Before.
    assertThat(printNodes.get(0).getChildren()).hasSize(1);
    assertThat(printNodes.get(0).getChild(0).getName()).isEqualTo("|truncate");
    performAutoescape(soyTree);
    // After.
    assertThat(printNodes.get(0).getChildren()).hasSize(2);
    assertThat(printNodes.get(0).getChild(0).getName()).isEqualTo(EscapeHtmlDirective.NAME);
    assertThat(printNodes.get(0).getChild(1).getName()).isEqualTo("|truncate");
}
Also used : SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) PrintNode(com.google.template.soy.soytree.PrintNode) Test(org.junit.Test)

Example 14 with PrintNode

use of com.google.template.soy.soytree.PrintNode in project closure-templates by google.

the class VeLogInstrumentationVisitor method visitHtmlAttributeNode.

/**
 * For HtmlAttributeNode that has a logging function as its value, replace the logging function
 * with its place holder, and append a new data attribute that contains all the desired
 * information that are used later by the runtime library.
 */
@Override
protected void visitHtmlAttributeNode(HtmlAttributeNode node) {
    // Skip attributes that do not have a value.
    if (!node.hasValue()) {
        return;
    }
    SourceLocation insertionLocation = node.getSourceLocation();
    for (FunctionNode function : SoyTreeUtils.getAllNodesOfType(node, FunctionNode.class)) {
        if (!(function.getSoyFunction() instanceof LoggingFunction)) {
            continue;
        }
        FunctionNode funcNode = new FunctionNode(VeLogJsSrcLoggingFunction.INSTANCE, insertionLocation);
        funcNode.addChild(new StringNode(function.getFunctionName(), QuoteStyle.SINGLE, insertionLocation));
        funcNode.addChild(new ListLiteralNode(function.getChildren(), insertionLocation));
        StandaloneNode attributeName = node.getChild(0);
        if (attributeName instanceof RawTextNode) {
            // If attribute name is a plain text, directly pass it as a function argument.
            funcNode.addChild(new StringNode(((RawTextNode) attributeName).getRawText(), QuoteStyle.SINGLE, insertionLocation));
        } else {
            // Otherwise wrap the print node or call node into a let block, and use the let variable
            // as a function argument.
            String varName = "soy_logging_function_attribute_" + counter;
            LetContentNode letNode = LetContentNode.forVariable(nodeIdGen.genId(), attributeName.getSourceLocation(), varName, null);
            // Adds a let var which references to the original attribute name, and move the name to
            // the let block.
            node.replaceChild(attributeName, new PrintNode(nodeIdGen.genId(), insertionLocation, /* isImplicit= */
            true, /* expr= */
            new VarRefNode(varName, insertionLocation, false, letNode.getVar()), /* attributes= */
            ImmutableList.of(), ErrorReporter.exploding()));
            letNode.addChild(attributeName);
            node.getParent().addChild(node.getParent().getChildIndex(node), letNode);
            funcNode.addChild(new VarRefNode(varName, insertionLocation, false, letNode.getVar()));
        }
        funcNode.addChild(new IntegerNode(counter++, insertionLocation));
        PrintNode loggingFunctionAttribute = new PrintNode(nodeIdGen.genId(), insertionLocation, /* isImplicit= */
        true, /* expr= */
        funcNode, /* attributes= */
        ImmutableList.of(), ErrorReporter.exploding());
        // Append the logging function attribute to its parent
        int appendIndex = node.getParent().getChildIndex(node) + 1;
        node.getParent().addChild(appendIndex, loggingFunctionAttribute);
        // Replace the original attribute value to the placeholder.
        HtmlAttributeValueNode placeHolder = new HtmlAttributeValueNode(nodeIdGen.genId(), insertionLocation, Quotes.DOUBLE);
        placeHolder.addChild(new RawTextNode(nodeIdGen.genId(), ((LoggingFunction) function.getSoyFunction()).getPlaceholder(), insertionLocation));
        node.replaceChild(node.getChild(1), placeHolder);
        // logging function in a html attribute value.
        break;
    }
    visitChildren(node);
}
Also used : SourceLocation(com.google.template.soy.base.SourceLocation) ListLiteralNode(com.google.template.soy.exprtree.ListLiteralNode) IntegerNode(com.google.template.soy.exprtree.IntegerNode) FunctionNode(com.google.template.soy.exprtree.FunctionNode) StandaloneNode(com.google.template.soy.soytree.SoyNode.StandaloneNode) VarRefNode(com.google.template.soy.exprtree.VarRefNode) StringNode(com.google.template.soy.exprtree.StringNode) LoggingFunction(com.google.template.soy.logging.LoggingFunction) PrintNode(com.google.template.soy.soytree.PrintNode) RawTextNode(com.google.template.soy.soytree.RawTextNode) LetContentNode(com.google.template.soy.soytree.LetContentNode) HtmlAttributeValueNode(com.google.template.soy.soytree.HtmlAttributeValueNode)

Example 15 with PrintNode

use of com.google.template.soy.soytree.PrintNode in project closure-templates by google.

the class SourceLocationTest method testIsJustBefore.

@Test
public void testIsJustBefore() throws Exception {
    String template = JOINER.join("{namespace ns}", "{template .t}", "{@param foo : ?}", "{@param bar : ?}", // pair 1
    "  {$foo}{$bar}", // pair 2
    "  {$foo} {$bar}", // pair 3
    "  {$foo}", "  {$bar}", // pair 4
    "{$foo}", "{$bar}", "{/template}");
    TemplateNode templateNode = SoyFileSetParserBuilder.forFileContents(template).parse().fileSet().getChild(0).getChild(0);
    List<PrintNode> nodes = SoyTreeUtils.getAllNodesOfType(templateNode, PrintNode.class);
    assertThat(nodes).hasSize(8);
    PrintNode foo1 = nodes.get(0);
    PrintNode bar1 = nodes.get(1);
    assertTrue(foo1.getSourceLocation().isJustBefore(bar1.getSourceLocation()));
    PrintNode foo2 = nodes.get(2);
    PrintNode bar2 = nodes.get(3);
    assertFalse(foo2.getSourceLocation().isJustBefore(bar2.getSourceLocation()));
    PrintNode foo3 = nodes.get(4);
    PrintNode bar3 = nodes.get(5);
    assertFalse(foo3.getSourceLocation().isJustBefore(bar3.getSourceLocation()));
    PrintNode foo4 = nodes.get(6);
    PrintNode bar4 = nodes.get(7);
    assertFalse(foo4.getSourceLocation().isJustBefore(bar4.getSourceLocation()));
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) PrintNode(com.google.template.soy.soytree.PrintNode) Test(org.junit.Test)

Aggregations

PrintNode (com.google.template.soy.soytree.PrintNode)25 Test (org.junit.Test)15 FunctionNode (com.google.template.soy.exprtree.FunctionNode)8 StandaloneNode (com.google.template.soy.soytree.SoyNode.StandaloneNode)8 StringNode (com.google.template.soy.exprtree.StringNode)6 TemplateNode (com.google.template.soy.soytree.TemplateNode)6 SoyFileSetNode (com.google.template.soy.soytree.SoyFileSetNode)5 RawTextNode (com.google.template.soy.soytree.RawTextNode)4 FieldAccessNode (com.google.template.soy.exprtree.FieldAccessNode)3 VarRefNode (com.google.template.soy.exprtree.VarRefNode)3 IfCondNode (com.google.template.soy.soytree.IfCondNode)3 IfNode (com.google.template.soy.soytree.IfNode)3 MsgHtmlTagNode (com.google.template.soy.soytree.MsgHtmlTagNode)3 MsgPlaceholderNode (com.google.template.soy.soytree.MsgPlaceholderNode)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 SourceLocation (com.google.template.soy.base.SourceLocation)2 ExprNode (com.google.template.soy.exprtree.ExprNode)2 IntegerNode (com.google.template.soy.exprtree.IntegerNode)2 PyExpr (com.google.template.soy.pysrc.restricted.PyExpr)2 SoyFunction (com.google.template.soy.shared.restricted.SoyFunction)2