Search in sources :

Example 6 with RawTextNode

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

the class TemplateParserTest method testParseMsgStmtWithNestedSelects.

@Test
public void testParseMsgStmtWithNestedSelects() throws Exception {
    String templateBody = "{@param gender1 : ?}{@param gender2: ?}{@param person1 : ?}{@param person2: ?}\n" + "{msg desc=\"A sample nested message\"}\n" + "  {select $gender1}\n" + "    {case 'female'}\n" + "      {select $gender2}\n" + "        {case 'female'}{$person1} added {$person2} and her friends to her circle.\n" + "        {default}{$person1} added {$person2} and his friends to her circle.\n" + "      {/select}\n" + "    {default}\n" + "      {select $gender2}\n" + "        {case 'female'}{$person1} put {$person2} and her friends to his circle.\n" + "        {default}{$person1} put {$person2} and his friends to his circle.\n" + "      {/select}\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 nested message", mn.getDesc());
    // Outer select
    MsgSelectNode sn = (MsgSelectNode) mn.getChild(0);
    assertEquals("$gender1", sn.getExpr().toSourceString());
    // female and default
    assertEquals(2, sn.numChildren());
    // Outer select: Case 'female'
    MsgSelectCaseNode cnf = (MsgSelectCaseNode) sn.getChild(0);
    assertEquals("female", cnf.getCaseValue());
    // Another select
    assertEquals(1, cnf.numChildren());
    // Outer select: Case 'female': Inner select
    MsgSelectNode sn2 = (MsgSelectNode) cnf.getChild(0);
    assertEquals("$gender2", sn2.getExpr().toSourceString());
    // female and default
    assertEquals(2, sn2.numChildren());
    // Outer select: Case 'female': Inner select: Case 'female'
    MsgSelectCaseNode cnf2 = (MsgSelectCaseNode) sn2.getChild(0);
    assertEquals("female", cnf2.getCaseValue());
    assertEquals(4, cnf2.numChildren());
    // Outer select: Case 'female': Inner select: Case 'female': Placeholder $person1
    MsgPlaceholderNode phn1 = (MsgPlaceholderNode) cnf2.getChild(0);
    assertEquals("{$person1}", phn1.toSourceString());
    // Outer select: Case 'female': Inner select: Case 'female': RawText
    RawTextNode rtn1 = (RawTextNode) cnf2.getChild(1);
    assertEquals(" added ", rtn1.getRawText());
    // Outer select: Case 'female': Inner select: Case 'female': Placeholder $person2
    MsgPlaceholderNode phn2 = (MsgPlaceholderNode) cnf2.getChild(2);
    assertEquals("{$person2}", phn2.toSourceString());
    // Outer select: Case 'female': Inner select: Case 'female': RawText
    RawTextNode rtn2 = (RawTextNode) cnf2.getChild(3);
    assertEquals(" and her friends to her circle.", rtn2.getRawText());
    // Outer select: Case 'female': Inner select: Default
    MsgSelectDefaultNode dn2 = (MsgSelectDefaultNode) sn2.getChild(1);
    assertEquals(4, dn2.numChildren());
    // Outer select: Case 'female': Inner select: Default: Placeholder $person1
    MsgPlaceholderNode phn21 = (MsgPlaceholderNode) dn2.getChild(0);
    assertEquals("{$person1}", phn21.toSourceString());
    // Outer select: Case 'female': Inner select: Default: RawText
    RawTextNode rtn21 = (RawTextNode) dn2.getChild(1);
    assertEquals(" added ", rtn21.getRawText());
    // Outer select: Case 'female': Inner select: Default: Placeholder $person2
    MsgPlaceholderNode phn22 = (MsgPlaceholderNode) dn2.getChild(2);
    assertEquals("{$person2}", phn22.toSourceString());
    // Outer select: Case 'female': Inner select: Default: RawText
    RawTextNode rtn22 = (RawTextNode) dn2.getChild(3);
    assertEquals(" and his friends to her circle.", rtn22.getRawText());
    // Outer select: Default
    MsgSelectDefaultNode dn = (MsgSelectDefaultNode) sn.getChild(1);
    // Another select
    assertEquals(1, dn.numChildren());
    // Outer select: Default: Inner select
    MsgSelectNode sn3 = (MsgSelectNode) dn.getChild(0);
    assertEquals("$gender2", sn3.getExpr().toSourceString());
    // female and default
    assertEquals(2, sn3.numChildren());
    // Outer select: Default: Inner select: Case 'female'
    MsgSelectCaseNode cnf3 = (MsgSelectCaseNode) sn3.getChild(0);
    assertEquals("female", cnf3.getCaseValue());
    assertEquals(4, cnf3.numChildren());
    // Outer select: Default: Inner select: Case 'female': Placeholder $person1
    MsgPlaceholderNode phn3 = (MsgPlaceholderNode) cnf3.getChild(0);
    assertEquals("{$person1}", phn3.toSourceString());
    // Outer select: Default: Inner select: Case 'female': RawText
    RawTextNode rtn3 = (RawTextNode) cnf3.getChild(1);
    assertEquals(" put ", rtn3.getRawText());
    // Outer select: Default: Inner select: Case 'female': Placeholder $person2
    MsgPlaceholderNode phn4 = (MsgPlaceholderNode) cnf3.getChild(2);
    assertEquals("{$person2}", phn4.toSourceString());
    // Outer select: Default: Inner select: Case 'female': RawText
    RawTextNode rtn4 = (RawTextNode) cnf3.getChild(3);
    assertEquals(" and her friends to his circle.", rtn4.getRawText());
    // Outer select: Default: Inner select: Default
    MsgSelectDefaultNode dn3 = (MsgSelectDefaultNode) sn3.getChild(1);
    assertEquals(4, dn3.numChildren());
    // Outer select: Default: Inner select: Default: Placeholder $person1
    MsgPlaceholderNode phn5 = (MsgPlaceholderNode) dn3.getChild(0);
    assertEquals("{$person1}", phn5.toSourceString());
    // Outer select: Default: Inner select: Default: RawText
    RawTextNode rtn5 = (RawTextNode) dn3.getChild(1);
    assertEquals(" put ", rtn5.getRawText());
    // Outer select: Default: Inner select: Default: Placeholder $person2
    MsgPlaceholderNode phn6 = (MsgPlaceholderNode) dn3.getChild(2);
    assertEquals("{$person2}", phn6.toSourceString());
    // Outer select: Default: Inner select: Default: RawText
    RawTextNode rtn6 = (RawTextNode) dn3.getChild(3);
    assertEquals(" and his friends to his circle.", rtn6.getRawText());
}
Also used : StandaloneNode(com.google.template.soy.soytree.SoyNode.StandaloneNode) MsgSelectDefaultNode(com.google.template.soy.soytree.MsgSelectDefaultNode) MsgFallbackGroupNode(com.google.template.soy.soytree.MsgFallbackGroupNode) MsgSelectNode(com.google.template.soy.soytree.MsgSelectNode) MsgSelectCaseNode(com.google.template.soy.soytree.MsgSelectCaseNode) RawTextNode(com.google.template.soy.soytree.RawTextNode) MsgNode(com.google.template.soy.soytree.MsgNode) MsgPlaceholderNode(com.google.template.soy.soytree.MsgPlaceholderNode) Test(org.junit.Test)

