Search in sources :

Example 1 with Attribute

use of org.sonar.plugins.html.node.Attribute 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 Attribute

use of org.sonar.plugins.html.node.Attribute 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 3 with Attribute

use of org.sonar.plugins.html.node.Attribute 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 4 with Attribute

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

the class DoctypeTokenizer method parseToken.

private static void parseToken(DirectiveNode node) {
    String code = node.getCode();
    StreamTokenizer tokenizer = new StreamTokenizer(new StringReader(code));
    tokenizer.quoteChar('"');
    try {
        while (tokenizer.nextToken() != StreamTokenizer.TT_EOF) {
            if (tokenizer.sval != null) {
                if (node.getNodeName() == null) {
                    node.setNodeName(tokenizer.sval);
                } else {
                    node.getAttributes().add(new Attribute(tokenizer.sval));
                }
            }
        }
    } catch (IOException e) {
    // ignore
    }
}
Also used : Attribute(org.sonar.plugins.html.node.Attribute) StringReader(java.io.StringReader) IOException(java.io.IOException) StreamTokenizer(java.io.StreamTokenizer)

Example 5 with Attribute

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

the class ElementTokenizer method handleBeforeAttributeName.

private static void handleBeforeAttributeName(CodeReader codeReader, TagNode element) {
    Attribute attribute;
    StringBuilder sbQName = new StringBuilder();
    codeReader.popTo(endQNameMatcher, sbQName);
    attribute = new Attribute(sbQName.toString().trim());
    attribute.setLine(codeReader.getLinePosition() + element.getStartLinePosition() - 1);
    element.getAttributes().add(attribute);
}
Also used : Attribute(org.sonar.plugins.html.node.Attribute)

Aggregations

Attribute (org.sonar.plugins.html.node.Attribute)7 StringReader (java.io.StringReader)3 Node (org.sonar.plugins.html.node.Node)3 TagNode (org.sonar.plugins.html.node.TagNode)3 Test (org.junit.Test)2 CommentNode (org.sonar.plugins.html.node.CommentNode)2 DirectiveNode (org.sonar.plugins.html.node.DirectiveNode)2 TextNode (org.sonar.plugins.html.node.TextNode)2 IOException (java.io.IOException)1 StreamTokenizer (java.io.StreamTokenizer)1