Search in sources :

Example 1 with PrintNode

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

the class VeLogInstrumentationVisitor method visitVeLogNode.

/**
 * Adds data-soylog attribute to the top-level DOM node in this {velog} block.
 */
@Override
protected void visitVeLogNode(VeLogNode node) {
    // VeLogValidationPass enforces that the first child is a open tag. We can safely cast it here.
    HtmlOpenTagNode tag = (HtmlOpenTagNode) node.getChild(0);
    SourceLocation insertionLocation = tag.getSourceLocation().getEndPoint().offset(0, tag.isSelfClosing() ? -2 : -1).asLocation(tag.getSourceLocation().getFilePath());
    FunctionNode funcNode = new FunctionNode(VeLogFunction.INSTANCE, insertionLocation);
    funcNode.addChild(new IntegerNode(node.getLoggingId(), insertionLocation));
    funcNode.addChild(node.getConfigExpression() == null ? new NullNode(insertionLocation) : node.getConfigExpression().copy(new CopyState()));
    if (node.getLogonlyExpression() != null) {
        funcNode.addChild(node.getLogonlyExpression().copy(new CopyState()));
    }
    PrintNode attributeNode = new PrintNode(nodeIdGen.genId(), insertionLocation, /* isImplicit= */
    true, /* expr= */
    funcNode, /* attributes= */
    ImmutableList.of(), ErrorReporter.exploding());
    tag.addChild(attributeNode);
    visitChildren(node);
}
Also used : SourceLocation(com.google.template.soy.base.SourceLocation) IntegerNode(com.google.template.soy.exprtree.IntegerNode) FunctionNode(com.google.template.soy.exprtree.FunctionNode) CopyState(com.google.template.soy.basetree.CopyState) PrintNode(com.google.template.soy.soytree.PrintNode) NullNode(com.google.template.soy.exprtree.NullNode) HtmlOpenTagNode(com.google.template.soy.soytree.HtmlOpenTagNode)

Example 2 with PrintNode

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

the class MsgFuncGenerator method collectVarNameListAndToPyExprMap.

/**
 * Private helper to process and collect all variables used within this msg node for code
 * generation.
 *
 * @return A Map populated with all the variables used with in this message node, using {@link
 *     MsgPlaceholderInitialNode#genBasePhName}.
 */
private Map<PyExpr, PyExpr> collectVarNameListAndToPyExprMap() {
    Map<PyExpr, PyExpr> nodePyVarToPyExprMap = new LinkedHashMap<>();
    for (Map.Entry<String, MsgSubstUnitNode> entry : msgNode.getVarNameToRepNodeMap().entrySet()) {
        MsgSubstUnitNode substUnitNode = entry.getValue();
        PyExpr substPyExpr = null;
        if (substUnitNode instanceof MsgPlaceholderNode) {
            SoyNode phInitialNode = ((AbstractParentSoyNode<?>) substUnitNode).getChild(0);
            if (phInitialNode instanceof PrintNode || phInitialNode instanceof CallNode || phInitialNode instanceof RawTextNode) {
                substPyExpr = PyExprUtils.concatPyExprs(genPyExprsVisitor.exec(phInitialNode)).toPyString();
            }
            // when the placeholder is generated by HTML tags
            if (phInitialNode instanceof MsgHtmlTagNode) {
                substPyExpr = PyExprUtils.concatPyExprs(genPyExprsVisitor.execOnChildren((ParentSoyNode<?>) phInitialNode)).toPyString();
            }
        } else if (substUnitNode instanceof MsgPluralNode) {
            // Translates {@link MsgPluralNode#pluralExpr} into a Python lookup expression.
            // Note that {@code pluralExpr} represents the soy expression of the {@code plural} attr,
            // i.e. the {@code $numDrafts} in {@code {plural $numDrafts}...{/plural}}.
            substPyExpr = translateToPyExprVisitor.exec(((MsgPluralNode) substUnitNode).getExpr());
        } else if (substUnitNode instanceof MsgSelectNode) {
            substPyExpr = translateToPyExprVisitor.exec(((MsgSelectNode) substUnitNode).getExpr());
        }
        if (substPyExpr != null) {
            nodePyVarToPyExprMap.put(new PyStringExpr("'" + entry.getKey() + "'"), substPyExpr);
        }
    }
    return nodePyVarToPyExprMap;
}
Also used : AbstractParentSoyNode(com.google.template.soy.soytree.AbstractParentSoyNode) SoyNode(com.google.template.soy.soytree.SoyNode) ParentSoyNode(com.google.template.soy.soytree.SoyNode.ParentSoyNode) AbstractParentSoyNode(com.google.template.soy.soytree.AbstractParentSoyNode) MsgSelectNode(com.google.template.soy.soytree.MsgSelectNode) PyStringExpr(com.google.template.soy.pysrc.restricted.PyStringExpr) MsgSubstUnitNode(com.google.template.soy.soytree.SoyNode.MsgSubstUnitNode) CallNode(com.google.template.soy.soytree.CallNode) LinkedHashMap(java.util.LinkedHashMap) PyExpr(com.google.template.soy.pysrc.restricted.PyExpr) ParentSoyNode(com.google.template.soy.soytree.SoyNode.ParentSoyNode) AbstractParentSoyNode(com.google.template.soy.soytree.AbstractParentSoyNode) MsgHtmlTagNode(com.google.template.soy.soytree.MsgHtmlTagNode) PrintNode(com.google.template.soy.soytree.PrintNode) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) RawTextNode(com.google.template.soy.soytree.RawTextNode) MsgPlaceholderNode(com.google.template.soy.soytree.MsgPlaceholderNode) MsgPluralNode(com.google.template.soy.soytree.MsgPluralNode)

