예제 #1
0
  @Test
  public void absAttr() {
    Document doc =
        Jsoup.parse("<a id=1 href='/foo'>One</a> <a id=2 href='http://jsoup.org'>Two</a>");
    Elements one = doc.select("#1");
    Elements two = doc.select("#2");
    Elements both = doc.select("a");

    assertEquals("", one.attr("abs:href"));
    assertEquals("http://jsoup.org", two.attr("abs:href"));
    assertEquals("http://jsoup.org", both.attr("abs:href"));
  }
예제 #2
0
  @Test
  public void attributes() {
    String h = "<p title=foo><p title=bar><p class=foo><p class=bar>";
    Document doc = Jsoup.parse(h);
    Elements withTitle = doc.select("p[title]");
    assertEquals(2, withTitle.size());
    assertTrue(withTitle.hasAttr("title"));
    assertFalse(withTitle.hasAttr("class"));
    assertEquals("foo", withTitle.attr("title"));

    withTitle.removeAttr("title");
    assertEquals(2, withTitle.size()); // existing Elements are not reevaluated
    assertEquals(0, doc.select("p[title]").size());

    Elements ps = doc.select("p").attr("style", "classy");
    assertEquals(4, ps.size());
    assertEquals("classy", ps.last().attr("style"));
    assertEquals("bar", ps.last().attr("class"));
  }