Search in sources :

Example 11 with MsgFallbackGroupNode

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

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

Example 13 with MsgFallbackGroupNode

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

the class RenderVisitorTest method testRenderMsgStmtWithFallback.

@Test
public void testRenderMsgStmtWithFallback() throws Exception {
    String templateBody = "" + "  {msg desc=\"\"}\n" + "    blah\n" + "  {fallbackmsg desc=\"\"}\n" + "    bleh\n" + "  {/msg}\n";
    // Without msg bundle.
    assertRender(templateBody, "blah");
    // With msg bundle.
    SoyFileNode file = SoyFileSetParserBuilder.forFileContents("{namespace test}\n{template .foo}\n" + templateBody + "{/template}").parse().fileSet().getChild(0);
    MsgFallbackGroupNode msgGroup = SoyTreeUtils.getAllNodesOfType(file.getChild(0), MsgFallbackGroupNode.class).get(0);
    MsgNode fallbackMsg = msgGroup.getChild(1);
    SoyMsg translatedFallbackMsg = SoyMsg.builder().setId(MsgUtils.computeMsgIdForDualFormat(fallbackMsg)).setLocaleString("x-zz").setParts(ImmutableList.<SoyMsgPart>of(SoyMsgRawTextPart.of("zbleh"))).build();
    SoyMsgBundle msgBundle = new SoyMsgBundleImpl("x-zz", Lists.newArrayList(translatedFallbackMsg));
    assertThat(renderWithDataAndMsgBundle(templateBody, TEST_DATA, msgBundle)).isEqualTo("zbleh");
}
Also used : SoyMsg(com.google.template.soy.msgs.restricted.SoyMsg) SoyMsgBundleImpl(com.google.template.soy.msgs.restricted.SoyMsgBundleImpl) MsgFallbackGroupNode(com.google.template.soy.soytree.MsgFallbackGroupNode) SoyFileNode(com.google.template.soy.soytree.SoyFileNode) SoyMsgBundle(com.google.template.soy.msgs.SoyMsgBundle) MsgNode(com.google.template.soy.soytree.MsgNode) SoyMsgPart(com.google.template.soy.msgs.restricted.SoyMsgPart) Test(org.junit.Test)

Example 14 with MsgFallbackGroupNode

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

the class InsertMsgsVisitorTest method testFallbackMsgsUsingSoySource.

@Test
public void testFallbackMsgsUsingSoySource() {
    SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(FALLBACK_TEST_FILE_CONTENT).parse().fileSet();
    TemplateNode template = (TemplateNode) SharedTestUtils.getNode(soyTree);
    // Before.
    assertThat(template.numChildren()).isEqualTo(4);
    assertThat(((MsgFallbackGroupNode) template.getChild(0)).numChildren()).isEqualTo(2);
    assertThat(((MsgFallbackGroupNode) template.getChild(1)).numChildren()).isEqualTo(2);
    assertThat(((MsgFallbackGroupNode) template.getChild(2)).numChildren()).isEqualTo(2);
    assertThat(((MsgFallbackGroupNode) template.getChild(3)).numChildren()).isEqualTo(2);
    // Execute the visitor.
    new InsertMsgsVisitor(null, /* msgBundle */
    FAIL).insertMsgs(template);
    // After.
    assertThat(template.numChildren()).isEqualTo(4);
    assertThat(((RawTextNode) template.getChild(0)).getRawText()).isEqualTo("noTrans1");
    assertThat(((RawTextNode) template.getChild(1)).getRawText()).isEqualTo("trans1");
    assertThat(((RawTextNode) template.getChild(2)).getRawText()).isEqualTo("noTrans1");
    assertThat(((RawTextNode) template.getChild(3)).getRawText()).isEqualTo("trans1");
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) MsgFallbackGroupNode(com.google.template.soy.soytree.MsgFallbackGroupNode) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) RawTextNode(com.google.template.soy.soytree.RawTextNode) Test(org.junit.Test)

Example 15 with MsgFallbackGroupNode

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

the class InsertMsgsVisitorTest method testBasicMsgsUsingSoySource.

