use of com.google.template.soy.soytree.MsgNode in project closure-templates by google.
the class TemplateParserTest method testParseMsgStmtWithSelect.
@Test
public void testParseMsgStmtWithSelect() throws Exception {
String templateBody = "{@param gender : ?}{@param person : ?}\n" + "{msg desc=\"A sample gender message\"}\n" + " {select $gender}\n" + " {case 'female'}{$person} added you to her circle.\n" + " {default}{$person} added you to his circle.\n" + " {/select}\n" + "{/msg}\n";
List<StandaloneNode> nodes = parseTemplateContent(templateBody, FAIL).getChildren();
assertEquals(1, nodes.size());
MsgNode mn = ((MsgFallbackGroupNode) nodes.get(0)).getChild(0);
assertEquals(1, mn.numChildren());
assertEquals("A sample gender message", mn.getDesc());
MsgSelectNode sn = (MsgSelectNode) mn.getChild(0);
assertEquals("$gender", sn.getExpr().toSourceString());
// female and default
assertEquals(2, sn.numChildren());
// Case 'female'
MsgSelectCaseNode cnf = (MsgSelectCaseNode) sn.getChild(0);
assertEquals("female", cnf.getCaseValue());
assertEquals(2, cnf.numChildren());
MsgPlaceholderNode phnf = (MsgPlaceholderNode) cnf.getChild(0);
assertEquals("{$person}", phnf.toSourceString());
RawTextNode rtnf = (RawTextNode) cnf.getChild(1);
assertEquals(" added you to her circle.", rtnf.getRawText());
// Default
MsgSelectDefaultNode dn = (MsgSelectDefaultNode) sn.getChild(1);
assertEquals(2, dn.numChildren());
MsgPlaceholderNode phnd = (MsgPlaceholderNode) dn.getChild(0);
assertEquals("{$person}", phnd.toSourceString());
RawTextNode rtnd = (RawTextNode) dn.getChild(1);
assertEquals(" added you to his circle.", rtnd.getRawText());
}
use of com.google.template.soy.soytree.MsgNode in project closure-templates by google.
the class TemplateParserTest method testParseMsgStmtWithCall.
@Test
public void testParseMsgStmtWithCall() throws Exception {
String templateBody = " {msg desc=\"Blah.\"}\n" + " Blah {call .helper_ data=\"all\" /} blah{sp}\n" + " {call .helper_}\n" + " {param foo : 'foo' /}\n" + " {/call}{sp}\n" + " blah.\n" + " {/msg}\n";
List<StandaloneNode> nodes = parseTemplateContent(templateBody, FAIL).getChildren();
assertEquals(1, nodes.size());
MsgNode mn = ((MsgFallbackGroupNode) nodes.get(0)).getChild(0);
assertEquals(5, mn.numChildren());
assertEquals("Blah ", ((RawTextNode) mn.getChild(0)).getRawText());
assertEquals(0, ((CallNode) ((MsgPlaceholderNode) mn.getChild(1)).getChild(0)).numChildren());
assertEquals(" blah ", ((RawTextNode) mn.getChild(2)).getRawText());
assertEquals(1, ((CallNode) ((MsgPlaceholderNode) mn.getChild(3)).getChild(0)).numChildren());
assertEquals(" blah.", ((RawTextNode) mn.getChild(4)).getRawText());
}
use of com.google.template.soy.soytree.MsgNode in project closure-templates by google.
the class TemplateParserTest method testParseMsgHtmlTagWithPhname.
@Test
public void testParseMsgHtmlTagWithPhname() throws Exception {
String templateBody = "{@param learnMoreUrl :?}\n" + " {msg desc=\"\"}\n" + " <a href=\"{$learnMoreUrl}\" phname=\"beginLearnMoreLink\">\n" + " Learn more\n" + " </A phname=\"end_LearnMore_LINK\">\n" + " <br phname=\"breakTag\" /><br phname=\"breakTag\" />" + "<br phname=\"break_tag\" />\n" + " {/msg}\n";
List<StandaloneNode> nodes = parseTemplateContent(templateBody, FAIL).getChildren();
assertEquals(1, nodes.size());
MsgNode mn0 = ((MsgFallbackGroupNode) nodes.get(0)).getChild(0);
assertEquals(6, mn0.numChildren());
MsgPlaceholderNode mpn0 = (MsgPlaceholderNode) mn0.getChild(0);
MsgHtmlTagNode mhtn0 = (MsgHtmlTagNode) mpn0.getChild(0);
assertEquals("a", mhtn0.getLcTagName());
assertEquals("BEGIN_LEARN_MORE_LINK", mhtn0.genBasePhName());
assertEquals("<a href=\"{$learnMoreUrl}\" phname=\"beginLearnMoreLink\">", mhtn0.toSourceString());
MsgPlaceholderNode mpn2 = (MsgPlaceholderNode) mn0.getChild(2);
MsgHtmlTagNode mhtn2 = (MsgHtmlTagNode) mpn2.getChild(0);
assertEquals("/a", mhtn2.getLcTagName());
assertEquals("END_LEARN_MORE_LINK", mhtn2.genBasePhName());
assertEquals("</A phname=\"end_LearnMore_LINK\">", mhtn2.toSourceString());
MsgPlaceholderNode mpn3 = (MsgPlaceholderNode) mn0.getChild(3);
MsgHtmlTagNode mhtn3 = (MsgHtmlTagNode) mpn3.getChild(0);
assertEquals("br", mhtn3.getLcTagName());
assertEquals("BREAK_TAG", mhtn3.genBasePhName());
assertEquals("<br phname=\"breakTag\"/>", mhtn3.toSourceString());
MsgPlaceholderNode mpn4 = (MsgPlaceholderNode) mn0.getChild(4);
MsgHtmlTagNode mhtn4 = (MsgHtmlTagNode) mpn4.getChild(0);
MsgPlaceholderNode mpn5 = (MsgPlaceholderNode) mn0.getChild(5);
MsgHtmlTagNode mhtn5 = (MsgHtmlTagNode) mpn5.getChild(0);
assertEquals("br", mhtn5.getLcTagName());
assertEquals("BREAK_TAG", mhtn5.genBasePhName());
assertEquals("<br phname=\"break_tag\"/>", mhtn5.toSourceString());
assertFalse(mhtn0.genSamenessKey().equals(mhtn2.genSamenessKey()));
assertFalse(mhtn0.genSamenessKey().equals(mhtn3.genSamenessKey()));
assertTrue(mhtn3.genSamenessKey().equals(mhtn4.genSamenessKey()));
assertFalse(mhtn3.genSamenessKey().equals(mhtn5.genSamenessKey()));
}
use of com.google.template.soy.soytree.MsgNode in project closure-templates by google.
the class SoyNodeCompiler method visitMsgFallbackGroupNode.
/**
* MsgFallbackGroupNodes have either one or two children. In the 2 child case the second child is
* the {@code {fallbackmsg}} entry. For this we generate code that looks like:
*
* <pre>{@code
* if (renderContext.hasMsg(primaryId)) {
* <render primary msg>
* } else {
* <render fallback msg>
* }
* }</pre>
*
* <p>All of the logic for actually rendering {@code msg} nodes is handled by the {@link
* MsgCompiler}.
*/
@Override
protected Statement visitMsgFallbackGroupNode(MsgFallbackGroupNode node) {
MsgNode msg = node.getMsg();
MsgPartsAndIds idAndParts = MsgUtils.buildMsgPartsAndComputeMsgIdForDualFormat(msg);
ImmutableList<SoyPrintDirective> escapingDirectives = node.getEscapingDirectives();
Statement renderDefault = getMsgCompiler().compileMessage(idAndParts, msg, escapingDirectives);
// need to check for presence.
if (node.hasFallbackMsg()) {
MsgNode fallback = node.getFallbackMsg();
MsgPartsAndIds fallbackIdAndParts = MsgUtils.buildMsgPartsAndComputeMsgIdForDualFormat(fallback);
// TODO(lukes): consider changing the control flow here by 'inlining' the usePrimaryMsg logic
// it would save some lookups. Right now we will do to 2- 3 calls to
// SoyMsgBundle.getMsgParts (each of which requires a binary search). We could reduce that
// to 1-2 in the worse case by inlining and storing the lists in local variables.
IfBlock ifAvailableRenderDefault = IfBlock.create(parameterLookup.getRenderContext().usePrimaryMsg(idAndParts.id, fallbackIdAndParts.id), renderDefault);
return ControlFlow.ifElseChain(ImmutableList.of(ifAvailableRenderDefault), Optional.of(getMsgCompiler().compileMessage(fallbackIdAndParts, fallback, escapingDirectives)));
} else {
return renderDefault;
}
}
use of com.google.template.soy.soytree.MsgNode 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);
}
Aggregations