Example 3 with PrintNode

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

the class ContentSecurityPolicyNonceInjectionPass method createCspInjection.

/**
 * Generates an AST fragment that looks like: {if $ij.csp_nonce}nonce="{$ij.csp_nonce}"{/if}
 *
 * @param insertionLocation The location where it is being inserted
 * @param nodeIdGen The id generator to use
 */
private static IfNode createCspInjection(SourceLocation insertionLocation, IdGenerator nodeIdGen) {
    IfNode ifNode = new IfNode(nodeIdGen.genId(), insertionLocation);
    IfCondNode ifCondNode = new IfCondNode(nodeIdGen.genId(), insertionLocation, "if", referenceCspNonce(insertionLocation));
    ifNode.addChild(ifCondNode);
    HtmlAttributeNode nonceAttribute = new HtmlAttributeNode(nodeIdGen.genId(), insertionLocation, insertionLocation.getBeginPoint());
    ifCondNode.addChild(nonceAttribute);
    nonceAttribute.addChild(new RawTextNode(nodeIdGen.genId(), "nonce", insertionLocation));
    HtmlAttributeValueNode attributeValue = new HtmlAttributeValueNode(nodeIdGen.genId(), insertionLocation, HtmlAttributeValueNode.Quotes.DOUBLE);
    nonceAttribute.addChild(attributeValue);
    // NOTE: we do not need to insert any print directives here, unlike the old implementation since
    // we are running before the autoescaper, so the escaper should insert whatever print directives
    // are appropriate.
    PrintNode printNode = new PrintNode(nodeIdGen.genId(), insertionLocation, // Implicit.  {$ij.csp_nonce} not {print $ij.csp_nonce}
    true, /* isImplicit= */
    referenceCspNonce(insertionLocation), /* attributes= */
    ImmutableList.of(), ErrorReporter.exploding());
    attributeValue.addChild(printNode);
    return ifNode;
}
Also used : IfCondNode(com.google.template.soy.soytree.IfCondNode) HtmlAttributeNode(com.google.template.soy.soytree.HtmlAttributeNode) IfNode(com.google.template.soy.soytree.IfNode) PrintNode(com.google.template.soy.soytree.PrintNode) RawTextNode(com.google.template.soy.soytree.RawTextNode) HtmlAttributeValueNode(com.google.template.soy.soytree.HtmlAttributeValueNode)

Example 4 with PrintNode

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

the class TemplateParserTest method testParseLogStmt.

@Test
public void testParseLogStmt() throws Exception {
    String templateBody = "{@param foo : ?}\n" + "{log}Blah {$foo}.{/log}";
    List<StandaloneNode> nodes = parseTemplateContent(templateBody, FAIL).getChildren();
    assertEquals(1, nodes.size());
    LogNode logNode = (LogNode) nodes.get(0);
    assertEquals(3, logNode.numChildren());
    assertEquals("Blah ", ((RawTextNode) logNode.getChild(0)).getRawText());
    assertEquals("$foo", ((PrintNode) logNode.getChild(1)).getExpr().toSourceString());
}
Also used : StandaloneNode(com.google.template.soy.soytree.SoyNode.StandaloneNode) LogNode(com.google.template.soy.soytree.LogNode) PrintNode(com.google.template.soy.soytree.PrintNode) Test(org.junit.Test)

Example 5 with PrintNode

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

the class TemplateParserTest method testParseMsgStmt.

