Search in sources :

Example 1 with HTMLElement

use of org.loboevolution.html.dom.HTMLElement in project LoboEvolution by LoboEvolution.

the class LoboUnitTest method checkSelectorsTest.

/**
 * <p>checkSelectorsTest.</p>
 *
 * @param html a {@link java.lang.String} object.
 * @param result1 a {@link java.lang.String} object.
 * @param result2 a {@link java.lang.String} object.
 */
public void checkSelectorsTest(final String html, final String result1, final String result2) {
    HTMLDocumentImpl doc = loadHtml(html);
    Window window = doc.getDefaultView();
    HTMLElement div = (HTMLElement) doc.getElementById("myId");
    HTMLElement div2 = (HTMLElement) doc.getElementById("myId2");
    ComputedCSSStyleDeclaration computedStyle = (ComputedCSSStyleDeclaration) window.getComputedStyle(div);
    ComputedCSSStyleDeclaration computedStyle2 = (ComputedCSSStyleDeclaration) window.getComputedStyle(div2);
    assertEquals(result1, computedStyle.getColor());
    assertEquals(result2, computedStyle2.getColor());
}
Also used : Window(org.loboevolution.html.node.js.Window) HTMLDocumentImpl(org.loboevolution.html.dom.domimpl.HTMLDocumentImpl) HTMLElement(org.loboevolution.html.dom.HTMLElement) ComputedCSSStyleDeclaration(org.loboevolution.html.style.ComputedCSSStyleDeclaration)

Example 2 with HTMLElement

use of org.loboevolution.html.dom.HTMLElement in project LoboEvolution by LoboEvolution.

the class HtmlContent method getMediaList.

/**
 * <p>getMediaList.</p>
 *
 * @return a {@link java.util.List} object.
 */
public List<MetaInfo> getMediaList() {
    final List<MetaInfo> infoList = new ArrayList<>();
    final HTMLDocumentImpl doc = (HTMLDocumentImpl) this.document;
    HTMLCollectionImpl nodeList = (HTMLCollectionImpl) doc.getElementsByTagName("img");
    if (nodeList == null) {
        return null;
    }
    nodeList.forEach(node -> {
        if (node instanceof HTMLElement) {
            final HTMLElement element = (HTMLElement) node;
            String src = element.getAttribute("src");
            if (Strings.isNotBlank(src)) {
                if (!Urls.isAbsolute(src)) {
                    src = doc.getFullURL(src).toString();
                }
                if (src.startsWith("//")) {
                    src = "http:" + src;
                }
                infoList.add(MetaInfo.builder().name(src).build());
            }
        }
    });
    nodeList = (HTMLCollectionImpl) doc.getElementsByTagName("link");
    nodeList.forEach(node -> {
        if (node instanceof HTMLElement) {
            final HTMLElement element = (HTMLElement) node;
            final String rel = element.getAttribute("rel");
            String href = element.getAttribute("href");
            if ("icon".equalsIgnoreCase(rel) && Strings.isNotBlank(href)) {
                if (!Urls.isAbsolute(href)) {
                    href = doc.getFullURL(href).toString();
                }
                if (href.startsWith("//")) {
                    href = "http:" + href;
                }
                infoList.add(MetaInfo.builder().name(href).build());
            }
        }
    });
    return infoList;
}
Also used : HTMLDocumentImpl(org.loboevolution.html.dom.domimpl.HTMLDocumentImpl) HTMLElement(org.loboevolution.html.dom.HTMLElement) MetaInfo(org.loboevolution.info.MetaInfo) ArrayList(java.util.ArrayList) HTMLCollectionImpl(org.loboevolution.html.dom.domimpl.HTMLCollectionImpl)

Example 3 with HTMLElement

use of org.loboevolution.html.dom.HTMLElement in project LoboEvolution by LoboEvolution.

the class HTMLDocumentTest method testCreateElement.

