Search in sources :

Example 26 with DOMException

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

the class HtmlParser method readTag.

/**
 * The reader offset should be
 *
 * @param reader
 * @return
 */
private String readTag(final Node parent, final LineNumberReader reader) throws IOException {
    final StringBuilder sb = new StringBuilder();
    int chInt;
    chInt = reader.read();
    if (chInt != -1) {
        boolean cont = true;
        char ch;
        LOOP: for (; ; ) {
            ch = (char) chInt;
            if (Character.isLetter(ch)) {
                // Speed up normal case
                break LOOP;
            } else if (ch == '!') {
                sb.append('!');
                chInt = reader.read();
                if (chInt != -1) {
                    ch = (char) chInt;
                    if (ch == '-') {
                        sb.append('-');
                        chInt = reader.read();
                        if (chInt != -1) {
                            ch = (char) chInt;
                            if (ch == '-') {
                                sb.append('-');
                                cont = false;
                            }
                        } else {
                            cont = false;
                        }
                    }
                } else {
                    cont = false;
                }
            } else if (ch == '/') {
                sb.append(ch);
                chInt = reader.read();
                if (chInt != -1) {
                    ch = (char) chInt;
                } else {
                    cont = false;
                }
            } else if (ch == '<') {
                final StringBuilder ltText = new StringBuilder(3);
                ltText.append('<');
                while ((chInt = reader.read()) == '<') {
                    ltText.append('<');
                }
                final Document doc = this.document;
                final Node textNode = doc.createTextNode(ltText.toString());
                try {
                    parent.appendChild(textNode);
                } catch (final DOMException de) {
                    if ((parent.getNodeType() != NodeType.DOCUMENT_NODE) || (de.getCode() != DOMException.HIERARCHY_REQUEST_ERR)) {
                        logger.log(Level.WARNING, "parseToken(): Unable to append child to " + parent + ".", de);
                    }
                }
                if (chInt == -1) {
                    cont = false;
                } else {
                    continue;
                }
            } else if (Character.isWhitespace(ch)) {
                final StringBuilder ltText = new StringBuilder();
                ltText.append('<');
                ltText.append(ch);
                while ((chInt = reader.read()) != -1) {
                    ch = (char) chInt;
                    if (ch == '<') {
                        chInt = reader.read();
                        break;
                    }
                    ltText.append(ch);
                }
                final Document doc = this.document;
                final Node textNode = doc.createTextNode(ltText.toString());
                try {
                    parent.appendChild(textNode);
                } catch (final DOMException de) {
                    if ((parent.getNodeType() != NodeType.DOCUMENT_NODE) || (de.getCode() != DOMException.HIERARCHY_REQUEST_ERR)) {
                        logger.log(Level.WARNING, "parseToken(): Unable to append child to " + parent + ".", de);
                    }
                }
                if (chInt == -1) {
                    cont = false;
                } else {
                    continue;
                }
            }
            break LOOP;
        }
        if (cont) {
            boolean lastCharSlash = false;
            for (; ; ) {
                if (Character.isWhitespace(ch)) {
                    break;
                } else if (ch == '>') {
                    this.justReadTagEnd = true;
                    this.justReadTagBegin = false;
                    this.justReadEmptyElement = lastCharSlash;
                    final String tag = sb.toString();
                    return tag;
                } else if (ch == '/') {
                    lastCharSlash = true;
                } else {
                    if (lastCharSlash) {
                        sb.append('/');
                    }
                    lastCharSlash = false;
                    sb.append(ch);
                }
                chInt = reader.read();
                if (chInt == -1) {
                    break;
                }
                ch = (char) chInt;
            }
        }
    }
    if (sb.length() > 0) {
        this.justReadTagEnd = false;
        this.justReadTagBegin = false;
        this.justReadEmptyElement = false;
    }
    final String tag = sb.toString();
    return tag;
}
Also used : DOMException(com.gargoylesoftware.css.dom.DOMException) Node(org.loboevolution.html.node.Node) Document(org.loboevolution.html.node.Document)

Example 27 with DOMException

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

the class AbstractCSSProperties method setCssText.

/**
 * <p>setCssText.</p>
 *
 * @param cssText a {@link java.lang.String} object.
 */
