Search in sources :

Example 1 with EscapeHtmlDirective

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 &lt; 2 &amp;amp;&amp;amp; 3 &lt; 4", "1 < 2 &amp;&amp; 3 < 4", escapeHtml);
    assertTofuOutput("42", 42, escapeHtml);
    new JsSrcPrintDirectiveTestBuilder().addTest("", " '' ", escapeHtml).addTest("1 &lt; 2 &amp;amp;&amp;amp; 3 &lt; 4", " '1 < 2 &amp;&amp; 3 < 4' ", escapeHtml).addTest("42", " 42 ", escapeHtml).runTests();
}
Also used : EscapeHtmlDirective(com.google.template.soy.coredirectives.EscapeHtmlDirective) Test(org.junit.Test)

Example 2 with EscapeHtmlDirective

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);
    }
}
Also used : EscapeHtmlDirective(com.google.template.soy.coredirectives.EscapeHtmlDirective) PrintDirectiveNode(com.google.template.soy.soytree.PrintDirectiveNode) SoyPrintDirective(com.google.template.soy.shared.restricted.SoyPrintDirective)

Example 3 with EscapeHtmlDirective

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>");
}
Also used : TemplateRegistry(com.google.template.soy.soytree.TemplateRegistry) EscapeHtmlDirective(com.google.template.soy.coredirectives.EscapeHtmlDirective) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) CompiledTemplates(com.google.template.soy.jbcsrc.shared.CompiledTemplates) CallDelegateNode(com.google.template.soy.soytree.CallDelegateNode) CompiledTemplate(com.google.template.soy.jbcsrc.shared.CompiledTemplate) Test(org.junit.Test)

Aggregations

EscapeHtmlDirective (com.google.template.soy.coredirectives.EscapeHtmlDirective)3 Test (org.junit.Test)2 CompiledTemplate (com.google.template.soy.jbcsrc.shared.CompiledTemplate)1 CompiledTemplates (com.google.template.soy.jbcsrc.shared.CompiledTemplates)1 SoyPrintDirective (com.google.template.soy.shared.restricted.SoyPrintDirective)1 CallDelegateNode (com.google.template.soy.soytree.CallDelegateNode)1 PrintDirectiveNode (com.google.template.soy.soytree.PrintDirectiveNode)1 SoyFileSetNode (com.google.template.soy.soytree.SoyFileSetNode)1 TemplateRegistry (com.google.template.soy.soytree.TemplateRegistry)1