Search in sources :

Example 1 with HTMLCollection

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

the class DOMNodeTest method removeChild2.

@Test
public void removeChild2() throws DOMException {
    Element html = document.getDocumentElement();
    Element body = document.createElement("body");
    html.appendChild(body);
    Element elm = document.createElement("span");
    body.appendChild(elm);
    assertSame(body, elm.getParentNode());
    assertFalse(elm.hasChildNodes());
    Text text = document.createTextNode("foo");
    elm.appendChild(text);
    assertTrue(elm.hasChildNodes());
    Text text2 = document.createTextNode("bar");
    body.appendChild(text2);
    assertSame(body, text2.getParentNode());
    Element div = document.createElement("div");
    div.appendChild(document.createTextNode("inside div"));
    body.appendChild(div);
    assertSame(body, div.getParentNode());
    Element p = document.createElement("p");
    p.appendChild(document.createTextNode("inside p"));
    body.appendChild(p);
    assertSame(body, p.getParentNode());
    assertSame(elm.getNextSibling(), text2);
    assertSame(text2.getNextSibling(), div);
    assertSame(div.getNextSibling(), p);
    assertNull(p.getNextSibling());
    assertSame(elm.getNextElementSibling(), div);
    assertSame(text2.getNextElementSibling(), div);
    assertSame(div.getNextElementSibling(), p);
    assertNull(p.getNextElementSibling());
    assertSame(elm, body.getFirstChild());
    assertSame(elm, body.getFirstElementChild());
    assertSame(p, body.getLastChild());
    assertSame(p, body.getLastElementChild());
    // 
    HTMLCollection listspan = body.getElementsByTagName("span");
    HTMLCollection listdiv = body.getElementsByTagName("div");
    HTMLCollection listp = body.getElementsByTagName("p");
    assertEquals(1, listspan.getLength());
    assertEquals(1, listdiv.getLength());
    assertEquals(1, listp.getLength());
    // 
    assertSame(p.getPreviousSibling(), div);
    assertSame(p.getPreviousElementSibling(), div);
    assertSame(div.getPreviousSibling(), text2);
    assertSame(div.getPreviousElementSibling(), elm);
    assertSame(text2.getPreviousSibling(), elm);
    assertSame(text2.getPreviousElementSibling(), elm);
    assertNull(elm.getPreviousSibling());
    assertNull(elm.getPreviousElementSibling());
    assertEquals(4, body.getChildNodes().getLength());
    assertSame(elm, body.getChildNodes().item(0));
    assertSame(text2, body.getChildNodes().item(1));
    assertSame(div, body.getChildNodes().item(2));
    // 
    body.removeChild(div);
    assertTrue(body.hasChildNodes());
    assertNull(div.getParentNode());
    assertNull(div.getNextSibling());
    assertNull(div.getPreviousSibling());
    assertNull(div.getNextElementSibling());
    assertNull(div.getPreviousSibling());
    assertNull(div.getPreviousElementSibling());
    assertSame(elm.getNextSibling(), text2);
    assertSame(text2.getNextSibling(), p);
    assertNull(p.getNextSibling());
    assertSame(elm.getNextElementSibling(), p);
    assertSame(text2.getNextElementSibling(), p);
    assertNull(p.getNextElementSibling());
    assertEquals(1, listspan.getLength());
    assertEquals(0, listdiv.getLength());
    assertEquals(1, listp.getLength());
    assertSame(elm, body.getFirstChild());
    assertSame(p, body.getLastChild());
    assertEquals(3, body.getChildNodes().getLength());
    assertSame(elm, body.getChildNodes().item(0));
    assertSame(p, body.getChildNodes().item(2));
    // 
    elm = (Element) body.removeChild(elm);
    assertTrue(body.hasChildNodes());
    assertNull(elm.getParentNode());
    assertNull(elm.getNextSibling());
    assertNull(elm.getPreviousSibling());
    assertNull(elm.getNextElementSibling());
    assertNull(elm.getPreviousSibling());
    assertNull(elm.getPreviousElementSibling());
    assertSame(text2.getNextSibling(), p);
    assertNull(p.getNextSibling());
    assertSame(text2, p.getPreviousSibling());
    assertSame(text2.getNextElementSibling(), p);
    assertNull(p.getNextElementSibling());
    assertNull(p.getPreviousElementSibling());
    assertEquals(0, listspan.getLength());
    assertEquals(0, listdiv.getLength());
    assertEquals(1, listp.getLength());
    assertSame(text2, body.getFirstChild());
    assertSame(p, body.getLastChild());
    assertSame(p, body.getFirstElementChild());
    assertSame(p, body.getLastElementChild());
    assertEquals(2, body.getChildNodes().getLength());
    assertSame(text2, body.getChildNodes().item(0));
    assertSame(p, body.getChildNodes().item(1));
    // 
    body.removeChild(text2);
    assertNull(text2.getParentNode());
    assertNull(text2.getNextSibling());
    assertNull(text2.getPreviousSibling());
    assertNull(text2.getNextElementSibling());
    assertNull(text2.getPreviousSibling());
    assertNull(text2.getPreviousElementSibling());
    assertNull(p.getNextElementSibling());
    assertNull(p.getPreviousElementSibling());
    assertNull(p.getNextSibling());
    assertNull(p.getPreviousSibling());
    assertEquals(0, listspan.getLength());
    assertEquals(0, listdiv.getLength());
    assertEquals(1, listp.getLength());
    assertSame(p, body.getFirstChild());
    assertSame(p, body.getLastChild());
    assertSame(p, body.getFirstElementChild());
    assertSame(p, body.getLastElementChild());
    assertEquals(1, body.getChildNodes().getLength());
    assertSame(p, body.getChildNodes().item(0));
    // 
    body.removeChild(p);
    assertFalse(body.hasChildNodes());
    assertEquals(0, body.getChildNodes().getLength());
    assertNull(body.getFirstChild());
    assertNull(body.getLastChild());
    assertNull(body.getFirstElementChild());
    assertNull(body.getLastElementChild());
    assertNull(p.getParentNode());
    assertNull(p.getNextSibling());
    assertNull(p.getPreviousSibling());
    assertNull(p.getNextElementSibling());
    assertNull(p.getPreviousSibling());
    assertNull(p.getPreviousElementSibling());
}
Also used : HTMLCollection(org.loboevolution.html.dom.HTMLCollection) Test(org.junit.Test) LoboUnitTest(org.loboevolution.driver.LoboUnitTest)

