Search in sources :

Example 16 with DOMException

use of com.gargoylesoftware.css.dom.DOMException in project LoboEvolution by LoboEvolution.

the class HTMLOptionsCollectionImpl method addElements.

private void addElements(HTMLOptionElement element, HTMLElement before) {
    List<Node> nodeList = getList();
    if (nodeList.size() == 0) {
        nodeList.add(0, element);
    } else {
        boolean found = false;
        HTMLOptionElement bef = (HTMLOptionElement) before;
        for (int i = 0; i < nodeList.size(); i++) {
            HTMLOptionElement elem = (HTMLOptionElement) nodeList.get(i);
            if (elem.getText().equals(bef.getText())) {
                nodeList.add(i, element);
                found = true;
                break;
            }
        }
        if (!found)
            throw new DOMException(DOMException.NOT_FOUND_ERR, "Record not found");
    }
}
Also used : DOMException(com.gargoylesoftware.css.dom.DOMException) HTMLOptionElement(org.loboevolution.html.dom.HTMLOptionElement) Node(org.loboevolution.html.node.Node)

Example 17 with DOMException

use of com.gargoylesoftware.css.dom.DOMException in project LoboEvolution by LoboEvolution.

the class HTMLTableElementImpl method insertRow.

private HTMLTableRowElementImpl insertRow(Object objIndex, String tagName) throws Exception {
    final Document doc = this.document;
    if (doc == null) {
        throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, "Orphan element");
    }
    final HTMLTableRowElementImpl rowElement = (HTMLTableRowElementImpl) doc.createElement("TR");
    int index = -1;
    if (objIndex instanceof Double) {
        index = ((Double) objIndex).intValue();
    } else {
        if (objIndex == null || "".equals(objIndex)) {
            index = 0;
        } else {
            index = Integer.parseInt(objIndex.toString());
        }
    }
    if (index == 0 || index == -1) {
        appendChild(rowElement);
        AtomicInteger cellIndex = new AtomicInteger(-1);
        if (index == -1) {
            NodeListImpl childNodes = (NodeListImpl) getChildNodes();
            childNodes.forEach(node -> {
                if (node instanceof HTMLTableRowElement) {
                    cellIndex.incrementAndGet();
                }
            });
        }
        rowElement.setIndex(index == -1 ? cellIndex.get() : 0);
        return rowElement;
    }
    AtomicInteger trcount = new AtomicInteger();
    nodeList.forEach(node -> {
        if (node instanceof HTMLTableRowElement) {
            trcount.incrementAndGet();
        }
    });
    if (trcount.get() < index) {
        throw new DOMException(DOMException.INDEX_SIZE_ERR, "The index is greater than the number of cells in the table ");
    } else {
        rowElement.setIndex(index);
        insertAt(rowElement, index);
    }
    return rowElement;
}
Also used : DOMException(com.gargoylesoftware.css.dom.DOMException) NodeListImpl(org.loboevolution.html.dom.nodeimpl.NodeListImpl) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Document(org.loboevolution.html.node.Document)

Example 18 with DOMException

use of com.gargoylesoftware.css.dom.DOMException in project LoboEvolution by LoboEvolution.

the class HTMLTableRowElementImpl method insertCell.

private HTMLTableCellElementImpl insertCell(Object objIndex, String tagName) {
    final Document doc = this.document;
    if (doc == null) {
        throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, "Orphan element");
    }
    HTMLTableCellElementImpl cellElement = (HTMLTableCellElementImpl) doc.createElement(tagName);
    int index = -1;
    if (objIndex instanceof Double) {
        index = ((Double) objIndex).intValue();
    } else {
        if (objIndex == null || "".equals(objIndex)) {
            index = 0;
        } else {
            index = Integer.parseInt(objIndex.toString());
        }
    }
    if (index == 0 || index == -1) {
        appendChild(cellElement);
        AtomicInteger cellIndex = new AtomicInteger(-1);
        if (index == -1) {
            NodeListImpl childNodes = (NodeListImpl) getParentNode().getChildNodes();
            childNodes.forEach(node -> {
                if (node instanceof HTMLTableCellElementImpl) {
                    cellIndex.incrementAndGet();
                }
            });
        }
        cellElement.setIndex(index == -1 ? cellIndex.get() : 0);
        return cellElement;
    }
    AtomicInteger trcount = new AtomicInteger();
    nodeList.forEach(node -> {
        if (node instanceof HTMLTableCellElement) {
            trcount.incrementAndGet();
        }
    });
    if (trcount.get() < index) {
        throw new DOMException(DOMException.INDEX_SIZE_ERR, "The index is greater than the number of cells in the table ");
    } else {
        cellElement.setIndex(index);
        insertAt(cellElement, index);
    }
    return cellElement;
}
Also used : DOMException(com.gargoylesoftware.css.dom.DOMException) NodeListImpl(org.loboevolution.html.dom.nodeimpl.NodeListImpl) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HTMLTableCellElement(org.loboevolution.html.dom.HTMLTableCellElement) Document(org.loboevolution.html.node.Document)

