com.gargoylesoftware.css.parser.CSSException Java Examples
The following examples show how to use
com.gargoylesoftware.css.parser.CSSException.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DomElement.java From HtmlUnit-Android with Apache License 2.0 | 6 votes |
/** * Returns true if the element would be selected by the specified selector string; otherwise, returns false. * @param selectorString the selector to test * @return true if the element would be selected by the specified selector string; otherwise, returns false. */ public boolean matches(final String selectorString) { try { final BrowserVersion browserVersion = getPage().getWebClient().getBrowserVersion(); final SelectorList selectorList = getSelectorList(selectorString, browserVersion); if (selectorList != null) { for (Selector selector : selectorList) { if (CSSStyleSheet.selects(browserVersion, selector, this, null, true)) { return true; } } } return false; } catch (final IOException e) { throw new CSSException("Error parsing CSS selectors from '" + selectorString + "': " + e.getMessage()); } }
Example #2
Source File: Element.java From htmlunit with Apache License 2.0 | 6 votes |
/** * Returns true if the element would be selected by the specified selector string; otherwise, returns false. * @param context the JavaScript context * @param thisObj the scriptable * @param args the arguments passed into the method * @param function the function * @return the value */ @JsxFunction({CHROME, FF, FF68, FF60}) public static boolean matches( final Context context, final Scriptable thisObj, final Object[] args, final Function function) { final String selectorString = (String) args[0]; if (!(thisObj instanceof Element)) { throw ScriptRuntime.typeError("Illegal invocation"); } try { final DomNode domNode = ((Element) thisObj).getDomNodeOrNull(); return domNode != null && ((DomElement) domNode).matches(selectorString); } catch (final CSSException e) { throw ScriptRuntime.constructError("SyntaxError", "An invalid or illegal selector was specified (selector: '" + selectorString + "' error: " + e.getMessage() + ")."); } }
Example #3
Source File: DomNode.java From HtmlUnit-Android with Apache License 2.0 | 6 votes |
/** * Returns the {@link SelectorList}. * @param selectors the selectors * @param browserVersion the {@link BrowserVersion} * @return the {@link SelectorList} * @throws IOException if an error occurs */ protected SelectorList getSelectorList(final String selectors, final BrowserVersion browserVersion) throws IOException { final CSSOMParser parser = new CSSOMParser(new CSS3Parser()); final CheckErrorHandler errorHandler = new CheckErrorHandler(); parser.setErrorHandler(errorHandler); final SelectorList selectorList = parser.parseSelectors(new InputSource(new StringReader(selectors))); // in case of error parseSelectors returns null if (errorHandler.errorDetected()) { throw new CSSException("Invalid selectors: " + selectors); } if (selectorList != null) { int documentMode = 9; if (browserVersion.hasFeature(QUERYSELECTORALL_NOT_IN_QUIRKS)) { final Object sobj = getPage().getScriptableObject(); if (sobj instanceof HTMLDocument) { documentMode = ((HTMLDocument) sobj).getDocumentMode(); } } CSSStyleSheet.validateSelectors(selectorList, documentMode, this); } return selectorList; }
Example #4
Source File: DomNode.java From HtmlUnit-Android with Apache License 2.0 | 6 votes |
/** * Retrieves all element nodes from descendants of the starting element node that match any selector * within the supplied selector strings. * @param selectors one or more CSS selectors separated by commas * @return list of all found nodes */ public DomNodeList<DomNode> querySelectorAll(final String selectors) { try { final BrowserVersion browserVersion = getPage().getWebClient().getBrowserVersion(); final SelectorList selectorList = getSelectorList(selectors, browserVersion); final List<DomNode> elements = new ArrayList<>(); if (selectorList != null) { for (final DomElement child : getDomElementDescendants()) { for (Selector selector : selectorList) { if (CSSStyleSheet.selects(browserVersion, selector, child, null, true)) { elements.add(child); break; } } } } return new StaticDomNodeList(elements); } catch (final IOException e) { throw new CSSException("Error parsing CSS selectors from '" + selectors + "': " + e.getMessage()); } }
Example #5
Source File: DomNode.java From htmlunit with Apache License 2.0 | 6 votes |
/** * Retrieves all element nodes from descendants of the starting element node that match any selector * within the supplied selector strings. * @param selectors one or more CSS selectors separated by commas * @return list of all found nodes */ public DomNodeList<DomNode> querySelectorAll(final String selectors) { try { final BrowserVersion browserVersion = getPage().getWebClient().getBrowserVersion(); final SelectorList selectorList = getSelectorList(selectors, browserVersion); final List<DomNode> elements = new ArrayList<>(); if (selectorList != null) { for (final DomElement child : getDomElementDescendants()) { for (Selector selector : selectorList) { if (CSSStyleSheet.selects(browserVersion, selector, child, null, true)) { elements.add(child); break; } } } } return new StaticDomNodeList(elements); } catch (final IOException e) { throw new CSSException("Error parsing CSS selectors from '" + selectors + "': " + e.getMessage()); } }
Example #6
Source File: DomNode.java From htmlunit with Apache License 2.0 | 6 votes |
/** * Returns the {@link SelectorList}. * @param selectors the selectors * @param browserVersion the {@link BrowserVersion} * @return the {@link SelectorList} * @throws IOException if an error occurs */ protected SelectorList getSelectorList(final String selectors, final BrowserVersion browserVersion) throws IOException { final CSSOMParser parser = new CSSOMParser(new CSS3Parser()); final CheckErrorHandler errorHandler = new CheckErrorHandler(); parser.setErrorHandler(errorHandler); final SelectorList selectorList = parser.parseSelectors(selectors); // in case of error parseSelectors returns null if (errorHandler.errorDetected()) { throw new CSSException("Invalid selectors: " + selectors); } if (selectorList != null) { int documentMode = 9; if (browserVersion.hasFeature(QUERYSELECTORALL_NOT_IN_QUIRKS)) { final Object sobj = getPage().getScriptableObject(); if (sobj instanceof HTMLDocument) { documentMode = ((HTMLDocument) sobj).getDocumentMode(); } } CSSStyleSheet.validateSelectors(selectorList, documentMode, this); } return selectorList; }
Example #7
Source File: Element.java From HtmlUnit-Android with Apache License 2.0 | 6 votes |
/** * Returns true if the element would be selected by the specified selector string; otherwise, returns false. * @param context the JavaScript context * @param thisObj the scriptable * @param args the arguments passed into the method * @param function the function * @return the value */ @JsxFunction({CHROME, FF, EDGE}) public static boolean matches( final Context context, final Scriptable thisObj, final Object[] args, final Function function) { final String selectorString = (String) args[0]; if (!(thisObj instanceof Element)) { throw ScriptRuntime.typeError("Illegal invocation"); } try { final DomNode domNode = ((Element) thisObj).getDomNodeOrNull(); return domNode != null && ((DomElement) domNode).matches(selectorString); } catch (final CSSException e) { throw ScriptRuntime.constructError("SyntaxError", "An invalid or illegal selector was specified (selector: '" + selectorString + "' error: " + e.getMessage() + ")."); } }
Example #8
Source File: DomElement.java From htmlunit with Apache License 2.0 | 6 votes |
/** * Returns true if the element would be selected by the specified selector string; otherwise, returns false. * @param selectorString the selector to test * @return true if the element would be selected by the specified selector string; otherwise, returns false. */ public boolean matches(final String selectorString) { try { final BrowserVersion browserVersion = getPage().getWebClient().getBrowserVersion(); final SelectorList selectorList = getSelectorList(selectorString, browserVersion); if (selectorList != null) { for (Selector selector : selectorList) { if (CSSStyleSheet.selects(browserVersion, selector, this, null, true)) { return true; } } } return false; } catch (final IOException e) { throw new CSSException("Error parsing CSS selectors from '" + selectorString + "': " + e.getMessage()); } }
Example #9
Source File: Element.java From HtmlUnit-Android with Apache License 2.0 | 5 votes |
/** * Retrieves all element nodes from descendants of the starting element node that match any selector * within the supplied selector strings. * The NodeList object returned by the querySelectorAll() method must be static, not live. * @param selectors the selectors * @return the static node list */ @JsxFunction public NodeList querySelectorAll(final String selectors) { try { return NodeList.staticNodeList(this, getDomNodeOrDie().querySelectorAll(selectors)); } catch (final CSSException e) { throw Context.reportRuntimeError("An invalid or illegal selector was specified (selector: '" + selectors + "' error: " + e.getMessage() + ")."); } }
Example #10
Source File: DocumentFragment.java From HtmlUnit-Android with Apache License 2.0 | 5 votes |
/** * Returns the first element within the document that matches the specified group of selectors. * @param selectors the selectors * @return null if no matches are found; otherwise, it returns the first matching element */ @JsxFunction public Node querySelector(final String selectors) { try { final DomNode node = getDomNodeOrDie().querySelector(selectors); if (node != null) { return (Node) node.getScriptableObject(); } return null; } catch (final CSSException e) { throw Context.reportRuntimeError("An invalid or illegal selector was specified (selector: '" + selectors + "' error: " + e.getMessage() + ")."); } }
Example #11
Source File: DocumentFragment.java From HtmlUnit-Android with Apache License 2.0 | 5 votes |
/** * Retrieves all element nodes from descendants of the starting element node that match any selector * within the supplied selector strings. * The NodeList object returned by the querySelectorAll() method must be static, not live. * @param selectors the selectors * @return the static node list */ @JsxFunction public NodeList querySelectorAll(final String selectors) { try { return NodeList.staticNodeList(this, getDomNodeOrDie().querySelectorAll(selectors)); } catch (final CSSException e) { throw Context.reportRuntimeError("An invalid or illegal selector was specified (selector: '" + selectors + "' error: " + e.getMessage() + ")."); } }
Example #12
Source File: Document.java From HtmlUnit-Android with Apache License 2.0 | 5 votes |
/** * Retrieves all element nodes from descendants of the starting element node that match any selector * within the supplied selector strings. * The NodeList object returned by the querySelectorAll() method must be static, not live. * @param selectors the selectors * @return the static node list */ @JsxFunction public NodeList querySelectorAll(final String selectors) { try { return NodeList.staticNodeList(this, getDomNodeOrDie().querySelectorAll(selectors)); } catch (final CSSException e) { throw Context.reportRuntimeError("An invalid or illegal selector was specified (selector: '" + selectors + "' error: " + e.getMessage() + ")."); } }
Example #13
Source File: Document.java From HtmlUnit-Android with Apache License 2.0 | 5 votes |
/** * Returns the first element within the document that matches the specified group of selectors. * @param selectors the selectors * @return null if no matches are found; otherwise, it returns the first matching element */ @JsxFunction public Node querySelector(final String selectors) { try { final DomNode node = getDomNodeOrDie().querySelector(selectors); if (node != null) { return (Node) node.getScriptableObject(); } return null; } catch (final CSSException e) { throw Context.reportRuntimeError("An invalid or illegal selector was specified (selector: '" + selectors + "' error: " + e.getMessage() + ")."); } }
Example #14
Source File: CSSStyleSheet.java From HtmlUnit-Android with Apache License 2.0 | 5 votes |
/** * Validates the list of selectors. * @param selectorList the selectors * @param documentMode see {@link HTMLDocument#getDocumentMode()} * @param domNode the dom node the query should work on * @throws CSSException if a selector is invalid */ public static void validateSelectors(final SelectorList selectorList, final int documentMode, final DomNode domNode) throws CSSException { for (Selector selector : selectorList) { if (!isValidSelector(selector, documentMode, domNode)) { throw new CSSException("Invalid selector: " + selector); } } }
Example #15
Source File: Element.java From HtmlUnit-Android with Apache License 2.0 | 5 votes |
/** * Returns the first element within the document that matches the specified group of selectors. * @param selectors the selectors * @return null if no matches are found; otherwise, it returns the first matching element */ @JsxFunction public Node querySelector(final String selectors) { try { final DomNode node = getDomNodeOrDie().querySelector(selectors); if (node != null) { return (Node) node.getScriptableObject(); } return null; } catch (final CSSException e) { throw Context.reportRuntimeError("An invalid or illegal selector was specified (selector: '" + selectors + "' error: " + e.getMessage() + ")."); } }
Example #16
Source File: Element.java From htmlunit with Apache License 2.0 | 5 votes |
/** * Retrieves all element nodes from descendants of the starting element node that match any selector * within the supplied selector strings. * The NodeList object returned by the querySelectorAll() method must be static, not live. * @param selectors the selectors * @return the static node list */ @JsxFunction public NodeList querySelectorAll(final String selectors) { try { return NodeList.staticNodeList(this, getDomNodeOrDie().querySelectorAll(selectors)); } catch (final CSSException e) { throw Context.reportRuntimeError("An invalid or illegal selector was specified (selector: '" + selectors + "' error: " + e.getMessage() + ")."); } }
Example #17
Source File: DocumentFragment.java From htmlunit with Apache License 2.0 | 5 votes |
/** * Returns the first element within the document that matches the specified group of selectors. * @param selectors the selectors * @return null if no matches are found; otherwise, it returns the first matching element */ @JsxFunction public Node querySelector(final String selectors) { try { final DomNode node = getDomNodeOrDie().querySelector(selectors); if (node != null) { return (Node) node.getScriptableObject(); } return null; } catch (final CSSException e) { throw Context.reportRuntimeError("An invalid or illegal selector was specified (selector: '" + selectors + "' error: " + e.getMessage() + ")."); } }
Example #18
Source File: DocumentFragment.java From htmlunit with Apache License 2.0 | 5 votes |
/** * Retrieves all element nodes from descendants of the starting element node that match any selector * within the supplied selector strings. * The NodeList object returned by the querySelectorAll() method must be static, not live. * @param selectors the selectors * @return the static node list */ @JsxFunction public NodeList querySelectorAll(final String selectors) { try { return NodeList.staticNodeList(this, getDomNodeOrDie().querySelectorAll(selectors)); } catch (final CSSException e) { throw Context.reportRuntimeError("An invalid or illegal selector was specified (selector: '" + selectors + "' error: " + e.getMessage() + ")."); } }
Example #19
Source File: Document.java From htmlunit with Apache License 2.0 | 5 votes |
/** * Retrieves all element nodes from descendants of the starting element node that match any selector * within the supplied selector strings. * The NodeList object returned by the querySelectorAll() method must be static, not live. * @param selectors the selectors * @return the static node list */ @JsxFunction public NodeList querySelectorAll(final String selectors) { try { return NodeList.staticNodeList(this, getDomNodeOrDie().querySelectorAll(selectors)); } catch (final CSSException e) { throw Context.reportRuntimeError("An invalid or illegal selector was specified (selector: '" + selectors + "' error: " + e.getMessage() + ")."); } }
Example #20
Source File: Document.java From htmlunit with Apache License 2.0 | 5 votes |
/** * Returns the first element within the document that matches the specified group of selectors. * @param selectors the selectors * @return null if no matches are found; otherwise, it returns the first matching element */ @JsxFunction public Node querySelector(final String selectors) { try { final DomNode node = getDomNodeOrDie().querySelector(selectors); if (node != null) { return node.getScriptableObject(); } return null; } catch (final CSSException e) { throw Context.reportRuntimeError("An invalid or illegal selector was specified (selector: '" + selectors + "' error: " + e.getMessage() + ")."); } }
Example #21
Source File: CSSStyleSheet.java From htmlunit with Apache License 2.0 | 5 votes |
/** * Validates the list of selectors. * @param selectorList the selectors * @param documentMode see {@link HTMLDocument#getDocumentMode()} * @param domNode the dom node the query should work on * @throws CSSException if a selector is invalid */ public static void validateSelectors(final SelectorList selectorList, final int documentMode, final DomNode domNode) throws CSSException { for (Selector selector : selectorList) { if (!isValidSelector(selector, documentMode, domNode)) { throw new CSSException("Invalid selector: " + selector); } } }
Example #22
Source File: Element.java From htmlunit with Apache License 2.0 | 5 votes |
/** * Returns the first element within the document that matches the specified group of selectors. * @param selectors the selectors * @return null if no matches are found; otherwise, it returns the first matching element */ @JsxFunction public Node querySelector(final String selectors) { try { final DomNode node = getDomNodeOrDie().querySelector(selectors); if (node != null) { return node.getScriptableObject(); } return null; } catch (final CSSException e) { throw Context.reportRuntimeError("An invalid or illegal selector was specified (selector: '" + selectors + "' error: " + e.getMessage() + ")."); } }
Example #23
Source File: DomNode.java From htmlunit with Apache License 2.0 | 4 votes |
@Override public void error(final CSSParseException exception) throws CSSException { errorDetected_ = true; }
Example #24
Source File: DomNode.java From htmlunit with Apache License 2.0 | 4 votes |
@Override public void fatalError(final CSSParseException exception) throws CSSException { errorDetected_ = true; }
Example #25
Source File: DomNode.java From htmlunit with Apache License 2.0 | 4 votes |
@Override public void warning(final CSSParseException exception) throws CSSException { // ignore }
Example #26
Source File: CSSStyleSheet.java From HtmlUnit-Android with Apache License 2.0 | 4 votes |
/** * @param documentMode see {@link HTMLDocument#getDocumentMode()} */ private static boolean isValidCondition(final Condition condition, final int documentMode, final DomNode domNode) { switch (condition.getConditionType()) { case ATTRIBUTE_CONDITION: case ID_CONDITION: case LANG_CONDITION: case ONE_OF_ATTRIBUTE_CONDITION: case BEGIN_HYPHEN_ATTRIBUTE_CONDITION: case CLASS_CONDITION: return true; case PSEUDO_CLASS_CONDITION: final PseudoClassCondition pcc = (PseudoClassCondition) condition; String value = pcc.getValue(); if (value.endsWith(")")) { if (value.endsWith("()")) { return false; } value = value.substring(0, value.indexOf('(') + 1) + ')'; } if (documentMode < 9) { return CSS2_PSEUDO_CLASSES.contains(value); } if (!CSS2_PSEUDO_CLASSES.contains(value) && domNode.hasFeature(QUERYSELECTOR_CSS3_PSEUDO_REQUIRE_ATTACHED_NODE) && !domNode.isAttachedToPage() && !domNode.hasChildNodes()) { throw new CSSException("Syntax Error"); } if ("nth-child()".equals(value)) { final String arg = StringUtils.substringBetween(pcc.getValue(), "(", ")").trim(); return "even".equalsIgnoreCase(arg) || "odd".equalsIgnoreCase(arg) || NTH_NUMERIC.matcher(arg).matches() || NTH_COMPLEX.matcher(arg).matches(); } return CSS3_PSEUDO_CLASSES.contains(value); default: LOG.warn("Unhandled CSS condition type '" + condition.getConditionType() + "'. Accepting it silently."); return true; } }
Example #27
Source File: CSSStyleSheet.java From htmlunit with Apache License 2.0 | 4 votes |
/** * @param documentMode see {@link HTMLDocument#getDocumentMode()} */ private static boolean isValidCondition(final Condition condition, final int documentMode, final DomNode domNode) { switch (condition.getConditionType()) { case ATTRIBUTE_CONDITION: case ID_CONDITION: case LANG_CONDITION: case ONE_OF_ATTRIBUTE_CONDITION: case BEGIN_HYPHEN_ATTRIBUTE_CONDITION: case CLASS_CONDITION: case PREFIX_ATTRIBUTE_CONDITION: case SUBSTRING_ATTRIBUTE_CONDITION: case SUFFIX_ATTRIBUTE_CONDITION: return true; case PSEUDO_CLASS_CONDITION: String value = condition.getValue(); if (value.endsWith(")")) { if (value.endsWith("()")) { return false; } value = value.substring(0, value.indexOf('(') + 1) + ')'; } if (documentMode < 9) { return CSS2_PSEUDO_CLASSES.contains(value); } if (!CSS2_PSEUDO_CLASSES.contains(value) && domNode.hasFeature(QUERYSELECTOR_CSS3_PSEUDO_REQUIRE_ATTACHED_NODE) && !domNode.isAttachedToPage() && !domNode.hasChildNodes()) { throw new CSSException("Syntax Error"); } if ("nth-child()".equals(value)) { final String arg = StringUtils.substringBetween(condition.getValue(), "(", ")").trim(); return "even".equalsIgnoreCase(arg) || "odd".equalsIgnoreCase(arg) || NTH_NUMERIC.matcher(arg).matches() || NTH_COMPLEX.matcher(arg).matches(); } return CSS3_PSEUDO_CLASSES.contains(value); default: if (LOG.isWarnEnabled()) { LOG.warn("Unhandled CSS condition type '" + condition.getConditionType() + "'. Accepting it silently."); } return true; } }
Example #28
Source File: DomNode.java From HtmlUnit-Android with Apache License 2.0 | 4 votes |
@Override public void warning(final CSSParseException exception) throws CSSException { // ignore }
Example #29
Source File: DomNode.java From HtmlUnit-Android with Apache License 2.0 | 4 votes |
@Override public void fatalError(final CSSParseException exception) throws CSSException { errorDetected_ = true; }
Example #30
Source File: DomNode.java From HtmlUnit-Android with Apache License 2.0 | 4 votes |
@Override public void error(final CSSParseException exception) throws CSSException { errorDetected_ = true; }