public void setCssText(final String cssText) throws DOMException {
    AbstractCSSProperties sds = new AbstractCSSProperties(null);
    if (Strings.isNotBlank(cssText)) {
        final CSSOMParser parser = new CSSOMParser(new CSS3Parser());
        try {
            final CSSStyleDeclarationImpl sd = parser.parseStyleDeclaration(cssText);
            sds.addStyleDeclaration(sd);
            setLocalStyleProperties(sds);
            if (Strings.isBlank(this.cssText)) {
                this.cssText = cssText;
                context.setAttribute("style", cssText);
            }
        } catch (final Exception err) {
            logger.log(Level.WARNING, "Unable to parse style attribute value", err);
        }
    }
}
Also used : CSSOMParser(com.gargoylesoftware.css.parser.CSSOMParser) CSS3Parser(com.gargoylesoftware.css.parser.javacc.CSS3Parser) CSSStyleDeclarationImpl(com.gargoylesoftware.css.dom.CSSStyleDeclarationImpl) DOMException(com.gargoylesoftware.css.dom.DOMException)

Example 28 with DOMException

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

the class HTMLDocumentTest method testAttributes.

@Test
public void testAttributes() {
    Element p = document.createElement("p");
    Attr attr = document.createAttribute("id");
    attr.setValue("theId");
    p.setAttributeNode(attr);
    Attr cloned = (Attr) attr.cloneNode(false);
    assertNotNull(cloned);
    assertEquals(attr.getName(), cloned.getName());
    assertEquals(attr.getNamespaceURI(), cloned.getNamespaceURI());
    assertEquals(attr.getValue(), cloned.getValue());
    try {
        document.createAttribute(null);
        fail("Must throw exception");
    } catch (DOMException e) {
        assertEquals(DOMException.INVALID_CHARACTER_ERR, e.getCode());
    }
    try {
        document.createAttribute("");
        fail("Must throw exception");
    } catch (DOMException e) {
        assertEquals(DOMException.INVALID_CHARACTER_ERR, e.getCode());
    }
    try {
        document.createAttribute("\u0000");
        fail("Must throw exception");
    } catch (DOMException e) {
        assertEquals(DOMException.INVALID_CHARACTER_ERR, e.getCode());
    }
    try {
        document.createAttribute("<");
        fail("Must throw exception");
    } catch (DOMException e) {
        assertEquals(DOMException.INVALID_CHARACTER_ERR, e.getCode());
    }
    try {
        document.createAttribute(">");
        fail("Must throw exception");
    } catch (DOMException e) {
        assertEquals(DOMException.INVALID_CHARACTER_ERR, e.getCode());
    }
    try {
        document.createAttribute("\"");
        fail("Must throw exception");
    } catch (DOMException e) {
        assertEquals(DOMException.INVALID_CHARACTER_ERR, e.getCode());
    }
    try {
        document.createAttributeNS(Document.XML_NAMESPACE_URI, null);
        fail("Must throw exception");
    } catch (DOMException e) {
        assertEquals(DOMException.INVALID_CHARACTER_ERR, e.getCode());
    }
    try {
        document.createAttributeNS(Document.XML_NAMESPACE_URI, "");
        fail("Must throw exception");
    } catch (DOMException e) {
        assertEquals(DOMException.INVALID_CHARACTER_ERR, e.getCode());
    }
    try {
        document.createAttributeNS(Document.XML_NAMESPACE_URI, ":");
        fail("Must throw exception");
    } catch (DOMException e) {
        assertEquals(DOMException.INVALID_CHARACTER_ERR, e.getCode());
    }
    try {
        document.createAttributeNS(Document.XML_NAMESPACE_URI, "x:");
        fail("Must throw exception");
    } catch (DOMException e) {
        assertEquals(DOMException.INVALID_CHARACTER_ERR, e.getCode());
    }
    try {
        document.createAttributeNS(Document.XML_NAMESPACE_URI, ":x");
        fail("Must throw exception");
    } catch (DOMException e) {
        assertEquals(DOMException.INVALID_CHARACTER_ERR, e.getCode());
    }
    try {
        document.createAttributeNS(Document.XML_NAMESPACE_URI, ">");
        fail("Must throw exception");
    } catch (DOMException e) {
        assertEquals(DOMException.INVALID_CHARACTER_ERR, e.getCode());
    }
    try {
        document.createAttributeNS(Document.XML_NAMESPACE_URI, "<");
        fail("Must throw exception");
    } catch (DOMException e) {
        assertEquals(DOMException.INVALID_CHARACTER_ERR, e.getCode());
    }
    try {
        document.createAttributeNS(Document.XML_NAMESPACE_URI, "\"");
        fail("Must throw exception");
    } catch (DOMException e) {
        assertEquals(DOMException.INVALID_CHARACTER_ERR, e.getCode());
    }
}
Also used : DOMException(com.gargoylesoftware.css.dom.DOMException) HTMLElement(org.loboevolution.html.dom.HTMLElement) HTMLLinkElement(org.loboevolution.html.dom.HTMLLinkElement) Test(org.junit.Test) LoboUnitTest(org.loboevolution.driver.LoboUnitTest)

