Search in sources :

Example 21 with Node

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

the class PageLexerTest method testNestedScriptlet.

@Test
public void testNestedScriptlet() {
    String fragment = "<option value=\"<%= key -%>\" <%= 'selected' if alert.operator==key -%>>";
    StringReader reader = new StringReader(fragment);
    PageLexer lexer = new PageLexer();
    List<Node> nodeList = lexer.parse(reader);
    assertEquals(1, nodeList.size());
    TagNode tagNode = (TagNode) nodeList.get(0);
    assertEquals(2, tagNode.getAttributes().size());
    // the embedded tags are added as attributes
    assertEquals(tagNode.getAttributes().get(0).getName(), "value");
    assertEquals(tagNode.getAttributes().get(0).getValue(), "<%= key -%>");
    assertEquals(tagNode.getAttributes().get(1).getName(), "<%= 'selected' if alert.operator==key -%>");
    assertThat(tagNode.getAttributes().get(1).getValue()).isEmpty();
}
Also used : 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)

Example 22 with Node

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

Example 23 with Node

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

the class NoSonarScannerTest method scanNoSonar.

@Test
public void scanNoSonar() {
    List<Node> nodeList = new PageLexer().parse(new StringReader("<table>\n<!-- //NOSONAR --><td>\n</table>"));
    WebSourceCode webSourceCode = new WebSourceCode(new DefaultInputFile("key", "dummy.jsp"));
    NoSonarFilter noSonarFilter = spy(new NoSonarFilter());
    HtmlAstScanner pageScanner = new HtmlAstScanner(Collections.emptyList());
    pageScanner.addVisitor(new NoSonarScanner(noSonarFilter));
    pageScanner.scan(nodeList, webSourceCode, Charsets.UTF_8);
    verify(noSonarFilter, times(1)).noSonarInFile(any(InputFile.class), isOnlyIgnoringLine2());
}
Also used : PageLexer(org.sonar.plugins.web.lex.PageLexer) NoSonarFilter(org.sonar.api.issue.NoSonarFilter) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) Node(org.sonar.plugins.web.node.Node) StringReader(java.io.StringReader) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) Test(org.junit.Test)

Example 24 with Node

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

the class TextTokenizer method consume.

@Override
public boolean consume(CodeReader codeReader, List<Node> nodeList) {
    Node node = createNode();
    setStartPosition(codeReader, node);
    StringBuilder stringBuilder = new StringBuilder();
    if (inScript(nodeList)) {
        codeReader.popTo(new EndScriptMatcher(codeReader), stringBuilder);
    } else {
        codeReader.popTo(endTokenMatcher, stringBuilder);
    }
    node.setCode(stringBuilder.toString());
    setEndPosition(codeReader, node);
    nodeList.add(node);
    return true;
}
Also used : TagNode(org.sonar.plugins.web.node.TagNode) Node(org.sonar.plugins.web.node.Node) TextNode(org.sonar.plugins.web.node.TextNode)

Example 25 with Node

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

the class PageCountLinesTest method testCountLinesJspFile.

@Test
public void testCountLinesJspFile() throws FileNotFoundException {
    List<Node> nodeList = lexer.parse(new FileReader(TestUtils.getResource("checks/AvoidHtmlCommentCheck/document.jsp")));
    String relativePath = "testdocument.jsp";
    WebSourceCode webSourceCode = new WebSourceCode(new DefaultInputFile("key", relativePath).setModuleBaseDir(new File(".").toPath()));
    scanner.scan(nodeList, webSourceCode, Charsets.UTF_8);
    assertThat(webSourceCode.getMeasure(CoreMetrics.NCLOC)).isEqualTo(2);
    assertThat(webSourceCode.getDetailedLinesOfCode()).containsOnly(1, 3);
    assertThat(webSourceCode.getMeasure(CoreMetrics.COMMENT_LINES)).isEqualTo(6);
    assertThat(webSourceCode.getDetailedLinesOfComments()).containsOnly(2, 4, 6, 7, 8, 10);
}
Also used : DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) Node(org.sonar.plugins.web.node.Node) WebSourceCode(org.sonar.plugins.web.visitor.WebSourceCode) FileReader(java.io.FileReader) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) Test(org.junit.Test)

Aggregations

Node (org.sonar.plugins.web.node.Node)25 TagNode (org.sonar.plugins.web.node.TagNode)19 Test (org.junit.Test)18 TextNode (org.sonar.plugins.web.node.TextNode)17 CommentNode (org.sonar.plugins.web.node.CommentNode)15 DirectiveNode (org.sonar.plugins.web.node.DirectiveNode)15 StringReader (java.io.StringReader)10 FileReader (java.io.FileReader)7 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)4 File (java.io.File)3 WebSourceCode (org.sonar.plugins.web.visitor.WebSourceCode)3 ArrayList (java.util.ArrayList)2 CodeReader (org.sonar.channel.CodeReader)2 Attribute (org.sonar.plugins.web.node.Attribute)2 List (java.util.List)1 InputFile (org.sonar.api.batch.fs.InputFile)1 NoSonarFilter (org.sonar.api.issue.NoSonarFilter)1 Channel (org.sonar.channel.Channel)1 PageLexer (org.sonar.plugins.web.lex.PageLexer)1