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