/// <summary> /// Runs a set of selectors and returns the combined result as a single enumerable. /// </summary> /// /// <param name="selectors"> /// A sequence of strings that area each selectors /// </param> /// /// <returns> /// An enumerator that allows foreach to be used to process merge selections in this collection. /// </returns> protected IEnumerable <IDomObject> MergeSelections(IEnumerable <string> selectors) { SelectionSet <IDomObject> allContent = new SelectionSet <IDomObject>(SelectionSetOrder.Ascending); Each(selectors, item => allContent.AddRange(Select(item))); return(allContent); }
/// <summary> /// Insert every element in the selection at or after the index of each target (adding offset to the index). /// If there is more than one target, the a clone is made of the selection for the 2nd and later targets. /// </summary> /// <param name="target"></param> /// <param name="offset"></param> /// <returns></returns> protected CQ InsertAtOffset(IEnumerable <IDomObject> target, int offset) { SelectionSet <IDomObject> sel = target as SelectionSet <IDomObject>; bool isCsQuery = sel != null; bool isFirst = true; // Copy the target list: it could change otherwise List <IDomObject> targets = new List <IDomObject>(target); if (isCsQuery && sel.Count == 0) { // If appending items to an empty selection, just add them to the selection set sel.AddRange(SelectionSet); } else { foreach (var el in targets) { if (el.IsDisconnected) { // Disconnected items are added to the selection set (if that's the target) if (!isCsQuery) { throw new InvalidOperationException("You can't add elements to a disconnected element list, it must be in a selection set"); } int index = sel.IndexOf(el); foreach (var item in SelectionSet) { sel.Insert(index + offset, item); } } else { if (isFirst) { InsertAtOffset(el, offset); isFirst = false; } else { Clone().InsertAtOffset(el, offset); } } } } return(this); }
/// <summary> /// Insert every element in the selection at or after the index of each target (adding offset to /// the index). If there is more than one target, the a clone is made of the selection for the /// 2nd and later targets. /// </summary> /// /// <exception cref="InvalidOperationException"> /// Thrown if attempting to add elements to a disconnected (parentless) sequence /// </exception> /// /// <param name="target"> /// The target element /// </param> /// <param name="offset"> /// The offset from the target at which to begin inserting /// </param> /// <param name="insertedElements"> /// [out] The inserted elements. /// </param> /// /// <returns> /// The current CQ object /// </returns> protected CQ InsertAtOffset(IEnumerable <IDomObject> target, int offset, out CQ insertedElements) { CQ cq = target as CQ; SelectionSet <IDomObject> targetSelectionSet = null; if (cq != null) { targetSelectionSet = cq.SelectionSet; } bool isTargetCsQuery = cq != null; bool isFirst = true; // Copy the target list: it could change otherwise List <IDomObject> targets = new List <IDomObject>(target); insertedElements = NewInstance(); bool isEmptyTarget = targetSelectionSet.Count == 0; // bind the source to the target's document if it was itself a CsQuery object, and update its selection set to reflect the // current document. if (isTargetCsQuery) { Document = cq.Document; } if (isTargetCsQuery && isEmptyTarget) { // If appending items to an empty selection, just add them to the selection set targetSelectionSet.AddRange(SelectionSet); insertedElements.AddSelection(SelectionSet); // selection set will be messed up if document was changed; rebuild it //SelectionSet.Clear(); //SelectionSet.AddRange(insertedElements); } else { foreach (var el in targets) { if (el.IsDisconnected) { // Disconnected items are added to the selection set (if that's the target) if (!isTargetCsQuery) { throw new InvalidOperationException("You can't add elements to a disconnected element list, it must be in a selection set"); } int index = targetSelectionSet.IndexOf(el); targetSelectionSet.OutputOrder = SelectionSetOrder.OrderAdded; foreach (var item in SelectionSet) { targetSelectionSet.Insert(index + offset, item); } insertedElements.AddSelection(SelectionSet); // selection set will be messed up if document was changed; rebuild it //SelectionSet.Clear(); //SelectionSet.AddRange(insertedElements); } else { if (isFirst) { insertedElements.AddSelection(SelectionSet); InsertAtOffset(el, offset); isFirst = false; // selection set will be messed up if document was changed; rebuild it //SelectionSet.Clear(); //SelectionSet.AddRange(insertedElements); } else { var clone = Clone(); clone.InsertAtOffset(el, offset); insertedElements.AddSelection(clone); } } } } return(this); }
/// <summary> /// Runs a set of selectors and returns the combined result as a single enumerable /// </summary> /// <param name="selectors"></param> /// <returns></returns> protected IEnumerable<IDomObject> mergeSelections(IEnumerable<string> selectors) { SelectionSet<IDomObject> allContent = new SelectionSet<IDomObject>(); Each(selectors, item => allContent.AddRange(Select(item))); return allContent; }