Search in sources :

Example 16 with VarRefNode

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));
        }
    }
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) SourceLocation(com.google.template.soy.base.SourceLocation) VarRefNode(com.google.template.soy.exprtree.VarRefNode) TemplateParam(com.google.template.soy.soytree.defn.TemplateParam) HtmlOpenTagNode(com.google.template.soy.soytree.HtmlOpenTagNode)

Example 17 with VarRefNode

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);
}
Also used : SourceLocation(com.google.template.soy.base.SourceLocation) ExprNode(com.google.template.soy.exprtree.ExprNode) VarRefNode(com.google.template.soy.exprtree.VarRefNode) IntegerNode(com.google.template.soy.exprtree.IntegerNode) ItemAccessNode(com.google.template.soy.exprtree.ItemAccessNode) FieldAccessNode(com.google.template.soy.exprtree.FieldAccessNode) Test(org.junit.Test)

Example 18 with VarRefNode

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());
}
Also used : StandaloneNode(com.google.template.soy.soytree.SoyNode.StandaloneNode) ForNode(com.google.template.soy.soytree.ForNode) IfCondNode(com.google.template.soy.soytree.IfCondNode) VarRefNode(com.google.template.soy.exprtree.VarRefNode) IfNode(com.google.template.soy.soytree.IfNode) PrintNode(com.google.template.soy.soytree.PrintNode) FieldAccessNode(com.google.template.soy.exprtree.FieldAccessNode) ForIfemptyNode(com.google.template.soy.soytree.ForIfemptyNode) ForNonemptyNode(com.google.template.soy.soytree.ForNonemptyNode) Test(org.junit.Test)

Example 19 with VarRefNode

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:
        }
    }
}
Also used : VarRefNode(com.google.template.soy.exprtree.VarRefNode) VarDefn(com.google.template.soy.exprtree.VarDefn) LocalVar(com.google.template.soy.soytree.defn.LocalVar) CopyState(com.google.template.soy.basetree.CopyState) Test(org.junit.Test)

Example 20 with VarRefNode

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());
}
Also used : StandaloneNode(com.google.template.soy.soytree.SoyNode.StandaloneNode) VarRefNode(com.google.template.soy.exprtree.VarRefNode) IntegerNode(com.google.template.soy.exprtree.IntegerNode) SwitchCaseNode(com.google.template.soy.soytree.SwitchCaseNode) SwitchNode(com.google.template.soy.soytree.SwitchNode) FieldAccessNode(com.google.template.soy.exprtree.FieldAccessNode) SwitchDefaultNode(com.google.template.soy.soytree.SwitchDefaultNode) Test(org.junit.Test)

Aggregations

VarRefNode (com.google.template.soy.exprtree.VarRefNode)22 Test (org.junit.Test)9 SourceLocation (com.google.template.soy.base.SourceLocation)5 ExprNode (com.google.template.soy.exprtree.ExprNode)4 FieldAccessNode (com.google.template.soy.exprtree.FieldAccessNode)4 IntegerNode (com.google.template.soy.exprtree.IntegerNode)4 StandaloneNode (com.google.template.soy.soytree.SoyNode.StandaloneNode)4 SoyDataException (com.google.template.soy.data.SoyDataException)3 PrintNode (com.google.template.soy.soytree.PrintNode)3 TemplateNode (com.google.template.soy.soytree.TemplateNode)3 LoopVar (com.google.template.soy.soytree.defn.LoopVar)3 IOException (java.io.IOException)3 HashSet (java.util.HashSet)3 ParentExprNode (com.google.template.soy.exprtree.ExprNode.ParentExprNode)2 FunctionNode (com.google.template.soy.exprtree.FunctionNode)2 GlobalNode (com.google.template.soy.exprtree.GlobalNode)2 ItemAccessNode (com.google.template.soy.exprtree.ItemAccessNode)2 StringNode (com.google.template.soy.exprtree.StringNode)2 IfCondNode (com.google.template.soy.soytree.IfCondNode)2 IfNode (com.google.template.soy.soytree.IfNode)2