@Test
public void testParseMsgStmt() throws Exception {
    String templateBody = "{@param usedMb :?}{@param learnMoreUrl :?}\n" + "  {msg desc=\"Tells user's quota usage.\"}\n" + "    You're currently using {$usedMb} MB of your quota.{sp}\n" + "    <a href=\"{$learnMoreUrl}\">Learn more</A>\n" + "    <br /><br />\n" + "  {/msg}\n" + "  {msg meaning=\"noun\" desc=\"\" hidden=\"true\"}Archive{/msg}\n" + "  {msg meaning=\"noun\" desc=\"The archive (noun).\"}Archive{/msg}\n" + "  {msg meaning=\"verb\" desc=\"\"}Archive{/msg}\n" + "  {msg desc=\"\"}Archive{/msg}\n";
    List<StandaloneNode> nodes = parseTemplateContent(templateBody, FAIL).getChildren();
    assertEquals(5, nodes.size());
    MsgNode mn0 = ((MsgFallbackGroupNode) nodes.get(0)).getMsg();
    assertEquals("Tells user's quota usage.", mn0.getDesc());
    assertEquals(null, mn0.getMeaning());
    assertEquals(false, mn0.isHidden());
    assertEquals(8, mn0.numChildren());
    assertEquals("You're currently using ", ((RawTextNode) mn0.getChild(0)).getRawText());
    MsgPlaceholderNode mpn1 = (MsgPlaceholderNode) mn0.getChild(1);
    assertEquals("$usedMb", ((PrintNode) mpn1.getChild(0)).getExpr().toSourceString());
    assertEquals(" MB of your quota. ", ((RawTextNode) mn0.getChild(2)).getRawText());
    MsgPlaceholderNode mpn3 = (MsgPlaceholderNode) mn0.getChild(3);
    MsgHtmlTagNode mhtn3 = (MsgHtmlTagNode) mpn3.getChild(0);
    assertEquals("a", mhtn3.getLcTagName());
    assertEquals("START_LINK", mhtn3.genBasePhName());
    assertEquals("<a href=\"{$learnMoreUrl}\">", mhtn3.toSourceString());
    assertEquals(3, mhtn3.numChildren());
    assertEquals("<a href=\"", ((RawTextNode) mhtn3.getChild(0)).getRawText());
    assertEquals("$learnMoreUrl", ((PrintNode) mhtn3.getChild(1)).getExpr().toSourceString());
    assertEquals("\">", ((RawTextNode) mhtn3.getChild(2)).getRawText());
    assertEquals("Learn more", ((RawTextNode) mn0.getChild(4)).getRawText());
    MsgPlaceholderNode mpn5 = (MsgPlaceholderNode) mn0.getChild(5);
    MsgHtmlTagNode mhtn5 = (MsgHtmlTagNode) mpn5.getChild(0);
    assertEquals("/a", mhtn5.getLcTagName());
    assertEquals("END_LINK", mhtn5.genBasePhName());
    assertEquals("</A>", mhtn5.toSourceString());
    MsgPlaceholderNode mpn6 = (MsgPlaceholderNode) mn0.getChild(6);
    MsgHtmlTagNode mhtn6 = (MsgHtmlTagNode) mpn6.getChild(0);
    assertEquals("BREAK", mhtn6.genBasePhName());
    assertTrue(mpn6.shouldUseSameVarNameAs((MsgPlaceholderNode) mn0.getChild(7)));
    assertFalse(mpn6.shouldUseSameVarNameAs(mpn5));
    assertFalse(mpn5.shouldUseSameVarNameAs(mpn3));
    MsgFallbackGroupNode mfgn1 = (MsgFallbackGroupNode) nodes.get(1);
    assertEquals("{msg meaning=\"noun\" desc=\"\" hidden=\"true\"}Archive{/msg}", mfgn1.toSourceString());
    MsgNode mn1 = mfgn1.getMsg();
    assertEquals("", mn1.getDesc());
    assertEquals("noun", mn1.getMeaning());
    assertEquals(true, mn1.isHidden());
    assertEquals(1, mn1.numChildren());
    assertEquals("Archive", ((RawTextNode) mn1.getChild(0)).getRawText());
}
Also used : StandaloneNode(com.google.template.soy.soytree.SoyNode.StandaloneNode) MsgFallbackGroupNode(com.google.template.soy.soytree.MsgFallbackGroupNode) MsgHtmlTagNode(com.google.template.soy.soytree.MsgHtmlTagNode) PrintNode(com.google.template.soy.soytree.PrintNode) MsgNode(com.google.template.soy.soytree.MsgNode) MsgPlaceholderNode(com.google.template.soy.soytree.MsgPlaceholderNode) Test(org.junit.Test)

Aggregations

PrintNode (com.google.template.soy.soytree.PrintNode)25 Test (org.junit.Test)15 FunctionNode (com.google.template.soy.exprtree.FunctionNode)8 StandaloneNode (com.google.template.soy.soytree.SoyNode.StandaloneNode)8 StringNode (com.google.template.soy.exprtree.StringNode)6 TemplateNode (com.google.template.soy.soytree.TemplateNode)6 SoyFileSetNode (com.google.template.soy.soytree.SoyFileSetNode)5 RawTextNode (com.google.template.soy.soytree.RawTextNode)4 FieldAccessNode (com.google.template.soy.exprtree.FieldAccessNode)3 VarRefNode (com.google.template.soy.exprtree.VarRefNode)3 IfCondNode (com.google.template.soy.soytree.IfCondNode)3 IfNode (com.google.template.soy.soytree.IfNode)3 MsgHtmlTagNode (com.google.template.soy.soytree.MsgHtmlTagNode)3 MsgPlaceholderNode (com.google.template.soy.soytree.MsgPlaceholderNode)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 SourceLocation (com.google.template.soy.base.SourceLocation)2 ExprNode (com.google.template.soy.exprtree.ExprNode)2 IntegerNode (com.google.template.soy.exprtree.IntegerNode)2 PyExpr (com.google.template.soy.pysrc.restricted.PyExpr)2 SoyFunction (com.google.template.soy.shared.restricted.SoyFunction)2