Search in sources :

Example 6 with PrintDirectiveNode

use of com.google.template.soy.soytree.PrintDirectiveNode 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 7 with PrintDirectiveNode

use of com.google.template.soy.soytree.PrintDirectiveNode in project closure-templates by google.

the class RenderVisitor method visitPrintNode.

@Override
protected void visitPrintNode(PrintNode node) {
    SoyValue result = eval(node.getExpr(), node);
    if (result instanceof UndefinedData) {
        throw RenderException.createWithSource("In 'print' tag, expression \"" + node.getExpr().toSourceString() + "\" evaluates to undefined.", node);
    }
    // Process directives.
    for (PrintDirectiveNode directiveNode : node.getChildren()) {
        // Evaluate directive args.
        List<ExprRootNode> argsExprs = directiveNode.getArgs();
        List<SoyValue> argsSoyDatas = Lists.newArrayListWithCapacity(argsExprs.size());
        for (ExprRootNode argExpr : argsExprs) {
            argsSoyDatas.add(eval(argExpr, directiveNode));
        }
        // Apply directive.
        result = applyDirective(directiveNode.getPrintDirective(), result, argsSoyDatas, node);
    }
    append(currOutputBuf, result, node);
}
Also used : PrintDirectiveNode(com.google.template.soy.soytree.PrintDirectiveNode) UndefinedData(com.google.template.soy.data.restricted.UndefinedData) SoyValue(com.google.template.soy.data.SoyValue) ExprRootNode(com.google.template.soy.exprtree.ExprRootNode)

Aggregations

PrintDirectiveNode (com.google.template.soy.soytree.PrintDirectiveNode)7 ExprRootNode (com.google.template.soy.exprtree.ExprRootNode)3 SoyPrintDirective (com.google.template.soy.shared.restricted.SoyPrintDirective)3 ArrayList (java.util.ArrayList)3 SoyExpression (com.google.template.soy.jbcsrc.restricted.SoyExpression)2 EscapeHtmlDirective (com.google.template.soy.coredirectives.EscapeHtmlDirective)1 SoyValue (com.google.template.soy.data.SoyValue)1 UndefinedData (com.google.template.soy.data.restricted.UndefinedData)1 FieldAccessNode (com.google.template.soy.exprtree.FieldAccessNode)1 PlusOpNode (com.google.template.soy.exprtree.OperatorNodes.PlusOpNode)1 StringNode (com.google.template.soy.exprtree.StringNode)1 BasicExpressionCompiler (com.google.template.soy.jbcsrc.ExpressionCompiler.BasicExpressionCompiler)1 Expression (com.google.template.soy.jbcsrc.restricted.Expression)1 CodeChunk (com.google.template.soy.jssrc.dsl.CodeChunk)1 WithValue (com.google.template.soy.jssrc.dsl.CodeChunk.WithValue)1 SoyJsSrcPrintDirective (com.google.template.soy.jssrc.restricted.SoyJsSrcPrintDirective)1 PyExpr (com.google.template.soy.pysrc.restricted.PyExpr)1 SoyPySrcPrintDirective (com.google.template.soy.pysrc.restricted.SoyPySrcPrintDirective)1 PrintNode (com.google.template.soy.soytree.PrintNode)1 StandaloneNode (com.google.template.soy.soytree.SoyNode.StandaloneNode)1