Search in sources :

Example 51 with Document

use of org.jsoup.nodes.Document in project jsoup by jhy.

the class SelectorTest method generalSiblings.

@Test
public void generalSiblings() {
    String h = "<ol><li id=1>One<li id=2>Two<li id=3>Three</ol>";
    Document doc = Jsoup.parse(h);
    Elements els = doc.select("#1 ~ #3");
    assertEquals(1, els.size());
    assertEquals("Three", els.first().text());
}
Also used : Document(org.jsoup.nodes.Document) Test(org.junit.Test)

Example 52 with Document

use of org.jsoup.nodes.Document in project jsoup by jhy.

the class SelectorTest method selectClassWithSpace.

@Test
public void selectClassWithSpace() {
    final String html = "<div class=\"value\">class without space</div>\n" + "<div class=\"value \">class with space</div>";
    Document doc = Jsoup.parse(html);
    Elements found = doc.select("div[class=value ]");
    assertEquals(2, found.size());
    assertEquals("class without space", found.get(0).text());
    assertEquals("class with space", found.get(1).text());
    found = doc.select("div[class=\"value \"]");
    assertEquals(2, found.size());
    assertEquals("class without space", found.get(0).text());
    assertEquals("class with space", found.get(1).text());
    found = doc.select("div[class=\"value\\ \"]");
    assertEquals(0, found.size());
}
Also used : Document(org.jsoup.nodes.Document) Test(org.junit.Test)

Example 53 with Document

use of org.jsoup.nodes.Document in project jsoup by jhy.

the class SelectorTest method handlesCommasInSelector.

@Test
public void handlesCommasInSelector() {
    Document doc = Jsoup.parse("<p name='1,2'>One</p><div>Two</div><ol><li>123</li><li>Text</li></ol>");
    Elements ps = doc.select("[name=1,2]");
    assertEquals(1, ps.size());
    Elements containers = doc.select("div, li:matches([0-9,]+)");
    assertEquals(2, containers.size());
    assertEquals("div", containers.get(0).tagName());
    assertEquals("li", containers.get(1).tagName());
    assertEquals("123", containers.get(1).text());
}
Also used : Document(org.jsoup.nodes.Document) Test(org.junit.Test)

Example 54 with Document

use of org.jsoup.nodes.Document in project jsoup by jhy.

the class SelectorTest method parentWithClassChild.

@Test
public void parentWithClassChild() {
    String h = "<h1 class=foo><a href=1 /></h1><h1 class=foo><a href=2 class=bar /></h1><h1><a href=3 /></h1>";
    Document doc = Jsoup.parse(h);
    Elements allAs = doc.select("h1 > a");
    assertEquals(3, allAs.size());
    assertEquals("a", allAs.first().tagName());
    Elements fooAs = doc.select("h1.foo > a");
    assertEquals(2, fooAs.size());
    assertEquals("a", fooAs.first().tagName());
    Elements barAs = doc.select("h1.foo > a.bar");
    assertEquals(1, barAs.size());
}
Also used : Document(org.jsoup.nodes.Document) Test(org.junit.Test)

Example 55 with Document

use of org.jsoup.nodes.Document in project jsoup by jhy.

the class SelectorTest method testGroupOr.

@Test
public void testGroupOr() {
    String h = "<div title=foo /><div title=bar /><div /><p></p><img /><span title=qux>";
    Document doc = Jsoup.parse(h);
    Elements els = doc.select("p,div,[title]");
    assertEquals(5, els.size());
    assertEquals("div", els.get(0).tagName());
    assertEquals("foo", els.get(0).attr("title"));
    assertEquals("div", els.get(1).tagName());
    assertEquals("bar", els.get(1).attr("title"));
    assertEquals("div", els.get(2).tagName());
    // missing attributes come back as empty string
    assertTrue(els.get(2).attr("title").length() == 0);
    assertFalse(els.get(2).hasAttr("title"));
    assertEquals("p", els.get(3).tagName());
    assertEquals("span", els.get(4).tagName());
}
Also used : Document(org.jsoup.nodes.Document) Test(org.junit.Test)

Aggregations

Document (org.jsoup.nodes.Document)405 Test (org.junit.Test)194 Element (org.jsoup.nodes.Element)164 IOException (java.io.IOException)102 File (java.io.File)81 Elements (org.jsoup.select.Elements)78 ElementHandlerImpl (org.asqatasun.ruleimplementation.ElementHandlerImpl)51 ArrayList (java.util.ArrayList)41 Connection (org.jsoup.Connection)38 URL (java.net.URL)25 HashMap (java.util.HashMap)17 InputStream (java.io.InputStream)14 List (java.util.List)10 MalformedURLException (java.net.MalformedURLException)8 Logger (org.slf4j.Logger)8 Matcher (java.util.regex.Matcher)7 Jsoup (org.jsoup.Jsoup)7 LoggerFactory (org.slf4j.LoggerFactory)7 Pattern (java.util.regex.Pattern)6 HttpGet (org.apache.http.client.methods.HttpGet)6