Search in sources :

Example 1 with Attribute

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

the class DeprecatedAttributesInHtml5Check method startElement.

@Override
public void startElement(TagNode element) {
    String elementName = element.getNodeName().toLowerCase();
    Set<String> deprecatedAttributes = DEPRECATED.get(elementName);
    if (deprecatedAttributes != null) {
        List<Attribute> attributes = element.getAttributes();
        for (Attribute attribute : attributes) {
            if (isDeprecated(element, deprecatedAttributes, attribute.getName().toLowerCase(), attribute.getValue().toLowerCase())) {
                createViolation(element.getStartLinePosition(), "Remove this deprecated \"" + attribute.getName() + "\" attribute.");
            }
        }
    }
}
Also used : Attribute(org.sonar.plugins.web.node.Attribute)

Example 2 with Attribute

use of org.sonar.plugins.web.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.web.node.Attribute) StringReader(java.io.StringReader) IOException(java.io.IOException) StreamTokenizer(java.io.StreamTokenizer)

Example 3 with Attribute

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

the class ElementTokenizer method handleBeforeAttributeValue.

private static void handleBeforeAttributeValue(CodeReader codeReader, TagNode element) {
    Attribute attribute;
    if (!element.getAttributes().isEmpty()) {
        attribute = element.getAttributes().get(element.getAttributes().size() - 1);
        StringBuilder sbValue = new StringBuilder();
        int ch = codeReader.peek();
        if (isQuote((char) ch)) {
            codeReader.pop();
            if (codeReader.peek() != ch) {
                codeReader.popTo(new QuoteMatcher((char) ch), sbValue);
                attribute.setValue(unescapeQuotes(sbValue.toString(), (char) ch));
            }
            codeReader.pop();
            attribute.setQuoteChar((char) ch);
        } else {
            codeReader.popTo(endUnquotedAttributeMatcher, sbValue);
            attribute.setValue(sbValue.toString().trim());
        }
    }
}
Also used : Attribute(org.sonar.plugins.web.node.Attribute)

Example 4 with Attribute

use of org.sonar.plugins.web.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.web.node.Attribute) TagNode(org.sonar.plugins.web.node.TagNode) Node(org.sonar.plugins.web.node.Node)

Example 5 with Attribute

use of org.sonar.plugins.web.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);
    assertEquals(1, nodeList.size());
    assertTrue(nodeList.get(0) instanceof TagNode);
    final TagNode node = (TagNode) nodeList.get(0);
    assertEquals(4, node.getAttributes().size());
    final Attribute attribute = node.getAttributes().get(0);
    assertEquals("src", attribute.getName());
    assertEquals("http://foo/sfds?sjg", attribute.getValue());
    final Attribute attributeA = node.getAttributes().get(1);
    assertEquals("a", attributeA.getName());
    assertEquals("1", attributeA.getValue());
    final Attribute attributeB = node.getAttributes().get(2);
    assertEquals("b", attributeB.getName());
    assertEquals("2", attributeB.getValue());
    final Attribute attributeC = node.getAttributes().get(3);
    assertEquals("c", attributeC.getName());
    assertEquals("3", attributeC.getValue());
}
Also used : Attribute(org.sonar.plugins.web.node.Attribute) TagNode(org.sonar.plugins.web.node.TagNode) TextNode(org.sonar.plugins.web.node.TextNode) CommentNode(org.sonar.plugins.web.node.CommentNode) Node(org.sonar.plugins.web.node.Node) DirectiveNode(org.sonar.plugins.web.node.DirectiveNode) StringReader(java.io.StringReader) TagNode(org.sonar.plugins.web.node.TagNode) Test(org.junit.Test)

Aggregations

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