Search in sources :

Example 1 with RawTextNode

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

the class MsgUtils method buildMsgPartsForChildren.

// -----------------------------------------------------------------------------------------------
// Private helpers for building the list of message parts.
/**
 * Builds the list of SoyMsgParts for all the children of a given parent node.
 *
 * @param parent Can be MsgNode, MsgPluralCaseNode, MsgPluralDefaultNode, MsgSelectCaseNode, or
 *     MsgSelectDefaultNode.
 * @param msgNode The MsgNode containing 'parent'.
 */
private static ImmutableList<SoyMsgPart> buildMsgPartsForChildren(BlockNode parent, MsgNode msgNode) {
    ImmutableList.Builder<SoyMsgPart> msgParts = ImmutableList.builder();
    for (StandaloneNode child : parent.getChildren()) {
        if (child instanceof RawTextNode) {
            String rawText = ((RawTextNode) child).getRawText();
            msgParts.add(SoyMsgRawTextPart.of(rawText));
        } else if (child instanceof MsgPlaceholderNode) {
            PlaceholderInfo placeholder = msgNode.getPlaceholder((MsgPlaceholderNode) child);
            msgParts.add(new SoyMsgPlaceholderPart(placeholder.name(), placeholder.example()));
        } else if (child instanceof MsgPluralNode) {
            msgParts.add(buildMsgPartForPlural((MsgPluralNode) child, msgNode));
        } else if (child instanceof MsgSelectNode) {
            msgParts.add(buildMsgPartForSelect((MsgSelectNode) child, msgNode));
        }
    }
    return msgParts.build();
}
Also used : StandaloneNode(com.google.template.soy.soytree.SoyNode.StandaloneNode) SoyMsgPlaceholderPart(com.google.template.soy.msgs.restricted.SoyMsgPlaceholderPart) ImmutableList(com.google.common.collect.ImmutableList) PlaceholderInfo(com.google.template.soy.soytree.MsgNode.PlaceholderInfo) MsgSelectNode(com.google.template.soy.soytree.MsgSelectNode) RawTextNode(com.google.template.soy.soytree.RawTextNode) MsgPlaceholderNode(com.google.template.soy.soytree.MsgPlaceholderNode) MsgPluralNode(com.google.template.soy.soytree.MsgPluralNode) SoyMsgPart(com.google.template.soy.msgs.restricted.SoyMsgPart)

Example 2 with RawTextNode

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

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

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

the class RawTextBuilder method build.

RawTextNode build() {
    maybeFinishBasic();
    String text = buffer.toString();
    RawTextNode.SourceOffsets sourceOffsets = offsets.build(text.length(), discontinuityReason);
    return new RawTextNode(nodeIdGen.genId(), text, sourceOffsets.getSourceLocation(fileName), sourceOffsets);
}
Also used : RawTextNode(com.google.template.soy.soytree.RawTextNode) SourceOffsets(com.google.template.soy.soytree.RawTextNode.SourceOffsets)

Example 5 with RawTextNode

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

the class SourceLocationTest method testRawTextSourceLocations.

