@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 hasAbsAttr() { 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"); assertFalse(one.hasAttr("abs:href")); assertTrue(two.hasAttr("abs:href")); assertTrue(both.hasAttr("abs:href")); // hits on #2 }
@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")); }