/// <summary> /// Appends the LAST selector to the complex of selectors. /// </summary> /// <param name="selector">The (final) selector to append.</param> /// <returns>The current complex selector.</returns> public ComplexSelector ConcludeSelector(Selector selector) { if (!IsReady) { selectors.Add(new CombinatorSelector { selector = selector, transform = null }); IsReady = true; } return this; }
/// <summary> /// Appends a selector to the complex of selectors. /// </summary> /// <param name="selector">The selector to append.</param> /// <param name="combinator">The combinator to use.</param> /// <returns>The current complex selector.</returns> public ComplexSelector AppendSelector(Selector selector, CssCombinator combinator) { if (IsReady) return this; Func<Element, IEnumerable<Element>> transform = null; char delim; switch (combinator) { case CssCombinator.Child: delim = Specification.GT; transform = el => Single(el.ParentElement); break; case CssCombinator.AdjacentSibling: delim = Specification.PLUS; transform = el => Single(el.PreviousElementSibling); break; case CssCombinator.Descendent: delim = Specification.SPACE; transform = el => { var parents = new List<Element>(); var parent = el.ParentElement; while(parent != null) { parents.Add(parent); parent = parent.ParentElement; } return parents; }; break; case CssCombinator.Sibling: delim = Specification.TILDE; transform = el => { var parent = el.ParentElement; if (parent == null) return new Element[0]; var kids = parent.Children; var passed = false; var siblings = new List<Element>(); for (int i = kids.Length - 1; i >= 0; i--) { if (kids[i] == el) passed = true; else if (passed) siblings.Add(kids[i]); } return siblings; }; break; default: return this; } selectors.Add(new CombinatorSelector { selector = selector, transform = transform, delimiter = delim }); return this; }