CsQuery.CQ.InsertAtOffset C# (CSharp) Method

InsertAtOffset() protected method

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.
This is a helper for Before and After. There is special handling when the target is not part of a DOM. Instead of altering the DOM, this method will alter the selection set, and return a CQ object that contains the new sequence. Normally, it would return the same CQ object (but alter the DOM).
protected InsertAtOffset ( CQ target, int offset, CQ &insertedElements ) : CQ
target CQ /// The target element. ///
offset int /// The offset from the target at which to begin inserting. ///
insertedElements CQ /// [out] The inserted elements. ///
return CQ
        protected CQ InsertAtOffset(CQ target, int offset, out CQ insertedElements)
        {
            
            // Copy the target list: it could change otherwise
            List<IDomObject> targets = new List<IDomObject>(target);
            bool isTargetDisconnected = (target.CsQueryParent != null && target.Document != target.CsQueryParent.Document) ||
                targets.Any(item => item.ParentNode == null);

            bool isFirst = true;

            
            insertedElements = NewCqUnbound();
            

            // 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.

            Document = target.Document;

            if (isTargetDisconnected)
            {
                // When the target is disconnected, just append elements to the selection set, not to the DOM
                    
                CQ result = NewCqInDomain();
                result.CsQueryParent = this.CsQueryParent;

                if (offset == 0)
                {
                    result.AddSelection(Selection);
                }
                result.AddSelection(target);

                if (offset == 1)
                {
                    result.AddSelection(Selection);
                }

                result.SelectionSet.OutputOrder = SelectionSetOrder.OrderAdded;
                insertedElements.AddSelection(Selection);
                return result;
            }
            else
            {

                foreach (var el in targets)
                {
                    bool isElDisconnected = el.ParentNode == null;
                    
                    if (isFirst)
                    {
                        insertedElements.AddSelection(SelectionSet);
                        InsertAtOffset(el, offset);
                        isFirst = false;
                    }
                    else
                    {
                        var clone = this.Clone();
                        clone.InsertAtOffset(el, offset);
                        insertedElements.AddSelection(clone);
                    }
                    
                }
            }
            return target;
        }
        #endregion

Same methods

CQ::InsertAtOffset ( IEnumerable target, int offset ) : CQ