Search in sources :

Example 1 with HtmlAttributeNode

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>
 * &lt;div key="test" /div&gt;
 * </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;
}
Also used : HtmlAttributeNode(com.google.template.soy.soytree.HtmlAttributeNode) HtmlAttributeValueNode(com.google.template.soy.soytree.HtmlAttributeValueNode) Nullable(javax.annotation.Nullable)

Example 2 with HtmlAttributeNode

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);
        }
    }
}
Also used : StandaloneNode(com.google.template.soy.soytree.SoyNode.StandaloneNode) AbstractSoyNodeVisitor(com.google.template.soy.soytree.AbstractSoyNodeVisitor) HtmlAttributeNode(com.google.template.soy.soytree.HtmlAttributeNode) Checkpoint(com.google.template.soy.error.ErrorReporter.Checkpoint) HtmlCloseTagNode(com.google.template.soy.soytree.HtmlCloseTagNode)

Example 3 with HtmlAttributeNode

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;
}
Also used : IfCondNode(com.google.template.soy.soytree.IfCondNode) HtmlAttributeNode(com.google.template.soy.soytree.HtmlAttributeNode) IfNode(com.google.template.soy.soytree.IfNode) PrintNode(com.google.template.soy.soytree.PrintNode) RawTextNode(com.google.template.soy.soytree.RawTextNode) HtmlAttributeValueNode(com.google.template.soy.soytree.HtmlAttributeValueNode)

Example 4 with HtmlAttributeNode

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);
}
Also used : HtmlAttributeNode(com.google.template.soy.soytree.HtmlAttributeNode)

Aggregations

HtmlAttributeNode (com.google.template.soy.soytree.HtmlAttributeNode)4 HtmlAttributeValueNode (com.google.template.soy.soytree.HtmlAttributeValueNode)2 Checkpoint (com.google.template.soy.error.ErrorReporter.Checkpoint)1 AbstractSoyNodeVisitor (com.google.template.soy.soytree.AbstractSoyNodeVisitor)1 HtmlCloseTagNode (com.google.template.soy.soytree.HtmlCloseTagNode)1 IfCondNode (com.google.template.soy.soytree.IfCondNode)1 IfNode (com.google.template.soy.soytree.IfNode)1 PrintNode (com.google.template.soy.soytree.PrintNode)1 RawTextNode (com.google.template.soy.soytree.RawTextNode)1 StandaloneNode (com.google.template.soy.soytree.SoyNode.StandaloneNode)1 Nullable (javax.annotation.Nullable)1