Search in sources :

Example 1 with TagNode

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

the class PageLexerTest method testAttributeWithoutQuotes.

@Test
public void testAttributeWithoutQuotes() {
    final StringReader reader = new StringReader("<img src=http://foo/sfds?sjg a=1\tb=2\r\nc=3 />");
    final PageLexer lexer = new PageLexer();
    final List<Node> nodeList = lexer.parse(reader);
    assertThat(nodeList).hasSize(1);
    assertThat(nodeList.get(0)).isInstanceOf(TagNode.class);
    final TagNode node = (TagNode) nodeList.get(0);
    assertThat(node.getAttributes()).hasSize(4);
    final Attribute attribute = node.getAttributes().get(0);
    assertThat(attribute.getName()).isEqualTo("src");
    assertThat(attribute.getValue()).isEqualTo("http://foo/sfds?sjg");
    final Attribute attributeA = node.getAttributes().get(1);
    assertThat(attributeA.getName()).isEqualTo("a");
    assertThat(attributeA.getValue()).isEqualTo("1");
    final Attribute attributeB = node.getAttributes().get(2);
    assertThat(attributeB.getName()).isEqualTo("b");
    assertThat(attributeB.getValue()).isEqualTo("2");
    final Attribute attributeC = node.getAttributes().get(3);
    assertThat(attributeC.getName()).isEqualTo("c");
    assertThat(attributeC.getValue()).isEqualTo("3");
}
Also used : Attribute(org.sonar.plugins.html.node.Attribute) 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 2 with TagNode

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

the class PageLexerTest method assertNodes.

private static void assertNodes(TagNode actual, TestNode expected) {
    assertThat(actual.getNodeName()).isEqualTo(expected.nodeName);
    assertThat(actual.getChildren()).as(actual.getNodeName() + " children:").hasSize(expected.children.length);
    int i = 0;
    for (TagNode child : actual.getChildren()) {
        assertNodes(child, expected.children[i]);
        i++;
    }
}
Also used : TagNode(org.sonar.plugins.html.node.TagNode)

Example 3 with TagNode

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

the class PageLexerTest method should_recover_on_invalid_attribute.

@Test
public void should_recover_on_invalid_attribute() {
    PageLexer lexer = new PageLexer();
    List<Node> nodes = lexer.parse(new StringReader("<foo = bar=42>"));
    assertThat(nodes).hasSize(1);
    assertThat(((TagNode) nodes.get(0)).getAttribute("bar")).isEqualTo("42");
}
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 4 with TagNode

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

the class PageLexerTest method attribute_value_starting_with_quote.

@Test
public void attribute_value_starting_with_quote() {
    StringReader reader = new StringReader("<img src=\"'a'\"/>");
    List<Node> nodeList = new PageLexer().parse(reader);
    assertThat(nodeList).hasSize(1);
    assertThat(nodeList.get(0)).isInstanceOf(TagNode.class);
    TagNode node = (TagNode) nodeList.get(0);
    Attribute attribute = node.getAttributes().get(0);
    assertThat(attribute.getName()).isEqualTo("src");
    assertThat(attribute.getValue()).isEqualTo("'a'");
}
Also used : Attribute(org.sonar.plugins.html.node.Attribute) 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 5 with TagNode

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

the class TableHeaderReferenceCheck method raiseViolationOnInvalidReference.

private void raiseViolationOnInvalidReference(Table table) {
    Map<TagNode, List<String>> referenceableHeaders = table.findReferenceableHeadersPerCellNode();
    Map<TagNode, List<String>> raisedFor = new HashMap<>();
    table.forEachCell((cell, row, column) -> {
        TagNode node = cell.node();
        List<String> actual = cell.headers();
        List<String> expected = referenceableHeaders.getOrDefault(node, Collections.emptyList());
        for (String header : actual) {
            if (!expected.contains(header) && !raisedFor.getOrDefault(node, Collections.emptyList()).contains(header)) {
                if (isExistingHeader(table, header)) {
                    createViolation(node, format("id \"%s\" in \"headers\" reference the header of another column/row.", header));
                } else {
                    createViolation(node, format("id \"%s\" in \"headers\" does not reference any <th> header.", header));
                }
                raisedFor.merge(node, Arrays.asList(header), (acc, val) -> {
                    acc.addAll(val);
                    return acc;
                });
                break;
            }
        }
    });
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) TagNode(org.sonar.plugins.html.node.TagNode)

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