Example #1
0
        /// <summary>
        /// Return the index of obj within its siblings, including only elements with the same node name.
        /// </summary>
        ///
        /// <param name="obj">
        /// The object to seek
        /// </param>
        /// <param name="onlyOfSameType">
        /// true to only objects of the same NodeName should be considered
        /// </param>
        /// <param name="fromLast">
        /// Count from the last element instead of the first.
        /// </param>
        ///
        /// <returns>
        /// The zero-based index of obj within its siblings (or its siblings of the same type)
        /// </returns>

        private int IndexOf(IDomElement obj, bool onlyOfSameType, bool fromLast = false)
        {
            // get the index just for this type
            int typeIndex = 0;

            var childNodes = obj.ParentNode.ChildNodes;
            int length     = childNodes.Count;

            for (int index = 0; index < length; index++)
            {
                var el = NthChildMatcher.GetEffectiveChild(childNodes, index, fromLast);
                if (el.NodeType == NodeType.ELEMENT_NODE)
                {
                    if (!onlyOfSameType || el.NodeNameID == obj.NodeNameID)
                    {
                        if (ReferenceEquals(el, obj))
                        {
                            return(typeIndex);
                        }
                        typeIndex++;
                    }
                }
            }
            return(-1);
        }