use of com.google.template.soy.coredirectives.EscapeHtmlDirective in project closure-templates by google.
the class BasicEscapeDirectiveTest method testApplyEscapeHtml.
@Test
public final void testApplyEscapeHtml() {
EscapeHtmlDirective escapeHtml = new EscapeHtmlDirective();
assertTofuOutput("", "", escapeHtml);
assertTofuOutput("1 < 2 &amp;&amp; 3 < 4", "1 < 2 && 3 < 4", escapeHtml);
assertTofuOutput("42", 42, escapeHtml);
new JsSrcPrintDirectiveTestBuilder().addTest("", " '' ", escapeHtml).addTest("1 < 2 &amp;&amp; 3 < 4", " '1 < 2 && 3 < 4' ", escapeHtml).addTest("42", " 42 ", escapeHtml).runTests();
}
use of com.google.template.soy.coredirectives.EscapeHtmlDirective in project closure-templates by google.
the class PerformDeprecatedNonContextualAutoescapeVisitor method visitPrintNode.
@Override
protected void visitPrintNode(PrintNode node) {
if (autoescapeMode != AutoescapeMode.NONCONTEXTUAL) {
// that this pass is never used without first running the contextual autoescaper.
if (node.getChildren().isEmpty()) {
throw new IllegalStateException(String.format("Internal error: A contextual or strict template has a print node that was never " + "assigned any escape directives: %s at %s", node.toSourceString(), node.getSourceLocation()));
}
return;
}
// Traverse the list to (a) record whether we saw any directive that cancels autoescape
// (including 'noAutoescape' of course) and (b) remove 'noAutoescape' directives.
boolean shouldCancelAutoescape = false;
for (PrintDirectiveNode directiveNode : ImmutableList.copyOf(node.getChildren())) {
SoyPrintDirective directive = directiveNode.getPrintDirective();
if (directive != null && directive.shouldCancelAutoescape()) {
shouldCancelAutoescape = true;
break;
}
}
// ideally should migrate off of deprecated-noncontextual autoescape.
if (autoescapeMode == AutoescapeMode.NONCONTEXTUAL && !shouldCancelAutoescape) {
PrintDirectiveNode newEscapeHtmlDirectiveNode = new PrintDirectiveNode(nodeIdGen.genId(), node.getSourceLocation(), ImmutableList.<ExprNode>of(), new EscapeHtmlDirective(), /* isSynthetic= */
true);
node.addChild(0, newEscapeHtmlDirectiveNode);
}
}
use of com.google.template.soy.coredirectives.EscapeHtmlDirective in project closure-templates by google.
the class BytecodeCompilerTest method testDelCallEscaping_separateCompilation.
// Tests for a bug where we would overescape deltemplates at the call site when the strict
// content kind of the deltemplate was unknown at compile time.
@Test
public void testDelCallEscaping_separateCompilation() throws IOException {
String soyFileContent1 = Joiner.on("\n").join("{namespace ns}", "", "{template .callerTemplate}", " {delcall myApp.myDelegate/}", "{/template}", "");
SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(soyFileContent1).parse().fileSet();
// apply an escaping directive to the callsite, just like the autoescaper would
CallDelegateNode cdn = SoyTreeUtils.getAllNodesOfType(soyTree.getChild(0), CallDelegateNode.class).get(0);
cdn.setEscapingDirectives(ImmutableList.of(new EscapeHtmlDirective()));
TemplateRegistry templateRegistry = new TemplateRegistry(soyTree, ErrorReporter.exploding());
CompiledTemplates templates = BytecodeCompiler.compile(templateRegistry, false, ErrorReporter.exploding()).get();
CompiledTemplate.Factory caller = templates.getTemplateFactory("ns.callerTemplate");
try {
renderWithContext(caller, getDefaultContext(templates));
fail();
} catch (IllegalArgumentException iae) {
assertThat(iae).hasMessageThat().isEqualTo("Found no active impl for delegate call to \"myApp.myDelegate\" (and delcall does " + "not set allowemptydefault=\"true\").");
}
String soyFileContent2 = Joiner.on("\n").join("{namespace ns2}", "", "{deltemplate myApp.myDelegate}", " <span>Hello</span>", "{/deltemplate}", "");
CompiledTemplates templatesWithDeltemplate = compileFiles(soyFileContent2);
// By passing an alternate context, we ensure the deltemplate selector contains the delegate
assertThat(renderWithContext(caller, getDefaultContext(templatesWithDeltemplate))).isEqualTo("<span>Hello</span>");
}
Aggregations