use of com.google.template.soy.soytree.RawTextNode 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()));
}
use of com.google.template.soy.soytree.RawTextNode 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");
}
use of com.google.template.soy.soytree.RawTextNode 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");
}
Aggregations