use of org.jsoup.nodes.Element in project jsoup by jhy.
the class AttributeParseTest method parsesRoughAttributeString.
@Test
public void parsesRoughAttributeString() {
String html = "<a id=\"123\" class=\"baz = 'bar'\" style = 'border: 2px'qux zim foo = 12 mux=18 />";
// should be: <id=123>, <class=baz = 'bar'>, <qux=>, <zim=>, <foo=12>, <mux.=18>
Element el = Jsoup.parse(html).getElementsByTag("a").get(0);
Attributes attr = el.attributes();
assertEquals(7, attr.size());
assertEquals("123", attr.get("id"));
assertEquals("baz = 'bar'", attr.get("class"));
assertEquals("border: 2px", attr.get("style"));
assertEquals("", attr.get("qux"));
assertEquals("", attr.get("zim"));
assertEquals("12", attr.get("foo"));
assertEquals("18", attr.get("mux"));
}
use of org.jsoup.nodes.Element in project jsoup by jhy.
the class SelectorTest method descendant.
@Test
public void descendant() {
String h = "<div class=head><p class=first>Hello</p><p>There</p></div><p>None</p>";
Document doc = Jsoup.parse(h);
Element root = doc.getElementsByClass("HEAD").first();
Elements els = root.select(".head p");
assertEquals(2, els.size());
assertEquals("Hello", els.get(0).text());
assertEquals("There", els.get(1).text());
Elements p = root.select("p.first");
assertEquals(1, p.size());
assertEquals("Hello", p.get(0).text());
// self, not descend, should not match
Elements empty = root.select("p .first");
assertEquals(0, empty.size());
Elements aboveRoot = root.select("body div.head");
assertEquals(0, aboveRoot.size());
}
use of org.jsoup.nodes.Element in project jsoup by jhy.
the class SelectorTest method deeperDescendant.
@Test
public void deeperDescendant() {
String h = "<div class=head><p><span class=first>Hello</div><div class=head><p class=first><span>Another</span><p>Again</div>";
Document doc = Jsoup.parse(h);
Element root = doc.getElementsByClass("head").first();
Elements els = root.select("div p .first");
assertEquals(1, els.size());
assertEquals("Hello", els.first().text());
assertEquals("span", els.first().tagName());
Elements aboveRoot = root.select("body p .first");
assertEquals(0, aboveRoot.size());
}
use of org.jsoup.nodes.Element in project jsoup by jhy.
the class ElementsTest method hasClassCaseInsensitive.
@Test
public void hasClassCaseInsensitive() {
Elements els = Jsoup.parse("<p Class=One>One <p class=Two>Two <p CLASS=THREE>THREE").select("p");
Element one = els.get(0);
Element two = els.get(1);
Element thr = els.get(2);
assertTrue(one.hasClass("One"));
assertTrue(one.hasClass("ONE"));
assertTrue(two.hasClass("TWO"));
assertTrue(two.hasClass("Two"));
assertTrue(thr.hasClass("ThreE"));
assertTrue(thr.hasClass("three"));
}
use of org.jsoup.nodes.Element in project AozoraEpub3 by hmdev.
the class WebAozoraConverter method getExtractText.
String getExtractText(Document doc, ExtractInfo[] extractInfos, boolean replace) {
if (extractInfos == null)
return null;
for (ExtractInfo extractInfo : extractInfos) {
String text = null;
Elements elements = doc.select(extractInfo.query);
if (elements == null || elements.size() == 0)
continue;
StringBuilder buf = new StringBuilder();
if (extractInfo.idx == null) {
for (Element element : elements) {
String html = element.html();
if (html != null)
buf.append(" ").append(replaceHtmlText(html, replace ? extractInfo : null));
}
} else {
for (int i = 0; i < extractInfo.idx.length; i++) {
if (elements.size() > extractInfo.idx[i]) {
int pos = extractInfo.idx[i];
//負の値なら後ろから
if (pos < 0)
pos = elements.size() + pos;
if (pos >= 0 && elements.size() > pos) {
String html = elements.get(pos).html();
if (html != null)
buf.append(" ").append(replaceHtmlText(html, replace ? extractInfo : null));
}
}
}
if (buf.length() > 0)
text = buf.deleteCharAt(0).toString();
}
//置換指定ならreplaceして返す
if (text != null && text.length() > 0) {
return text;
}
}
return null;
}
Aggregations