Example 29 with DOMException

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

the class HTMLDocumentTest method testBaseElement.

@Test
public void testBaseElement() {
    assertEquals("http://www.example.com/xhtml/htmlsample.html", document.getDocumentURI());
    assertEquals("http://www.example.com/", document.getBaseURI());
    assertEquals("http://www.example.com/", document.getBaseURI());
    Element base = (Element) document.getElementsByTagName("base").item(0);
    assertEquals("http://www.example.com/", base.getBaseURI());
    base.setAttribute("href", "http://www.example.com/newbase/");
    assertEquals("http://www.example.com/newbase/", document.getBaseURI());
    assertEquals("http://www.example.com/newbase/", base.getBaseURI());
    Element anchor = (Element) document.getElementsByTagName("a").item(0);
    anchor.setAttribute("href", "http://www.example.com/foo/");
    assertEquals("http://www.example.com/foo/", anchor.getAttribute("href"));
    assertEquals("http://www.example.com/newbase/", document.getBaseURI());
    Attr attr = document.createAttribute("href");
    attr.setValue("http://www.example.com/other/base/");
    base.setAttributeNode(attr);
    assertEquals("http://www.example.com/other/base/", document.getBaseURI());
    Node parent = base.getParentNode();
    parent.removeChild(base);
    attr.setValue("http://www.example.com/yet/another/base/");
    assertEquals("http://www.example.com/xhtml/htmlsample.html", document.getBaseURI());
    parent.appendChild(base);
    assertEquals("http://www.example.com/yet/another/base/", document.getBaseURI());
    base.removeAttributeNode(attr);
    assertEquals("http://www.example.com/xhtml/htmlsample.html", document.getBaseURI());
    try {
        base.removeAttributeNode(attr);
        fail("Must throw exception.");
    } catch (DOMException e) {
    }
    base.setAttributeNode(attr);
    assertEquals("http://www.example.com/yet/another/base/", document.getBaseURI());
    attr.setValue("foo:");
    assertEquals("http://www.example.com/xhtml/htmlsample.html", document.getBaseURI());
}
Also used : DOMException(com.gargoylesoftware.css.dom.DOMException) HTMLElement(org.loboevolution.html.dom.HTMLElement) HTMLLinkElement(org.loboevolution.html.dom.HTMLLinkElement) Test(org.junit.Test) LoboUnitTest(org.loboevolution.driver.LoboUnitTest)

Example 30 with DOMException

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

the class HTMLDocumentTest method testAppendChildToTextError.

@Test
public void testAppendChildToTextError() throws DOMException {
    Text text = document.createTextNode("text");
    Element p = document.createElement("p");
    try {
        text.appendChild(p);
        fail("Must throw exception.");
    } catch (DOMException e) {
        assertEquals(DOMException.HIERARCHY_REQUEST_ERR, e.getCode());
    }
    Attr foo = document.createAttribute("foo");
    try {
        text.appendChild(foo);
        fail("Must throw exception.");
    } catch (DOMException e) {
        assertEquals(DOMException.HIERARCHY_REQUEST_ERR, e.getCode());
    }
    ProcessingInstruction pi = document.createProcessingInstruction("xml-stylesheet", "type=\"text/css\" href=\"sheet.css\"");
    try {
        text.appendChild(pi);
        fail("Must throw 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) HTMLLinkElement(org.loboevolution.html.dom.HTMLLinkElement) 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