Search in sources :

Example 1 with Tag

use of org.jsoup.parser.Tag in project structr by structr.

the class MicroformatParser method recurse.

private void recurse(final Element element, final Map<String, Object> values, final int depth) {
    final Tag tag = element.tag();
    final Set<String> classes = element.classNames();
    final String link = element.attr("href");
    final Object content = extractChildContent(element);
    if (!classes.isEmpty()) {
        removeEmpty(classes);
        // toplevel classes define type
        if (tag.isBlock()) {
            if (depth == 0) {
                // store type attribute
                values.put("type", classes);
                for (final Element child : element.children()) {
                    recurse(child, values, depth + 1);
                }
            } else {
                final Map<String, Object> childMap = new LinkedHashMap<>();
                values.put(classes.iterator().next(), childMap);
                if (content != null) {
                    childMap.put("name", content);
                }
                for (final Element child : element.children()) {
                    recurse(child, childMap, depth + 1);
                }
            }
        } else if (tag.isInline()) {
            // extract href and store as URL
            if (classes.contains("url") && StringUtils.isNotBlank(link)) {
                values.put("url", link);
                classes.remove("url");
            }
            if (content != null) {
                for (final String type : classes) {
                    values.put(type, content);
                }
            }
        }
    }
}
Also used : Element(org.jsoup.nodes.Element) Tag(org.jsoup.parser.Tag) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with Tag

use of org.jsoup.parser.Tag in project jsoup by jhy.

the class ElementTest method testHasClassDomMethods.

@Test
public void testHasClassDomMethods() {
    Tag tag = Tag.valueOf("a");
    Attributes attribs = new Attributes();
    Element el = new Element(tag, "", attribs);
    attribs.put("class", "toto");
    boolean hasClass = el.hasClass("toto");
    assertTrue(hasClass);
    attribs.put("class", " toto");
    hasClass = el.hasClass("toto");
    assertTrue(hasClass);
    attribs.put("class", "toto ");
    hasClass = el.hasClass("toto");
    assertTrue(hasClass);
    attribs.put("class", "\ttoto ");
    hasClass = el.hasClass("toto");
    assertTrue(hasClass);
    attribs.put("class", "  toto ");
    hasClass = el.hasClass("toto");
    assertTrue(hasClass);
    attribs.put("class", "ab");
    hasClass = el.hasClass("toto");
    assertFalse(hasClass);
    attribs.put("class", "     ");
    hasClass = el.hasClass("toto");
    assertFalse(hasClass);
    attribs.put("class", "tototo");
    hasClass = el.hasClass("toto");
    assertFalse(hasClass);
    attribs.put("class", "raulpismuth  ");
    hasClass = el.hasClass("raulpismuth");
    assertTrue(hasClass);
    attribs.put("class", " abcd  raulpismuth efgh ");
    hasClass = el.hasClass("raulpismuth");
    assertTrue(hasClass);
    attribs.put("class", " abcd efgh raulpismuth");
    hasClass = el.hasClass("raulpismuth");
    assertTrue(hasClass);
    attribs.put("class", " abcd efgh raulpismuth ");
    hasClass = el.hasClass("raulpismuth");
    assertTrue(hasClass);
}
Also used : Tag(org.jsoup.parser.Tag) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with Tag

use of org.jsoup.parser.Tag in project spring-boot-quick by vector4wang.

the class HTML2Md method processElement.

private static void processElement(Element element, ArrayList<MDLine> lines) {
    Tag tag = element.tag();
    String tagName = tag.getName();
    if (tagName.equals("div")) {
        div(element, lines);
    } else if (tagName.equals("p")) {
        p(element, lines);
    } else if (tagName.equals("br")) {
        br(lines);
    } else if (tagName.matches("^h[0-9]+$")) {
        h(element, lines);
    } else if (tagName.equals("strong") || tagName.equals("b")) {
        strong(element, lines);
    } else if (tagName.equals("em")) {
        em(element, lines);
    } else if (tagName.equals("hr")) {
        hr(lines);
    } else if (tagName.equals("a")) {
        a(element, lines);
    } else if (tagName.equals("img")) {
        img(element, lines);
    } else if (tagName.equals("code")) {
        code(element, lines);
    } else if (tagName.equals("ul")) {
        ul(element, lines);
    } else if (tagName.equals("ol")) {
        ol(element, lines);
    } else if (tagName.equals("li")) {
        li(element, lines);
    } else if (tagName.equals("pre")) {
        // 个人添加
        pre(element, lines);
    } else {
        MDLine line = getLastLine(lines);
        line.append(getTextContent(element));
    }
}
Also used : Tag(org.jsoup.parser.Tag)

Example 4 with Tag

use of org.jsoup.parser.Tag in project jsoup by jhy.

the class NodeTest method handlesBaseUri.

@Test
public void handlesBaseUri() {
    Tag tag = Tag.valueOf("a");
    Attributes attribs = new Attributes();
    attribs.put("relHref", "/foo");
    attribs.put("absHref", "http://bar/qux");
    Element noBase = new Element(tag, "", attribs);
    // with no base, should NOT fallback to href attrib, whatever it is
    assertEquals("", noBase.absUrl("relHref"));
    // no base but valid attrib, return attrib
    assertEquals("http://bar/qux", noBase.absUrl("absHref"));
    Element withBase = new Element(tag, "http://foo/", attribs);
    // construct abs from base + rel
    assertEquals("http://foo/foo", withBase.absUrl("relHref"));
    // href is abs, so returns that
    assertEquals("http://bar/qux", withBase.absUrl("absHref"));
    assertEquals("", withBase.absUrl("noval"));
    Element dodgyBase = new Element(tag, "wtf://no-such-protocol/", attribs);
    // base fails, but href good, so get that
    assertEquals("http://bar/qux", dodgyBase.absUrl("absHref"));
    // base fails, only rel href, so return nothing
    assertEquals("", dodgyBase.absUrl("relHref"));
}
Also used : Tag(org.jsoup.parser.Tag) Test(org.junit.jupiter.api.Test)

Example 5 with Tag

use of org.jsoup.parser.Tag in project jsoup by jhy.

the class ElementTest method whiteSpaceClassElement.

@Test
public void whiteSpaceClassElement() {
    Tag tag = Tag.valueOf("a");
    Attributes attribs = new Attributes();
    Element el = new Element(tag, "", attribs);
    attribs.put("class", "abc ");
    boolean hasClass = el.hasClass("ab");
    assertFalse(hasClass);
}
Also used : Tag(org.jsoup.parser.Tag) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

Tag (org.jsoup.parser.Tag)6 Test (org.junit.jupiter.api.Test)3 Element (org.jsoup.nodes.Element)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1