use of com.google.template.soy.exprtree.VarRefNode in project closure-templates by google.
the class ContentSecurityPolicyNonceInjectionPass method run.
@Override
public void run(SoyFileNode file, IdGenerator nodeIdGen) {
// * $ij references
for (TemplateNode template : file.getChildren()) {
for (TemplateParam param : template.getAllParams()) {
if (param.isInjected() && param.name().equals(CSP_NONCE_VARIABLE_NAME)) {
errorReporter.report(param.nameLocation(), IJ_CSP_NONCE_REFERENCE);
}
}
}
for (VarRefNode var : SoyTreeUtils.getAllNodesOfType(file, VarRefNode.class)) {
// of isInjected
if (var.isDollarSignIjParameter() && var.getName().equals(CSP_NONCE_VARIABLE_NAME)) {
errorReporter.report(var.getSourceLocation(), IJ_CSP_NONCE_REFERENCE);
}
}
for (HtmlOpenTagNode openTag : SoyTreeUtils.getAllNodesOfType(file, HtmlOpenTagNode.class)) {
if (isTagNonceable(openTag)) {
// this should point to the character immediately before the '>' or '/>' at the end of the
// open tag
SourceLocation insertionLocation = openTag.getSourceLocation().getEndPoint().offset(0, openTag.isSelfClosing() ? -2 : -1).asLocation(openTag.getSourceLocation().getFilePath());
openTag.addChild(createCspInjection(insertionLocation, nodeIdGen));
}
}
}
use of com.google.template.soy.exprtree.VarRefNode in project closure-templates by google.
the class ParseExpressionTest method testParseDataReference.
@Test
public void testParseDataReference() throws Exception {
SourceLocation loc = SourceLocation.UNKNOWN;
ExprNode dataRef = assertThatExpression("$boo").isValidExpression();
assertNodeEquals(new VarRefNode("boo", loc, false, null), dataRef);
dataRef = assertThatExpression("$boo.foo").isValidExpression();
assertNodeEquals(new FieldAccessNode(new VarRefNode("boo", loc, false, null), "foo", loc, false), dataRef);
dataRef = assertThatExpression("$boo[0][$foo]").isValidExpression();
assertNodeEquals(new ItemAccessNode(new ItemAccessNode(new VarRefNode("boo", loc, false, null), new IntegerNode(0, loc), loc, false), new VarRefNode("foo", loc, false, null), loc, false), dataRef);
dataRef = assertThatExpression("$boo?[0]?[$foo]").isValidExpression();
assertNodeEquals(new ItemAccessNode(new ItemAccessNode(new VarRefNode("boo", loc, false, null), new IntegerNode(0, loc), loc, true), new VarRefNode("foo", loc, false, null), loc, true), dataRef);
dataRef = assertThatExpression("$ij.boo?[0][$ij.foo]").isValidExpression();
assertNodeEquals(new ItemAccessNode(new ItemAccessNode(new VarRefNode("boo", loc, true, null), new IntegerNode(0, loc), loc, true), new VarRefNode("foo", loc, true, null), loc, false), dataRef);
}
use of com.google.template.soy.exprtree.VarRefNode in project closure-templates by google.
the class TemplateParserTest method testParseForeachStmt.
@Test
public void testParseForeachStmt() throws Exception {
String templateBody = "{@param goose : ?}{@param foo: ?}\n" + " {for $goo in $goose}\n" + " {$goose.numKids} goslings.{\\n}\n" + " {/for}\n" + " {for $boo in $foo.booze}\n" + " Scary drink {$boo.name}!\n" + " {if not isLast($boo)}{\\n}{/if}\n" + " {ifempty}\n" + " Sorry, no booze.\n" + " {/for}\n";
List<StandaloneNode> nodes = parseTemplateContent(templateBody, FAIL).getChildren();
assertEquals(2, nodes.size());
ForNode fn0 = (ForNode) nodes.get(0);
assertEquals("$goose", fn0.getExpr().toSourceString());
assertTrue(fn0.getExpr().getRoot() instanceof VarRefNode);
assertEquals(1, fn0.numChildren());
ForNonemptyNode fn0fnn0 = (ForNonemptyNode) fn0.getChild(0);
assertEquals("goo", fn0fnn0.getVarName());
assertEquals(2, fn0fnn0.numChildren());
assertEquals("$goose.numKids", ((PrintNode) fn0fnn0.getChild(0)).getExpr().toSourceString());
assertEquals(" goslings.\n", ((RawTextNode) fn0fnn0.getChild(1)).getRawText());
ForNode fn1 = (ForNode) nodes.get(1);
assertEquals("$foo.booze", fn1.getExpr().toSourceString());
assertTrue(fn1.getExpr().getRoot() instanceof FieldAccessNode);
assertEquals(2, fn1.numChildren());
ForNonemptyNode fn1fnn0 = (ForNonemptyNode) fn1.getChild(0);
assertEquals("boo", fn1fnn0.getVarName());
assertEquals("$foo.booze", fn1fnn0.getExpr().toSourceString());
assertEquals("boo", fn1fnn0.getVarName());
assertEquals(4, fn1fnn0.numChildren());
IfNode fn1fnn0in = (IfNode) fn1fnn0.getChild(3);
assertEquals(1, fn1fnn0in.numChildren());
assertEquals("not isLast($boo)", ((IfCondNode) fn1fnn0in.getChild(0)).getExpr().toSourceString());
ForIfemptyNode fn1fin1 = (ForIfemptyNode) fn1.getChild(1);
assertEquals(1, fn1fin1.numChildren());
assertEquals("Sorry, no booze.", ((RawTextNode) fn1fin1.getChild(0)).getRawText());
}
use of com.google.template.soy.exprtree.VarRefNode in project closure-templates by google.
the class SoyTreeUtilsTest method testClone.
@Test
public final void testClone() throws Exception {
SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(SOY_SOURCE_FOR_TESTING_CLONING).declaredSyntaxVersion(SyntaxVersion.V2_4).parse().fileSet();
SoyFileSetNode clone = soyTree.copy(new CopyState());
assertEquals(1, clone.numChildren());
assertEquals(clone.getChild(0).toSourceString(), soyTree.getChild(0).toSourceString());
// All the localvarnodes, there is one of each type
ForNonemptyNode forNonemptyNode = Iterables.getOnlyElement(SoyTreeUtils.getAllNodesOfType(clone, ForNonemptyNode.class));
LetValueNode letValueNode = Iterables.getOnlyElement(SoyTreeUtils.getAllNodesOfType(clone, LetValueNode.class));
for (VarRefNode varRef : SoyTreeUtils.getAllNodesOfType(clone, VarRefNode.class)) {
VarDefn defn = varRef.getDefnDecl();
LocalVar local;
switch(varRef.getName()) {
case "local":
local = (LocalVar) defn;
assertSame(letValueNode, local.declaringNode());
assertSame(letValueNode.getVar(), local);
break;
case "item":
local = (LocalVar) defn;
assertSame(forNonemptyNode, local.declaringNode());
assertSame(forNonemptyNode.getVar(), defn);
break;
default:
}
}
}
use of com.google.template.soy.exprtree.VarRefNode in project closure-templates by google.
the class TemplateParserTest method testParseSwitchStmt.
@Test
public void testParseSwitchStmt() throws Exception {
String templateBody = "{@param boo: ?}{@param foo : ?}{@param moo : ?}\n" + " {switch $boo} {case 0}Blah\n" + " {case $foo.goo}\n" + " Bleh\n" + " {case -1, 1, $moo}\n" + " Bluh\n" + " {default}\n" + " Bloh\n" + " {/switch}\n";
List<StandaloneNode> nodes = parseTemplateContent(templateBody, FAIL).getChildren();
assertEquals(1, nodes.size());
SwitchNode sn = (SwitchNode) nodes.get(0);
assertEquals("$boo", sn.getExpr().toSourceString());
assertTrue(sn.getExpr().getRoot() instanceof VarRefNode);
assertEquals(4, sn.numChildren());
SwitchCaseNode scn0 = (SwitchCaseNode) sn.getChild(0);
assertEquals(1, scn0.getExprList().size());
assertTrue(scn0.getExprList().get(0).getRoot() instanceof IntegerNode);
assertEquals(0, ((IntegerNode) scn0.getExprList().get(0).getRoot()).getValue());
SwitchCaseNode scn1 = (SwitchCaseNode) sn.getChild(1);
assertEquals(1, scn1.getExprList().size());
assertTrue(scn1.getExprList().get(0).getRoot() instanceof FieldAccessNode);
assertEquals("$foo.goo", scn1.getExprList().get(0).getRoot().toSourceString());
SwitchCaseNode scn2 = (SwitchCaseNode) sn.getChild(2);
assertEquals(3, scn2.getExprList().size());
assertTrue(scn2.getExprList().get(0).getRoot() instanceof IntegerNode);
assertTrue(scn2.getExprList().get(1).getRoot() instanceof IntegerNode);
assertTrue(scn2.getExprList().get(2).getRoot() instanceof VarRefNode);
assertEquals("-1", scn2.getExprList().get(0).getRoot().toSourceString());
assertEquals("1", scn2.getExprList().get(1).getRoot().toSourceString());
assertEquals("$moo", scn2.getExprList().get(2).getRoot().toSourceString());
assertEquals("Bluh", ((RawTextNode) scn2.getChild(0)).getRawText());
assertEquals("Bloh", ((RawTextNode) ((SwitchDefaultNode) sn.getChild(3)).getChild(0)).getRawText());
assertEquals("{switch $boo}{case 0}Blah{case $foo.goo}Bleh{case -1, 1, $moo}Bluh{default}Bloh{/switch}", sn.toSourceString());
}
Aggregations