Example 19 with DOMException

use of com.gargoylesoftware.css.dom.DOMException in project LoboEvolution by LoboEvolution.

the class DOMImplementationTest method testCreateDocument2.

@Test
public void testCreateDocument2() {
    DocumentType doctype = domImpl.createDocumentType("html", null, null);
    assertNull(doctype.getNextSibling());
    assertNull(doctype.getPreviousSibling());
    assertNull(doctype.getParentNode());
    assertNull(doctype.getOwnerDocument());
    Document document = domImpl.createDocument(null, null, doctype);
    assertNull(doctype.getNextSibling());
    assertNull(doctype.getPreviousSibling());
    assertSame(document, doctype.getOwnerDocument());
    try {
        domImpl.createDocument(null, null, doctype);
        fail("Must throw an exception");
    } catch (DOMException e) {
        assertEquals(DOMException.WRONG_DOCUMENT_ERR, e.getCode());
    }
}
Also used : DOMException(com.gargoylesoftware.css.dom.DOMException) DocumentType(org.loboevolution.html.node.DocumentType) Document(org.loboevolution.html.node.Document) Test(org.junit.Test) LoboUnitTest(org.loboevolution.driver.LoboUnitTest)

Example 20 with DOMException

use of com.gargoylesoftware.css.dom.DOMException in project LoboEvolution by LoboEvolution.

the class HTMLDocumentTest method testStyleElement.

