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