@Test
public void testCreateElement() {
    Element elm = document.createElement("link");
    assertTrue(elm instanceof HTMLLinkElement);
    elm = document.createElement("LINK");
    assertTrue(elm instanceof HTMLLinkElement);
    assertEquals("LINK", elm.getLocalName());
    assertEquals("LINK", elm.getTagName());
    elm = document.createElement("style");
    assertTrue(elm instanceof HTMLLinkElement);
    elm = document.createElement("STYLE");
    assertTrue(elm instanceof HTMLLinkElement);
    assertEquals("style", elm.getLocalName());
    HTMLElement html = (HTMLElement) document.createElement("html");
    try {
        elm.appendChild(html);
        fail("Must throw exception");
    } catch (DOMException e) {
        assertEquals(DOMException.HIERARCHY_REQUEST_ERR, e.getCode());
    }
    try {
        document.createElement(null);
        fail("Must throw exception");
    } catch (DOMException e) {
        assertEquals(DOMException.INVALID_CHARACTER_ERR, e.getCode());
    }
    try {
        document.createElement("");
        fail("Must throw exception");
    } catch (DOMException e) {
        assertEquals(DOMException.INVALID_CHARACTER_ERR, e.getCode());
    }
    try {
        document.createElement("\u0000");
        fail("Must throw exception");
    } catch (DOMException e) {
        assertEquals(DOMException.INVALID_CHARACTER_ERR, e.getCode());
    }
    try {
        document.createElement("<");
        fail("Must throw exception");
    } catch (DOMException e) {
        assertEquals(DOMException.INVALID_CHARACTER_ERR, e.getCode());
    }
    try {
        document.createElement(">");
        fail("Must throw exception");
    } catch (DOMException e) {
        assertEquals(DOMException.INVALID_CHARACTER_ERR, e.getCode());
    }
}
Also used : HTMLLinkElement(org.loboevolution.html.dom.HTMLLinkElement) DOMException(com.gargoylesoftware.css.dom.DOMException) HTMLElement(org.loboevolution.html.dom.HTMLElement) HTMLElement(org.loboevolution.html.dom.HTMLElement) HTMLLinkElement(org.loboevolution.html.dom.HTMLLinkElement) Test(org.junit.Test) LoboUnitTest(org.loboevolution.driver.LoboUnitTest)

Example 4 with HTMLElement

use of org.loboevolution.html.dom.HTMLElement in project LoboEvolution by LoboEvolution.

the class HTMLDocumentTest method testReplaceChild.

@Test
public void testReplaceChild() {
    HTMLElement html = (HTMLElement) document.getDocumentElement();
    Element head = (HTMLElement) html.getElementsByTagName("head").item(0);
    Element body = (HTMLElement) html.getElementsByTagName("body").item(0);
    Element newBody = document.createElement("body");
    Element newHead = document.createElement("head");
    try {
        html.replaceChild(newBody, head);
        fail("Should throw an exception");
    } catch (DOMException e) {
        assertEquals(DOMException.HIERARCHY_REQUEST_ERR, e.getCode());
    }
    try {
        html.replaceChild(newHead, body);
        fail("Should throw an exception");
    } catch (DOMException e) {
        assertEquals(DOMException.HIERARCHY_REQUEST_ERR, e.getCode());
    }
}
Also used : DOMException(com.gargoylesoftware.css.dom.DOMException) HTMLElement(org.loboevolution.html.dom.HTMLElement) HTMLElement(org.loboevolution.html.dom.HTMLElement) HTMLLinkElement(org.loboevolution.html.dom.HTMLLinkElement) Test(org.junit.Test) LoboUnitTest(org.loboevolution.driver.LoboUnitTest)

Example 5 with HTMLElement

use of org.loboevolution.html.dom.HTMLElement in project LoboEvolution by LoboEvolution.

the class HTMLDocumentTest method testContains.

@Test
public void testContains() {
    HTMLElement docelm = (HTMLElement) document.getDocumentElement();
    assertTrue(document.contains(document));
    assertTrue(document.contains(docelm));
    assertTrue(docelm.contains(docelm));
    assertFalse(docelm.contains(document));
    Element h1 = document.getElementById("h1");
    Element span1 = document.getElementById("span1");
    assertTrue(document.contains(h1));
    assertTrue(document.contains(span1));
    assertTrue(docelm.contains(h1));
    assertTrue(docelm.contains(span1));
    assertFalse(h1.contains(docelm));
    assertFalse(span1.contains(docelm));
    assertFalse(h1.contains(document));
    assertFalse(span1.contains(document));
    assertFalse(h1.contains(span1));
    assertFalse(span1.contains(h1));
}
Also used : HTMLElement(org.loboevolution.html.dom.HTMLElement) HTMLElement(org.loboevolution.html.dom.HTMLElement) HTMLLinkElement(org.loboevolution.html.dom.HTMLLinkElement) Test(org.junit.Test) LoboUnitTest(org.loboevolution.driver.LoboUnitTest)

Aggregations

HTMLElement (org.loboevolution.html.dom.HTMLElement)14 HTMLDocumentImpl (org.loboevolution.html.dom.domimpl.HTMLDocumentImpl)5 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)4 LoboUnitTest (org.loboevolution.driver.LoboUnitTest)4 HTMLLinkElement (org.loboevolution.html.dom.HTMLLinkElement)4 HTMLCollectionImpl (org.loboevolution.html.dom.domimpl.HTMLCollectionImpl)4 MetaInfo (org.loboevolution.info.MetaInfo)4 CSS3Properties (org.loboevolution.html.node.css.CSS3Properties)3 DOMException (com.gargoylesoftware.css.dom.DOMException)2 HTMLCollection (org.loboevolution.html.dom.HTMLCollection)2 CSSValues (org.loboevolution.html.CSSValues)1 BodyFilter (org.loboevolution.html.dom.filter.BodyFilter)1 Node (org.loboevolution.html.node.Node)1 Window (org.loboevolution.html.node.js.Window)1 ComputedCSSStyleDeclaration (org.loboevolution.html.style.ComputedCSSStyleDeclaration)1