@Test public void val() { Document doc = Jsoup.parse("<input value='one' /><textarea>two</textarea>"); Elements els = doc.select("input, textarea"); assertEquals(2, els.size()); assertEquals("one", els.val()); assertEquals("two", els.last().val()); els.val("three"); assertEquals("three", els.first().val()); assertEquals("three", els.last().val()); assertEquals("<textarea>three</textarea>", els.last().outerHtml()); }
@Test public void notParas() { Document doc = Jsoup.parse("<p id=1>One</p> <p>Two</p> <p><span>Three</span></p>"); Elements el1 = doc.select("p:not([id=1])"); assertEquals(2, el1.size()); assertEquals("Two", el1.first().text()); assertEquals("Three", el1.last().text()); Elements el2 = doc.select("p:not(:has(span))"); assertEquals(2, el2.size()); assertEquals("One", el2.first().text()); assertEquals("Two", el2.last().text()); }
@Test public void testPseudoContains() { Document doc = Jsoup.parse( "<div><p>The Rain.</p> <p class=light>The <i>rain</i>.</p> <p>Rain, the.</p></div>"); Elements ps1 = doc.select("p:contains(Rain)"); assertEquals(3, ps1.size()); Elements ps2 = doc.select("p:contains(the rain)"); assertEquals(2, ps2.size()); assertEquals("The Rain.", ps2.first().html()); assertEquals("The <i>rain</i>.", ps2.last().html()); Elements ps3 = doc.select("p:contains(the Rain):has(i)"); assertEquals(1, ps3.size()); assertEquals("light", ps3.first().className()); Elements ps4 = doc.select(".light:contains(rain)"); assertEquals(1, ps4.size()); assertEquals("light", ps3.first().className()); Elements ps5 = doc.select(":contains(rain)"); assertEquals(8, ps5.size()); // html, body, div,... }
@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 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")); }
@Test public void testByAttribute() { String h = "<div Title=Foo /><div Title=Bar /><div Style=Qux /><div title=Bam /><div title=SLAM />" + "<div data-name='with spaces'/>"; Document doc = Jsoup.parse(h); Elements withTitle = doc.select("[title]"); assertEquals(4, withTitle.size()); Elements foo = doc.select("[TITLE=foo]"); assertEquals(1, foo.size()); Elements foo2 = doc.select("[title=\"foo\"]"); assertEquals(1, foo2.size()); Elements foo3 = doc.select("[title=\"Foo\"]"); assertEquals(1, foo3.size()); Elements dataName = doc.select("[data-name=\"with spaces\"]"); assertEquals(1, dataName.size()); assertEquals("with spaces", dataName.first().attr("data-name")); Elements not = doc.select("div[title!=bar]"); assertEquals(5, not.size()); assertEquals("Foo", not.first().attr("title")); Elements starts = doc.select("[title^=ba]"); assertEquals(2, starts.size()); assertEquals("Bar", starts.first().attr("title")); assertEquals("Bam", starts.last().attr("title")); Elements ends = doc.select("[title$=am]"); assertEquals(2, ends.size()); assertEquals("Bam", ends.first().attr("title")); assertEquals("SLAM", ends.last().attr("title")); Elements contains = doc.select("[title*=a]"); assertEquals(3, contains.size()); assertEquals("Bar", contains.first().attr("title")); assertEquals("SLAM", contains.last().attr("title")); }
@Test public void testByAttributeStarting() { Document doc = Jsoup.parse( "<div id=1 data-name=jsoup>Hello</div><p data-val=5 id=2>There</p><p id=3>No</p>"); Elements withData = doc.select("[^data-]"); assertEquals(2, withData.size()); assertEquals("1", withData.first().id()); assertEquals("2", withData.last().id()); withData = doc.select("p[^data-]"); assertEquals(1, withData.size()); assertEquals("2", withData.first().id()); }
@Test public void testWildcardNamespacedTag() { Document doc = Jsoup.parse( "<div><abc:def id=1>Hello</abc:def></div> <abc:def class=bold id=2>There</abc:def>"); Elements byTag = doc.select("*|def"); assertEquals(2, byTag.size()); assertEquals("1", byTag.first().id()); assertEquals("2", byTag.last().id()); Elements byAttr = doc.select(".bold"); assertEquals(1, byAttr.size()); assertEquals("2", byAttr.last().id()); Elements byTagAttr = doc.select("*|def.bold"); assertEquals(1, byTagAttr.size()); assertEquals("2", byTagAttr.last().id()); Elements byContains = doc.select("*|def:contains(e)"); assertEquals(2, byContains.size()); assertEquals("1", byContains.first().id()); assertEquals("2", byContains.last().id()); }
@Test public void containsData() { String html = "<p>jsoup</p><script>jsoup</script><span><!-- comments --></span>"; Document doc = Jsoup.parse(html); Element body = doc.body(); Elements dataEls1 = body.select(":containsData(jsoup)"); Elements dataEls2 = body.select("script:containsData(jsoup)"); Elements dataEls3 = body.select("span:containsData(comments)"); Elements dataEls4 = body.select(":containsData(s)"); assertEquals(2, dataEls1.size()); // body and script assertEquals(1, dataEls2.size()); assertEquals(dataEls1.last(), dataEls2.first()); assertEquals("<script>jsoup</script>", dataEls2.outerHtml()); assertEquals(1, dataEls3.size()); assertEquals("span", dataEls3.first().tagName()); assertEquals(3, dataEls4.size()); assertEquals("body", dataEls4.first().tagName()); assertEquals("script", dataEls4.get(1).tagName()); assertEquals("span", dataEls4.get(2).tagName()); }