/// <summary> /// Searches for the closest tab index either before or after the given relative element. /// Note that relative is expected to be a child of this element. /// </summary> /// <param name='forward'>True if you want to search forwards; false for backwards.</param> private bool SearchRelativeFocusable(HtmlElement relativeTo, bool forward, int search, ref int bestSoFar, ref HtmlElement best) { if (childNodes_ == null) { return(false); } int start = (relativeTo == null) ? -1 : relativeTo.childIndex; if (forward) { for (int i = start + 1; i < childNodes_.length; i++) { HtmlElement el = childNodes_[i] as HtmlElement; // Search in this child node: if (el != null && el.SearchChildFocusable(null, true, search, ref bestSoFar, ref best)) { return(true); } } } else { for (int i = start - 1; i >= 0; i--) { HtmlElement el = childNodes_[i] as HtmlElement; // Search in this child node: if (el != null && el.SearchChildFocusable(null, false, search, ref bestSoFar, ref best)) { return(true); } } } return(false); }
/// <summary>Searches for the closest focusable element with/ without a tabindex. Returns true if it gets a match.</summary> public bool SearchChildFocusable(HtmlElement stopAt, bool forward, int search, ref int bestSoFar, ref HtmlElement best) { if (this == best || this == stopAt) { // Terminate return(true); } // It must not declare a tabindex: if (focusable) { if (search == -1 && !hasAttribute("tabindex")) { // This is the best one! best = this; return(true); } else if (search != -1 && hasAttribute("tabindex")) { // Is its tabIndex suitable? int index = tabIndex; if (index >= 0 && ( (forward && index < bestSoFar && index >= search) || (!forward && index > bestSoFar && index <= search) ) ) { // This is the best one so far. best = this; bestSoFar = index; if (index == search) { // Direct match - stop! return(true); } } } } // Any kids? if (childNodes_ == null) { return(false); } if (forward) { for (int i = 0; i < childNodes_.length; i++) { HtmlElement el = childNodes_[i] as HtmlElement; // Search in it: if (el != null && el.SearchChildFocusable(stopAt, true, search, ref bestSoFar, ref best)) { return(true); } } } else { for (int i = childNodes_.length - 1; i >= 0; i--) { HtmlElement el = childNodes_[i] as HtmlElement; // Search in it: if (el != null && el.SearchChildFocusable(stopAt, false, search, ref bestSoFar, ref best)) { return(true); } } } return(false); }