use of com.google.template.soy.soytree.PrintNode in project closure-templates by google.
the class ResolvePackageRelativeCssNamesVisitorTest method testUnprefixedNode.
@Test
public void testUnprefixedNode() {
TemplateNode template = compileTemplate("{namespace boo cssbase=\"some.test.package\"}\n\n" + "/** Test template. */\n" + "{template .foo}\n" + " <p class=\"{css('AAA')}\">\n" + "{/template}\n");
PrintNode printNode = Iterables.getOnlyElement(SoyTreeUtils.getAllNodesOfType(template, PrintNode.class));
FunctionNode cssFn = (FunctionNode) printNode.getExpr().getRoot();
assertThat(((StringNode) cssFn.getChild(0)).getValue()).isEqualTo("AAA");
}
use of com.google.template.soy.soytree.PrintNode 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.PrintNode 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.PrintNode 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);
}
use of com.google.template.soy.soytree.PrintNode in project closure-templates by google.
the class PreevalVisitorTest method preeval.
// -----------------------------------------------------------------------------------------------
// Helpers.
/**
* Evaluates the given expression and returns the result.
*
* @param expression The expression to preevaluate.
* @return The expression result.
*/
private static SoyValue preeval(String expression, String... params) {
String header = "";
for (String param : params) {
header += "{@param " + param + " : ?}\n";
}
PrintNode code = (PrintNode) SoyFileSetParserBuilder.forTemplateContents(header + "{" + expression + "}").parse().fileSet().getChild(0).getChild(0).getChild(0);
ExprRootNode expr = code.getExpr();
Environment env = TestingEnvironment.createForTest(SoyValueConverterUtility.newDict("boo", 8), ImmutableMap.<String, SoyValueProvider>of());
return new PreevalVisitor(env).exec(expr);
}
Aggregations