Search in sources :

Example 11 with MsgPlaceholderNode

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

the class MsgCompiler method putPlaceholderIntoMap.

private void putPlaceholderIntoMap(Expression mapExpression, MsgNode originalMsg, Map<String, Statement> placeholderNameToPutStatement, SoyMsgPlaceholderPart placeholder) throws AssertionError {
    String placeholderName = placeholder.getPlaceholderName();
    if (!placeholderNameToPutStatement.containsKey(placeholderName)) {
        MsgPlaceholderNode repPlaceholderNode = originalMsg.getRepPlaceholderNode(placeholder.getPlaceholderName());
        if (repPlaceholderNode.numChildren() == 0) {
            throw new IllegalStateException("empty rep node for: " + placeholderName);
        }
        StandaloneNode initialNode = repPlaceholderNode.getChild(0);
        Statement putEntyInMap;
        if (initialNode instanceof MsgHtmlTagNode) {
            putEntyInMap = addHtmlTagNodeToPlaceholderMap(mapExpression, placeholderName, (MsgHtmlTagNode) initialNode);
        } else if (initialNode instanceof CallNode) {
            putEntyInMap = addCallNodeToPlaceholderMap(mapExpression, placeholderName, (CallNode) initialNode);
        } else if (initialNode instanceof PrintNode) {
            putEntyInMap = addPrintNodeToPlaceholderMap(mapExpression, placeholderName, (PrintNode) initialNode);
        } else if (initialNode instanceof RawTextNode) {
            putEntyInMap = addRawTextNodeToPlaceholderMap(mapExpression, placeholderName, (RawTextNode) initialNode);
        } else {
            // the AST for MsgNodes guarantee that these are the only options
            throw new AssertionError("Unexpected child: " + initialNode.getClass());
        }
        placeholderNameToPutStatement.put(placeholder.getPlaceholderName(), putEntyInMap.withSourceLocation(repPlaceholderNode.getSourceLocation()));
    }
}
Also used : StandaloneNode(com.google.template.soy.soytree.SoyNode.StandaloneNode) Statement(com.google.template.soy.jbcsrc.restricted.Statement) MsgHtmlTagNode(com.google.template.soy.soytree.MsgHtmlTagNode) PrintNode(com.google.template.soy.soytree.PrintNode) CallNode(com.google.template.soy.soytree.CallNode) RawTextNode(com.google.template.soy.soytree.RawTextNode) MsgPlaceholderNode(com.google.template.soy.soytree.MsgPlaceholderNode)

Example 12 with MsgPlaceholderNode

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

the class UnescapingVisitor method visitRawTextNode.

@Override
protected void visitRawTextNode(RawTextNode node) {
    if (node.getHtmlContext() != HtmlContext.HTML_PCDATA && node.getHtmlContext() != HtmlContext.HTML_NORMAL_ATTR_VALUE) {
        return;
    }
    // Don't unescape the raw translated text nodes within an {msg} tag.  Do escape text nodes which
    // are nested in placeholders inside {msg} tags (since they aren't directly translated).  Unless
    // they're further in {msg} tags that are nested in placeholders.  Don't worry; we've got tests!
    MsgFallbackGroupNode containingMsg = node.getNearestAncestor(MsgFallbackGroupNode.class);
    if (containingMsg != null) {
        MsgPlaceholderNode containingPlaceholder = node.getNearestAncestor(MsgPlaceholderNode.class);
        // Unless we're _directly_ in a placeholder.
        if (containingPlaceholder == null || !SoyTreeUtils.isDescendantOf(containingPlaceholder, containingMsg)) {
            return;
        }
    }
    node.getParent().replaceChild(node, new RawTextNode(node.getId(), UnescapeUtils.unescapeHtml(node.getRawText()), node.getSourceLocation(), node.getHtmlContext()));
}
Also used : MsgFallbackGroupNode(com.google.template.soy.soytree.MsgFallbackGroupNode) RawTextNode(com.google.template.soy.soytree.RawTextNode) MsgPlaceholderNode(com.google.template.soy.soytree.MsgPlaceholderNode)

Example 13 with MsgPlaceholderNode

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

the class SimplifyVisitorTest method testMsgBlockNodeChildrenAreNotReplaced.

@Test
public void testMsgBlockNodeChildrenAreNotReplaced() throws Exception {
    String soyFileContent = "{namespace boo}\n" + "\n" + "{template .foo}\n" + "\n" + "  {msg desc=\"\"}\n" + "    blah\n" + "    {'blah'}\n" + "    blah\n" + "    {call .aaa /}\n" + "    blah\n" + "    <div class=\"{call .aaa /}\">\n" + "    </div>\n" + "    blah\n" + "  {/msg}\n" + "{/template}\n" + "\n" + "/***/\n" + "{template .aaa}\n" + "  blah\n" + "{/template}";
    MsgNode msgNode = ((MsgFallbackGroupNode) simplifySoyFiles(soyFileContent).getChild(0).getChild(0).getChild(0)).getChild(0);
    assertThat(msgNode.numChildren()).isEqualTo(8);
    // The MsgPlaceholderNode children are not replaced.
    assertThat(msgNode.getChild(1)).isInstanceOf(MsgPlaceholderNode.class);
    assertThat(msgNode.getChild(3)).isInstanceOf(MsgPlaceholderNode.class);
    assertThat(msgNode.getChild(5)).isInstanceOf(MsgPlaceholderNode.class);
    assertThat(msgNode.getChild(6)).isInstanceOf(MsgPlaceholderNode.class);
    // But the contents within the MsgPlaceholderNode children can be replaced.
    assertThat(((MsgPlaceholderNode) msgNode.getChild(1)).getChild(0)).isInstanceOf(RawTextNode.class);
}
Also used : MsgFallbackGroupNode(com.google.template.soy.soytree.MsgFallbackGroupNode) MsgNode(com.google.template.soy.soytree.MsgNode) MsgPlaceholderNode(com.google.template.soy.soytree.MsgPlaceholderNode) Test(org.junit.Test)

Aggregations

MsgPlaceholderNode (com.google.template.soy.soytree.MsgPlaceholderNode)13 StandaloneNode (com.google.template.soy.soytree.SoyNode.StandaloneNode)9 MsgFallbackGroupNode (com.google.template.soy.soytree.MsgFallbackGroupNode)8 RawTextNode (com.google.template.soy.soytree.RawTextNode)8 MsgNode (com.google.template.soy.soytree.MsgNode)7 Test (org.junit.Test)7 MsgHtmlTagNode (com.google.template.soy.soytree.MsgHtmlTagNode)5 MsgSelectNode (com.google.template.soy.soytree.MsgSelectNode)4 MsgPluralNode (com.google.template.soy.soytree.MsgPluralNode)3 PrintNode (com.google.template.soy.soytree.PrintNode)3 SoyMsgPart (com.google.template.soy.msgs.restricted.SoyMsgPart)2 SoyMsgPlaceholderPart (com.google.template.soy.msgs.restricted.SoyMsgPlaceholderPart)2 CallNode (com.google.template.soy.soytree.CallNode)2 MsgSelectCaseNode (com.google.template.soy.soytree.MsgSelectCaseNode)2 MsgSelectDefaultNode (com.google.template.soy.soytree.MsgSelectDefaultNode)2 Map (java.util.Map)2 ImmutableList (com.google.common.collect.ImmutableList)1 Statement (com.google.template.soy.jbcsrc.restricted.Statement)1 CodeChunk (com.google.template.soy.jssrc.dsl.CodeChunk)1 SwitchBuilder (com.google.template.soy.jssrc.dsl.SwitchBuilder)1