@Test
public void testRawTextSourceLocations() throws Exception {
    // RawTextNode has some special methods to calculating the source location of characters within
    // the strings, test those
    String template = JOINER.join("{namespace ns}", "{template .foo}", "  Hello,{sp}", "  {\\n}{nil}<span>Bob</span>", "  // and end of line comment", "  !", "  What's /*hello comment world*/up?", "{/template}", "");
    RawTextNode rawText = (RawTextNode) SoyFileSetParserBuilder.forFileContents(template).parse().fileSet().getChild(0).getChild(0).getChild(0);
    assertThat(rawText.getRawText()).isEqualTo("Hello, \n<span>Bob</span>! What's up?");
    assertThat(rawText.getRawText().substring(0, 5)).isEqualTo("Hello");
    SourceLocation loc = rawText.substringLocation(0, 5);
    assertThat(loc.getBeginLine()).isEqualTo(3);
    assertThat(loc.getBeginColumn()).isEqualTo(3);
    assertThat(loc.getEndLine()).isEqualTo(3);
    assertThat(loc.getEndColumn()).isEqualTo(7);
    assertThat(rawText.getRawText().substring(8, 14)).isEqualTo("<span>");
    loc = rawText.substringLocation(8, 14);
    assertThat(loc.getBeginLine()).isEqualTo(4);
    assertThat(loc.getBeginColumn()).isEqualTo(12);
    assertThat(loc.getEndLine()).isEqualTo(4);
    assertThat(loc.getEndColumn()).isEqualTo(17);
    assertThat(rawText.getRawText().substring(24, 25)).isEqualTo("!");
    loc = rawText.substringLocation(24, 25);
    assertThat(loc.getBeginLine()).isEqualTo(6);
    assertThat(loc.getBeginColumn()).isEqualTo(3);
    assertThat(loc.getEndLine()).isEqualTo(6);
    assertThat(loc.getEndColumn()).isEqualTo(3);
    assertThat(rawText.getRawText().substring(33, 36)).isEqualTo("up?");
    loc = rawText.substringLocation(33, 36);
    assertThat(loc.getBeginLine()).isEqualTo(7);
    assertThat(loc.getBeginColumn()).isEqualTo(33);
    assertThat(loc.getEndLine()).isEqualTo(7);
    assertThat(loc.getEndColumn()).isEqualTo(35);
    // doesn't matter
    final int id = 1337;
    RawTextNode subStringNode = rawText.substring(id, 0, 5);
    assertThat(subStringNode.getRawText()).isEqualTo("Hello");
    loc = subStringNode.getSourceLocation();
    assertThat(loc.getBeginLine()).isEqualTo(3);
    assertThat(loc.getBeginColumn()).isEqualTo(3);
    assertThat(loc.getEndLine()).isEqualTo(3);
    assertThat(loc.getEndColumn()).isEqualTo(7);
    subStringNode = rawText.substring(id, 24, 25);
    assertThat(subStringNode.getRawText()).isEqualTo("!");
    loc = subStringNode.getSourceLocation();
    assertThat(loc.getBeginLine()).isEqualTo(6);
    assertThat(loc.getBeginColumn()).isEqualTo(3);
    assertThat(loc.getEndLine()).isEqualTo(6);
    assertThat(loc.getEndColumn()).isEqualTo(3);
    // Can't create empty raw text nodes.
    try {
        rawText.substring(id, 24, 24);
        fail();
    } catch (IllegalArgumentException expected) {
    }
    try {
        rawText.substring(id, 24, 23);
        fail();
    } catch (IllegalArgumentException expected) {
    }
    try {
        rawText.substring(id, 24, Integer.MAX_VALUE);
        fail();
    } catch (IllegalArgumentException expected) {
    }
}
Also used : SourceLocation(com.google.template.soy.base.SourceLocation) RawTextNode(com.google.template.soy.soytree.RawTextNode) Test(org.junit.Test)

Aggregations

RawTextNode (com.google.template.soy.soytree.RawTextNode)28 Test (org.junit.Test)15 StandaloneNode (com.google.template.soy.soytree.SoyNode.StandaloneNode)10 TemplateNode (com.google.template.soy.soytree.TemplateNode)9 MsgFallbackGroupNode (com.google.template.soy.soytree.MsgFallbackGroupNode)8 MsgPlaceholderNode (com.google.template.soy.soytree.MsgPlaceholderNode)8 SoyFileSetNode (com.google.template.soy.soytree.SoyFileSetNode)8 MsgNode (com.google.template.soy.soytree.MsgNode)6 MsgHtmlTagNode (com.google.template.soy.soytree.MsgHtmlTagNode)5 PrintNode (com.google.template.soy.soytree.PrintNode)4 ErrorReporter (com.google.template.soy.error.ErrorReporter)3 FunctionNode (com.google.template.soy.exprtree.FunctionNode)3 SoyMsgPlaceholderPart (com.google.template.soy.msgs.restricted.SoyMsgPlaceholderPart)3 HtmlAttributeValueNode (com.google.template.soy.soytree.HtmlAttributeValueNode)3 MsgPluralNode (com.google.template.soy.soytree.MsgPluralNode)3 MsgSelectNode (com.google.template.soy.soytree.MsgSelectNode)3 ParentSoyNode (com.google.template.soy.soytree.SoyNode.ParentSoyNode)3 SourceLocation (com.google.template.soy.base.SourceLocation)2 Point (com.google.template.soy.base.SourceLocation.Point)2 SoyMsgBundle (com.google.template.soy.msgs.SoyMsgBundle)2