use of com.google.template.soy.soytree.HtmlAttributeNode in project closure-templates by google.
the class GenIncrementalDomCodeVisitor method maybeGetKeyNodeValue.
/**
* Gets the 'key' for an element to use in Incremental DOM to be used in the {@code
* incrementalDom.elementOpen} or {@code incrementalDom.elementVoid} calls.
*
* <pre>
* <div key="test" /div>
* </pre>
*
* generates
*
* <pre>
* incrementalDom.elementVoid('div', 'test')
* </pre>
*
* @param parentNode The SoyNode representing the parent.
* @return A string containing the JavaScript expression to retrieve the key, or null if the
* parent has no attribute child.
*/
@Nullable
private CodeChunk.WithValue maybeGetKeyNodeValue(HtmlOpenTagNode parentNode) {
// TODO(lukes): it seems like we should be able to support conditional keys
HtmlAttributeNode keyAttr = parentNode.getDirectAttributeNamed(KEY_ATTRIBUTE_NAME);
if (keyAttr != null) {
List<CodeChunk.WithValue> chunks = ImmutableList.of();
if (keyAttr.hasValue()) {
// TODO(lukes): add a dedicated method for this to HtmlAttributeNode? if there is a value
// it should _always_ be an HtmlAttributeValueNode
HtmlAttributeValueNode value = (HtmlAttributeValueNode) keyAttr.getChild(1);
// TODO(lukes): this limitation is arbitrary. fix it.
checkState(isComputableAsJsExprsVisitor.execOnChildren(value), "Attribute values that cannot be evalutated to simple expressions is not yet" + " supported for Incremental DOM code generation");
chunks = genJsExprsVisitor.execOnChildren(value);
}
// to be string (RawTextNode or PrintNode)
return CodeChunkUtils.concatChunksForceString(chunks);
}
return null;
}
use of com.google.template.soy.soytree.HtmlAttributeNode in project closure-templates by google.
the class HtmlRewritePass method run.
@Override
public void run(SoyFileNode file, IdGenerator nodeIdGen) {
new Visitor(nodeIdGen, file.getFilePath(), errorReporter).exec(file);
/*
* Validates that the only children of close tags can be {@code phname} attributes.
*
* <p>Later passes validate that phnames for close tags only appear in messages.
*/
for (HtmlCloseTagNode closeTag : SoyTreeUtils.getAllNodesOfType(file, HtmlCloseTagNode.class)) {
List<StandaloneNode> children = closeTag.getChildren();
HtmlAttributeNode phNameAttribute = closeTag.getDirectAttributeNamed(MessagePlaceholders.PHNAME_ATTR);
HtmlAttributeNode phExAttribute = closeTag.getDirectAttributeNamed(MessagePlaceholders.PHEX_ATTR);
// the child at index 0 is the tag name
for (int i = 1; i < children.size(); i++) {
StandaloneNode child = children.get(i);
if (child == phNameAttribute || child == phExAttribute) {
// the phname and phex attributes are validated later and allowed in close nodes
continue;
}
errorReporter.report(child.getSourceLocation(), UNEXPECTED_CLOSE_TAG_CONTENT);
}
}
}
use of com.google.template.soy.soytree.HtmlAttributeNode 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.HtmlAttributeNode in project closure-templates by google.
the class ContentSecurityPolicyNonceInjectionPass method isNonceableLink.
private boolean isNonceableLink(HtmlOpenTagNode tag) {
HtmlAttributeNode attr = tag.getDirectAttributeNamed("rel");
if (attr == null) {
return false;
}
String attrValue = attr.getStaticContent();
if (attrValue == null) {
return false;
}
return Ascii.equalsIgnoreCase("import", attrValue);
}
Aggregations