use of com.google.template.soy.soytree.TemplateNode in project closure-templates by google.
the class ContextualAutoescaperTest method testStrictModeOptimizesDelegates.
@Test
public void testStrictModeOptimizesDelegates() {
String source = "{namespace ns}\n\n" + "{template .main}\n" + "{delcall ns.delegateHtml /}" + "{delcall ns.delegateText /}" + "\n{/template}\n\n" + "/** A delegate returning HTML. */\n" + "{deltemplate ns.delegateHtml}\n" + "Hello World" + "\n{/deltemplate}\n\n" + "/** A delegate returning JS. */\n" + "{deltemplate ns.delegateText kind=\"text\"}\n" + "Hello World" + "\n{/deltemplate}";
TemplateNode mainTemplate = rewrite(source).getChild(0);
assertWithMessage("Sanity check").that(mainTemplate.getTemplateName()).isEqualTo("ns.main");
final List<CallNode> callNodes = SoyTreeUtils.getAllNodesOfType(mainTemplate, CallNode.class);
assertThat(callNodes).hasSize(2);
assertWithMessage("We're compiling a complete set; we can optimize based on usages.").that(callNodes.get(0).getEscapingDirectives()).isEmpty();
assertWithMessage("HTML -> TEXT requires escaping").that(getDirectiveNames(callNodes.get(1).getEscapingDirectives())).containsExactly("|escapeHtml");
}
use of com.google.template.soy.soytree.TemplateNode in project closure-templates by google.
the class InferenceEngineTest method assertTransitions.
private static void assertTransitions(SanitizedContentKind kind, String src) {
TemplateNode template = SoyFileSetParserBuilder.forFileContents("{namespace ns}\n{template .foo" + (kind == SanitizedContentKind.HTML ? "" : " kind=\"" + Ascii.toLowerCase(kind.toString()) + '"') + " stricthtml=\"false\"}" + src + "{/template}").addSoyFunction(new AssertFunction()).desugarHtmlNodes(false).parse().fileSet().getChild(0).getChild(0);
Inferences inferences = new Inferences(new IncrementingIdGenerator(), ImmutableListMultimap.<String, TemplateNode>of());
InferenceEngine.inferTemplateEndContext(template, Context.getStartContextForContentKind(kind), inferences, ErrorReporter.exploding());
for (PrintNode print : SoyTreeUtils.getAllNodesOfType(template, PrintNode.class)) {
if (print.getExpr().getChild(0) instanceof FunctionNode) {
FunctionNode fn = (FunctionNode) print.getExpr().getChild(0);
if (fn.getSoyFunction() instanceof AssertFunction) {
assertWithMessage("expected print node at " + print.getSourceLocation() + " to have context").that(contextString(inferences.getContextForNode(print))).isEqualTo(((StringNode) fn.getChild(0)).getValue());
}
}
}
}
use of com.google.template.soy.soytree.TemplateNode in project closure-templates by google.
the class AddHtmlCommentsForDebugPassTest method runPass.
/**
* Parses the given input as a Soy file content, runs the AddHtmlCommentsForDebug pass and returns
* a map that contains pairs of template names and rewritten template body.
*/
private static ImmutableMap<String, String> runPass(String input, String fileName) {
String soyFile = "{namespace ns}" + input;
IncrementingIdGenerator nodeIdGen = new IncrementingIdGenerator();
ErrorReporter boom = ErrorReporter.exploding();
SoyFileSetNode soyTree = SoyFileSetParserBuilder.forSuppliers(SoyFileSupplier.Factory.create(soyFile, SoyFileKind.SRC, fileName)).errorReporter(boom).parse().fileSet();
TemplateRegistry registry = new TemplateRegistry(soyTree, boom);
// We need to run HtmlRewritePass to produce HTML nodes. Otherwise we will not add any comments.
new HtmlRewritePass(boom).run(soyTree.getChild(0), nodeIdGen);
new AddHtmlCommentsForDebugPass().run(soyTree, registry);
ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
for (TemplateNode node : soyTree.getChild(0).getChildren()) {
StringBuilder sb = new StringBuilder();
node.appendSourceStringForChildren(sb);
String templateName = (node instanceof TemplateDelegateNode) ? ((TemplateDelegateNode) node).getDelTemplateName() : node.getTemplateName();
builder.put(templateName, sb.toString());
}
return builder.build();
}
use of com.google.template.soy.soytree.TemplateNode in project closure-templates by google.
the class InsertMsgsVisitorTest method testFallbackMsgsUsingSoySource.
@Test
public void testFallbackMsgsUsingSoySource() {
SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(FALLBACK_TEST_FILE_CONTENT).parse().fileSet();
TemplateNode template = (TemplateNode) SharedTestUtils.getNode(soyTree);
// Before.
assertThat(template.numChildren()).isEqualTo(4);
assertThat(((MsgFallbackGroupNode) template.getChild(0)).numChildren()).isEqualTo(2);
assertThat(((MsgFallbackGroupNode) template.getChild(1)).numChildren()).isEqualTo(2);
assertThat(((MsgFallbackGroupNode) template.getChild(2)).numChildren()).isEqualTo(2);
assertThat(((MsgFallbackGroupNode) template.getChild(3)).numChildren()).isEqualTo(2);
// Execute the visitor.
new InsertMsgsVisitor(null, /* msgBundle */
FAIL).insertMsgs(template);
// After.
assertThat(template.numChildren()).isEqualTo(4);
assertThat(((RawTextNode) template.getChild(0)).getRawText()).isEqualTo("noTrans1");
assertThat(((RawTextNode) template.getChild(1)).getRawText()).isEqualTo("trans1");
assertThat(((RawTextNode) template.getChild(2)).getRawText()).isEqualTo("noTrans1");
assertThat(((RawTextNode) template.getChild(3)).getRawText()).isEqualTo("trans1");
}
use of com.google.template.soy.soytree.TemplateNode in project closure-templates by google.
the class InsertMsgsVisitorTest method testBasicMsgsUsingSoySource.
@Test
public void testBasicMsgsUsingSoySource() {
SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(BASIC_TEST_FILE_CONTENT).parse().fileSet();
TemplateNode template = (TemplateNode) SharedTestUtils.getNode(soyTree);
// Before.
assertThat(template.numChildren()).isEqualTo(5);
MsgNode msg = ((MsgFallbackGroupNode) template.getChild(2)).getChild(0);
assertThat(msg.numChildren()).isEqualTo(5);
MsgHtmlTagNode msgHtmlTag2 = (MsgHtmlTagNode) ((MsgPlaceholderNode) msg.getChild(2)).getChild(0);
assertThat(msgHtmlTag2.numChildren()).isEqualTo(3);
MsgHtmlTagNode msgHtmlTag4 = (MsgHtmlTagNode) ((MsgPlaceholderNode) msg.getChild(4)).getChild(0);
assertThat(msgHtmlTag4.numChildren()).isEqualTo(1);
assertThat(((RawTextNode) msgHtmlTag4.getChild(0)).getRawText()).isEqualTo("</a>");
// Execute the visitor.
new InsertMsgsVisitor(null, /* msgBundle */
FAIL).insertMsgs(template);
// After.
assertThat(template.numChildren()).isEqualTo(12);
assertThat(((PrintNode) template.getChild(0)).getExpr().toSourceString()).isEqualTo("$boo");
assertThat(((RawTextNode) template.getChild(1)).getRawText()).isEqualTo("scary ");
assertThat(((RawTextNode) template.getChild(2)).getRawText()).isEqualTo("random");
assertThat(((PrintNode) template.getChild(3)).getExpr().toSourceString()).isEqualTo("$foo");
assertThat(((RawTextNode) template.getChild(4)).getRawText()).isEqualTo("<a href=\"");
assertThat(((PrintNode) template.getChild(5)).getExpr().toSourceString()).isEqualTo("$goo");
assertThat(((RawTextNode) template.getChild(6)).getRawText()).isEqualTo("\">");
assertThat(((RawTextNode) template.getChild(7)).getRawText()).isEqualTo("slimy");
assertThat(((RawTextNode) template.getChild(8)).getRawText()).isEqualTo("</a>");
assertThat(((RawTextNode) template.getChild(9)).getRawText()).isEqualTo(" ");
assertThat(((RawTextNode) template.getChild(10)).getRawText()).isEqualTo("dairy");
assertThat(((PrintNode) template.getChild(11)).getExpr().toSourceString()).isEqualTo("$moo");
}
Aggregations