protected List<IJavaFXElement> found(List<IJavaFXElement> pElements, IJavaFXAgent driver) { List<IJavaFXElement> r = new ArrayList<IJavaFXElement>(); for (IJavaFXElement je : pElements) { Node component = je.getComponent(); if (!(component instanceof Parent)) continue; int index = getIndexOfComponentInParent(component); if (index < 0) continue; Parent parent = component.getParent(); JFXWindow topContainer = driver.switchTo().getTopContainer(); ObservableList<Node> children = parent.getChildrenUnmodifiable(); for (int i = index + 1; i < children.size(); i++) { Node c = children.get(i); IJavaFXElement je2 = JavaFXElementFactory.createElement(c, driver, driver.switchTo().getTopContainer()); if (sibling.matchesSelector(je2).size() > 0) { IJavaFXElement e = topContainer.addElement(JavaFXElementFactory.createElement(c, driver, topContainer)); if (!r.contains(e)) r.add(e); } } } return r; }
/* * Scans for a CSS 'simple selector'. * Returns true if it found one. * Returns false if there was an error or the input is empty. */ public boolean nextSimpleSelector(Selector selector) throws SAXException { if (empty()) return false; int start = position; Combinator combinator = null; SimpleSelector selectorPart = null; if (!selector.isEmpty()) { if (consume('>')) { combinator = Combinator.CHILD; skipWhitespace(); } else if (consume('+')) { combinator = Combinator.FOLLOWS; skipWhitespace(); } } if (consume('*')) { selectorPart = new SimpleSelector(combinator, null); } else { String tag = nextIdentifier(); if (tag != null) { selectorPart = new SimpleSelector(combinator, tag); selector.addedElement(); } } while (!empty()) { if (consume('.')) { // ".foo" is equivalent to *[class="foo"] if (selectorPart == null) selectorPart = new SimpleSelector(combinator, null); String value = nextIdentifier(); if (value == null) throw new SAXException("Invalid \".class\" selector in <style> element"); selectorPart.addAttrib(CLASS, AttribOp.EQUALS, value); selector.addedAttributeOrPseudo(); continue; } if (consume('#')) { // "#foo" is equivalent to *[id="foo"] if (selectorPart == null) selectorPart = new SimpleSelector(combinator, null); String value = nextIdentifier(); if (value == null) throw new SAXException("Invalid \"#id\" selector in <style> element"); selectorPart.addAttrib(ID, AttribOp.EQUALS, value); selector.addedIdAttribute(); } if (selectorPart == null) break; // Now check for attribute selection and pseudo selectors if (consume('[')) { skipWhitespace(); String attrName = nextIdentifier(); String attrValue = null; if (attrName == null) throw new SAXException("Invalid attribute selector in <style> element"); skipWhitespace(); AttribOp op = null; if (consume('=')) op = AttribOp.EQUALS; else if (consume("~=")) op = AttribOp.INCLUDES; else if (consume("|=")) op = AttribOp.DASHMATCH; if (op != null) { skipWhitespace(); attrValue = nextAttribValue(); if (attrValue == null) throw new SAXException("Invalid attribute selector in <style> element"); skipWhitespace(); } if (!consume(']')) throw new SAXException("Invalid attribute selector in <style> element"); selectorPart.addAttrib(attrName, (op == null) ? AttribOp.EXISTS : op, attrValue); selector.addedAttributeOrPseudo(); continue; } if (consume(':')) { // skip pseudo int pseudoStart = position; if (nextIdentifier() != null) { if (consume('(')) { skipWhitespace(); if (nextIdentifier() != null) { skipWhitespace(); if (!consume(')')) { position = pseudoStart - 1; break; } } } selectorPart.addPseudo(input.substring(pseudoStart, position)); selector.addedAttributeOrPseudo(); } } break; } if (selectorPart != null) { selector.add(selectorPart); return true; } // Otherwise 'fail' position = start; return false; }
public String toString() { return selector.toString() + condition.toString(); }
/** * Adds the rule. * * @param styleSheet the style sheet * @param rule the rule * @throws MalformedURLException the malformed url exception * @throws UnsupportedEncodingException */ private final void addRule(CSSStyleSheet styleSheet, CSSRule rule) throws MalformedURLException, UnsupportedEncodingException { HTMLDocumentImpl document = this.document; if (rule instanceof CSSStyleRule) { CSSStyleRule sr = (CSSStyleRule) rule; String selectorList = sr.getSelectorText(); StringTokenizer commaTok = new StringTokenizer(selectorList, ","); while (commaTok.hasMoreTokens()) { String selectorPart = commaTok.nextToken().toLowerCase(); ArrayList<SimpleSelector> simpleSelectors = null; String lastSelectorText = null; StringTokenizer tok = new StringTokenizer(selectorPart, " \t\r\n"); if (tok.hasMoreTokens()) { simpleSelectors = new ArrayList<SimpleSelector>(); SimpleSelector prevSelector = null; SELECTOR_FOR: for (; ; ) { String token = tok.nextToken(); if (">".equals(token)) { if (prevSelector != null) { prevSelector.setSelectorType(SimpleSelector.PARENT); } continue SELECTOR_FOR; } else if ("+".equals(token)) { if (prevSelector != null) { prevSelector.setSelectorType(SimpleSelector.PRECEEDING_SIBLING); } continue SELECTOR_FOR; } int colonIdx = token.indexOf(':'); String simpleSelectorText = colonIdx == -1 ? token : token.substring(0, colonIdx); String pseudoElement = colonIdx == -1 ? null : token.substring(colonIdx + 1); prevSelector = new SimpleSelector(simpleSelectorText, pseudoElement); simpleSelectors.add(prevSelector); if (!tok.hasMoreTokens()) { lastSelectorText = simpleSelectorText; break; } } } if (lastSelectorText != null) { int dotIdx = lastSelectorText.indexOf('.'); if (dotIdx != -1) { String elemtl = lastSelectorText.substring(0, dotIdx); String classtl = lastSelectorText.substring(dotIdx + 1); this.addClassRule(elemtl, classtl, sr, simpleSelectors); } else { int poundIdx = lastSelectorText.indexOf('#'); if (poundIdx != -1) { String elemtl = lastSelectorText.substring(0, poundIdx); String idtl = lastSelectorText.substring(poundIdx + 1); this.addIdRule(elemtl, idtl, sr, simpleSelectors); } else { String elemtl = lastSelectorText; this.addElementRule(elemtl, sr, simpleSelectors); } } } } // TODO: Attribute selectors } else if (rule instanceof CSSImportRule) { UserAgentContext uacontext = document.getUserAgentContext(); if (uacontext.isExternalCSSEnabled()) { CSSImportRule importRule = (CSSImportRule) rule; if (CSSUtilities.matchesMedia(importRule.getMedia(), uacontext)) { String href = importRule.getHref(); CSSStyleSheet sheet = null; try { sheet = CSSUtilities.parse(href, document); } catch (Exception err) { logger.severe("Unable to parse CSS. URI=[" + href + "]." + err); } if (sheet != null) { this.addStyleSheet(sheet); } } } } else if (rule instanceof CSSMediaRule) { CSSMediaRule mrule = (CSSMediaRule) rule; MediaList mediaList = mrule.getMedia(); if (CSSUtilities.matchesMedia(mediaList, document.getUserAgentContext())) { CSSRuleList ruleList = mrule.getCssRules(); int length = ruleList.getLength(); for (int i = 0; i < length; i++) { CSSRule subRule = ruleList.item(i); this.addRule(styleSheet, subRule); } } } }