Search in sources :

Example 11 with CSSException

use of com.gargoylesoftware.css.parser.CSSException in project LoboEvolution by LoboEvolution.

the class StyleSheetAggregator method selectsPseudoClass.

private boolean selectsPseudoClass(final Condition condition, final HTMLElement element, final boolean mouseOver) {
    final String value = condition.getValue();
    switch(value) {
        case "hover":
            setMouseOver(true);
            return mouseOver;
        case "root":
            NodeImpl parentDOMNodeImpl = (NodeImpl) element.getParentNode();
            return parentDOMNodeImpl != null && parentDOMNodeImpl.getNodeType() == NodeType.DOCUMENT_TYPE_NODE;
        case "enabled":
            return element.hasAttribute("enabled") || (!element.hasAttribute("enabled") && !element.hasAttribute("disabled"));
        case "disabled":
            return element.hasAttribute("disabled");
        case "placeholder":
            return element.hasAttribute("placeholder");
        case "read-only":
            return element.hasAttribute("readonly");
        case "read-write":
            return !element.hasAttribute("readonly");
        case "out-of-range":
            if (element instanceof HTMLInputElement) {
                HTMLInputElementImpl input = (HTMLInputElementImpl) element;
                if ("number".equals(input.getType())) {
                    String minTxt = input.getAttribute("min");
                    String maxTxt = input.getAttribute("max");
                    int min = minTxt == null ? 0 : Integer.parseInt(input.getAttribute("min"));
                    int max = maxTxt == null ? Integer.MAX_VALUE : Integer.parseInt(input.getAttribute("max"));
                    int valueNumber = Integer.parseInt(input.getValue());
                    return (valueNumber < min || valueNumber > max);
                }
            }
        case "checked":
            return (element instanceof HTMLInputElement && ((HTMLInputElement) element).isChecked()) || (element instanceof HTMLOptionElement && ((HTMLOptionElement) element).isSelected());
        case "required":
            return (element instanceof HTMLInputElement || element instanceof HTMLSelectElement || element instanceof HTMLTextAreaElement) && element.hasAttribute("required");
        case "optional":
            return (element instanceof HTMLInputElement || element instanceof HTMLSelectElement || element instanceof HTMLTextAreaElement) && !element.hasAttribute("required");
        case "link":
            return (element instanceof HTMLLinkElement);
        case "visited":
            if (element instanceof HTMLLinkElement) {
                HTMLLinkElement elem = (HTMLLinkElement) element;
                return LinkStore.isVisited(elem.getHref());
            } else {
                return false;
            }
        case "first-child":
            for (Node n = element.getPreviousSibling(); n != null; n = n.getPreviousSibling()) {
                if (n instanceof HTMLElement) {
                    return false;
                }
            }
            return true;
        case "last-child":
            for (Node n = element.getNextSibling(); n != null; n = n.getNextSibling()) {
                if (n instanceof HTMLElement) {
                    return false;
                }
            }
            return true;
        case "first-of-type":
            final String firstType = element.getNodeName();
            for (Node n = element.getPreviousSibling(); n != null; n = n.getPreviousSibling()) {
                if (n instanceof HTMLElement && n.getNodeName().equals(firstType)) {
                    return false;
                }
            }
            return true;
        case "last-of-type":
            final String lastType = element.getNodeName();
            for (Node n = element.getNextSibling(); n != null; n = n.getNextSibling()) {
                if (n instanceof HTMLElement && n.getNodeName().equals(lastType)) {
                    return false;
                }
            }
            return true;
        case "only-child":
            for (Node n = element.getPreviousSibling(); n != null; n = n.getPreviousSibling()) {
                if (n instanceof HTMLElement) {
                    return false;
                }
            }
            for (Node n = element.getNextSibling(); n != null; n = n.getNextSibling()) {
                if (n instanceof HTMLElement) {
                    return false;
                }
            }
            return true;
        case "only-of-type":
            final String type = element.getNodeName();
            for (Node n = element.getPreviousSibling(); n != null; n = n.getPreviousSibling()) {
                if (n instanceof HTMLElement && n.getNodeName().equals(type)) {
                    return false;
                }
            }
            for (Node n = element.getNextSibling(); n != null; n = n.getNextSibling()) {
                if (n instanceof HTMLElement && n.getNodeName().equals(type)) {
                    return false;
                }
            }
            return true;
        case "empty":
            return isEmpty(element);
        default:
            if (value.startsWith("nth-child(")) {
                final String nth = value.substring(value.indexOf('(') + 1, value.length() - 1);
                int index = 0;
                for (Node n = element; n != null; n = n.getPreviousSibling()) {
                    if (n instanceof HTMLElement) {
                        index++;
                    }
                }
                return getNth(element, nth, index);
            } else if (value.startsWith("nth-last-child(")) {
                final String nth = value.substring(value.indexOf('(') + 1, value.length() - 1);
                int index = 0;
                for (Node n = element; n != null; n = n.getNextSibling()) {
                    if (n instanceof HTMLElement) {
                        index++;
                    }
                }
                return getNth(element, nth, index);
            } else if (value.startsWith("nth-of-type(")) {
                final String nthType = element.getNodeName();
                final String nth = value.substring(value.indexOf('(') + 1, value.length() - 1);
                int index = 0;
                for (Node n = element; n != null; n = n.getPreviousSibling()) {
                    if (n instanceof HTMLElement && n.getNodeName().equals(nthType)) {
                        index++;
                    }
                }
                return getNth(element, nth, index);
            } else if (value.startsWith("nth-last-of-type(")) {
                final String nthLastType = element.getNodeName();
                final String nth = value.substring(value.indexOf('(') + 1, value.length() - 1);
                int index = 0;
                for (Node n = element; n != null; n = n.getNextSibling()) {
                    if (n instanceof HTMLElement && n.getNodeName().equals(nthLastType)) {
                        index++;
                    }
                }
                return getNth(element, nth, index);
            } else if (value.startsWith("not(")) {
                final String selectors = value.substring(value.indexOf('(') + 1, value.length() - 1);
                final AtomicBoolean errorOccured = new AtomicBoolean(false);
                final CSSErrorHandler errorHandler = new CSSErrorHandler() {

                    @Override
                    public void warning(final CSSParseException exception) throws CSSException {
                    // ignore
                    }

                    @Override
                    public void fatalError(final CSSParseException exception) throws CSSException {
                        errorOccured.set(true);
                    }

                    @Override
                    public void error(final CSSParseException exception) throws CSSException {
                        errorOccured.set(true);
                    }
                };
                final CSSOMParser parser = new CSSOMParser(new CSS3Parser());
                parser.setErrorHandler(errorHandler);
                try {
                    final SelectorList selectorList = parser.parseSelectors(selectors);
                    if (errorOccured.get() || selectorList == null || selectorList.size() != 1) {
                        throw new CSSException("Invalid selectors: " + selectors);
                    }
                    return !selects(selectorList.get(0), element, null, mouseOver);
                } catch (final IOException e) {
                    throw new CSSException("Error parsing CSS selectors from '" + selectors + "': " + e.getMessage());
                }
            }
            return false;
    }
}
Also used : NodeImpl(org.loboevolution.html.dom.nodeimpl.NodeImpl) Node(org.loboevolution.html.node.Node) CSSOMParser(com.gargoylesoftware.css.parser.CSSOMParser) HTMLInputElementImpl(org.loboevolution.html.dom.domimpl.HTMLInputElementImpl) IOException(java.io.IOException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CSSParseException(com.gargoylesoftware.css.parser.CSSParseException) CSSErrorHandler(com.gargoylesoftware.css.parser.CSSErrorHandler) CSS3Parser(com.gargoylesoftware.css.parser.javacc.CSS3Parser) CSSException(com.gargoylesoftware.css.parser.CSSException)

Example 12 with CSSException

use of com.gargoylesoftware.css.parser.CSSException in project LoboEvolution by LoboEvolution.

the class CSSCharsetRuleImpl method setCssText.

/**
 * {@inheritDoc}
 */
@Override
public void setCssText(final String cssText) throws DOMException {
    try {
        final CSSOMParser parser = new CSSOMParser();
        final AbstractCSSRuleImpl r = parser.parseRule(cssText);
        // The rule must be a charset rule
        if (r instanceof CSSCharsetRuleImpl) {
            encoding_ = ((CSSCharsetRuleImpl) r).encoding_;
        } else {
            throw new DOMException(DOMException.INVALID_MODIFICATION_ERR, DOMException.EXPECTING_CHARSET_RULE);
        }
    } catch (final CSSException e) {
        throw new DOMException(DOMException.SYNTAX_ERR, DOMException.SYNTAX_ERROR, e.getMessage());
    } catch (final IOException e) {
        throw new DOMException(DOMException.SYNTAX_ERR, DOMException.SYNTAX_ERROR, e.getMessage());
    }
}
Also used : CSSOMParser(com.gargoylesoftware.css.parser.CSSOMParser) CSSException(com.gargoylesoftware.css.parser.CSSException) IOException(java.io.IOException)

Example 13 with CSSException

use of com.gargoylesoftware.css.parser.CSSException in project LoboEvolution by LoboEvolution.

the class CSSImportRuleImpl method setCssText.

/**
 * {@inheritDoc}
 */
@Override
public void setCssText(final String cssText) throws DOMException {
    try {
        final CSSOMParser parser = new CSSOMParser();
        final AbstractCSSRuleImpl r = parser.parseRule(cssText);
        // The rule must be an import rule
        if (r instanceof CSSImportRuleImpl) {
            href_ = ((CSSImportRuleImpl) r).href_;
            media_ = ((CSSImportRuleImpl) r).media_;
        } else {
            throw new DOMException(DOMException.INVALID_MODIFICATION_ERR, DOMException.EXPECTING_IMPORT_RULE);
        }
    } catch (final CSSException e) {
        throw new DOMException(DOMException.SYNTAX_ERR, DOMException.SYNTAX_ERROR, e.getMessage());
    } catch (final IOException e) {
        throw new DOMException(DOMException.SYNTAX_ERR, DOMException.SYNTAX_ERROR, e.getMessage());
    }
}
Also used : CSSOMParser(com.gargoylesoftware.css.parser.CSSOMParser) CSSException(com.gargoylesoftware.css.parser.CSSException) IOException(java.io.IOException)

Example 14 with CSSException

use of com.gargoylesoftware.css.parser.CSSException in project LoboEvolution by LoboEvolution.

the class CSSPageRuleImpl method setCssText.

/**
 * {@inheritDoc}
 */
@Override
public void setCssText(final String cssText) throws DOMException {
    try {
        final CSSOMParser parser = new CSSOMParser();
        final AbstractCSSRuleImpl r = parser.parseRule(cssText);
        // The rule must be a page rule
        if (r instanceof CSSPageRuleImpl) {
            pseudoPage_ = ((CSSPageRuleImpl) r).pseudoPage_;
            style_ = ((CSSPageRuleImpl) r).style_;
        } else {
            throw new DOMException(DOMException.INVALID_MODIFICATION_ERR, DOMException.EXPECTING_PAGE_RULE);
        }
    } catch (final CSSException e) {
        throw new DOMException(DOMException.SYNTAX_ERR, DOMException.SYNTAX_ERROR, e.getMessage());
    } catch (final IOException e) {
        throw new DOMException(DOMException.SYNTAX_ERR, DOMException.SYNTAX_ERROR, e.getMessage());
    }
}
Also used : CSSOMParser(com.gargoylesoftware.css.parser.CSSOMParser) CSSException(com.gargoylesoftware.css.parser.CSSException) IOException(java.io.IOException)

Example 15 with CSSException

use of com.gargoylesoftware.css.parser.CSSException in project LoboEvolution by LoboEvolution.

the class CSSMediaRuleImpl method insertRule.

/**
 * Insert a new rule at the given index.
 *
 * @param rule the rule to be inserted
 * @param index the insert pos
 * @throws DOMException in case of error
 */
public void insertRule(final String rule, final int index) throws DOMException {
    final CSSStyleSheetImpl parentStyleSheet = getParentStyleSheet();
    try {
        final CSSOMParser parser = new CSSOMParser();
        parser.setParentStyleSheet(parentStyleSheet);
        parser.setErrorHandler(ThrowCssExceptionErrorHandler.INSTANCE);
        final AbstractCSSRuleImpl r = parser.parseRule(rule);
        // Insert the rule into the list of rules
        getCssRules().insert(r, index);
        r.setParentRule(this);
    } catch (final IndexOutOfBoundsException e) {
        throw new DOMException(DOMException.INDEX_SIZE_ERR, DOMException.INDEX_OUT_OF_BOUNDS, e.getMessage());
    } catch (final CSSException e) {
        throw new DOMException(DOMException.SYNTAX_ERR, DOMException.SYNTAX_ERROR, e.getMessage());
    } catch (final IOException e) {
        throw new DOMException(DOMException.SYNTAX_ERR, e.getMessage());
    }
}
Also used : CSSOMParser(com.gargoylesoftware.css.parser.CSSOMParser) CSSException(com.gargoylesoftware.css.parser.CSSException) IOException(java.io.IOException)

Aggregations

CSSException (com.gargoylesoftware.css.parser.CSSException)18 IOException (java.io.IOException)16 CSSOMParser (com.gargoylesoftware.css.parser.CSSOMParser)13 SelectorList (com.gargoylesoftware.css.parser.selector.SelectorList)5 CSSErrorHandler (com.gargoylesoftware.css.parser.CSSErrorHandler)3 CSSParseException (com.gargoylesoftware.css.parser.CSSParseException)3 CSS3Parser (com.gargoylesoftware.css.parser.javacc.CSS3Parser)3 Selector (com.gargoylesoftware.css.parser.selector.Selector)3 BrowserVersion (com.gargoylesoftware.htmlunit.BrowserVersion)3 DomElement (com.gargoylesoftware.htmlunit.html.DomElement)2 DomNode (com.gargoylesoftware.htmlunit.html.DomNode)2 HtmlElement (com.gargoylesoftware.htmlunit.html.HtmlElement)2 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)2 HTMLDocument (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 IncorrectnessListener (com.gargoylesoftware.htmlunit.IncorrectnessListener)1 ScriptException (com.gargoylesoftware.htmlunit.ScriptException)1 WebClient (com.gargoylesoftware.htmlunit.WebClient)1 WebClientOptions (com.gargoylesoftware.htmlunit.WebClientOptions)1 DisabledElement (com.gargoylesoftware.htmlunit.html.DisabledElement)1