Search in sources :

Example 1 with CSSErrorHandler

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

the class CSSStyleSheet method selectsPseudoClass.

private static boolean selectsPseudoClass(final BrowserVersion browserVersion, final Condition condition, final DomElement element, final boolean fromQuerySelectorAll) {
    if (browserVersion.hasFeature(QUERYSELECTORALL_NOT_IN_QUIRKS)) {
        final Object sobj = element.getPage().getScriptableObject();
        if (sobj instanceof HTMLDocument && ((HTMLDocument) sobj).getDocumentMode() < 8) {
            return false;
        }
    }
    final String value = condition.getValue();
    switch(value) {
        case "root":
            return element == element.getPage().getDocumentElement();
        case "enabled":
            return element instanceof DisabledElement && !((DisabledElement) element).isDisabled();
        case "disabled":
            return element instanceof DisabledElement && ((DisabledElement) element).isDisabled();
        case "focus":
            final HtmlPage htmlPage = element.getHtmlPageOrNull();
            if (htmlPage != null) {
                final DomElement focus = htmlPage.getFocusedElement();
                return element == focus;
            }
            return false;
        case "checked":
            return (element instanceof HtmlCheckBoxInput && ((HtmlCheckBoxInput) element).isChecked()) || (element instanceof HtmlRadioButtonInput && ((HtmlRadioButtonInput) element).isChecked() || (element instanceof HtmlOption && ((HtmlOption) element).isSelected()));
        case "required":
            return element instanceof HtmlElement && ((HtmlElement) element).isRequired();
        case "optional":
            return element instanceof HtmlElement && ((HtmlElement) element).isOptional();
        case "first-child":
            for (DomNode n = element.getPreviousSibling(); n != null; n = n.getPreviousSibling()) {
                if (n instanceof DomElement) {
                    return false;
                }
            }
            return true;
        case "last-child":
            for (DomNode n = element.getNextSibling(); n != null; n = n.getNextSibling()) {
                if (n instanceof DomElement) {
                    return false;
                }
            }
            return true;
        case "first-of-type":
            final String firstType = element.getNodeName();
            for (DomNode n = element.getPreviousSibling(); n != null; n = n.getPreviousSibling()) {
                if (n instanceof DomElement && n.getNodeName().equals(firstType)) {
                    return false;
                }
            }
            return true;
        case "last-of-type":
            final String lastType = element.getNodeName();
            for (DomNode n = element.getNextSibling(); n != null; n = n.getNextSibling()) {
                if (n instanceof DomElement && n.getNodeName().equals(lastType)) {
                    return false;
                }
            }
            return true;
        case "only-child":
            for (DomNode n = element.getPreviousSibling(); n != null; n = n.getPreviousSibling()) {
                if (n instanceof DomElement) {
                    return false;
                }
            }
            for (DomNode n = element.getNextSibling(); n != null; n = n.getNextSibling()) {
                if (n instanceof DomElement) {
                    return false;
                }
            }
            return true;
        case "only-of-type":
            final String type = element.getNodeName();
            for (DomNode n = element.getPreviousSibling(); n != null; n = n.getPreviousSibling()) {
                if (n instanceof DomElement && n.getNodeName().equals(type)) {
                    return false;
                }
            }
            for (DomNode n = element.getNextSibling(); n != null; n = n.getNextSibling()) {
                if (n instanceof DomElement && n.getNodeName().equals(type)) {
                    return false;
                }
            }
            return true;
        case "valid":
            return element instanceof HtmlElement && ((HtmlElement) element).isValid();
        case "invalid":
            return element instanceof HtmlElement && !((HtmlElement) element).isValid();
        case "empty":
            return isEmpty(element);
        case "target":
            final String ref = element.getPage().getUrl().getRef();
            return StringUtils.isNotBlank(ref) && ref.equals(element.getId());
        case "hover":
            return element.isMouseOver();
        case "placeholder-shown":
            if (browserVersion.hasFeature(CSS_PSEUDO_SELECTOR_PLACEHOLDER_SHOWN)) {
                return element instanceof HtmlInput && StringUtils.isEmpty(((HtmlInput) element).getValueAttribute()) && StringUtils.isNotEmpty(((HtmlInput) element).getPlaceholder());
            }
        case "-ms-input-placeholder":
            if (browserVersion.hasFeature(CSS_PSEUDO_SELECTOR_MS_PLACEHHOLDER)) {
                return element instanceof HtmlInput && StringUtils.isEmpty(((HtmlInput) element).getValueAttribute()) && StringUtils.isNotEmpty(((HtmlInput) element).getPlaceholder());
            }
        default:
            if (value.startsWith("nth-child(")) {
                final String nth = value.substring(value.indexOf('(') + 1, value.length() - 1);
                int index = 0;
                for (DomNode n = element; n != null; n = n.getPreviousSibling()) {
                    if (n instanceof DomElement) {
                        index++;
                    }
                }
                return getNth(nth, index);
            } else if (value.startsWith("nth-last-child(")) {
                final String nth = value.substring(value.indexOf('(') + 1, value.length() - 1);
                int index = 0;
                for (DomNode n = element; n != null; n = n.getNextSibling()) {
                    if (n instanceof DomElement) {
                        index++;
                    }
                }
                return getNth(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 (DomNode n = element; n != null; n = n.getPreviousSibling()) {
                    if (n instanceof DomElement && n.getNodeName().equals(nthType)) {
                        index++;
                    }
                }
                return getNth(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 (DomNode n = element; n != null; n = n.getNextSibling()) {
                    if (n instanceof DomElement && n.getNodeName().equals(nthLastType)) {
                        index++;
                    }
                }
                return getNth(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);
                    }
                    validateSelectors(selectorList, 9, element);
                    return !selects(browserVersion, selectorList.get(0), element, null, fromQuerySelectorAll);
                } catch (final IOException e) {
                    throw new CSSException("Error parsing CSS selectors from '" + selectors + "': " + e.getMessage());
                }
            }
            return false;
    }
}
Also used : DisabledElement(com.gargoylesoftware.htmlunit.html.DisabledElement) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) HTMLDocument(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) CSSOMParser(com.gargoylesoftware.css.parser.CSSOMParser) IOException(java.io.IOException) HtmlInput(com.gargoylesoftware.htmlunit.html.HtmlInput) HtmlCheckBoxInput(com.gargoylesoftware.htmlunit.html.HtmlCheckBoxInput) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DomNode(com.gargoylesoftware.htmlunit.html.DomNode) DomElement(com.gargoylesoftware.htmlunit.html.DomElement) CSSParseException(com.gargoylesoftware.css.parser.CSSParseException) CSSErrorHandler(com.gargoylesoftware.css.parser.CSSErrorHandler) CSS3Parser(com.gargoylesoftware.css.parser.javacc.CSS3Parser) SelectorList(com.gargoylesoftware.css.parser.selector.SelectorList) CSSException(com.gargoylesoftware.css.parser.CSSException) HtmlOption(com.gargoylesoftware.htmlunit.html.HtmlOption) HtmlRadioButtonInput(com.gargoylesoftware.htmlunit.html.HtmlRadioButtonInput)