Example 7 with RawTextNode

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

the class HtmlRewritePassTest method testAttributes.

@Test
public void testAttributes() {
    TemplateNode node = runPass("<div class=\"foo\"></div>");
    assertThatSourceString(node).isEqualTo("<div class=\"foo\"></div>");
    String structure = "" + "HTML_OPEN_TAG_NODE\n" + "  RAW_TEXT_NODE\n" + "  HTML_ATTRIBUTE_NODE\n" + "    RAW_TEXT_NODE\n" + "    HTML_ATTRIBUTE_VALUE_NODE\n" + "      RAW_TEXT_NODE\n" + "HTML_CLOSE_TAG_NODE\n" + "  RAW_TEXT_NODE\n" + "";
    assertThatASTString(node).isEqualTo(structure);
    // test alternate quotation marks
    node = runPass("<div class='foo'></div>");
    assertThatSourceString(node).isEqualTo("<div class='foo'></div>");
    assertThatASTString(node).isEqualTo(structure);
    node = runPass("<div class=foo></div>");
    assertThatSourceString(node).isEqualTo("<div class=foo></div>");
    assertThatASTString(node).isEqualTo(structure);
    // This is a tricky case, according to the spec the '/' belongs to the attribute, not the tag
    node = runPass("<input class=foo/>");
    assertThatSourceString(node).isEqualTo("<input class=foo/>");
    HtmlOpenTagNode openTag = (HtmlOpenTagNode) node.getChild(0);
    assertThat(openTag.isSelfClosing()).isFalse();
    HtmlAttributeValueNode attributeValue = (HtmlAttributeValueNode) ((HtmlAttributeNode) openTag.getChild(1)).getChild(1);
    assertThat(attributeValue.getQuotes()).isEqualTo(HtmlAttributeValueNode.Quotes.NONE);
    assertThat(((RawTextNode) attributeValue.getChild(0)).getRawText()).isEqualTo("foo/");
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) RawTextNode(com.google.template.soy.soytree.RawTextNode) HtmlOpenTagNode(com.google.template.soy.soytree.HtmlOpenTagNode) HtmlAttributeValueNode(com.google.template.soy.soytree.HtmlAttributeValueNode) Test(org.junit.Test)

Example 8 with RawTextNode

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

the class SimplifyVisitorTest method testCombineConsecutiveRawTextNodes.

