Search in sources :

Example 1 with IfNode

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

the class ContentSecurityPolicyNonceInjectionPass method createCspInjection.

/**
 * Generates an AST fragment that looks like: {if $ij.csp_nonce}nonce="{$ij.csp_nonce}"{/if}
 *
 * @param insertionLocation The location where it is being inserted
 * @param nodeIdGen The id generator to use
 */
private static IfNode createCspInjection(SourceLocation insertionLocation, IdGenerator nodeIdGen) {
    IfNode ifNode = new IfNode(nodeIdGen.genId(), insertionLocation);
    IfCondNode ifCondNode = new IfCondNode(nodeIdGen.genId(), insertionLocation, "if", referenceCspNonce(insertionLocation));
    ifNode.addChild(ifCondNode);
    HtmlAttributeNode nonceAttribute = new HtmlAttributeNode(nodeIdGen.genId(), insertionLocation, insertionLocation.getBeginPoint());
    ifCondNode.addChild(nonceAttribute);
    nonceAttribute.addChild(new RawTextNode(nodeIdGen.genId(), "nonce", insertionLocation));
    HtmlAttributeValueNode attributeValue = new HtmlAttributeValueNode(nodeIdGen.genId(), insertionLocation, HtmlAttributeValueNode.Quotes.DOUBLE);
    nonceAttribute.addChild(attributeValue);
    // NOTE: we do not need to insert any print directives here, unlike the old implementation since
    // we are running before the autoescaper, so the escaper should insert whatever print directives
    // are appropriate.
    PrintNode printNode = new PrintNode(nodeIdGen.genId(), insertionLocation, // Implicit.  {$ij.csp_nonce} not {print $ij.csp_nonce}
    true, /* isImplicit= */
    referenceCspNonce(insertionLocation), /* attributes= */
    ImmutableList.of(), ErrorReporter.exploding());
    attributeValue.addChild(printNode);
    return ifNode;
}
Also used : IfCondNode(com.google.template.soy.soytree.IfCondNode) HtmlAttributeNode(com.google.template.soy.soytree.HtmlAttributeNode) IfNode(com.google.template.soy.soytree.IfNode) PrintNode(com.google.template.soy.soytree.PrintNode) RawTextNode(com.google.template.soy.soytree.RawTextNode) HtmlAttributeValueNode(com.google.template.soy.soytree.HtmlAttributeValueNode)

Example 2 with IfNode

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

the class TemplateParserTest method testParseIfStmt.

@Test
public void testParseIfStmt() throws Exception {
    String templateBody = "{@param zoo : ?}{@param boo: ?}{@param foo : ?}{@param moo : ?}\n" + "  {if $zoo}{$zoo}{/if}\n" + "  {if $boo}\n" + "    Blah\n" + "  {elseif $foo.goo > 2}\n" + "    {$moo}\n" + "  {else}\n" + "    Blah {$moo}\n" + "  {/if}\n";
    List<StandaloneNode> nodes = parseTemplateContent(templateBody, FAIL).getChildren();
    assertEquals(2, nodes.size());
    IfNode in0 = (IfNode) nodes.get(0);
    assertEquals(1, in0.numChildren());
    IfCondNode in0icn0 = (IfCondNode) in0.getChild(0);
    assertEquals("$zoo", in0icn0.getExpr().toSourceString());
    assertEquals(1, in0icn0.numChildren());
    assertEquals("$zoo", ((PrintNode) in0icn0.getChild(0)).getExpr().toSourceString());
    assertTrue(in0icn0.getExpr().getRoot() instanceof VarRefNode);
    IfNode in1 = (IfNode) nodes.get(1);
    assertEquals(3, in1.numChildren());
    IfCondNode in1icn0 = (IfCondNode) in1.getChild(0);
    assertEquals("$boo", in1icn0.getExpr().toSourceString());
    assertTrue(in1icn0.getExpr().getRoot() instanceof VarRefNode);
    IfCondNode in1icn1 = (IfCondNode) in1.getChild(1);
    assertEquals("$foo.goo > 2", in1icn1.getExpr().toSourceString());
    assertTrue(in1icn1.getExpr().getRoot() instanceof GreaterThanOpNode);
    assertEquals("{if $boo}Blah{elseif $foo.goo > 2}{$moo}{else}Blah {$moo}{/if}", in1.toSourceString());
}
Also used : StandaloneNode(com.google.template.soy.soytree.SoyNode.StandaloneNode) IfCondNode(com.google.template.soy.soytree.IfCondNode) GreaterThanOpNode(com.google.template.soy.exprtree.OperatorNodes.GreaterThanOpNode) VarRefNode(com.google.template.soy.exprtree.VarRefNode) IfNode(com.google.template.soy.soytree.IfNode) PrintNode(com.google.template.soy.soytree.PrintNode) Test(org.junit.Test)

