use of com.google.template.soy.soytree.SoyFileSetNode 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);
}
use of com.google.template.soy.soytree.SoyFileSetNode in project closure-templates by google.
the class ResolveNamesVisitorTest method testLetNameLookupSuccess.
@Test
public void testLetNameLookupSuccess() {
SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(constructTemplateSource("{let $pa: 1 /}", "{$pa}")).parse().fileSet();
new ResolveNamesVisitor(ErrorReporter.exploding()).exec(soyTree);
TemplateNode n = soyTree.getChild(0).getChild(0);
assertThat(n.getMaxLocalVariableTableSize()).isEqualTo(1);
assertThat(((LetValueNode) n.getChild(0)).getVar().localVariableIndex()).isEqualTo(0);
}
use of com.google.template.soy.soytree.SoyFileSetNode in project closure-templates by google.
the class ContextualAutoescaperTest method rewrite.
public SoyFileNode rewrite(String... inputs) {
ErrorReporter reporter = ErrorReporter.createForTest();
SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(inputs).errorReporter(reporter).allowUnboundGlobals(true).addPrintDirectives(SOY_PRINT_DIRECTIVES).runAutoescaper(true).parse().fileSet();
if (!reporter.getErrors().isEmpty()) {
SoyError soyError = reporter.getErrors().get(0);
String message = soyError.message();
if (message.startsWith(ContextualAutoescaper.AUTOESCAPE_ERROR_PREFIX)) {
// Grab the part after the prefix (and the "- " used for indentation).
message = message.substring(ContextualAutoescaper.AUTOESCAPE_ERROR_PREFIX.length() + 2);
// reporter, we can stop throwing and simply add explicit checks in the cases.
throw new RewriteError(soyError, message);
} else {
throw new IllegalStateException("Unexpected error: " + message);
}
}
return soyTree.getChild(0);
}
use of com.google.template.soy.soytree.SoyFileSetNode in project closure-templates by google.
the class PerformDeprecatedNonContextualAutoescapeVisitorTest method testAutoescapeOnWithEscapeHtml.
@Test
public void testAutoescapeOnWithEscapeHtml() throws Exception {
String testPrintTags = "{'<br>' |escapeHtml}{'<br>' |noAutoescape |escapeHtml}{'<br>' |escapeHtml |noAutoescape}";
SoyFileSetNode soyTree = parseTestPrintTagsHelper(testPrintTags);
List<PrintNode> printNodes = SoyTreeUtils.getAllNodesOfType(soyTree, PrintNode.class);
// Before.
assertThat(printNodes.get(0).getChildren()).hasSize(1);
assertThat(printNodes.get(0).getChild(0).getName()).isEqualTo(EscapeHtmlDirective.NAME);
assertThat(printNodes.get(1).getChildren()).hasSize(2);
assertThat(printNodes.get(1).getChild(0).getName()).isEqualTo(NoAutoescapeDirective.NAME);
assertThat(printNodes.get(1).getChild(1).getName()).isEqualTo(EscapeHtmlDirective.NAME);
assertThat(printNodes.get(2).getChildren()).hasSize(2);
assertThat(printNodes.get(2).getChild(0).getName()).isEqualTo(EscapeHtmlDirective.NAME);
assertThat(printNodes.get(2).getChild(1).getName()).isEqualTo(NoAutoescapeDirective.NAME);
performAutoescape(soyTree);
// After. Note that noAutoescape remains to filter against ContentKind.TEXT.
assertThat(printNodes.get(0).getChildren()).hasSize(1);
assertThat(printNodes.get(0).getChild(0).getName()).isEqualTo(EscapeHtmlDirective.NAME);
assertThat(printNodes.get(1).getChildren()).hasSize(2);
assertThat(printNodes.get(1).getChild(0).getName()).isEqualTo(NoAutoescapeDirective.NAME);
assertThat(printNodes.get(1).getChild(1).getName()).isEqualTo(EscapeHtmlDirective.NAME);
assertThat(printNodes.get(2).getChildren()).hasSize(2);
assertThat(printNodes.get(2).getChild(0).getName()).isEqualTo(EscapeHtmlDirective.NAME);
assertThat(printNodes.get(2).getChild(1).getName()).isEqualTo(NoAutoescapeDirective.NAME);
}
use of com.google.template.soy.soytree.SoyFileSetNode in project closure-templates by google.
the class PerformDeprecatedNonContextualAutoescapeVisitorTest method testAutoescapeOnWithNoAutoescape.
@Test
public void testAutoescapeOnWithNoAutoescape() throws Exception {
String testPrintTags = "{'<br>' |noAutoescape}{'<br>' |noAutoescape |noAutoescape}";
SoyFileSetNode soyTree = parseTestPrintTagsHelper(testPrintTags);
List<PrintNode> printNodes = SoyTreeUtils.getAllNodesOfType(soyTree, PrintNode.class);
// Before.
assertThat(printNodes.get(0).getChildren()).hasSize(1);
assertThat(printNodes.get(0).getChild(0).getName()).isEqualTo(NoAutoescapeDirective.NAME);
assertThat(printNodes.get(1).getChildren()).hasSize(2);
assertThat(printNodes.get(1).getChild(0).getName()).isEqualTo(NoAutoescapeDirective.NAME);
assertThat(printNodes.get(1).getChild(1).getName()).isEqualTo(NoAutoescapeDirective.NAME);
performAutoescape(soyTree);
// After. Note that noAutoescape remains to filter against ContentKind.TEXT.
assertThat(printNodes.get(0).getChildren()).hasSize(1);
assertThat(printNodes.get(0).getChild(0).getName()).isEqualTo(NoAutoescapeDirective.NAME);
assertThat(printNodes.get(1).getChildren()).hasSize(2);
assertThat(printNodes.get(1).getChild(0).getName()).isEqualTo(NoAutoescapeDirective.NAME);
assertThat(printNodes.get(1).getChild(1).getName()).isEqualTo(NoAutoescapeDirective.NAME);
}
Aggregations