Search in sources :

Example 1 with HTMLCollectionImpl

use of org.loboevolution.html.dom.domimpl.HTMLCollectionImpl in project LoboEvolution by LoboEvolution.

the class DOMDocumentTest method getElementsByClassName.

@Test
public void getElementsByClassName() {
    Document document = domImpl.createDocument("", "doc", null);
    Element docElm = document.getDocumentElement();
    Element elem1 = document.createElement("div");
    elem1.setAttribute("class", "foo bar");
    docElm.appendChild(elem1);
    Element elem2 = document.createElement("p");
    elem2.setAttribute("class", "foo");
    elem1.appendChild(elem2);
    Element elem3 = document.createElement("span");
    elem3.setAttribute("class", "foo");
    elem2.appendChild(elem3);
    Element elem4 = document.createElement("section");
    elem4.setAttribute("class", "bar foo otherclass");
    docElm.appendChild(elem4);
    HTMLCollectionImpl list = (HTMLCollectionImpl) document.getElementsByClassName("foo");
    assertNotNull(list);
    assertEquals(4, list.getLength());
    assertNull(list.item(-1));
    assertSame(elem1, list.item(0));
    assertSame(elem2, list.item(1));
    assertSame(elem3, list.item(2));
    assertSame(elem4, list.item(3));
    assertNull(list.item(4));
    assertFalse(list.isEmpty());
    assertTrue(list.contains(elem1));
    assertTrue(list.contains(elem2));
    assertTrue(list.contains(elem3));
    assertTrue(list.contains(elem4));
    assertFalse(list.contains(docElm));
    Iterator<Node> it = list.iterator();
    assertTrue(it.hasNext());
    assertSame(elem1, it.next());
    assertSame(elem2, it.next());
    assertSame(elem3, it.next());
    assertSame(elem4, it.next());
    assertFalse(it.hasNext());
    try {
        it.next();
        fail("Must throw exception.");
    } catch (NoSuchElementException e) {
    }
    list = (HTMLCollectionImpl) document.getElementsByClassName("bar");
    assertNotNull(list);
    assertEquals(2, list.getLength());
    assertNull(list.item(-1));
    assertSame(elem1, list.item(0));
    assertSame(elem4, list.item(1));
    assertNull(list.item(2));
    assertTrue(list.contains(elem1));
    assertFalse(list.contains(elem2));
    assertFalse(list.contains(elem3));
    assertTrue(list.contains(elem4));
    assertFalse(list.contains(docElm));
    it = list.iterator();
    assertTrue(it.hasNext());
    assertSame(elem1, it.next());
    assertSame(elem4, it.next());
    assertFalse(it.hasNext());
    try {
        it.next();
        fail("Must throw exception.");
    } catch (NoSuchElementException e) {
    }
    list = (HTMLCollectionImpl) document.getElementsByClassName("foo bar");
    assertNotNull(list);
    assertEquals(2, list.getLength());
    assertNull(list.item(-1));
    assertSame(elem1, list.item(0));
    assertSame(elem4, list.item(1));
    assertNull(list.item(2));
    assertTrue(list.contains(elem1));
    assertFalse(list.contains(elem2));
    assertFalse(list.contains(elem3));
    assertTrue(list.contains(elem4));
    assertFalse(list.contains(docElm));
    it = list.iterator();
    assertTrue(it.hasNext());
    assertSame(elem1, it.next());
    assertSame(elem4, it.next());
    assertFalse(it.hasNext());
    try {
        it.next();
        fail("Must throw exception.");
    } catch (NoSuchElementException e) {
    }
    list = (HTMLCollectionImpl) document.getElementsByClassName("bar foo");
    assertNotNull(list);
    assertEquals(2, list.getLength());
    assertNull(list.item(-1));
    assertSame(elem1, list.item(0));
    assertSame(elem4, list.item(1));
    assertNull(list.item(2));
    assertTrue(list.contains(elem1));
    assertFalse(list.contains(elem2));
    assertFalse(list.contains(elem3));
    assertTrue(list.contains(elem4));
    assertFalse(list.contains(docElm));
    it = list.iterator();
    assertTrue(it.hasNext());
    assertSame(elem1, it.next());
    assertSame(elem4, it.next());
    assertFalse(it.hasNext());
    try {
        it.next();
        fail("Must throw exception.");
    } catch (NoSuchElementException e) {
    }
    list = (HTMLCollectionImpl) document.getElementsByClassName("otherclass");
    assertNotNull(list);
    assertEquals(1, list.getLength());
    assertNull(list.item(-1));
    assertSame(elem4, list.item(0));
    assertNull(list.item(1));
    assertFalse(list.contains(elem1));
    assertFalse(list.contains(elem2));
    assertFalse(list.contains(elem3));
    assertTrue(list.contains(elem4));
    assertFalse(list.contains(docElm));
    it = list.iterator();
    assertTrue(it.hasNext());
    assertSame(elem4, it.next());
    assertFalse(it.hasNext());
    try {
        it.next();
        fail("Must throw exception.");
    } catch (NoSuchElementException e) {
    }
    list = (HTMLCollectionImpl) document.getElementsByClassName("noclass");
    assertNotNull(list);
    assertEquals(0, list.getLength());
    assertNull(list.item(-1));
    assertNull(list.item(0));
    assertTrue(list.isEmpty());
    assertFalse(list.contains(elem1));
    assertFalse(list.contains(elem2));
    assertFalse(list.contains(elem3));
    assertFalse(list.contains(elem4));
    assertFalse(list.contains(docElm));
    it = list.iterator();
    assertFalse(it.hasNext());
    list = (HTMLCollectionImpl) document.getElementsByClassName("");
    assertNotNull(list);
    assertEquals(0, list.getLength());
    assertNull(list.item(-1));
    assertNull(list.item(0));
    assertTrue(list.isEmpty());
    assertFalse(list.contains(elem1));
    assertFalse(list.contains(elem2));
    assertFalse(list.contains(elem3));
    assertFalse(list.contains(elem4));
    assertFalse(list.contains(docElm));
    it = list.iterator();
    assertFalse(it.hasNext());
    try {
        it.next();
        fail("Must throw exception.");
    } catch (NoSuchElementException e) {
    }
    list = (HTMLCollectionImpl) elem2.getElementsByClassName("bar");
    assertNotNull(list);
    assertEquals(0, list.getLength());
    assertNull(list.item(-1));
    assertNull(list.item(0));
    assertTrue(list.isEmpty());
    assertFalse(list.contains(elem1));
    assertFalse(list.contains(elem2));
    assertFalse(list.contains(elem3));
    assertFalse(list.contains(elem4));
    assertFalse(list.contains(docElm));
    it = list.iterator();
    assertFalse(it.hasNext());
    try {
        it.next();
        fail("Must throw exception.");
    } catch (NoSuchElementException e) {
    }
    list = (HTMLCollectionImpl) elem3.getElementsByClassName("foo");
    assertNotNull(list);
    assertEquals(0, list.getLength());
    assertNull(list.item(-1));
    assertNull(list.item(0));
    assertTrue(list.isEmpty());
    assertFalse(list.contains(elem1));
    assertFalse(list.contains(elem2));
    assertFalse(list.contains(elem3));
    assertFalse(list.contains(elem4));
    assertFalse(list.contains(docElm));
    it = list.iterator();
    assertFalse(it.hasNext());
    try {
        it.next();
        fail("Must throw exception.");
    } catch (NoSuchElementException e) {
    }
    list = (HTMLCollectionImpl) elem2.getElementsByClassName("foo");
    assertNotNull(list);
    assertEquals(1, list.getLength());
    assertNull(list.item(-1));
    assertSame(elem3, list.item(0));
    assertNull(list.item(1));
    assertFalse(list.isEmpty());
    assertFalse(list.contains(elem1));
    assertFalse(list.contains(elem2));
    assertFalse(list.contains(elem4));
    assertTrue(list.contains(elem3));
    assertFalse(list.contains(docElm));
    it = list.iterator();
    assertTrue(it.hasNext());
    assertSame(elem3, it.next());
    assertFalse(it.hasNext());
    try {
        it.next();
        fail("Must throw exception.");
    } catch (NoSuchElementException e) {
    }
}
Also used : HTMLCollectionImpl(org.loboevolution.html.dom.domimpl.HTMLCollectionImpl) NoSuchElementException(java.util.NoSuchElementException) Test(org.junit.Test) LoboUnitTest(org.loboevolution.driver.LoboUnitTest)

