Search in sources :

Example 21 with Node

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

the class PageLexerTest method assertOnlyText.

private static void assertOnlyText(String code) {
    StringReader reader = new StringReader(code);
    List<Node> nodeList = new PageLexer().parse(reader);
    assertThat(nodeList).isNotEmpty().allMatch(node -> node.getNodeType() == NodeType.TEXT);
}
Also used : CommentNode(org.sonar.plugins.html.node.CommentNode) Node(org.sonar.plugins.html.node.Node) TextNode(org.sonar.plugins.html.node.TextNode) DirectiveNode(org.sonar.plugins.html.node.DirectiveNode) TagNode(org.sonar.plugins.html.node.TagNode) StringReader(java.io.StringReader)

Example 22 with Node

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

the class PageLexerTest method testLexer.

@Test
public void testLexer() throws FileNotFoundException {
    String fileName = "src/test/resources/src/main/webapp/create-salesorder.xhtml";
    PageLexer lexer = new PageLexer();
    List<Node> nodeList = lexer.parse(new FileReader(fileName));
    assertThat(nodeList.size()).isGreaterThan(50);
    // check tagnodes
    for (Node node : nodeList) {
        if (node instanceof TagNode) {
            assertThat(node.getCode()).startsWith("<");
            assertThat(node.getCode()).endsWith(">");
        }
    }
    showHierarchy(nodeList);
    // check hierarchy
    for (Node node : nodeList) {
        if (node instanceof TagNode) {
            TagNode tagNode = (TagNode) node;
            if (!tagNode.isEndElement()) {
                if (tagNode.equalsElementName("define")) {
                    assertThat(tagNode.getChildren()).as("Tag should have children: " + tagNode.getCode()).isNotEmpty();
                } else if (tagNode.equalsElementName("outputText")) {
                    assertThat(tagNode.getChildren()).isEmpty();
                }
            }
        }
    }
}
Also used : CommentNode(org.sonar.plugins.html.node.CommentNode) Node(org.sonar.plugins.html.node.Node) TextNode(org.sonar.plugins.html.node.TextNode) DirectiveNode(org.sonar.plugins.html.node.DirectiveNode) TagNode(org.sonar.plugins.html.node.TagNode) FileReader(java.io.FileReader) TagNode(org.sonar.plugins.html.node.TagNode) Test(org.junit.Test)

Example 23 with Node

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

the class PageLexerTest method testNestedTagInValue.

@Test
public void testNestedTagInValue() {
    String fragment = "<td label=\"Hello <c:if test='${param == true}'>World</c:if>\">Type</td>";
    StringReader reader = new StringReader(fragment);
    PageLexer lexer = new PageLexer();
    List<Node> nodeList = lexer.parse(reader);
    assertThat(nodeList).hasSize(3);
    assertThat(nodeList.get(0)).isInstanceOf(TagNode.class);
    assertThat(nodeList.get(1)).isInstanceOf(TextNode.class);
    assertThat(nodeList.get(2)).isInstanceOf(TagNode.class);
    TagNode tagNode = (TagNode) nodeList.get(0);
    assertThat(tagNode.getAttributes()).hasSize(1);
}
Also used : CommentNode(org.sonar.plugins.html.node.CommentNode) Node(org.sonar.plugins.html.node.Node) TextNode(org.sonar.plugins.html.node.TextNode) DirectiveNode(org.sonar.plugins.html.node.DirectiveNode) TagNode(org.sonar.plugins.html.node.TagNode) StringReader(java.io.StringReader) TagNode(org.sonar.plugins.html.node.TagNode) Test(org.junit.Test)

Example 24 with Node

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

the class PageLexerTest method showHierarchy.

private void showHierarchy(List<Node> nodeList) {
    StringBuilder sb = new StringBuilder();
    for (Node node : nodeList) {
        if (node.getClass() == TagNode.class && ((TagNode) node).getParent() == null) {
            TagNode root = (TagNode) node;
            printTag(sb, root, 0);
        // System.out.print(sb.toString());
        }
    }
}
Also used : CommentNode(org.sonar.plugins.html.node.CommentNode) Node(org.sonar.plugins.html.node.Node) TextNode(org.sonar.plugins.html.node.TextNode) DirectiveNode(org.sonar.plugins.html.node.DirectiveNode) TagNode(org.sonar.plugins.html.node.TagNode) TagNode(org.sonar.plugins.html.node.TagNode)

Example 25 with Node

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

the class PageLexerTest method testComment.

@Test
public void testComment() {
    String fragment = "<!-- text --><p>aaa</p>";
    StringReader reader = new StringReader(fragment);
    PageLexer lexer = new PageLexer();
    List<Node> nodeList = lexer.parse(reader);
    assertThat(nodeList).hasSize(4);
    assertThat(nodeList.get(0)).isInstanceOf(CommentNode.class);
}
Also used : CommentNode(org.sonar.plugins.html.node.CommentNode) Node(org.sonar.plugins.html.node.Node) TextNode(org.sonar.plugins.html.node.TextNode) DirectiveNode(org.sonar.plugins.html.node.DirectiveNode) TagNode(org.sonar.plugins.html.node.TagNode) StringReader(java.io.StringReader) Test(org.junit.Test)

Aggregations

Node (org.sonar.plugins.html.node.Node)39 Test (org.junit.Test)28 TagNode (org.sonar.plugins.html.node.TagNode)24 StringReader (java.io.StringReader)23 TextNode (org.sonar.plugins.html.node.TextNode)21 CommentNode (org.sonar.plugins.html.node.CommentNode)19 DirectiveNode (org.sonar.plugins.html.node.DirectiveNode)19 FileReader (java.io.FileReader)4 Attribute (org.sonar.plugins.html.node.Attribute)3 HtmlSourceCode (org.sonar.plugins.html.visitor.HtmlSourceCode)3 ArrayList (java.util.ArrayList)2 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)2 CodeReader (org.sonar.channel.CodeReader)2 File (java.io.File)1 Reader (java.io.Reader)1 ArrayDeque (java.util.ArrayDeque)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 InputFile (org.sonar.api.batch.fs.InputFile)1 NoSonarFilter (org.sonar.api.issue.NoSonarFilter)1