Search in sources :

Example 6 with StringNode

use of com.google.template.soy.exprtree.StringNode in project closure-templates by google.

the class TemplateDelegateNodeBuilder method genInternalTemplateNameHelper.

/**
 * Private helper for both setCmdText() and setCmdTextInfo() to generate and set the internal-use
 * partial template name and template name.
 */
private void genInternalTemplateNameHelper() {
    Preconditions.checkState(id != null);
    // encode all the deltemplate information into the name to get a unique string
    // though... it might make more sense for this to not have a user visible name given that the
    // calling convention is indirect.
    String variant = "";
    if (delTemplateVariantExpr != null) {
        // this is hacky.  perhaps we should come up with a less ambiguous strategy
        ExprNode expr = delTemplateVariantExpr.getRoot();
        if (expr instanceof StringNode) {
            variant = ((StringNode) expr).getValue();
        } else {
            variant = expr.toSourceString();
        }
    }
    String delPackageTemplateAndVariantStr = (soyFileHeaderInfo.delPackageName == null ? "" : soyFileHeaderInfo.delPackageName) + "_" + delTemplateName.replace('.', '_') + "_" + variant;
    delPackageTemplateAndVariantStr = delPackageTemplateAndVariantStr.replace('.', '_');
    // Generate the actual internal-use template name.
    String generatedPartialTemplateName = ".__deltemplate_" + delPackageTemplateAndVariantStr;
    String generatedTemplateName = soyFileHeaderInfo.namespace + generatedPartialTemplateName;
    setTemplateNames(generatedTemplateName, generatedPartialTemplateName);
}
Also used : ExprNode(com.google.template.soy.exprtree.ExprNode) StringNode(com.google.template.soy.exprtree.StringNode)

Example 7 with StringNode

use of com.google.template.soy.exprtree.StringNode in project closure-templates by google.

the class TemplateDelegateNode method resolveVariantExpression.

/**
 * Calculate a DeltemplateKey for the variant.
 *
 * <p>This is done lazily so that global references can be resolved. This is not ideal since
 * nothing guarantees that resolution happens before access.
 *
 * <p>Note we don't do validation of the variant values since that is handled by the
 * TemplateDelegateNodeBuilder during construction
 */
private DelTemplateKey resolveVariantExpression() {
    if (delTemplateVariantExpr == null) {
        delTemplateKey = DelTemplateKey.create(delTemplateName, "");
        return delTemplateKey;
    }
    ExprNode exprNode = delTemplateVariantExpr.getRoot();
    if (exprNode instanceof GlobalNode) {
        GlobalNode globalNode = (GlobalNode) exprNode;
        if (globalNode.isResolved()) {
            exprNode = globalNode.getValue();
        } else {
            // For this reason we also don't store the key, instead we just return it.
            return DelTemplateKey.create(delTemplateName, globalNode.getName());
        }
    }
    if (exprNode instanceof IntegerNode) {
        // Globals were already substituted: We may now create the definitive variant and key fields
        // on this node.
        long variantValue = ((IntegerNode) exprNode).getValue();
        delTemplateKey = DelTemplateKey.create(delTemplateName, String.valueOf(variantValue));
    } else if (exprNode instanceof StringNode) {
        // Globals were already substituted: We may now create the definitive variant and key fields
        // on this node.
        delTemplateKey = DelTemplateKey.create(delTemplateName, ((StringNode) exprNode).getValue());
    } else {
        // We must have already reported an error, just create an arbitrary variant expr.
        delTemplateKey = DelTemplateKey.create(delTemplateName, exprNode.toSourceString());
    }
    return delTemplateKey;
}
Also used : ExprNode(com.google.template.soy.exprtree.ExprNode) IntegerNode(com.google.template.soy.exprtree.IntegerNode) StringNode(com.google.template.soy.exprtree.StringNode) GlobalNode(com.google.template.soy.exprtree.GlobalNode)

Example 8 with StringNode

use of com.google.template.soy.exprtree.StringNode in project closure-templates by google.

the class TemplateParserTest method testParsePrintStmt.