Example 2 with HTMLCollectionImpl

use of org.loboevolution.html.dom.domimpl.HTMLCollectionImpl in project LoboEvolution by LoboEvolution.

the class DOMDocumentTest method getElementsByTagNameNSAsterisk.

@Test
public void getElementsByTagNameNSAsterisk() {
    Document document = domImpl.createDocument(Document.XML_NAMESPACE_URI, "doc", null);
    Element docElm = document.getDocumentElement();
    Element elem1 = document.createElementNS(Document.XML_NAMESPACE_URI, "div");
    elem1.setAttribute("id", "div1");
    docElm.appendChild(elem1);
    Element elem2 = document.createElementNS(Document.XML_NAMESPACE_URI, "div");
    elem2.setAttribute("id", "div2");
    elem1.appendChild(elem2);
    Element elem3 = document.createElementNS(Document.XML_NAMESPACE_URI, "div");
    elem3.setAttribute("id", "div3");
    elem2.appendChild(elem3);
    Element elem4 = document.createElementNS(Document.XML_NAMESPACE_URI, "div");
    elem4.setAttribute("id", "div4");
    docElm.appendChild(elem4);
    HTMLCollectionImpl list = (HTMLCollectionImpl) document.getElementsByTagNameNS("*", "div");
    assertNotNull(list);
    assertEquals(4, list.getLength());
    assertNull(list.item(-1));
    assertSame(elem1, list.item(0));
    assertSame(elem2, list.item(1));
    assertSame(elem3, list.item(2));
    assertSame(elem4, list.item(3));
    assertNull(list.item(4));
    Iterator<Node> it = list.iterator();
    assertTrue(it.hasNext());
    assertSame(elem1, it.next());
    assertSame(elem2, it.next());
    assertSame(elem3, it.next());
    assertSame(elem4, it.next());
    assertFalse(it.hasNext());
    try {
        it.next();
        fail("Must throw exception.");
    } catch (NoSuchElementException e) {
    }
}
Also used : HTMLCollectionImpl(org.loboevolution.html.dom.domimpl.HTMLCollectionImpl) NoSuchElementException(java.util.NoSuchElementException) Test(org.junit.Test) LoboUnitTest(org.loboevolution.driver.LoboUnitTest)