@Test
public void testStyleElement() {
    Element style = (Element) document.getElementsByTagName("style").item(0);
    CSSStyleSheetImpl sheet = (CSSStyleSheetImpl) ((HTMLStyleElement) style).getStyleSheet();
    assertNotNull(sheet);
    assertEquals(0, sheet.getMedia().getLength());
    assertTrue(sheet.getCssRules().getLength() > 0);
    assertSame(sheet.getOwnerNode(), style);
    style.setAttribute("media", "screen");
    CSSStyleSheetImpl sheet2 = (CSSStyleSheetImpl) ((HTMLStyleElement) style).getStyleSheet();
    assertNotNull(sheet2);
    assertSame(sheet2, sheet);
    assertEquals(1, sheet2.getMedia().getLength());
    assertEquals("screen", sheet2.getMedia().item(0));
    assertTrue(sheet2.getCssRules().getLength() > 0);
    style.setTextContent("body {font-size: 14pt; margin-left: 7%;} h1 {font-size: 2.4em;}");
    sheet = (CSSStyleSheetImpl) ((HTMLLinkElement) style).getSheet();
    assertSame(sheet2, sheet);
    assertEquals(2, sheet.getCssRules().getLength());
    assertSame(sheet.getOwnerNode(), style);
    assertEquals(2, sheet.insertRule("h3 {font-family: Arial}", 2));
    style.normalize();
    assertEquals("body {font-size: 14pt; margin-left: 7%; }h1 {font-size: 2.4em; }h3 {font-family: Arial; }", style.getTextContent());
    Attr type = style.getAttributeNode("type");
    type.setNodeValue("foo");
    assertNull(((HTMLLinkElement) style).getSheet());
    assertEquals("body {font-size: 14pt; margin-left: 7%; }h1 {font-size: 2.4em; }h3 {font-family: Arial; }", style.getTextContent());
    type.setNodeValue("");
    assertNotNull(((HTMLLinkElement) style).getSheet());
    assertEquals("body {font-size: 14pt; margin-left: 7%; }h1 {font-size: 2.4em; }h3 {font-family: Arial; }", style.getTextContent());
    type.setNodeValue("text/CSS");
    sheet = (CSSStyleSheetImpl) ((HTMLLinkElement) style).getSheet();
    assertNotNull(sheet);
    Attr media = style.getAttributeNode("media");
    media.setNodeValue("&%/(*");
    assertNull(((HTMLLinkElement) style).getSheet());
    media.setNodeValue("screen");
    sheet = (CSSStyleSheetImpl) ((HTMLLinkElement) style).getSheet();
    assertNotNull(sheet);
    long sz = sheet.getCssRules().getLength();
    assertEquals(3, sz);
    Text text = document.createTextNode("@namespace svg url('http://www.w3.org/2000/svg');\n");
    style.insertBefore(text, style.getFirstChild());
    long szp1 = sz + 1;
    assertEquals(szp1, sheet.getCssRules().getLength());
    sheet = (CSSStyleSheetImpl) ((HTMLLinkElement) style).getSheet();
    CSSRuleList rules = sheet.getCssRules();
    assertEquals(szp1, rules.getLength());
    Text text2 = document.createTextNode("@font-feature-values Some Font, Other Font {@swash{swishy:1;flowing:2;}@styleset{double-W:14;sharp-terminals:16 1;}}\n");
    style.replaceChild(text2, text);
    assertEquals(szp1, sheet.getCssRules().getLength());
    sheet = (CSSStyleSheetImpl) ((HTMLLinkElement) style).getSheet();
    rules = sheet.getCssRules();
    assertEquals(szp1, rules.getLength());
    try {
        style.removeChild(text);
        fail("Must throw exception");
    } catch (DOMException e) {
        assertEquals(DOMException.NOT_FOUND_ERR, e.getCode());
    }
    style.removeChild(text2);
    assertEquals(sz, sheet.getCssRules().getLength());
    sheet = (CSSStyleSheetImpl) ((HTMLLinkElement) style).getSheet();
    rules = sheet.getCssRules();
    assertEquals(sz, rules.getLength());
    style.setTextContent("$@foo{bar}");
    type.setNodeValue("text/xsl");
    style.setTextContent("<?xml version=\"1.0\"?><xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">" + "<xsl:output method=\"text\"/><xsl:template match=\"foo\">bar<xsl:value-of select=\".\"/>" + "</xsl:template></xsl:stylesheet>");
    assertNull(((HTMLLinkElement) style).getSheet());
    assertEquals("<?xml version=\"1.0\"?><xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">" + "<xsl:output method=\"text\"/><xsl:template match=\"foo\">bar<xsl:value-of select=\".\"/>" + "</xsl:template></xsl:stylesheet>", style.getTextContent());
    style.normalize();
    assertEquals("<?xml version=\"1.0\"?><xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">" + "<xsl:output method=\"text\"/><xsl:template match=\"foo\">bar<xsl:value-of select=\".\"/>" + "</xsl:template></xsl:stylesheet>", style.getTextContent());
}
Also used : DOMException(com.gargoylesoftware.css.dom.DOMException) CSSStyleSheetImpl(org.loboevolution.html.js.css.CSSStyleSheetImpl) CSSRuleList(org.loboevolution.html.node.css.CSSRuleList) Test(org.junit.Test) LoboUnitTest(org.loboevolution.driver.LoboUnitTest)

Aggregations

DOMException (com.gargoylesoftware.css.dom.DOMException)31 Test (org.junit.Test)12 LoboUnitTest (org.loboevolution.driver.LoboUnitTest)12 HTMLElement (org.loboevolution.html.dom.HTMLElement)8 HTMLLinkElement (org.loboevolution.html.dom.HTMLLinkElement)8 Document (org.loboevolution.html.node.Document)8 NodeListImpl (org.loboevolution.html.dom.nodeimpl.NodeListImpl)6 Selector (com.gargoylesoftware.css.parser.selector.Selector)4 SelectorList (com.gargoylesoftware.css.parser.selector.SelectorList)4 Node (org.loboevolution.html.node.Node)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 HTMLBodyElement (org.loboevolution.html.dom.HTMLBodyElement)2 ElementFilter (org.loboevolution.html.dom.filter.ElementFilter)2 DocumentType (org.loboevolution.html.node.DocumentType)2 UserAgentContext (org.loboevolution.http.UserAgentContext)2 CSSStyleDeclarationImpl (com.gargoylesoftware.css.dom.CSSStyleDeclarationImpl)1 CSSOMParser (com.gargoylesoftware.css.parser.CSSOMParser)1 CSS3Parser (com.gargoylesoftware.css.parser.javacc.CSS3Parser)1