Java Code Examples for com.gargoylesoftware.htmlunit.html.DomElement#getPage()
The following examples show how to use
com.gargoylesoftware.htmlunit.html.DomElement#getPage() .
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: CSSStyleSheet.java From htmlunit with Apache License 2.0 | 4 votes |
/** * Returns {@code true} if the specified selector selects the specified element. * * @param browserVersion the browser version * @param selector the selector to test * @param element the element to test * @param pseudoElement the pseudo element to match, (can be {@code null}) * @param fromQuerySelectorAll whether this is called from {@link DomNode#querySelectorAll(String)} * @return {@code true} if it does apply, {@code false} if it doesn't apply */ public static boolean selects(final BrowserVersion browserVersion, final Selector selector, final DomElement element, final String pseudoElement, final boolean fromQuerySelectorAll) { switch (selector.getSelectorType()) { case ELEMENT_NODE_SELECTOR: final ElementSelector es = (ElementSelector) selector; final String name = es.getLocalNameLowerCase(); if (name == null || name.equals(element.getLowercaseName())) { final List<Condition> conditions = es.getConditions(); if (conditions != null) { for (Condition condition : conditions) { if (!selects(browserVersion, condition, element, fromQuerySelectorAll)) { return false; } } } return true; } return false; case CHILD_SELECTOR: final DomNode parentNode = element.getParentNode(); if (parentNode == element.getPage()) { return false; } if (!(parentNode instanceof HtmlElement)) { return false; // for instance parent is a DocumentFragment } final ChildSelector cs = (ChildSelector) selector; return selects(browserVersion, cs.getSimpleSelector(), element, pseudoElement, fromQuerySelectorAll) && selects(browserVersion, cs.getAncestorSelector(), (HtmlElement) parentNode, pseudoElement, fromQuerySelectorAll); case DESCENDANT_SELECTOR: final DescendantSelector ds = (DescendantSelector) selector; final SimpleSelector simpleSelector = ds.getSimpleSelector(); if (selects(browserVersion, simpleSelector, element, pseudoElement, fromQuerySelectorAll)) { DomNode ancestor = element; if (simpleSelector.getSelectorType() != SelectorType.PSEUDO_ELEMENT_SELECTOR) { ancestor = ancestor.getParentNode(); } final Selector dsAncestorSelector = ds.getAncestorSelector(); while (ancestor instanceof HtmlElement) { if (selects(browserVersion, dsAncestorSelector, (HtmlElement) ancestor, pseudoElement, fromQuerySelectorAll)) { return true; } ancestor = ancestor.getParentNode(); } } return false; case DIRECT_ADJACENT_SELECTOR: final DirectAdjacentSelector das = (DirectAdjacentSelector) selector; if (selects(browserVersion, das.getSimpleSelector(), element, pseudoElement, fromQuerySelectorAll)) { DomNode prev = element.getPreviousSibling(); while (prev != null && !(prev instanceof HtmlElement)) { prev = prev.getPreviousSibling(); } return prev != null && selects(browserVersion, das.getSelector(), (HtmlElement) prev, pseudoElement, fromQuerySelectorAll); } return false; case GENERAL_ADJACENT_SELECTOR: final GeneralAdjacentSelector gas = (GeneralAdjacentSelector) selector; if (selects(browserVersion, gas.getSimpleSelector(), element, pseudoElement, fromQuerySelectorAll)) { for (DomNode prev1 = element.getPreviousSibling(); prev1 != null; prev1 = prev1.getPreviousSibling()) { if (prev1 instanceof HtmlElement && selects(browserVersion, gas.getSelector(), (HtmlElement) prev1, pseudoElement, fromQuerySelectorAll)) { return true; } } } return false; case PSEUDO_ELEMENT_SELECTOR: if (pseudoElement != null && pseudoElement.length() != 0 && pseudoElement.charAt(0) == ':') { final String pseudoName = ((PseudoElementSelector) selector).getLocalName(); return pseudoName.equals(pseudoElement.substring(1)); } return false; default: if (LOG.isErrorEnabled()) { LOG.error("Unknown CSS selector type '" + selector.getSelectorType() + "'."); } return false; } }
Example 2
Source File: CSSStyleSheet.java From HtmlUnit-Android with Apache License 2.0 | 4 votes |
/** * Returns {@code true} if the specified selector selects the specified element. * * @param browserVersion the browser version * @param selector the selector to test * @param element the element to test * @param pseudoElement the pseudo element to match, (can be {@code null}) * @param fromQuerySelectorAll whether this is called from {@link DomNode#querySelectorAll(String)} * @return {@code true} if it does apply, {@code false} if it doesn't apply */ public static boolean selects(final BrowserVersion browserVersion, final Selector selector, final DomElement element, final String pseudoElement, final boolean fromQuerySelectorAll) { switch (selector.getSelectorType()) { case ELEMENT_NODE_SELECTOR: final ElementSelector es = (ElementSelector) selector; final String name = es.getLocalNameLowerCase(); if (name == null || name.equals(element.getLowercaseName())) { final List<Condition> conditions = es.getConditions(); if (conditions != null) { for (Condition condition : conditions) { if (!selects(browserVersion, condition, element, fromQuerySelectorAll)) { return false; } } } return true; } return false; case CHILD_SELECTOR: final DomNode parentNode = element.getParentNode(); if (parentNode == element.getPage()) { return false; } if (!(parentNode instanceof HtmlElement)) { return false; // for instance parent is a DocumentFragment } final ChildSelector cs = (ChildSelector) selector; return selects(browserVersion, cs.getSimpleSelector(), element, pseudoElement, fromQuerySelectorAll) && selects(browserVersion, cs.getAncestorSelector(), (HtmlElement) parentNode, pseudoElement, fromQuerySelectorAll); case DESCENDANT_SELECTOR: final DescendantSelector ds = (DescendantSelector) selector; final SimpleSelector simpleSelector = ds.getSimpleSelector(); if (selects(browserVersion, simpleSelector, element, pseudoElement, fromQuerySelectorAll)) { DomNode ancestor = element; if (simpleSelector.getSelectorType() != SelectorType.PSEUDO_ELEMENT_SELECTOR) { ancestor = ancestor.getParentNode(); } final Selector dsAncestorSelector = ds.getAncestorSelector(); while (ancestor instanceof HtmlElement) { if (selects(browserVersion, dsAncestorSelector, (HtmlElement) ancestor, pseudoElement, fromQuerySelectorAll)) { return true; } ancestor = ancestor.getParentNode(); } } return false; case DIRECT_ADJACENT_SELECTOR: final DirectAdjacentSelector das = (DirectAdjacentSelector) selector; if (selects(browserVersion, das.getSimpleSelector(), element, pseudoElement, fromQuerySelectorAll)) { DomNode prev = element.getPreviousSibling(); while (prev != null && !(prev instanceof HtmlElement)) { prev = prev.getPreviousSibling(); } return prev != null && selects(browserVersion, das.getSelector(), (HtmlElement) prev, pseudoElement, fromQuerySelectorAll); } return false; case GENERAL_ADJACENT_SELECTOR: final GeneralAdjacentSelector gas = (GeneralAdjacentSelector) selector; if (selects(browserVersion, gas.getSimpleSelector(), element, pseudoElement, fromQuerySelectorAll)) { for (DomNode prev1 = element.getPreviousSibling(); prev1 != null; prev1 = prev1.getPreviousSibling()) { if (prev1 instanceof HtmlElement && selects(browserVersion, gas.getSelector(), (HtmlElement) prev1, pseudoElement, fromQuerySelectorAll)) { return true; } } } return false; case PSEUDO_ELEMENT_SELECTOR: if (pseudoElement != null && !pseudoElement.isEmpty() && pseudoElement.charAt(0) == ':') { final String pseudoName = ((PseudoElementSelector) selector).getLocalName(); return pseudoName.equals(pseudoElement.substring(1)); } return false; default: LOG.error("Unknown CSS selector type '" + selector.getSelectorType() + "'."); return false; } }