use of com.google.template.soy.soytree.RawTextNode in project closure-templates by google.
the class MsgUtils method buildMsgPartsForChildren.
// -----------------------------------------------------------------------------------------------
// Private helpers for building the list of message parts.
/**
* Builds the list of SoyMsgParts for all the children of a given parent node.
*
* @param parent Can be MsgNode, MsgPluralCaseNode, MsgPluralDefaultNode, MsgSelectCaseNode, or
* MsgSelectDefaultNode.
* @param msgNode The MsgNode containing 'parent'.
*/
private static ImmutableList<SoyMsgPart> buildMsgPartsForChildren(BlockNode parent, MsgNode msgNode) {
ImmutableList.Builder<SoyMsgPart> msgParts = ImmutableList.builder();
for (StandaloneNode child : parent.getChildren()) {
if (child instanceof RawTextNode) {
String rawText = ((RawTextNode) child).getRawText();
msgParts.add(SoyMsgRawTextPart.of(rawText));
} else if (child instanceof MsgPlaceholderNode) {
PlaceholderInfo placeholder = msgNode.getPlaceholder((MsgPlaceholderNode) child);
msgParts.add(new SoyMsgPlaceholderPart(placeholder.name(), placeholder.example()));
} else if (child instanceof MsgPluralNode) {
msgParts.add(buildMsgPartForPlural((MsgPluralNode) child, msgNode));
} else if (child instanceof MsgSelectNode) {
msgParts.add(buildMsgPartForSelect((MsgSelectNode) child, msgNode));
}
}
return msgParts.build();
}
use of com.google.template.soy.soytree.RawTextNode in project closure-templates by google.
the class MsgFuncGenerator method collectVarNameListAndToPyExprMap.
/**
* Private helper to process and collect all variables used within this msg node for code
* generation.
*
* @return A Map populated with all the variables used with in this message node, using {@link
* MsgPlaceholderInitialNode#genBasePhName}.
*/
private Map<PyExpr, PyExpr> collectVarNameListAndToPyExprMap() {
Map<PyExpr, PyExpr> nodePyVarToPyExprMap = new LinkedHashMap<>();
for (Map.Entry<String, MsgSubstUnitNode> entry : msgNode.getVarNameToRepNodeMap().entrySet()) {
MsgSubstUnitNode substUnitNode = entry.getValue();
PyExpr substPyExpr = null;
if (substUnitNode instanceof MsgPlaceholderNode) {
SoyNode phInitialNode = ((AbstractParentSoyNode<?>) substUnitNode).getChild(0);
if (phInitialNode instanceof PrintNode || phInitialNode instanceof CallNode || phInitialNode instanceof RawTextNode) {
substPyExpr = PyExprUtils.concatPyExprs(genPyExprsVisitor.exec(phInitialNode)).toPyString();
}
// when the placeholder is generated by HTML tags
if (phInitialNode instanceof MsgHtmlTagNode) {
substPyExpr = PyExprUtils.concatPyExprs(genPyExprsVisitor.execOnChildren((ParentSoyNode<?>) phInitialNode)).toPyString();
}
} else if (substUnitNode instanceof MsgPluralNode) {
// Translates {@link MsgPluralNode#pluralExpr} into a Python lookup expression.
// Note that {@code pluralExpr} represents the soy expression of the {@code plural} attr,
// i.e. the {@code $numDrafts} in {@code {plural $numDrafts}...{/plural}}.
substPyExpr = translateToPyExprVisitor.exec(((MsgPluralNode) substUnitNode).getExpr());
} else if (substUnitNode instanceof MsgSelectNode) {
substPyExpr = translateToPyExprVisitor.exec(((MsgSelectNode) substUnitNode).getExpr());
}
if (substPyExpr != null) {
nodePyVarToPyExprMap.put(new PyStringExpr("'" + entry.getKey() + "'"), substPyExpr);
}
}
return nodePyVarToPyExprMap;
}
use of com.google.template.soy.soytree.RawTextNode in project closure-templates by google.
the class ContentSecurityPolicyNonceInjectionPass method createCspInjection.
/**
* Generates an AST fragment that looks like: {if $ij.csp_nonce}nonce="{$ij.csp_nonce}"{/if}
*
* @param insertionLocation The location where it is being inserted
* @param nodeIdGen The id generator to use
*/
private static IfNode createCspInjection(SourceLocation insertionLocation, IdGenerator nodeIdGen) {
IfNode ifNode = new IfNode(nodeIdGen.genId(), insertionLocation);
IfCondNode ifCondNode = new IfCondNode(nodeIdGen.genId(), insertionLocation, "if", referenceCspNonce(insertionLocation));
ifNode.addChild(ifCondNode);
HtmlAttributeNode nonceAttribute = new HtmlAttributeNode(nodeIdGen.genId(), insertionLocation, insertionLocation.getBeginPoint());
ifCondNode.addChild(nonceAttribute);
nonceAttribute.addChild(new RawTextNode(nodeIdGen.genId(), "nonce", insertionLocation));
HtmlAttributeValueNode attributeValue = new HtmlAttributeValueNode(nodeIdGen.genId(), insertionLocation, HtmlAttributeValueNode.Quotes.DOUBLE);
nonceAttribute.addChild(attributeValue);
// NOTE: we do not need to insert any print directives here, unlike the old implementation since
// we are running before the autoescaper, so the escaper should insert whatever print directives
// are appropriate.
PrintNode printNode = new PrintNode(nodeIdGen.genId(), insertionLocation, // Implicit. {$ij.csp_nonce} not {print $ij.csp_nonce}
true, /* isImplicit= */
referenceCspNonce(insertionLocation), /* attributes= */
ImmutableList.of(), ErrorReporter.exploding());
attributeValue.addChild(printNode);
return ifNode;
}
use of com.google.template.soy.soytree.RawTextNode in project closure-templates by google.
the class RawTextBuilder method build.
RawTextNode build() {
maybeFinishBasic();
String text = buffer.toString();
RawTextNode.SourceOffsets sourceOffsets = offsets.build(text.length(), discontinuityReason);
return new RawTextNode(nodeIdGen.genId(), text, sourceOffsets.getSourceLocation(fileName), sourceOffsets);
}
use of com.google.template.soy.soytree.RawTextNode in project closure-templates by google.
the class SourceLocationTest method testRawTextSourceLocations.
@Test
public void testRawTextSourceLocations() throws Exception {
// RawTextNode has some special methods to calculating the source location of characters within
// the strings, test those
String template = JOINER.join("{namespace ns}", "{template .foo}", " Hello,{sp}", " {\\n}{nil}<span>Bob</span>", " // and end of line comment", " !", " What's /*hello comment world*/up?", "{/template}", "");
RawTextNode rawText = (RawTextNode) SoyFileSetParserBuilder.forFileContents(template).parse().fileSet().getChild(0).getChild(0).getChild(0);
assertThat(rawText.getRawText()).isEqualTo("Hello, \n<span>Bob</span>! What's up?");
assertThat(rawText.getRawText().substring(0, 5)).isEqualTo("Hello");
SourceLocation loc = rawText.substringLocation(0, 5);
assertThat(loc.getBeginLine()).isEqualTo(3);
assertThat(loc.getBeginColumn()).isEqualTo(3);
assertThat(loc.getEndLine()).isEqualTo(3);
assertThat(loc.getEndColumn()).isEqualTo(7);
assertThat(rawText.getRawText().substring(8, 14)).isEqualTo("<span>");
loc = rawText.substringLocation(8, 14);
assertThat(loc.getBeginLine()).isEqualTo(4);
assertThat(loc.getBeginColumn()).isEqualTo(12);
assertThat(loc.getEndLine()).isEqualTo(4);
assertThat(loc.getEndColumn()).isEqualTo(17);
assertThat(rawText.getRawText().substring(24, 25)).isEqualTo("!");
loc = rawText.substringLocation(24, 25);
assertThat(loc.getBeginLine()).isEqualTo(6);
assertThat(loc.getBeginColumn()).isEqualTo(3);
assertThat(loc.getEndLine()).isEqualTo(6);
assertThat(loc.getEndColumn()).isEqualTo(3);
assertThat(rawText.getRawText().substring(33, 36)).isEqualTo("up?");
loc = rawText.substringLocation(33, 36);
assertThat(loc.getBeginLine()).isEqualTo(7);
assertThat(loc.getBeginColumn()).isEqualTo(33);
assertThat(loc.getEndLine()).isEqualTo(7);
assertThat(loc.getEndColumn()).isEqualTo(35);
// doesn't matter
final int id = 1337;
RawTextNode subStringNode = rawText.substring(id, 0, 5);
assertThat(subStringNode.getRawText()).isEqualTo("Hello");
loc = subStringNode.getSourceLocation();
assertThat(loc.getBeginLine()).isEqualTo(3);
assertThat(loc.getBeginColumn()).isEqualTo(3);
assertThat(loc.getEndLine()).isEqualTo(3);
assertThat(loc.getEndColumn()).isEqualTo(7);
subStringNode = rawText.substring(id, 24, 25);
assertThat(subStringNode.getRawText()).isEqualTo("!");
loc = subStringNode.getSourceLocation();
assertThat(loc.getBeginLine()).isEqualTo(6);
assertThat(loc.getBeginColumn()).isEqualTo(3);
assertThat(loc.getEndLine()).isEqualTo(6);
assertThat(loc.getEndColumn()).isEqualTo(3);
// Can't create empty raw text nodes.
try {
rawText.substring(id, 24, 24);
fail();
} catch (IllegalArgumentException expected) {
}
try {
rawText.substring(id, 24, 23);
fail();
} catch (IllegalArgumentException expected) {
}
try {
rawText.substring(id, 24, Integer.MAX_VALUE);
fail();
} catch (IllegalArgumentException expected) {
}
}
Aggregations