Example 3 with HTMLCollectionImpl

use of org.loboevolution.html.dom.domimpl.HTMLCollectionImpl in project LoboEvolution by LoboEvolution.

the class DOMDocumentTest method getElementsByTagName.

@Test
public void getElementsByTagName() {
    Document document = domImpl.createDocument("", "doc", null);
    Element docElm = document.getDocumentElement();
    Element elem1 = document.createElement("div");
    elem1.setAttribute("id", "div1");
    docElm.appendChild(elem1);
    Element elem2 = document.createElement("div");
    elem2.setAttribute("id", "div2");
    elem1.appendChild(elem2);
    Element elem3 = document.createElement("div");
    elem3.setAttribute("id", "div3");
    elem2.appendChild(elem3);
    Element elem4 = document.createElement("div");
    elem4.setAttribute("id", "div4");
    docElm.appendChild(elem4);
    HTMLCollectionImpl list = (HTMLCollectionImpl) document.getElementsByTagName("div");
    assertNotNull(list);
    assertEquals(4, list.getLength());
    assertNull(list.item(-1));
    assertSame(elem1, list.item(0));
    assertSame(elem2, list.item(1));
    assertSame(elem3, list.item(2));
    assertSame(elem4, list.item(3));
    assertNull(list.item(4));
    assertNotEquals(0, list.getLength());
    assertTrue(list.contains(elem1));
    assertTrue(list.contains(elem2));
    assertTrue(list.contains(elem3));
    assertTrue(list.contains(elem4));
    assertFalse(list.contains(docElm));
    Iterator<Node> it = list.iterator();
    assertTrue(it.hasNext());
    assertFalse(list.isEmpty());
    assertSame(elem1, it.next());
    assertSame(elem2, it.next());
    assertSame(elem3, it.next());
    assertSame(elem4, it.next());
    assertFalse(it.hasNext());
    try {
        it.next();
        fail("Must throw exception.");
    } catch (NoSuchElementException e) {
    }
    list = (HTMLCollectionImpl) elem4.getElementsByTagName("div");
    assertNotNull(list);
    assertEquals(0, list.getLength());
    assertNull(list.item(-1));
    assertNull(list.item(0));
    assertTrue(list.isEmpty());
    assertFalse(list.contains(elem1));
    assertFalse(list.contains(elem4));
    it = list.iterator();
    assertFalse(it.hasNext());
    try {
        it.next();
        fail("Must throw exception.");
    } catch (NoSuchElementException e) {
    }
}
Also used : HTMLCollectionImpl(org.loboevolution.html.dom.domimpl.HTMLCollectionImpl) NoSuchElementException(java.util.NoSuchElementException) Test(org.junit.Test) LoboUnitTest(org.loboevolution.driver.LoboUnitTest)