Example 2 with HTMLCollection

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

the class DOMNodeTest method replaceChild2.

@Test
public void replaceChild2() throws DOMException {
    Element html = document.getDocumentElement();
    Element body = document.createElement("body");
    html.appendChild(body);
    Element span = document.createElement("span");
    body.appendChild(span);
    assertSame(body, span.getParentNode());
    assertFalse(span.hasChildNodes());
    Text text = document.createTextNode("foo");
    span.appendChild(text);
    assertTrue(span.hasChildNodes());
    Text text2 = document.createTextNode("bar");
    body.appendChild(text2);
    assertSame(body, text2.getParentNode());
    Element div = document.createElement("div");
    div.appendChild(document.createTextNode("inside div"));
    body.appendChild(div);
    assertSame(body, div.getParentNode());
    Element p = document.createElement("p");
    p.appendChild(document.createTextNode("inside p"));
    body.appendChild(p);
    // 
    HTMLCollection listspan = body.getElementsByTagName("span");
    HTMLCollection listdiv = body.getElementsByTagName("div");
    HTMLCollection listp = body.getElementsByTagName("p");
    assertEquals(1, listspan.getLength());
    assertEquals(1, listdiv.getLength());
    assertEquals(1, listp.getLength());
    // 
    Element div2 = document.createElement("div");
    div2.setAttribute("id", "div2");
    Element elm = (Element) body.replaceChild(div2, div);
    assertSame(div, elm);
    assertSame(body, div2.getParentNode());
    assertNull(elm.getParentNode());
    assertNull(elm.getNextSibling());
    assertNull(elm.getPreviousSibling());
    assertNull(elm.getNextElementSibling());
    assertNull(elm.getPreviousElementSibling());
    // 
    assertNull(span.getPreviousSibling());
    assertNull(span.getPreviousElementSibling());
    assertSame(span.getNextSibling(), text2);
    assertSame(text2.getNextSibling(), div2);
    assertSame(div2.getNextSibling(), p);
    assertNull(p.getNextSibling());
    assertSame(span.getNextElementSibling(), div2);
    assertSame(text2.getNextElementSibling(), div2);
    assertSame(div2.getNextElementSibling(), p);
    assertNull(p.getNextElementSibling());
    assertSame(span, body.getFirstChild());
    assertSame(span, body.getFirstElementChild());
    assertSame(p, body.getLastChild());
    assertSame(p, body.getLastElementChild());
    assertEquals("div2", div2.getAttribute("id"));
    assertEquals(1, listspan.getLength());
    assertEquals(1, listdiv.getLength());
    assertEquals(1, listp.getLength());
    // 
    assertEquals(4, body.getChildNodes().getLength());
    assertSame(span, body.getChildNodes().item(0));
    assertSame(text2, body.getChildNodes().item(1));
    assertSame(div2, body.getChildNodes().item(2));
    assertSame(p, body.getChildNodes().item(3));
    assertEquals(3, body.getChildren().getLength());
    assertSame(span, body.getChildren().item(0));
    assertSame(div2, body.getChildren().item(1));
    assertSame(p, body.getChildren().item(2));
}
Also used : HTMLCollection(org.loboevolution.html.dom.HTMLCollection) Test(org.junit.Test) LoboUnitTest(org.loboevolution.driver.LoboUnitTest)

