use of com.google.template.soy.soytree.PrintNode in project closure-templates by google.
the class VeLogInstrumentationVisitor method visitVeLogNode.
/**
* Adds data-soylog attribute to the top-level DOM node in this {velog} block.
*/
@Override
protected void visitVeLogNode(VeLogNode node) {
// VeLogValidationPass enforces that the first child is a open tag. We can safely cast it here.
HtmlOpenTagNode tag = (HtmlOpenTagNode) node.getChild(0);
SourceLocation insertionLocation = tag.getSourceLocation().getEndPoint().offset(0, tag.isSelfClosing() ? -2 : -1).asLocation(tag.getSourceLocation().getFilePath());
FunctionNode funcNode = new FunctionNode(VeLogFunction.INSTANCE, insertionLocation);
funcNode.addChild(new IntegerNode(node.getLoggingId(), insertionLocation));
funcNode.addChild(node.getConfigExpression() == null ? new NullNode(insertionLocation) : node.getConfigExpression().copy(new CopyState()));
if (node.getLogonlyExpression() != null) {
funcNode.addChild(node.getLogonlyExpression().copy(new CopyState()));
}
PrintNode attributeNode = new PrintNode(nodeIdGen.genId(), insertionLocation, /* isImplicit= */
true, /* expr= */
funcNode, /* attributes= */
ImmutableList.of(), ErrorReporter.exploding());
tag.addChild(attributeNode);
visitChildren(node);
}
use of com.google.template.soy.soytree.PrintNode 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.PrintNode 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.PrintNode in project closure-templates by google.
the class TemplateParserTest method testParseLogStmt.
@Test
public void testParseLogStmt() throws Exception {
String templateBody = "{@param foo : ?}\n" + "{log}Blah {$foo}.{/log}";
List<StandaloneNode> nodes = parseTemplateContent(templateBody, FAIL).getChildren();
assertEquals(1, nodes.size());
LogNode logNode = (LogNode) nodes.get(0);
assertEquals(3, logNode.numChildren());
assertEquals("Blah ", ((RawTextNode) logNode.getChild(0)).getRawText());
assertEquals("$foo", ((PrintNode) logNode.getChild(1)).getExpr().toSourceString());
}
use of com.google.template.soy.soytree.PrintNode in project closure-templates by google.
the class TemplateParserTest method testParseMsgStmt.
@Test
public void testParseMsgStmt() throws Exception {
String templateBody = "{@param usedMb :?}{@param learnMoreUrl :?}\n" + " {msg desc=\"Tells user's quota usage.\"}\n" + " You're currently using {$usedMb} MB of your quota.{sp}\n" + " <a href=\"{$learnMoreUrl}\">Learn more</A>\n" + " <br /><br />\n" + " {/msg}\n" + " {msg meaning=\"noun\" desc=\"\" hidden=\"true\"}Archive{/msg}\n" + " {msg meaning=\"noun\" desc=\"The archive (noun).\"}Archive{/msg}\n" + " {msg meaning=\"verb\" desc=\"\"}Archive{/msg}\n" + " {msg desc=\"\"}Archive{/msg}\n";
List<StandaloneNode> nodes = parseTemplateContent(templateBody, FAIL).getChildren();
assertEquals(5, nodes.size());
MsgNode mn0 = ((MsgFallbackGroupNode) nodes.get(0)).getMsg();
assertEquals("Tells user's quota usage.", mn0.getDesc());
assertEquals(null, mn0.getMeaning());
assertEquals(false, mn0.isHidden());
assertEquals(8, mn0.numChildren());
assertEquals("You're currently using ", ((RawTextNode) mn0.getChild(0)).getRawText());
MsgPlaceholderNode mpn1 = (MsgPlaceholderNode) mn0.getChild(1);
assertEquals("$usedMb", ((PrintNode) mpn1.getChild(0)).getExpr().toSourceString());
assertEquals(" MB of your quota. ", ((RawTextNode) mn0.getChild(2)).getRawText());
MsgPlaceholderNode mpn3 = (MsgPlaceholderNode) mn0.getChild(3);
MsgHtmlTagNode mhtn3 = (MsgHtmlTagNode) mpn3.getChild(0);
assertEquals("a", mhtn3.getLcTagName());
assertEquals("START_LINK", mhtn3.genBasePhName());
assertEquals("<a href=\"{$learnMoreUrl}\">", mhtn3.toSourceString());
assertEquals(3, mhtn3.numChildren());
assertEquals("<a href=\"", ((RawTextNode) mhtn3.getChild(0)).getRawText());
assertEquals("$learnMoreUrl", ((PrintNode) mhtn3.getChild(1)).getExpr().toSourceString());
assertEquals("\">", ((RawTextNode) mhtn3.getChild(2)).getRawText());
assertEquals("Learn more", ((RawTextNode) mn0.getChild(4)).getRawText());
MsgPlaceholderNode mpn5 = (MsgPlaceholderNode) mn0.getChild(5);
MsgHtmlTagNode mhtn5 = (MsgHtmlTagNode) mpn5.getChild(0);
assertEquals("/a", mhtn5.getLcTagName());
assertEquals("END_LINK", mhtn5.genBasePhName());
assertEquals("</A>", mhtn5.toSourceString());
MsgPlaceholderNode mpn6 = (MsgPlaceholderNode) mn0.getChild(6);
MsgHtmlTagNode mhtn6 = (MsgHtmlTagNode) mpn6.getChild(0);
assertEquals("BREAK", mhtn6.genBasePhName());
assertTrue(mpn6.shouldUseSameVarNameAs((MsgPlaceholderNode) mn0.getChild(7)));
assertFalse(mpn6.shouldUseSameVarNameAs(mpn5));
assertFalse(mpn5.shouldUseSameVarNameAs(mpn3));
MsgFallbackGroupNode mfgn1 = (MsgFallbackGroupNode) nodes.get(1);
assertEquals("{msg meaning=\"noun\" desc=\"\" hidden=\"true\"}Archive{/msg}", mfgn1.toSourceString());
MsgNode mn1 = mfgn1.getMsg();
assertEquals("", mn1.getDesc());
assertEquals("noun", mn1.getMeaning());
assertEquals(true, mn1.isHidden());
assertEquals(1, mn1.numChildren());
assertEquals("Archive", ((RawTextNode) mn1.getChild(0)).getRawText());
}
Aggregations