Example 3 with IfNode

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

the class AddHtmlCommentsForDebugPass method createSoyDebug.

/**
 * Generates an AST fragment that looks like:
 *
 * <p>{@code {if debugSoyTemplateInfo()}<!--dta_of...-->{/if}}
 *
 * @param insertionLocation The location where it is being inserted
 * @param nodeIdGen The id generator to use
 * @param htmlComment The content of the HTML comment
 */
private IfNode createSoyDebug(SourceLocation insertionLocation, IdGenerator nodeIdGen, String htmlComment) {
    IfNode ifNode = new IfNode(nodeIdGen.genId(), insertionLocation);
    FunctionNode funcNode = new FunctionNode(DebugSoyTemplateInfoFunction.INSTANCE, insertionLocation);
    funcNode.setType(BoolType.getInstance());
    IfCondNode ifCondNode = new IfCondNode(nodeIdGen.genId(), insertionLocation, "if", funcNode);
    HtmlCommentNode htmlCommentNode = new HtmlCommentNode(nodeIdGen.genId(), insertionLocation);
    // We need to escape the input HTML comments, in cases the file location contains "-->".
    htmlCommentNode.addChild(new RawTextNode(nodeIdGen.genId(), htmlEscaper().escape(htmlComment), insertionLocation));
    ifCondNode.addChild(htmlCommentNode);
    ifNode.addChild(ifCondNode);
    return ifNode;
}
Also used : IfCondNode(com.google.template.soy.soytree.IfCondNode) FunctionNode(com.google.template.soy.exprtree.FunctionNode) IfNode(com.google.template.soy.soytree.IfNode) RawTextNode(com.google.template.soy.soytree.RawTextNode) HtmlCommentNode(com.google.template.soy.soytree.HtmlCommentNode)

Example 4 with IfNode

use of com.google.template.soy.soytree.IfNode 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 5 with IfNode

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

the class ResolveNamesVisitorTest method testLetContentSlotLifetime.

@Test
public void testLetContentSlotLifetime() {
    SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(constructTemplateSource("{let $a kind=\"text\"}", // introduce an extra scope
    "  {if true}", "    {let $b: 2 /}", "    {$b}", "  {/if}", "{/let}", "{$a}")).parse().fileSet();
    new ResolveNamesVisitor(ErrorReporter.exploding()).exec(soyTree);
    TemplateNode n = soyTree.getChild(0).getChild(0);
    // 1 because each new $la binding overwrites the prior one
    assertThat(n.getMaxLocalVariableTableSize()).isEqualTo(2);
    LetContentNode aLetNode = (LetContentNode) n.getChild(0);
    assertThat(aLetNode.getVar().localVariableIndex()).isEqualTo(1);
    LetValueNode bLetNode = (LetValueNode) ((IfCondNode) ((IfNode) aLetNode.getChild(0)).getChild(0)).getChild(0);
    assertThat(bLetNode.getVar().localVariableIndex()).isEqualTo(0);
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) LetValueNode(com.google.template.soy.soytree.LetValueNode) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) IfNode(com.google.template.soy.soytree.IfNode) LetContentNode(com.google.template.soy.soytree.LetContentNode) Test(org.junit.Test)

Aggregations

IfNode (com.google.template.soy.soytree.IfNode)5 IfCondNode (com.google.template.soy.soytree.IfCondNode)4 PrintNode (com.google.template.soy.soytree.PrintNode)3 Test (org.junit.Test)3 VarRefNode (com.google.template.soy.exprtree.VarRefNode)2 RawTextNode (com.google.template.soy.soytree.RawTextNode)2 StandaloneNode (com.google.template.soy.soytree.SoyNode.StandaloneNode)2 FieldAccessNode (com.google.template.soy.exprtree.FieldAccessNode)1 FunctionNode (com.google.template.soy.exprtree.FunctionNode)1 GreaterThanOpNode (com.google.template.soy.exprtree.OperatorNodes.GreaterThanOpNode)1 ForIfemptyNode (com.google.template.soy.soytree.ForIfemptyNode)1 ForNode (com.google.template.soy.soytree.ForNode)1 ForNonemptyNode (com.google.template.soy.soytree.ForNonemptyNode)1 HtmlAttributeNode (com.google.template.soy.soytree.HtmlAttributeNode)1 HtmlAttributeValueNode (com.google.template.soy.soytree.HtmlAttributeValueNode)1 HtmlCommentNode (com.google.template.soy.soytree.HtmlCommentNode)1 LetContentNode (com.google.template.soy.soytree.LetContentNode)1 LetValueNode (com.google.template.soy.soytree.LetValueNode)1 SoyFileSetNode (com.google.template.soy.soytree.SoyFileSetNode)1 TemplateNode (com.google.template.soy.soytree.TemplateNode)1