Search in sources :

Example 16 with Element

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"));
}
Also used : Element(org.jsoup.nodes.Element) Attributes(org.jsoup.nodes.Attributes) Test(org.junit.Test)

Example 17 with Element

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());
}
Also used : Element(org.jsoup.nodes.Element) Document(org.jsoup.nodes.Document) Test(org.junit.Test)

Example 18 with Element

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());
}
Also used : Element(org.jsoup.nodes.Element) Document(org.jsoup.nodes.Document) Test(org.junit.Test)

Example 19 with Element

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"));
}
Also used : Element(org.jsoup.nodes.Element) FormElement(org.jsoup.nodes.FormElement) Test(org.junit.Test)

Example 20 with Element

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;
}
Also used : Element(org.jsoup.nodes.Element) Elements(org.jsoup.select.Elements)

Aggregations

Element (org.jsoup.nodes.Element)343 Document (org.jsoup.nodes.Document)152 Elements (org.jsoup.select.Elements)95 ElementHandlerImpl (org.asqatasun.ruleimplementation.ElementHandlerImpl)87 IOException (java.io.IOException)63 File (java.io.File)62 ArrayList (java.util.ArrayList)45 Test (org.junit.Test)34 TestSolutionHandler (org.asqatasun.ruleimplementation.TestSolutionHandler)21 URL (java.net.URL)15 TestSolutionHandlerImpl (org.asqatasun.ruleimplementation.TestSolutionHandlerImpl)15 SimpleElementSelector (org.asqatasun.rules.elementselector.SimpleElementSelector)13 TestSolution (org.asqatasun.entity.audit.TestSolution)11 HashMap (java.util.HashMap)9 ElementSelector (org.asqatasun.rules.elementselector.ElementSelector)9 Node (org.jsoup.nodes.Node)9 InputStream (java.io.InputStream)8 EvidenceElement (org.asqatasun.entity.audit.EvidenceElement)8 SSPHandler (org.asqatasun.processor.SSPHandler)7 ProcessRemarkService (org.asqatasun.service.ProcessRemarkService)7