Example 4 with HTMLCollectionImpl

use of org.loboevolution.html.dom.domimpl.HTMLCollectionImpl in project LoboEvolution by LoboEvolution.

the class DOMDocumentTest method getElementsByTagNameNSMixed.

@Test
public void getElementsByTagNameNSMixed() {
    Document document = domImpl.createDocument(Document.XML_NAMESPACE_URI, "doc", null);
    Element docElm = document.getDocumentElement();
    Element elem1 = document.createElementNS("http://www.example.com/differentns", "div");
    elem1.setAttribute("id", "div1");
    docElm.appendChild(elem1);
    Element elem2 = document.createElementNS("http://www.example.com/differentns", "div");
    elem2.setAttribute("id", "div2");
    elem1.appendChild(elem2);
    Element elem3 = document.createElementNS(Document.XML_NAMESPACE_URI, "div");
    elem3.setAttribute("id", "div3");
    elem2.appendChild(elem3);
    Element elem4 = document.createElementNS("http://www.example.com/differentns", "div");
    elem4.setAttribute("id", "div4");
    docElm.appendChild(elem4);
    HTMLCollectionImpl list = (HTMLCollectionImpl) document.getElementsByTagNameNS(Document.XML_NAMESPACE_URI, "div");
    assertNotNull(list);
    assertEquals(1, list.getLength());
    assertNull(list.item(-1));
    assertSame(elem3, list.item(0));
    assertNull(list.item(1));
    Iterator<Node> it = list.iterator();
    assertTrue(it.hasNext());
    assertSame(elem3, it.next());
    try {
        it.next();
        fail("Must throw exception.");
    } catch (NoSuchElementException e) {
    }
    list = (HTMLCollectionImpl) document.getElementsByTagNameNS("http://www.example.com/differentns", "div");
    assertNotNull(list);
    assertEquals(3, list.getLength());
    assertNull(list.item(-1));
    assertSame(elem1, list.item(0));
    assertSame(elem2, list.item(1));
    assertSame(elem4, list.item(2));
    assertNull(list.item(3));
    it = list.iterator();
    assertTrue(it.hasNext());
    assertSame(elem1, it.next());
    assertSame(elem2, it.next());
    assertSame(elem4, it.next());
    assertFalse(it.hasNext());
    try {
        it.next();
        fail("Must throw exception.");
    } catch (NoSuchElementException e) {
    }
    list = (HTMLCollectionImpl) elem1.getElementsByTagNameNS("http://www.example.com/differentns", "div");
    assertNotNull(list);
    assertEquals(1, list.getLength());
    assertNull(list.item(-1));
    assertSame(elem2, list.item(0));
    assertNull(list.item(1));
    it = list.iterator();
    assertTrue(it.hasNext());
    assertSame(elem2, it.next());
    assertFalse(it.hasNext());
    try {
        it.next();
        fail("Must throw exception.");
    } catch (NoSuchElementException e) {
    }
    list = (HTMLCollectionImpl) elem1.getElementsByTagNameNS(Document.XML_NAMESPACE_URI, "div");
    assertNotNull(list);
    assertEquals(1, list.getLength());
    assertNull(list.item(-1));
    assertSame(elem3, list.item(0));
    assertNull(list.item(1));
    it = list.iterator();
    assertTrue(it.hasNext());
    assertSame(elem3, it.next());
    assertFalse(it.hasNext());
    try {
        it.next();
        fail("Must throw exception.");
    } catch (NoSuchElementException e) {
    }
    list = (HTMLCollectionImpl) elem3.getElementsByTagNameNS(Document.XML_NAMESPACE_URI, "div");
    assertNotNull(list);
    assertEquals(0, list.getLength());
    assertNull(list.item(-1));
    assertNull(list.item(0));
    it = list.iterator();
    assertFalse(it.hasNext());
    try {
        it.next();
        fail("Must throw exception.");
    } catch (NoSuchElementException e) {
    }
}
Also used : HTMLCollectionImpl(org.loboevolution.html.dom.domimpl.HTMLCollectionImpl) NoSuchElementException(java.util.NoSuchElementException) Test(org.junit.Test) LoboUnitTest(org.loboevolution.driver.LoboUnitTest)