@Test
public void testCombineConsecutiveRawTextNodes() throws Exception {
    String soyCode = "{@param boo : ?}\n" + "blah{$boo}blah" + "{for $i in range(5)}" + "  blah{$boo}blah" + "{/for}";
    SoyFileSetNode soyTree = SoyFileSetParserBuilder.forTemplateContents(soyCode).parse().fileSet();
    TemplateNode template = soyTree.getChild(0).getChild(0);
    ForNonemptyNode forNode = (ForNonemptyNode) ((ForNode) template.getChild(3)).getChild(0);
    forNode.addChild(new RawTextNode(0, "bleh", SourceLocation.UNKNOWN));
    forNode.addChild(new RawTextNode(0, "bluh", SourceLocation.UNKNOWN));
    template.addChild(0, new RawTextNode(0, "bleh", SourceLocation.UNKNOWN));
    template.addChild(0, new RawTextNode(0, "bluh", SourceLocation.UNKNOWN));
    assertThat(template.numChildren()).isEqualTo(6);
    assertThat(forNode.numChildren()).isEqualTo(5);
    SimplifyVisitor simplifyVisitor = SimplifyVisitor.create();
    simplifyVisitor.simplify(soyTree, new TemplateRegistry(soyTree, ErrorReporter.exploding()));
    assertThat(template.numChildren()).isEqualTo(4);
    assertThat(forNode.numChildren()).isEqualTo(3);
    assertThat(((RawTextNode) template.getChild(0)).getRawText()).isEqualTo("bluhblehblah");
    assertThat(((RawTextNode) forNode.getChild(2)).getRawText()).isEqualTo("blahblehbluh");
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) TemplateRegistry(com.google.template.soy.soytree.TemplateRegistry) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) RawTextNode(com.google.template.soy.soytree.RawTextNode) ForNonemptyNode(com.google.template.soy.soytree.ForNonemptyNode) Test(org.junit.Test)

Example 9 with RawTextNode

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

the class CombineConsecutiveRawTextNodesPassTest method testPathologicalPerformance.

// There used to be a pathological performance issue when merging many raw text nodes, this stress
// test would have timed out under the old implementation but now succeeds quickly.
// Before the fix this test took > 2 minutes
// After the fix it was down to about 1.5s
@Test
public void testPathologicalPerformance() {
    String testFileContent = "{namespace boo}{template .foo}{/template}\n";
    ErrorReporter boom = ErrorReporter.exploding();
    SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(testFileContent).errorReporter(boom).parse().fileSet();
    TemplateNode template = soyTree.getChild(0).getChild(0);
    // Things like this like this could happen in templates with a large number of html tags (e.g.
    // in a literal block). since this is how they would be desugared.
    final int numCopies = 100_000;
    for (int i = 0; i < numCopies; i++) {
        template.addChild(new RawTextNode(0, "<", template.getSourceLocation()));
        template.addChild(new RawTextNode(0, "div", template.getSourceLocation()));
        template.addChild(new RawTextNode(0, " ", template.getSourceLocation()));
        template.addChild(new RawTextNode(0, "class", template.getSourceLocation()));
        template.addChild(new RawTextNode(0, "=", template.getSourceLocation()));
        template.addChild(new RawTextNode(0, "'", template.getSourceLocation()));
        template.addChild(new RawTextNode(0, "foo", template.getSourceLocation()));
        template.addChild(new RawTextNode(0, "'", template.getSourceLocation()));
        template.addChild(new RawTextNode(0, ">", template.getSourceLocation()));
    }
    new CombineConsecutiveRawTextNodesPass().run(soyTree);
    assertThat(template.numChildren()).isEqualTo(1);
    assertThat(((RawTextNode) template.getChild(0)).getRawText()).isEqualTo(Strings.repeat("<div class='foo'>", numCopies));
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) ErrorReporter(com.google.template.soy.error.ErrorReporter) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) RawTextNode(com.google.template.soy.soytree.RawTextNode) Point(com.google.template.soy.base.SourceLocation.Point) Test(org.junit.Test)

Example 10 with RawTextNode

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

the class CombineConsecutiveRawTextNodesPassTest method testCombineConsecutiveRawTextNodes.

@Test
public void testCombineConsecutiveRawTextNodes() {
    String testFileContent = "{namespace boo}\n" + "\n" + "/** @param goo */\n" + "{template .foo}\n" + "  Blah{$goo}blah\n" + "{/template}\n";
    ErrorReporter boom = ErrorReporter.exploding();
    SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(testFileContent).errorReporter(boom).parse().fileSet();
    TemplateNode template = (TemplateNode) SharedTestUtils.getNode(soyTree);
    template.addChild(new RawTextNode(0, "bleh", template.getSourceLocation()));
    template.addChild(new RawTextNode(0, "bluh", template.getSourceLocation()));
    assertThat(template.numChildren()).isEqualTo(5);
    new CombineConsecutiveRawTextNodesPass().run(soyTree);
    assertThat(template.numChildren()).isEqualTo(3);
    assertThat(((RawTextNode) template.getChild(0)).getRawText()).isEqualTo("Blah");
    assertThat(((RawTextNode) template.getChild(2)).getRawText()).isEqualTo("blahblehbluh");
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) ErrorReporter(com.google.template.soy.error.ErrorReporter) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) 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