@Test
public void testBasicMsgsUsingSoySource() {
    SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(BASIC_TEST_FILE_CONTENT).parse().fileSet();
    TemplateNode template = (TemplateNode) SharedTestUtils.getNode(soyTree);
    // Before.
    assertThat(template.numChildren()).isEqualTo(5);
    MsgNode msg = ((MsgFallbackGroupNode) template.getChild(2)).getChild(0);
    assertThat(msg.numChildren()).isEqualTo(5);
    MsgHtmlTagNode msgHtmlTag2 = (MsgHtmlTagNode) ((MsgPlaceholderNode) msg.getChild(2)).getChild(0);
    assertThat(msgHtmlTag2.numChildren()).isEqualTo(3);
    MsgHtmlTagNode msgHtmlTag4 = (MsgHtmlTagNode) ((MsgPlaceholderNode) msg.getChild(4)).getChild(0);
    assertThat(msgHtmlTag4.numChildren()).isEqualTo(1);
    assertThat(((RawTextNode) msgHtmlTag4.getChild(0)).getRawText()).isEqualTo("</a>");
    // Execute the visitor.
    new InsertMsgsVisitor(null, /* msgBundle */
    FAIL).insertMsgs(template);
    // After.
    assertThat(template.numChildren()).isEqualTo(12);
    assertThat(((PrintNode) template.getChild(0)).getExpr().toSourceString()).isEqualTo("$boo");
    assertThat(((RawTextNode) template.getChild(1)).getRawText()).isEqualTo("scary ");
    assertThat(((RawTextNode) template.getChild(2)).getRawText()).isEqualTo("random");
    assertThat(((PrintNode) template.getChild(3)).getExpr().toSourceString()).isEqualTo("$foo");
    assertThat(((RawTextNode) template.getChild(4)).getRawText()).isEqualTo("<a href=\"");
    assertThat(((PrintNode) template.getChild(5)).getExpr().toSourceString()).isEqualTo("$goo");
    assertThat(((RawTextNode) template.getChild(6)).getRawText()).isEqualTo("\">");
    assertThat(((RawTextNode) template.getChild(7)).getRawText()).isEqualTo("slimy");
    assertThat(((RawTextNode) template.getChild(8)).getRawText()).isEqualTo("</a>");
    assertThat(((RawTextNode) template.getChild(9)).getRawText()).isEqualTo(" ");
    assertThat(((RawTextNode) template.getChild(10)).getRawText()).isEqualTo("dairy");
    assertThat(((PrintNode) template.getChild(11)).getExpr().toSourceString()).isEqualTo("$moo");
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) MsgFallbackGroupNode(com.google.template.soy.soytree.MsgFallbackGroupNode) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) MsgHtmlTagNode(com.google.template.soy.soytree.MsgHtmlTagNode) RawTextNode(com.google.template.soy.soytree.RawTextNode) MsgNode(com.google.template.soy.soytree.MsgNode) Test(org.junit.Test)

Aggregations

MsgFallbackGroupNode (com.google.template.soy.soytree.MsgFallbackGroupNode)15 MsgNode (com.google.template.soy.soytree.MsgNode)13 Test (org.junit.Test)13 MsgPlaceholderNode (com.google.template.soy.soytree.MsgPlaceholderNode)8 RawTextNode (com.google.template.soy.soytree.RawTextNode)8 StandaloneNode (com.google.template.soy.soytree.SoyNode.StandaloneNode)7 MsgHtmlTagNode (com.google.template.soy.soytree.MsgHtmlTagNode)4 SoyFileSetNode (com.google.template.soy.soytree.SoyFileSetNode)4 TemplateNode (com.google.template.soy.soytree.TemplateNode)4 SoyMsgBundle (com.google.template.soy.msgs.SoyMsgBundle)3 SoyMsg (com.google.template.soy.msgs.restricted.SoyMsg)3 SoyMsgBundleImpl (com.google.template.soy.msgs.restricted.SoyMsgBundleImpl)3 MsgSelectCaseNode (com.google.template.soy.soytree.MsgSelectCaseNode)2 MsgSelectDefaultNode (com.google.template.soy.soytree.MsgSelectDefaultNode)2 MsgSelectNode (com.google.template.soy.soytree.MsgSelectNode)2 SoyMsgPart (com.google.template.soy.msgs.restricted.SoyMsgPart)1 SoyMsgPlaceholderPart (com.google.template.soy.msgs.restricted.SoyMsgPlaceholderPart)1 MsgPluralCaseNode (com.google.template.soy.soytree.MsgPluralCaseNode)1 MsgPluralDefaultNode (com.google.template.soy.soytree.MsgPluralDefaultNode)1 MsgPluralNode (com.google.template.soy.soytree.MsgPluralNode)1