Example 2 with CSSErrorHandler

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

the class MediaQueryList method isMatches.

/**
 * Returns whether the document currently matches the media query list or not.
 * @return whether the document currently matches the media query list or not
 */
@JsxGetter
public boolean isMatches() {
    final CSSErrorHandler errorHandler = getWindow().getWebWindow().getWebClient().getCssErrorHandler();
    final MediaListImpl mediaList = CSSStyleSheet.parseMedia(errorHandler, media_);
    return CSSStyleSheet.isActive(this, mediaList);
}
Also used : CSSErrorHandler(com.gargoylesoftware.css.parser.CSSErrorHandler) MediaListImpl(com.gargoylesoftware.css.dom.MediaListImpl) JsxGetter(com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter)

Example 3 with CSSErrorHandler

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

the class StyleMedia method matchMedium.

/**
 * Returns whether the specified media is supported by the object that displays the document object.
 * @param media the media query
 * @return whether the specified media is supported or not
 */
@JsxFunction
public boolean matchMedium(final String media) {
    final CSSErrorHandler errorHandler = getWindow().getWebWindow().getWebClient().getCssErrorHandler();
    final MediaListImpl mediaList = CSSStyleSheet.parseMedia(errorHandler, media);
    return CSSStyleSheet.isActive(this, mediaList);
}
Also used : CSSErrorHandler(com.gargoylesoftware.css.parser.CSSErrorHandler) MediaListImpl(com.gargoylesoftware.css.dom.MediaListImpl) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 4 with CSSErrorHandler

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