Example 3 with HTMLCollection

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

the class HTMLElementTest method getChildren.

@Test
public void getChildren() {
    Element html = document.getDocumentElement();
    Element body = document.createElement("body");
    html.appendChild(body);
    body.appendChild(document.createTextNode("\n   \n"));
    Element div1 = document.createElement("div");
    body.appendChild(div1);
    body.appendChild(document.createTextNode("\n   \n"));
    Element div2 = document.createElement("div");
    body.appendChild(div2);
    body.appendChild(document.createTextNode("\n   \n"));
    Element div3 = document.createElement("div");
    body.appendChild(div3);
    body.appendChild(document.createTextNode("\n   \n"));
    body.appendChild(document.createComment("This is a comment"));
    Element div4 = document.createElement("div");
    body.appendChild(div4);
    body.appendChild(document.createTextNode("\n   \n"));
    body.appendChild(document.createTextNode("\n   \n"));
    HTMLCollection list = body.getChildren();
    assertNotNull(list);
    assertEquals(4, list.getLength());
    assertSame(div1, list.item(0));
    assertSame(div2, list.item(1));
    assertSame(div3, list.item(2));
    assertSame(div4, list.item(3));
    assertNull(list.item(4));
    assertSame(div1, body.getFirstElementChild());
    assertSame(div4, body.getLastElementChild());
    assertEquals(list.getLength(), body.getChildElementCount());
    assertSame(list, body.getChildren());
    list = document.getChildren();
    assertNotNull(list);
    assertEquals(1, list.getLength());
    assertSame(html, list.item(0));
    assertEquals(1, document.getChildElementCount());
    list = html.getChildren();
    assertNotNull(list);
    assertEquals(1, list.getLength());
    assertSame(body, list.item(0));
    assertNull(list.item(1));
    assertNull(list.item(-1));
    assertSame(1, html.getChildElementCount());
    assertSame(body, html.getFirstElementChild());
    assertSame(body, html.getLastElementChild());
    assertSame(list, html.getChildren());
}
Also used : HTMLCollection(org.loboevolution.html.dom.HTMLCollection) Test(org.junit.Test) LoboUnitTest(org.loboevolution.driver.LoboUnitTest)

