Search in sources :

Example 1 with JsCodeBuilder

use of com.google.template.soy.jssrc.internal.JsCodeBuilder in project closure-templates by google.

the class GenIncrementalDomCodeVisitor method visitPrintNode.

/**
 * Visit an {@link PrintNode}, with special cases for a variable being printed within an attribute
 * declaration or as HTML content.
 *
 * <p>For attributes, if the variable is of kind attributes, it is invoked. Any other kind of
 * variable is an error.
 *
 * <p>For HTML, if the variable is of kind HTML, it is invoked. Any other kind of variable gets
 * wrapped in a call to {@code incrementalDom.text}, resulting in a Text node.
 */
@Override
protected void visitPrintNode(PrintNode node) {
    ExprNode firstNode = node.getExpr().getRoot();
    // TODO(b/71896143): directives are not handled correctly in the html_tag case.
    switch(node.getHtmlContext()) {
        case HTML_TAG:
            if (tryGenerateFunctionCall(SoyType.Kind.ATTRIBUTES, firstNode) == GenerateFunctionCallResult.INDIRECT_NODE) {
                // Inside an HTML tag, we cannot emit indirect calls (like incrementalDom.text); the only
                // valid commands
                // are idom incrementalDom.attr() calls (which direct ATTRIBUTES functions will call).
                // If we can't emit the print node as a direct call, give up and report an error.
                errorReporter.report(node.getSourceLocation(), PRINT_ATTR_INVALID_KIND, firstNode.getType().getKind());
            }
            break;
        case HTML_PCDATA:
            // But if we statically know that it's an HTML function, we can call it directly.
            if (tryGenerateFunctionCall(SoyType.Kind.HTML, firstNode) == GenerateFunctionCallResult.INDIRECT_NODE) {
                List<CodeChunk.WithValue> chunks = genJsExprsVisitor.exec(node);
                CodeChunk.WithValue printCall = SOY_IDOM_PRINT.call(CodeChunkUtils.concatChunks(chunks));
                JsCodeBuilder codeBuilder = getJsCodeBuilder();
                codeBuilder.append(printCall);
            }
            break;
        default:
            super.visitPrintNode(node);
            break;
    }
}
Also used : ExprNode(com.google.template.soy.exprtree.ExprNode) CodeChunk(com.google.template.soy.jssrc.dsl.CodeChunk) JsCodeBuilder(com.google.template.soy.jssrc.internal.JsCodeBuilder)

Example 2 with JsCodeBuilder

use of com.google.template.soy.jssrc.internal.JsCodeBuilder in project closure-templates by google.

the class GenIncrementalDomCodeVisitor method visitRawTextNode.

/**
 * Visits a {@link RawTextNode}, which occurs either as a child of any BlockNode or the 'child' of
 * an HTML tag. Note that in the soy tree, tags and their logical HTML children do not have a
 * parent-child relationship, but are rather siblings. For example:
 *
 * <pre>
 * &lt;div&gt;Hello world&lt;/div&gt;
 * </pre>
 *
 * The text "Hello world" translates to
 *
 * <pre>
 * incrementalDom.text('Hello world');
 * </pre>
 */
@Override
protected void visitRawTextNode(RawTextNode node) {
    CodeChunk.WithValue textArg = stringLiteral(node.getRawText());
    JsCodeBuilder jsCodeBuilder = getJsCodeBuilder();
    if (node.getHtmlContext() == HtmlContext.HTML_PCDATA) {
        // Note - we don't use generateTextCall since this text can never be null.
        jsCodeBuilder.append(INCREMENTAL_DOM_TEXT.call(textArg));
    } else {
        jsCodeBuilder.addChunkToOutputVar(textArg);
    }
}
Also used : CodeChunk(com.google.template.soy.jssrc.dsl.CodeChunk) JsCodeBuilder(com.google.template.soy.jssrc.internal.JsCodeBuilder)

Aggregations

CodeChunk (com.google.template.soy.jssrc.dsl.CodeChunk)2 JsCodeBuilder (com.google.template.soy.jssrc.internal.JsCodeBuilder)2 ExprNode (com.google.template.soy.exprtree.ExprNode)1