the class WebClientTest method cssErrorHandler.

/**
 * @throws Exception if an error occurs
 */
@Test
public void cssErrorHandler() throws Exception {
    final WebClient client = getWebClient();
    assertTrue(client.getCssErrorHandler() instanceof DefaultCssErrorHandler);
    final MutableInt fatals = new MutableInt();
    final MutableInt errors = new MutableInt();
    final MutableInt warnings = new MutableInt();
    final StringBuilder errorUri = new StringBuilder();
    final CSSErrorHandler handler = new CSSErrorHandler() {

        @Override
        public void warning(final CSSParseException exception) throws CSSException {
            warnings.increment();
        }

        @Override
        public void fatalError(final CSSParseException exception) throws CSSException {
            fatals.increment();
        }

        @Override
        public void error(final CSSParseException exception) throws CSSException {
            errors.increment();
            errorUri.append(exception.getURI());
        }
    };
    client.setCssErrorHandler(handler);
    assertEquals(handler, client.getCssErrorHandler());
    final MockWebConnection conn = new MockWebConnection();
    conn.setResponse(URL_FIRST, "<html><body><style></style></body></html>");
    conn.setResponse(URL_SECOND, "<html><body><style>.x{color:red;}</style></body></html>");
    conn.setResponse(URL_THIRD, "<html><body><style>.x{color{}}}</style></body></html>");
    client.setWebConnection(conn);
    final HtmlPage page1 = client.getPage(URL_FIRST);
    ((HTMLStyleElement) page1.getBody().getFirstChild().getScriptableObject()).getSheet();
    assertEquals(0, warnings.intValue());
    assertEquals(0, errors.intValue());
    assertEquals(0, fatals.intValue());
    final HtmlPage page2 = client.getPage(URL_SECOND);
    ((HTMLStyleElement) page2.getBody().getFirstChild().getScriptableObject()).getSheet();
    assertEquals(0, warnings.intValue());
    assertEquals(0, errors.intValue());
    assertEquals(0, fatals.intValue());
    final HtmlPage page3 = client.getPage(URL_THIRD);
    ((HTMLStyleElement) page3.getBody().getFirstChild().getScriptableObject()).getSheet();
    assertEquals(1, warnings.intValue());
    assertEquals(2, errors.intValue());
    assertEquals(0, fatals.intValue());
    assertEquals("http://127.0.0.1:" + PORT + "/third/http://127.0.0.1:" + PORT + "/third/", errorUri.toString());
}
Also used : CSSParseException(com.gargoylesoftware.css.parser.CSSParseException) HTMLStyleElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLStyleElement) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) CSSErrorHandler(com.gargoylesoftware.css.parser.CSSErrorHandler) MutableInt(org.apache.commons.lang3.mutable.MutableInt) Test(org.junit.Test)

Example 5 with CSSErrorHandler

use of com.gargoylesoftware.css.parser.CSSErrorHandler in project yacy_grid_loader by yacy.

the class HtmlUnitLoader method getClient.