@Test
public void testParsePrintStmt() throws Exception {
    String templateBody = "{@param boo : ?}{@param goo : ?}\n" + "  {$boo.foo}{$boo.foo}\n" + "  {$goo + 1 |noAutoescape}\n" + "  {print 'blah    blahblahblah' |escapeHtml|insertWordBreaks:8}\n";
    List<StandaloneNode> nodes = parseTemplateContent(templateBody, FAIL).getChildren();
    assertEquals(4, nodes.size());
    PrintNode pn0 = (PrintNode) nodes.get(0);
    assertEquals("$boo.foo", pn0.getExpr().toSourceString());
    assertEquals(0, pn0.getChildren().size());
    assertEquals("FOO", pn0.genBasePhName());
    assertEquals("{$boo.foo}", pn0.toSourceString());
    assertTrue(pn0.getExpr().getRoot() instanceof FieldAccessNode);
    PrintNode pn1 = (PrintNode) nodes.get(1);
    assertTrue(pn0.genSamenessKey().equals(pn1.genSamenessKey()));
    assertTrue(pn1.getExpr().getRoot() instanceof FieldAccessNode);
    PrintNode pn2 = (PrintNode) nodes.get(2);
    assertEquals("$goo + 1", pn2.getExpr().toSourceString());
    assertEquals(1, pn2.getChildren().size());
    PrintDirectiveNode pn2d0 = pn2.getChild(0);
    assertEquals("|noAutoescape", pn2d0.getName());
    assertEquals("XXX", pn2.genBasePhName());
    assertTrue(pn2.getExpr().getRoot() instanceof PlusOpNode);
    PrintNode pn3 = (PrintNode) nodes.get(3);
    assertEquals("'blah    blahblahblah'", pn3.getExpr().toSourceString());
    assertEquals(2, pn3.getChildren().size());
    PrintDirectiveNode pn3d0 = pn3.getChild(0);
    assertEquals("|escapeHtml", pn3d0.getName());
    PrintDirectiveNode pn3d1 = pn3.getChild(1);
    assertEquals("|insertWordBreaks", pn3d1.getName());
    assertEquals(8, ((IntegerNode) pn3d1.getArgs().get(0).getRoot()).getValue());
    assertEquals("XXX", pn3.genBasePhName());
    assertTrue(pn3.getExpr().getRoot() instanceof StringNode);
    assertFalse(pn0.genSamenessKey().equals(pn2.genSamenessKey()));
    assertFalse(pn3.genSamenessKey().equals(pn0.genSamenessKey()));
}
Also used : StandaloneNode(com.google.template.soy.soytree.SoyNode.StandaloneNode) PrintDirectiveNode(com.google.template.soy.soytree.PrintDirectiveNode) StringNode(com.google.template.soy.exprtree.StringNode) PlusOpNode(com.google.template.soy.exprtree.OperatorNodes.PlusOpNode) PrintNode(com.google.template.soy.soytree.PrintNode) FieldAccessNode(com.google.template.soy.exprtree.FieldAccessNode) Test(org.junit.Test)

Example 9 with StringNode

use of com.google.template.soy.exprtree.StringNode in project closure-templates by google.

the class ResolveExpressionTypesVisitorTest method assertTypes.

/**
 * Traverses the tree and checks all the calls to {@code assertType}
 */
private void assertTypes(SoyNode node) {
    for (FunctionNode fn : SoyTreeUtils.getAllNodesOfType(node, FunctionNode.class)) {
        if (fn.getFunctionName().equals("assertType")) {
            StringNode expected = (StringNode) fn.getChild(0);
            SoyType actualType = fn.getChild(1).getType();
            assertWithMessage("assertion @ " + fn.getSourceLocation()).that(actualType.toString()).isEqualTo(expected.getValue());
        }
    }
}
Also used : SoyType(com.google.template.soy.types.SoyType) FunctionNode(com.google.template.soy.exprtree.FunctionNode) StringNode(com.google.template.soy.exprtree.StringNode)

Example 10 with StringNode

use of com.google.template.soy.exprtree.StringNode in project closure-templates by google.

the class ResolvePackageRelativeCssNamesVisitorTest method testRequireCssOnNamespace.

@Test
public void testRequireCssOnNamespace() {
    TemplateNode template = compileTemplate("{namespace boo requirecss=\"some.test.package,some.other.package\"}\n\n" + "/** Test template. */\n" + "{template .foo}\n" + "  <p class=\"{css('%AAA')}\">\n" + "{/template}\n");
    PrintNode printNode = Iterables.getOnlyElement(SoyTreeUtils.getAllNodesOfType(template, PrintNode.class));
    FunctionNode cssFn = (FunctionNode) printNode.getExpr().getRoot();
    assertThat(((StringNode) cssFn.getChild(0)).getValue()).isEqualTo("someTestPackageAAA");
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) FunctionNode(com.google.template.soy.exprtree.FunctionNode) StringNode(com.google.template.soy.exprtree.StringNode) PrintNode(com.google.template.soy.soytree.PrintNode) Test(org.junit.Test)

Aggregations

StringNode (com.google.template.soy.exprtree.StringNode)20 Test (org.junit.Test)9 ExprNode (com.google.template.soy.exprtree.ExprNode)7 FunctionNode (com.google.template.soy.exprtree.FunctionNode)7 IntegerNode (com.google.template.soy.exprtree.IntegerNode)6 PrintNode (com.google.template.soy.soytree.PrintNode)6 GlobalNode (com.google.template.soy.exprtree.GlobalNode)4 TemplateNode (com.google.template.soy.soytree.TemplateNode)4 BooleanNode (com.google.template.soy.exprtree.BooleanNode)2 ExprRootNode (com.google.template.soy.exprtree.ExprRootNode)2 FloatNode (com.google.template.soy.exprtree.FloatNode)2 VarRefNode (com.google.template.soy.exprtree.VarRefNode)2 CodeChunk (com.google.template.soy.jssrc.dsl.CodeChunk)2 StandaloneNode (com.google.template.soy.soytree.SoyNode.StandaloneNode)2 ImmutableList (com.google.common.collect.ImmutableList)1 SourceLocation (com.google.template.soy.base.SourceLocation)1 NullData (com.google.template.soy.data.restricted.NullData)1 PrimitiveNode (com.google.template.soy.exprtree.ExprNode.PrimitiveNode)1 FieldAccessNode (com.google.template.soy.exprtree.FieldAccessNode)1 ListLiteralNode (com.google.template.soy.exprtree.ListLiteralNode)1