Example 4 with HTMLCollection

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

the class HTMLDocumentTest method getElementsByTagNameNS.

@Test
public void getElementsByTagNameNS() {
    HTMLCollection list = document.getElementsByTagNameNS("http://www.w3.org/2000/svg", "*");
    assertNotNull(list);
    assertEquals(3, list.getLength());
    Element svg = (Element) list.item(0);
    assertEquals("svg", svg.getNodeName());
    Attr version = svg.getAttributeNode("version");
    assertNull(version.getNamespaceURI());
    assertNull(svg.getPrefix());
    assertEquals("rect", list.item(1).getNodeName());
    list.item(0).appendChild(document.createElementNS("http://www.w3.org/2000/svg", "circle"));
    assertEquals(4, list.getLength());
    HTMLCollection svglist = document.getElementsByTagNameNS("http://www.w3.org/2000/svg", "svg");
    assertNotNull(svglist);
    assertEquals(1, svglist.getLength());
    assertEquals("svg", svglist.item(0).getNodeName());
    list = document.getElementsByTagNameNS("http://www.w3.org/2000/svg", "rect");
    assertNotNull(list);
    assertEquals(1, list.getLength());
    Node oldrect = list.item(0);
    assertEquals("rect", oldrect.getNodeName());
    Element newrect = document.createElementNS("http://www.w3.org/2000/svg", "rect");
    oldrect.getParentNode().appendChild(newrect);
    assertEquals(Node.DOCUMENT_POSITION_PRECEDING, oldrect.compareDocumentPosition(newrect));
    assertEquals(2, list.getLength());
    Node node = svglist.item(0);
    assertEquals("svg", node.getNodeName());
    node.getParentNode().removeChild(node);
    assertEquals(0, svglist.getLength());
    list = document.getElementsByTagNameNS("http://www.w3.org/2000/svg", "xxxxxx");
    assertNotNull(list);
    assertEquals(0, list.getLength());
}
Also used : HTMLCollection(org.loboevolution.html.dom.HTMLCollection) HTMLElement(org.loboevolution.html.dom.HTMLElement) HTMLLinkElement(org.loboevolution.html.dom.HTMLLinkElement) Test(org.junit.Test) LoboUnitTest(org.loboevolution.driver.LoboUnitTest)

Example 5 with HTMLCollection

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

the class HTMLDocumentImpl method getHead.

/**
 * {@inheritDoc}
 */
@Override
public HTMLHeadElementImpl getHead() {
    synchronized (this) {
        final List<Node> list = new LinkedList<>(Arrays.asList(this.getNodeList(new HeadFilter()).toArray()));
        HTMLCollection collection = new HTMLCollectionImpl(this, list);
        if (collection.getLength() > 0) {
            return (HTMLHeadElementImpl) collection.item(0);
        } else {
            return null;
        }
    }
}
Also used : HTMLCollection(org.loboevolution.html.dom.HTMLCollection) Node(org.loboevolution.html.node.Node) HeadFilter(org.loboevolution.html.dom.filter.HeadFilter)

Aggregations

HTMLCollection (org.loboevolution.html.dom.HTMLCollection)14 Test (org.junit.Test)9 LoboUnitTest (org.loboevolution.driver.LoboUnitTest)9 HTMLElement (org.loboevolution.html.dom.HTMLElement)4 HTMLLinkElement (org.loboevolution.html.dom.HTMLLinkElement)3 BodyFilter (org.loboevolution.html.dom.filter.BodyFilter)2 Node (org.loboevolution.html.node.Node)2 HeadFilter (org.loboevolution.html.dom.filter.HeadFilter)1 InputFilter (org.loboevolution.html.dom.filter.InputFilter)1 NodeListImpl (org.loboevolution.html.dom.nodeimpl.NodeListImpl)1