public static WebClient getClient(String userAgent) {
    WebClient webClient = new WebClient(getBrowser(userAgent));
    WebClientOptions options = webClient.getOptions();
    options.setJavaScriptEnabled(true);
    options.setCssEnabled(false);
    options.setPopupBlockerEnabled(true);
    options.setRedirectEnabled(true);
    options.setDownloadImages(false);
    options.setGeolocationEnabled(false);
    options.setPrintContentOnFailingStatusCode(false);
    options.setThrowExceptionOnScriptError(false);
    options.setMaxInMemory(0);
    options.setHistoryPageCacheLimit(0);
    options.setHistorySizeLimit(0);
    // ProxyConfig proxyConfig = new ProxyConfig();
    // proxyConfig.setProxyHost("127.0.0.1");
    // proxyConfig.setProxyPort(Service.getPort());
    // options.setProxyConfig(proxyConfig);
    // this might be a bit large, is regulated with throttling and client cache clear in short memory status
    webClient.getCache().setMaxSize(10000);
    webClient.setIncorrectnessListener(new IncorrectnessListener() {

        @Override
        public void notify(String arg0, Object arg1) {
        }
    });
    webClient.setCssErrorHandler(new CSSErrorHandler() {

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

        @Override
        public void error(CSSParseException exception) throws CSSException {
        }

        @Override
        public void fatalError(CSSParseException exception) throws CSSException {
        }
    });
    webClient.setJavaScriptErrorListener(new JavaScriptErrorListener() {

        @Override
        public void timeoutError(HtmlPage arg0, long arg1, long arg2) {
        }

        @Override
        public void scriptException(HtmlPage arg0, ScriptException arg1) {
        }

        @Override
        public void malformedScriptURL(HtmlPage arg0, String arg1, MalformedURLException arg2) {
        }

        @Override
        public void loadScriptError(HtmlPage arg0, URL arg1, Exception arg2) {
        }

        @Override
        public void warn(String message, String sourceName, int line, String lineSource, int lineOffset) {
        }
    });
    webClient.setHTMLParserListener(new HTMLParserListener() {

        @Override
        public void error(String message, URL url, String html, int line, int column, String key) {
        }

        @Override
        public void warning(String message, URL url, String html, int line, int column, String key) {
        }
    });
    return webClient;
}
Also used : HTMLParserListener(com.gargoylesoftware.htmlunit.html.parser.HTMLParserListener) WebClientOptions(com.gargoylesoftware.htmlunit.WebClientOptions) MalformedURLException(java.net.MalformedURLException) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) WebClient(com.gargoylesoftware.htmlunit.WebClient) IncorrectnessListener(com.gargoylesoftware.htmlunit.IncorrectnessListener) URL(java.net.URL) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) CSSParseException(com.gargoylesoftware.css.parser.CSSParseException) CSSException(com.gargoylesoftware.css.parser.CSSException) ScriptException(com.gargoylesoftware.htmlunit.ScriptException) JavaScriptErrorListener(com.gargoylesoftware.htmlunit.javascript.JavaScriptErrorListener) ScriptException(com.gargoylesoftware.htmlunit.ScriptException) CSSParseException(com.gargoylesoftware.css.parser.CSSParseException) CSSErrorHandler(com.gargoylesoftware.css.parser.CSSErrorHandler) CSSException(com.gargoylesoftware.css.parser.CSSException)

Aggregations

CSSErrorHandler (com.gargoylesoftware.css.parser.CSSErrorHandler)8 CSSOMParser (com.gargoylesoftware.css.parser.CSSOMParser)4 CSSParseException (com.gargoylesoftware.css.parser.CSSParseException)4 CSS3Parser (com.gargoylesoftware.css.parser.javacc.CSS3Parser)4 CSSException (com.gargoylesoftware.css.parser.CSSException)3 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)3 IOException (java.io.IOException)3 MediaListImpl (com.gargoylesoftware.css.dom.MediaListImpl)2 SelectorList (com.gargoylesoftware.css.parser.selector.SelectorList)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 CSSStyleSheetImpl (com.gargoylesoftware.css.dom.CSSStyleSheetImpl)1 SelectorListImpl (com.gargoylesoftware.css.parser.selector.SelectorListImpl)1 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 DomElement (com.gargoylesoftware.htmlunit.html.DomElement)1 DomNode (com.gargoylesoftware.htmlunit.html.DomNode)1 HtmlCheckBoxInput (com.gargoylesoftware.htmlunit.html.HtmlCheckBoxInput)1