Search in sources :

Example 6 with CSS3Parser

use of com.gargoylesoftware.css.parser.javacc.CSS3Parser 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 7 with CSS3Parser

use of com.gargoylesoftware.css.parser.javacc.CSS3Parser in project LoboEvolution by LoboEvolution.

the class HTMLStyleElementImpl method processStyle.

/**
 * <p>processStyle.</p>
 */
private void processStyle() {
    this.styleSheet = null;
    final HTMLDocumentImpl doc = (HTMLDocumentImpl) getOwnerDocument();
    if (CSSUtilities.matchesMedia(getMedia(), doc.getDefaultView())) {
        final String text = getRawInnerText(true);
        if (Strings.isNotBlank(text)) {
            final String processedText = CSSUtilities.preProcessCss(text);
            final CSSOMParser parser = new CSSOMParser(new CSS3Parser());
            final String baseURI = doc.getBaseURI();
            final InputSource is = CSSUtilities.getCssInputSourceForStyleSheet(processedText, baseURI);
            try {
                final com.gargoylesoftware.css.dom.CSSStyleSheetImpl sheet = parser.parseStyleSheet(is, null);
                sheet.setHref(baseURI);
                sheet.setDisabled(this.disabled);
                CSSStyleSheetImpl cssStyleSheet = new CSSStyleSheetImpl(sheet);
                cssStyleSheet.setOwnerNode(this);
                doc.addStyleSheet(cssStyleSheet);
                this.styleSheet = cssStyleSheet;
            } catch (final Throwable err) {
                this.warn("Unable to parse style sheet", err);
            }
        }
    }
}
Also used : InputSource(com.gargoylesoftware.css.parser.InputSource) CSSStyleSheetImpl(org.loboevolution.html.js.css.CSSStyleSheetImpl) CSSOMParser(com.gargoylesoftware.css.parser.CSSOMParser) CSS3Parser(com.gargoylesoftware.css.parser.javacc.CSS3Parser)

Example 8 with CSS3Parser

use of com.gargoylesoftware.css.parser.javacc.CSS3Parser in project htmlunit by HtmlUnit.

the class CSSStyleSheet method parseSelectors.

/**
 * Parses the selectors at the specified input source. If anything at all goes wrong, this
 * method returns an empty selector list.
 *
 * @param source the source from which to retrieve the selectors to be parsed
 * @return the selectors parsed from the specified input source
 */
public SelectorList parseSelectors(final String source) {
    SelectorList selectors;
    try {
        final CSSErrorHandler errorHandler = getWindow().getWebWindow().getWebClient().getCssErrorHandler();
        final CSSOMParser parser = new CSSOMParser(new CSS3Parser());
        parser.setErrorHandler(errorHandler);
        selectors = parser.parseSelectors(source);
        // in case of error parseSelectors returns null
        if (null == selectors) {
            selectors = new SelectorListImpl();
        }
    } catch (final Throwable t) {
        if (LOG.isErrorEnabled()) {
            LOG.error("Error parsing CSS selectors from '" + source + "': " + t.getMessage(), t);
        }
        selectors = new SelectorListImpl();
    }
    return selectors;
}
Also used : SelectorListImpl(com.gargoylesoftware.css.parser.selector.SelectorListImpl) CSSErrorHandler(com.gargoylesoftware.css.parser.CSSErrorHandler) SelectorList(com.gargoylesoftware.css.parser.selector.SelectorList) CSSOMParser(com.gargoylesoftware.css.parser.CSSOMParser) CSS3Parser(com.gargoylesoftware.css.parser.javacc.CSS3Parser)

Example 9 with CSS3Parser

use of com.gargoylesoftware.css.parser.javacc.CSS3Parser in project htmlunit by HtmlUnit.

the class CSSStyleSheet method parseMedia.

/**
 * Parses the given media string. If anything at all goes wrong, this
 * method returns an empty MediaList list.
 *
 * @param source the source from which to retrieve the media to be parsed
 * @return the media parsed from the specified input source
 */
