Search in sources :

Example 1 with CSSParseException

use of com.gargoylesoftware.css.parser.CSSParseException 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 CSSParseException

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

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

the class CSS3Parser method styleSheetRuleList.

public final void styleSheetRuleList() throws ParseException {
    boolean ruleFound = false;
    label_1: while (true) {
        switch((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
            case S:
            case CDO:
            case CDC:
                {
                    ;
                    break;
                }
            default:
                jj_la1[0] = jj_gen;
                break label_1;
        }
        switch((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
            case S:
                {
                    jj_consume_token(S);
                    break;
                }
            case CDO:
                {
                    jj_consume_token(CDO);
                    break;
                }
            case CDC:
                {
                    jj_consume_token(CDC);
                    break;
                }
            default:
                jj_la1[1] = jj_gen;
                jj_consume_token(-1);
                throw new ParseException();
        }
    }
    switch((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
        case CHARSET_SYM:
            {
                charsetRule();
                label_2: while (true) {
                    switch((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
                        case S:
                        case CDO:
                        case CDC:
                            {
                                ;
                                break;
                            }
                        default:
                            jj_la1[2] = jj_gen;
                            break label_2;
                    }
                    switch((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
                        case S:
                            {
                                jj_consume_token(S);
                                break;
                            }
                        case CDO:
                            {
                                jj_consume_token(CDO);
                                break;
                            }
                        case CDC:
                            {
                                jj_consume_token(CDC);
                                break;
                            }
                        default:
                            jj_la1[3] = jj_gen;
                            jj_consume_token(-1);
                            throw new ParseException();
                    }
                }
                break;
            }
        default:
            jj_la1[4] = jj_gen;
            ;
    }
    label_3: while (true) {
        ;
        switch((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
            case IDENT:
            case DOT:
            case COLON:
            case ASTERISK:
            case LSQUARE:
            case HASH:
            case IMPORT_SYM:
            case PAGE_SYM:
            case MEDIA_SYM:
            case FONT_FACE_SYM:
            case ATKEYWORD:
                {
                    switch((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
                        case IMPORT_SYM:
                            {
                                importRule(ruleFound);
                                break;
                            }
                        case IDENT:
                        case DOT:
                        case COLON:
                        case ASTERISK:
                        case LSQUARE:
                        case HASH:
                        case PAGE_SYM:
                        case MEDIA_SYM:
                        case FONT_FACE_SYM:
                        case ATKEYWORD:
                            {
                                switch((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
                                    case IDENT:
                                    case DOT:
                                    case COLON:
                                    case ASTERISK:
                                    case LSQUARE:
                                    case HASH:
                                        {
                                            styleRule();
                                            break;
                                        }
                                    case MEDIA_SYM:
                                        {
                                            mediaRule();
                                            break;
                                        }
                                    case PAGE_SYM:
                                        {
                                            pageRule();
                                            break;
                                        }
                                    case FONT_FACE_SYM:
                                        {
                                            fontFaceRule();
                                            break;
                                        }
                                    case ATKEYWORD:
                                        {
                                            unknownAtRule();
                                            break;
                                        }
                                    default:
                                        jj_la1[5] = jj_gen;
                                        jj_consume_token(-1);
                                        throw new ParseException();
                                }
                                ruleFound = true;
                                break;
                            }
                        default:
                            jj_la1[6] = jj_gen;
                            jj_consume_token(-1);
                            throw new ParseException();
                    }
                    break;
                }
            default:
                jj_la1[7] = jj_gen;
                ParseException e = generateParseException();
                invalidRule();
                Token t = getNextToken();
                boolean charsetProcessed = false;
                if (t.kind == CHARSET_SYM) {
                    t = getNextToken();
                    if (t.kind == S) {
                        t = getNextToken();
                        if (t.kind == STRING) {
                            t = getNextToken();
                            if (t.kind == SEMICOLON) {
                                getNextToken();
                                charsetProcessed = true;
                            }
                        }
                    }
                    CSSParseException cpe = toCSSParseException("misplacedCharsetRule", e);
                    getErrorHandler().error(cpe);
                    getErrorHandler().warning(createSkipWarning("ignoringRule", cpe));
                }
                if (!charsetProcessed) {
                    if (t.kind == EOF) {
                        return;
                    }
                    CSSParseException cpe = toCSSParseException("invalidRule", e);
                    getErrorHandler().error(cpe);
                    getErrorHandler().warning(createSkipWarning("ignoringRule", cpe));
                    while (t.kind != RBRACE && t.kind != EOF) {
                        t = getNextToken();
                    }
                    if (t.kind == EOF) {
                        return;
                    }
                }
        }
        label_4: while (true) {
            switch((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
                case S:
                case CDO:
                case CDC:
                    {
                        ;
                        break;
                    }
                default:
                    jj_la1[8] = jj_gen;
                    break label_4;
            }
            switch((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
                case S:
                    {
                        jj_consume_token(S);
                        break;
                    }
                case CDO:
                    {
                        jj_consume_token(CDO);
                        break;
                    }
                case CDC:
                    {
                        jj_consume_token(CDC);
                        break;
                    }
                default:
                    jj_la1[9] = jj_gen;
                    jj_consume_token(-1);
                    throw new ParseException();
            }
        }
    }
}
Also used : CSSParseException(com.gargoylesoftware.css.parser.CSSParseException) CSSParseException(com.gargoylesoftware.css.parser.CSSParseException)

Example 4 with CSSParseException

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

the class CSS3Parser method pageRule.

// 
// page
// : PAGE_SYM S* pseudo_page? S*
// '{' S* declaration [ ';' S* declaration ]* '}' S*
// ;
// 
public final void pageRule() throws ParseException {
    String sel = null;
    boolean start = false;
    Locator locator;
    try {
        jj_consume_token(PAGE_SYM);
        locator = createLocator(token);
        label_24: while (true) {
            switch((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
                case S:
                    {
                        ;
                        break;
                    }
                default:
                    jj_la1[38] = jj_gen;
                    break label_24;
            }
            jj_consume_token(S);
        }
        switch((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
            case IDENT:
            case COLON:
                {
                    sel = pageSelectorList();
                    break;
                }
            default:
                jj_la1[39] = jj_gen;
                ;
        }
        jj_consume_token(LBRACE);
        label_25: while (true) {
            switch((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
                case S:
                    {
                        ;
                        break;
                    }
                default:
                    jj_la1[40] = jj_gen;
                    break label_25;
            }
            jj_consume_token(S);
        }
        start = true;
        handleStartPage(null, sel, locator);
        styleDeclaration();
        jj_consume_token(RBRACE);
    } catch (CSSParseException e) {
        getErrorHandler().error(e);
        error_skipblock("ignoringRule", e);
    } catch (ParseException e) {
        CSSParseException cpe = toCSSParseException("invalidPageRule", e);
        getErrorHandler().error(cpe);
        error_skipblock("ignoringRule", cpe);
    } finally {
        if (start) {
            handleEndPage(null, sel);
        }
    }
}
Also used : Locator(com.gargoylesoftware.css.parser.Locator) CSSParseException(com.gargoylesoftware.css.parser.CSSParseException) CSSParseException(com.gargoylesoftware.css.parser.CSSParseException)

Example 5 with CSSParseException

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

the class CSS3Parser method styleRule.

// 
// ruleset
// : selector [ COMMA S* selector ]*
// '{' S* declaration [ ';' S* declaration ]* '}' S*
// ;
// 
public final void styleRule() throws ParseException {
    SelectorList selList = null;
    boolean start = false;
    Token t;
    try {
        t = token;
        selList = selectorList();
        jj_consume_token(LBRACE);
        label_40: while (true) {
            switch((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
                case S:
                    {
                        ;
                        break;
                    }
                default:
                    jj_la1[62] = jj_gen;
                    break label_40;
            }
            jj_consume_token(S);
        }
        start = true;
        handleStartSelector(selList, createLocator(t.next));
        styleDeclaration();
        switch((jj_ntk == -1) ? jj_ntk_f() : jj_ntk) {
            case RBRACE:
                {
                    jj_consume_token(RBRACE);
                    break;
                }
            case 0:
                {
                    jj_consume_token(0);
                    break;
                }
            default:
                jj_la1[63] = jj_gen;
                jj_consume_token(-1);
                throw new ParseException();
        }
    } catch (CSSParseException e) {
        getErrorHandler().error(e);
        error_skipblock("ignoringRule", e);
    } catch (ParseException e) {
        CSSParseException cpe = toCSSParseException("invalidStyleRule", e);
        getErrorHandler().error(cpe);
        error_skipblock("ignoringFollowingDeclarations", cpe);
    } finally {
        if (start) {
            handleEndSelector(selList);
        }
    }
}
Also used : CSSParseException(com.gargoylesoftware.css.parser.CSSParseException) SelectorList(com.gargoylesoftware.css.parser.selector.SelectorList) CSSParseException(com.gargoylesoftware.css.parser.CSSParseException)

Aggregations

CSSParseException (com.gargoylesoftware.css.parser.CSSParseException)11 CSSErrorHandler (com.gargoylesoftware.css.parser.CSSErrorHandler)4 Locator (com.gargoylesoftware.css.parser.Locator)4 IOException (java.io.IOException)4 CSSException (com.gargoylesoftware.css.parser.CSSException)3 CSSOMParser (com.gargoylesoftware.css.parser.CSSOMParser)3 MediaQueryList (com.gargoylesoftware.css.parser.media.MediaQueryList)3 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)3 CSS3Parser (com.gargoylesoftware.css.parser.javacc.CSS3Parser)2 SelectorList (com.gargoylesoftware.css.parser.selector.SelectorList)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 LexicalUnit (com.gargoylesoftware.css.parser.LexicalUnit)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