@Test
 public void testAllWithClass() {
   String h = "<p class=first>One<p class=first>Two<p>Three";
   Document doc = Jsoup.parse(h);
   Elements ps = doc.select("*.first");
   assertEquals(2, ps.size());
 }
Exemple #2
0
 @Test
 public void outerHtml() {
   Document doc = Jsoup.parse("<div><p>Hello</p></div><div><p>There</p></div>");
   Elements divs = doc.select("div");
   assertEquals(
       "<div><p>Hello</p></div><div><p>There</p></div>", TextUtil.stripNewlines(divs.outerHtml()));
 }
Exemple #3
0
 @Test
 public void hasAttr() {
   Document doc = Jsoup.parse("<p title=foo><p title=bar><p class=foo><p class=bar>");
   Elements ps = doc.select("p");
   assertTrue(ps.hasAttr("class"));
   assertFalse(ps.hasAttr("style"));
 }
 @Test
 public void notAdjacent() {
   String h = "<ol><li id=1>One<li id=2>Two<li id=3>Three</ol>";
   Document doc = Jsoup.parse(h);
   Elements sibs = doc.select("li#1 + li#3");
   assertEquals(0, sibs.size());
 }
 @Test
 public void testPseudoBetween() {
   Document doc = Jsoup.parse("<div><p>One</p><p>Two</p><p>Three</>p></div><div><p>Four</p>");
   Elements ps = doc.select("div p:gt(0):lt(2)");
   assertEquals(1, ps.size());
   assertEquals("Two", ps.get(0).text());
 }
 @Test
 public void testByAttributeRegexCombined() {
   Document doc = Jsoup.parse("<div><table class=x><td>Hello</td></table></div>");
   Elements els = doc.select("div table[class~=x|y]");
   assertEquals(1, els.size());
   assertEquals("Hello", els.text());
 }
Exemple #7
0
 @Test
 public void hasText() {
   Document doc = Jsoup.parse("<div><p>Hello</p></div><div><p></p></div>");
   Elements divs = doc.select("div");
   assertTrue(divs.hasText());
   assertFalse(doc.select("div + div").hasText());
 }
Exemple #8
0
 @Test
 public void is() {
   String h = "<p>Hello<p title=foo>there<p>world";
   Document doc = Jsoup.parse(h);
   Elements ps = doc.select("p");
   assertTrue(ps.is("[title=foo]"));
   assertFalse(ps.is("[title=bar]"));
 }
 @Test
 public void multiChildDescent() {
   String h = "<div id=foo><h1 class=bar><a href=http://example.com/>One</a></h1></div>";
   Document doc = Jsoup.parse(h);
   Elements els = doc.select("div#foo > h1.bar > a[href*=example]");
   assertEquals(1, els.size());
   assertEquals("a", els.first().tagName());
 }
 @Test
 public void testPseudoGreaterThan() {
   Document doc = Jsoup.parse("<div><p>One</p><p>Two</p><p>Three</p></div><div><p>Four</p>");
   Elements ps = doc.select("div p:gt(0)");
   assertEquals(2, ps.size());
   assertEquals("Two", ps.get(0).text());
   assertEquals("Three", ps.get(1).text());
 }
  @Test
  public void notClass() {
    Document doc = Jsoup.parse("<div class=left>One</div><div class=right id=1><p>Two</p></div>");

    Elements el1 = doc.select("div:not(.left)");
    assertEquals(1, el1.size());
    assertEquals("1", el1.first().id());
  }
 @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());
 }
 @Test
 public void testCombinedWithContains() {
   Document doc = Jsoup.parse("<p id=1>One</p><p>Two +</p><p>Three +</p>");
   Elements els = doc.select("p#1 + :contains(+)");
   assertEquals(1, els.size());
   assertEquals("Two +", els.text());
   assertEquals("p", els.first().tagName());
 }
 @Test
 public void adjacentSiblingsWithId() {
   String h = "<ol><li id=1>One<li id=2>Two<li id=3>Three</ol>";
   Document doc = Jsoup.parse(h);
   Elements sibs = doc.select("li#1 + li#2");
   assertEquals(1, sibs.size());
   assertEquals("Two", sibs.get(0).text());
 }
  @Test
  public void containsOwn() {
    Document doc = Jsoup.parse("<p id=1>Hello <b>there</b> now</p>");
    Elements ps = doc.select("p:containsOwn(Hello now)");
    assertEquals(1, ps.size());
    assertEquals("1", ps.first().id());

    assertEquals(0, doc.select("p:containsOwn(there)").size());
  }
  @Test
  public void notAll() {
    Document doc = Jsoup.parse("<p>Two</p> <p><span>Three</span></p>");

    Elements el1 = doc.body().select(":not(p)"); // should just be the span
    assertEquals(2, el1.size());
    assertEquals("body", el1.first().tagName());
    assertEquals("span", el1.last().tagName());
  }
 @Test
 public void adjacentSiblings() {
   String h = "<ol><li>One<li>Two<li>Three</ol>";
   Document doc = Jsoup.parse(h);
   Elements sibs = doc.select("li + li");
   assertEquals(2, sibs.size());
   assertEquals("Two", sibs.get(0).text());
   assertEquals("Three", sibs.get(1).text());
 }
 @Test
 public void testPseudoCombined() {
   Document doc =
       Jsoup.parse(
           "<div class='foo'><p>One</p><p>Two</p></div><div><p>Three</p><p>Four</p></div>");
   Elements ps = doc.select("div.foo p:gt(0)");
   assertEquals(1, ps.size());
   assertEquals("Two", ps.get(0).text());
 }