static MediaListImpl parseMedia(final CSSErrorHandler errorHandler, final String mediaString) {
    MediaListImpl media = media_.get(mediaString);
    if (media != null) {
        return media;
    }
    try {
        final CSSOMParser parser = new CSSOMParser(new CSS3Parser());
        parser.setErrorHandler(errorHandler);
        media = new MediaListImpl(parser.parseMedia(mediaString));
        media_.put(mediaString, media);
        return media;
    } catch (final Exception e) {
        if (LOG.isErrorEnabled()) {
            LOG.error("Error parsing CSS media from '" + mediaString + "': " + e.getMessage(), e);
        }
    }
    media = new MediaListImpl(null);
    media_.put(mediaString, media);
    return media;
}
Also used : CSSOMParser(com.gargoylesoftware.css.parser.CSSOMParser) CSS3Parser(com.gargoylesoftware.css.parser.javacc.CSS3Parser) MediaListImpl(com.gargoylesoftware.css.dom.MediaListImpl) FailingHttpStatusCodeException(com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException) CSSException(com.gargoylesoftware.css.parser.CSSException) IOException(java.io.IOException) CSSParseException(com.gargoylesoftware.css.parser.CSSParseException) DOMException(org.w3c.dom.DOMException) MalformedURLException(java.net.MalformedURLException)

Example 10 with CSS3Parser

use of com.gargoylesoftware.css.parser.javacc.CSS3Parser in project htmlunit by HtmlUnit.

the class CSSStyleSheet method parseCSS.

/**
 * Parses the CSS at the specified input source. If anything at all goes wrong, this method
 * returns an empty stylesheet.
 *
 * @param source the source from which to retrieve the CSS to be parsed
 * @param client the client
 * @return the stylesheet parsed from the specified input source
 */
private static CSSStyleSheetImpl parseCSS(final InputSource source, final WebClient client) {
    CSSStyleSheetImpl ss;
    try {
        final CSSErrorHandler errorHandler = client.getCssErrorHandler();
        final CSSOMParser parser = new CSSOMParser(new CSS3Parser());
        parser.setErrorHandler(errorHandler);
        ss = parser.parseStyleSheet(source, null);
    } catch (final Throwable t) {
        if (LOG.isErrorEnabled()) {
            LOG.error("Error parsing CSS from '" + toString(source) + "': " + t.getMessage(), t);
        }
        ss = new CSSStyleSheetImpl();
    }
    return ss;
}
Also used : CSSStyleSheetImpl(com.gargoylesoftware.css.dom.CSSStyleSheetImpl) CSSErrorHandler(com.gargoylesoftware.css.parser.CSSErrorHandler) CSSOMParser(com.gargoylesoftware.css.parser.CSSOMParser) CSS3Parser(com.gargoylesoftware.css.parser.javacc.CSS3Parser)

Aggregations

CSSOMParser (com.gargoylesoftware.css.parser.CSSOMParser)10 CSS3Parser (com.gargoylesoftware.css.parser.javacc.CSS3Parser)10 CSSErrorHandler (com.gargoylesoftware.css.parser.CSSErrorHandler)4 CSSException (com.gargoylesoftware.css.parser.CSSException)4 SelectorList (com.gargoylesoftware.css.parser.selector.SelectorList)4 IOException (java.io.IOException)4 CSSParseException (com.gargoylesoftware.css.parser.CSSParseException)3 CSSStyleDeclarationImpl (com.gargoylesoftware.css.dom.CSSStyleDeclarationImpl)2 DOMException (com.gargoylesoftware.css.dom.DOMException)2 HTMLDocument (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 CSSStyleSheetImpl (com.gargoylesoftware.css.dom.CSSStyleSheetImpl)1 MediaListImpl (com.gargoylesoftware.css.dom.MediaListImpl)1 InputSource (com.gargoylesoftware.css.parser.InputSource)1 SelectorListImpl (com.gargoylesoftware.css.parser.selector.SelectorListImpl)1 FailingHttpStatusCodeException (com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException)1 DisabledElement (com.gargoylesoftware.htmlunit.html.DisabledElement)1 DomElement (com.gargoylesoftware.htmlunit.html.DomElement)1 DomNode (com.gargoylesoftware.htmlunit.html.DomNode)1 HtmlCheckBoxInput (com.gargoylesoftware.htmlunit.html.HtmlCheckBoxInput)1