Search in sources :

Example 1 with HtmlOpenTagNode

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);
}
Also used : SourceLocation(com.google.template.soy.base.SourceLocation) IntegerNode(com.google.template.soy.exprtree.IntegerNode) FunctionNode(com.google.template.soy.exprtree.FunctionNode) CopyState(com.google.template.soy.basetree.CopyState) PrintNode(com.google.template.soy.soytree.PrintNode) NullNode(com.google.template.soy.exprtree.NullNode) HtmlOpenTagNode(com.google.template.soy.soytree.HtmlOpenTagNode)

Example 2 with HtmlOpenTagNode

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

Example 3 with HtmlOpenTagNode

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/");
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) RawTextNode(com.google.template.soy.soytree.RawTextNode) HtmlOpenTagNode(com.google.template.soy.soytree.HtmlOpenTagNode) HtmlAttributeValueNode(com.google.template.soy.soytree.HtmlAttributeValueNode) Test(org.junit.Test)

Example 4 with HtmlOpenTagNode

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();
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) HtmlOpenTagNode(com.google.template.soy.soytree.HtmlOpenTagNode) Test(org.junit.Test)

Example 5 with HtmlOpenTagNode

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));
        }
    }
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) SourceLocation(com.google.template.soy.base.SourceLocation) VarRefNode(com.google.template.soy.exprtree.VarRefNode) TemplateParam(com.google.template.soy.soytree.defn.TemplateParam) HtmlOpenTagNode(com.google.template.soy.soytree.HtmlOpenTagNode)

Aggregations

HtmlOpenTagNode (com.google.template.soy.soytree.HtmlOpenTagNode)5 TemplateNode (com.google.template.soy.soytree.TemplateNode)3 SourceLocation (com.google.template.soy.base.SourceLocation)2 Test (org.junit.Test)2 CopyState (com.google.template.soy.basetree.CopyState)1 FunctionNode (com.google.template.soy.exprtree.FunctionNode)1 IntegerNode (com.google.template.soy.exprtree.IntegerNode)1 NullNode (com.google.template.soy.exprtree.NullNode)1 VarRefNode (com.google.template.soy.exprtree.VarRefNode)1 HtmlAttributeValueNode (com.google.template.soy.soytree.HtmlAttributeValueNode)1 PrintNode (com.google.template.soy.soytree.PrintNode)1 RawTextNode (com.google.template.soy.soytree.RawTextNode)1 TemplateParam (com.google.template.soy.soytree.defn.TemplateParam)1