Exemple #19
0
 @Test
 public void filter() {
   String h =
       "<p>Excl</p><div class=headline><p>Hello</p><p>There</p></div><div class=headline><h1>Headline</h1></div>";
   Document doc = Jsoup.parse(h);
   Elements els = doc.select(".headline").select("p");
   assertEquals(2, els.size());
   assertEquals("Hello", els.get(0).text());
   assertEquals("There", els.get(1).text());
 }
Exemple #20
0
  @Test
  public void parents() {
    Document doc = Jsoup.parse("<div><p>Hello</p></div><p>There</p>");
    Elements parents = doc.select("p").parents();

    assertEquals(3, parents.size());
    assertEquals("div", parents.get(0).tagName());
    assertEquals("body", parents.get(1).tagName());
    assertEquals("html", parents.get(2).tagName());
  }
 @Test
 public void parentChildStar() {
   String h = "<div id=1><p>Hello<p><b>there</b></p></div><div id=2><span>Hi</span></div>";
   Document doc = Jsoup.parse(h);
   Elements divChilds = doc.select("div > *");
   assertEquals(3, divChilds.size());
   assertEquals("p", divChilds.get(0).tagName());
   assertEquals("p", divChilds.get(1).tagName());
   assertEquals("span", divChilds.get(2).tagName());
 }
  @Test
  public void testGroupOrAttribute() {
    String h = "<div id=1 /><div id=2 /><div title=foo /><div title=bar />";
    Elements els = Jsoup.parse(h).select("[id],[title=foo]");

    assertEquals(3, els.size());
    assertEquals("1", els.get(0).id());
    assertEquals("2", els.get(1).id());
    assertEquals("foo", els.get(2).attr("title"));
  }
 @Test
 public void testAllElements() {
   String h = "<div><p>Hello</p><p><b>there</b></p></div>";
   Document doc = Jsoup.parse(h);
   Elements allDoc = doc.select("*");
   Elements allUnderDiv = doc.select("div *");
   assertEquals(8, allDoc.size());
   assertEquals(3, allUnderDiv.size());
   assertEquals("p", allUnderDiv.first().tagName());
 }
  @Test
  public void mixCombinator() {
    String h = "<div class=foo><ol><li>One<li>Two<li>Three</ol></div>";
    Document doc = Jsoup.parse(h);
    Elements sibs = doc.select("body > div.foo li + li");

    assertEquals(2, sibs.size());
    assertEquals("Two", sibs.get(0).text());
    assertEquals("Three", sibs.get(1).text());
  }
 @Test
 public void testByAttributeRegexCharacterClass() {
   Document doc =
       Jsoup.parse(
           "<p><img src=foo.png id=1><img src=bar.jpg id=2><img src=qux.JPEG id=3><img src=old.gif id=4></p>");
   Elements imgs = doc.select("img[src~=[o]]");
   assertEquals(2, imgs.size());
   assertEquals("1", imgs.get(0).id());
   assertEquals("4", imgs.get(1).id());
 }
  @Test
  public void matchesOwn() {
    Document doc = Jsoup.parse("<p id=1>Hello <b>there</b> now</p>");

    Elements p1 = doc.select("p:matchesOwn((?i)hello now)");
    assertEquals(1, p1.size());
    assertEquals("1", p1.first().id());

    assertEquals(0, doc.select("p:matchesOwn(there)").size());
  }
  @Test
  public void selectSameElements() {
    final String html = "<div>one</div><div>one</div>";

    Document doc = Jsoup.parse(html);
    Elements els = doc.select("div");
    assertEquals(2, els.size());

    Elements subSelect = els.select(":contains(one)");
    assertEquals(2, subSelect.size());
  }
 @Test
 public void testByAttributeRegex() {
   Document doc =
       Jsoup.parse(
           "<p><img src=foo.png id=1><img src=bar.jpg id=2><img src=qux.JPEG id=3><img src=old.gif><img></p>");
   Elements imgs = doc.select("img[src~=(?i)\\.(png|jpe?g)]");
   assertEquals(3, imgs.size());
   assertEquals("1", imgs.get(0).id());
   assertEquals("2", imgs.get(1).id());
   assertEquals("3", imgs.get(2).id());
 }
  @Test
  public void mixCombinatorGroup() {
    String h = "<div class=foo><ol><li>One<li>Two<li>Three</ol></div>";
    Document doc = Jsoup.parse(h);
    Elements els = doc.select(".foo > ol, ol > li + li");

    assertEquals(3, els.size());
    assertEquals("ol", els.get(0).tagName());
    assertEquals("Two", els.get(1).text());
    assertEquals("Three", els.get(2).text());
  }
Exemple #30
0
  @Test
  public void setHtml() {
    Document doc = Jsoup.parse("<p>One</p><p>Two</p><p>Three</p>");
    Elements ps = doc.select("p");

    ps.prepend("<b>Bold</b>").append("<i>Ital</i>");
    assertEquals("<p><b>Bold</b>Two<i>Ital</i></p>", TextUtil.stripNewlines(ps.get(1).outerHtml()));

    ps.html("<span>Gone</span>");
    assertEquals("<p><span>Gone</span></p>", TextUtil.stripNewlines(ps.get(1).outerHtml()));
  }