Example 5 with HTMLCollectionImpl

use of org.loboevolution.html.dom.domimpl.HTMLCollectionImpl in project LoboEvolution by LoboEvolution.

the class DOMElementTest method testGetChildren.

@Test
public void testGetChildren() {
    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"));
    HTMLCollectionImpl list = (HTMLCollectionImpl) 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());
    assertFalse(list.isEmpty());
    // 
    list = (HTMLCollectionImpl) document.getChildren();
    assertNotNull(list);
    assertEquals(1, list.getLength());
    assertSame(html, list.item(0));
    assertEquals(1, document.getChildElementCount());
    assertFalse(list.isEmpty());
    // 
    list = (HTMLCollectionImpl) html.getChildren();
    assertNotNull(list);
    assertEquals(1, list.getLength());
    assertSame(body, list.item(0));
    assertNull(list.item(1));
    assertNull(list.item(-1));
    assertEquals(1, html.getChildElementCount());
    assertSame(body, html.getFirstElementChild());
    assertSame(body, html.getLastElementChild());
    assertSame(list, html.getChildren());
    assertFalse(list.isEmpty());
    // 
    list = (HTMLCollectionImpl) div4.getChildren();
    assertTrue(list.isEmpty());
    div4.appendChild(document.createTextNode(" "));
    assertTrue(list.isEmpty());
}
Also used : HTMLCollectionImpl(org.loboevolution.html.dom.domimpl.HTMLCollectionImpl) Test(org.junit.Test) LoboUnitTest(org.loboevolution.driver.LoboUnitTest)

Aggregations

HTMLCollectionImpl (org.loboevolution.html.dom.domimpl.HTMLCollectionImpl)14 Test (org.junit.Test)10 LoboUnitTest (org.loboevolution.driver.LoboUnitTest)10 NoSuchElementException (java.util.NoSuchElementException)6 ArrayList (java.util.ArrayList)4 HTMLElement (org.loboevolution.html.dom.HTMLElement)4 HTMLDocumentImpl (org.loboevolution.html.dom.domimpl.HTMLDocumentImpl)4 MetaInfo (org.loboevolution.info.MetaInfo)4