use of com.google.template.soy.soytree.HtmlOpenTagNode 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.HtmlOpenTagNode in project closure-templates by google.
the class HtmlContextVisitor method visitHtmlCloseTagNode.
@Override
protected void visitHtmlCloseTagNode(HtmlCloseTagNode node) {
if (!node.getTagName().isStatic()) {
errorReporter.report(node.getTagName().getTagLocation(), DYNAMIC_TAG_NAME);
return;
}
pushState(HtmlContext.HTML_TAG_NAME);
visitChildren(node);
popState();
boolean tagMatches = false;
// When encountering a closing tag, need to pop off any unclosed tags.
while (!openTagStack.isEmpty() && !tagMatches) {
HtmlOpenTagNode htmlOpenTagNode = openTagStack.pop();
tagMatches = htmlOpenTagNode.getTagName().getStaticTagNameAsLowerCase().equals(node.getTagName().getStaticTagNameAsLowerCase());
}
}
use of com.google.template.soy.soytree.HtmlOpenTagNode 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/");
}
use of com.google.template.soy.soytree.HtmlOpenTagNode in project closure-templates by google.
the class HtmlRewritePassTest method testUnquotedAttributeValue.
@Test
public void testUnquotedAttributeValue() {
TemplateNode node = runPass("<img class=foo />");
assertThat(((HtmlOpenTagNode) node.getChild(0)).isSelfClosing()).isTrue();
node = runPass("<img class=foo/>");
assertThat(((HtmlOpenTagNode) node.getChild(0)).isSelfClosing()).isFalse();
node = runPass("<img class/>");
assertThat(((HtmlOpenTagNode) node.getChild(0)).isSelfClosing()).isTrue();
}
use of com.google.template.soy.soytree.HtmlOpenTagNode in project closure-templates by google.
the class ContentSecurityPolicyNonceInjectionPass method run.
@Override
public void run(SoyFileNode file, IdGenerator nodeIdGen) {
// * $ij references
for (TemplateNode template : file.getChildren()) {
for (TemplateParam param : template.getAllParams()) {
if (param.isInjected() && param.name().equals(CSP_NONCE_VARIABLE_NAME)) {
errorReporter.report(param.nameLocation(), IJ_CSP_NONCE_REFERENCE);
}
}
}
for (VarRefNode var : SoyTreeUtils.getAllNodesOfType(file, VarRefNode.class)) {
// of isInjected
if (var.isDollarSignIjParameter() && var.getName().equals(CSP_NONCE_VARIABLE_NAME)) {
errorReporter.report(var.getSourceLocation(), IJ_CSP_NONCE_REFERENCE);
}
}
for (HtmlOpenTagNode openTag : SoyTreeUtils.getAllNodesOfType(file, HtmlOpenTagNode.class)) {
if (isTagNonceable(openTag)) {
// this should point to the character immediately before the '>' or '/>' at the end of the
// open tag
SourceLocation insertionLocation = openTag.getSourceLocation().getEndPoint().offset(0, openTag.isSelfClosing() ? -2 : -1).asLocation(openTag.getSourceLocation().getFilePath());
openTag.addChild(createCspInjection(insertionLocation, nodeIdGen));
}
}
}
Aggregations