Search in sources :

Example 6 with TagNode

use of org.sonar.plugins.html.node.TagNode in project sonar-web by SonarSource.

the class CdataTokenizer method createNode.

@Override
Node createNode() {
    TagNode node = new TagNode();
    node.setNodeName("![CDATA[");
    return node;
}
Also used : TagNode(org.sonar.plugins.html.node.TagNode)

Example 7 with TagNode

use of org.sonar.plugins.html.node.TagNode in project sonar-web by SonarSource.

the class ElementTokenizer method parseNestedTag.

/**
 * Parse a nested tag with PageLexer.
 * The nested tag is added as an attribute to its parent element.
 */
private static void parseNestedTag(CodeReader codeReader, TagNode element) {
    PageLexer nestedPageLexer = new PageLexer();
    List<Node> nodeList = nestedPageLexer.nestedParse(codeReader);
    // add the nested tags as attribute.
    for (Node node : nodeList) {
        element.getAttributes().add(new Attribute(node.getCode()));
    }
}
Also used : Attribute(org.sonar.plugins.html.node.Attribute) Node(org.sonar.plugins.html.node.Node) TagNode(org.sonar.plugins.html.node.TagNode)

Example 8 with TagNode

use of org.sonar.plugins.html.node.TagNode in project sonar-web by SonarSource.

the class VueLexer method parse.

@Override
public List<Node> parse(Reader reader) {
    List<Node> nodes = super.parse(reader);
    boolean firstTemplateTag = true;
    List<Node> templateNodes = new LinkedList<>();
    Deque<Object> templateLevels = new LinkedList<>();
    for (Node node : nodes) {
        if (node.getNodeType() == NodeType.TAG) {
            TagNode tagNode = (TagNode) node;
            if (tagNode.equalsElementName(TEMPLATE)) {
                if (tagNode.isEndElement()) {
                    if (!templateLevels.isEmpty()) {
                        templateLevels.pop();
                        if (templateLevels.isEmpty()) {
                            break;
                        }
                    }
                } else if (!tagNode.hasEnd()) {
                    templateLevels.push(TEMPLATE_LEVEL);
                }
            }
        }
        if (!templateLevels.isEmpty()) {
            if (firstTemplateTag) {
                firstTemplateTag = false;
            } else {
                templateNodes.add(node);
            }
        }
    }
    return templateNodes;
}
Also used : Node(org.sonar.plugins.html.node.Node) TagNode(org.sonar.plugins.html.node.TagNode) LinkedList(java.util.LinkedList) TagNode(org.sonar.plugins.html.node.TagNode)

Example 9 with TagNode

use of org.sonar.plugins.html.node.TagNode in project sonar-web by SonarSource.

the class UnclosedTagCheck method endElement.

@Override
public void endElement(TagNode element) {
    if (skipFile) {
        return;
    }
    if (isNotIgnoreTag(element) && !nodes.isEmpty()) {
        TagNode previousNode = nodes.remove(0);
        if (!previousNode.getNodeName().equals(element.getNodeName())) {
            createViolation(previousNode, "The tag \"" + previousNode.getNodeName() + "\" has no corresponding closing tag.");
            List<TagNode> rollup = new ArrayList<>();
            for (TagNode node : nodes) {
                rollup.add(node);
                if (node.getNodeName().equals(element.getNodeName())) {
                    nodes.removeAll(rollup);
                    break;
                }
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) TagNode(org.sonar.plugins.html.node.TagNode)

Example 10 with TagNode

use of org.sonar.plugins.html.node.TagNode in project sonar-web by SonarSource.

the class PageLexer method createNodeHierarchy.

/**
 * Scan the nodes and build the hierarchy of parent and child nodes.
 */
private static void createNodeHierarchy(List<Node> nodeList) {
    Deque<TagNode> openElementStack = new ArrayDeque<>();
    for (Node node : nodeList) {
        if (node.getNodeType() != NodeType.TAG) {
            continue;
        }
        TagNode element = (TagNode) node;
        // start element
        if (!element.isEndElement()) {
            TagNode parent = openElementStack.peek();
            while (parent != null && (shouldCloseParent(nodeName(element), nodeName(parent)) || isVoidElement(parent))) {
                openElementStack.pop();
                parent = openElementStack.peek();
            }
            element.setParent(parent);
            openElementStack.push(element);
        }
        // end element
        if (isEndElement(element) && !openElementStack.isEmpty()) {
            TagNode openElement = openElementStack.peek();
            if (openElement.equalsElementName(element.getNodeName())) {
                openElementStack.pop();
            } else {
                // non-well formed, close HTML elements if there is matching open element
                if (openElementStack.stream().anyMatch(tag -> tag.equalsElementName(element.getNodeName()))) {
                    while (!openElement.equalsElementName(element.getNodeName()) && isHtmlElement(openElement)) {
                        openElement = openElementStack.pop();
                    }
                }
            }
        }
    }
}
Also used : Node(org.sonar.plugins.html.node.Node) TagNode(org.sonar.plugins.html.node.TagNode) TagNode(org.sonar.plugins.html.node.TagNode) ArrayDeque(java.util.ArrayDeque)

Aggregations

TagNode (org.sonar.plugins.html.node.TagNode)22 Node (org.sonar.plugins.html.node.Node)15 CommentNode (org.sonar.plugins.html.node.CommentNode)12 DirectiveNode (org.sonar.plugins.html.node.DirectiveNode)12 TextNode (org.sonar.plugins.html.node.TextNode)12 Test (org.junit.Test)10 StringReader (java.io.StringReader)9 Attribute (org.sonar.plugins.html.node.Attribute)3 FileReader (java.io.FileReader)2 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 ArrayDeque (java.util.ArrayDeque)1 HashMap (java.util.HashMap)1 List (java.util.List)1 CodeReader (org.sonar.channel.CodeReader)1