Пример #1
0
        public virtual Node ReplaceChild(Node newChild, Node oldChild)
        {
            if (newChild is Attr || newChild is Document || newChild.Contains(this))
                throw new DOMException(ErrorCode.HierarchyRequestError);
            else if (newChild == oldChild)
                return oldChild;
            else if (newChild.ParentNode != null)
                throw new DOMException(ErrorCode.InUse);

            var n = _children.Length;

            for (int i = 0; i < n; i++)
            {
                if (_children[i] == oldChild)
                {
                    RemoveChild(oldChild);
                    InsertChild(i, newChild);
                    return oldChild;
                }
            }

            return null;
        }
Пример #2
0
        public virtual Node InsertBefore(Node newElement, Node referenceElement)
        {
            if (newElement is Attr || newElement is Document || newElement.Contains(this))
                throw new DOMException(ErrorCode.HierarchyRequestError);

            var n = _children.Length;

            for (int i = 0; i < n; i++)
            {
                if (_children[i] == referenceElement)
                    return InsertChild(i, newElement);
            }

            return AppendChild(newElement);
        }
Пример #3
0
        public virtual Node InsertChild(Int32 index, Node child)
        {
            if (child is DocumentFragment)
            {
                var childs = child.ChildNodes;

                for (int i = 0; i < childs.Length; i++)
                    InsertChild(index + i, childs[i]);
            }
            else if (child is Attr || child is Document || child.Contains(this))
            {
                throw new DOMException(ErrorCode.HierarchyRequestError);
            }
            else
            {
                if (child.ParentNode != null)
                    throw new DOMException(ErrorCode.InUse);

                child._parent = this;
                child.OwnerDocument = _owner ?? (this as Document);
                _children.Insert(index, child);
            }

            return child;
        }
Пример #4
0
        public virtual DocumentPosition CompareDocumentPosition(Node otherNode)
        {
            if (this == otherNode)
                return DocumentPosition.Same;

            if(this.OwnerDocument != otherNode.OwnerDocument)
            {
                return DocumentPosition.Disconnected | DocumentPosition.ImplementationSpecific |
                    (otherNode.GetHashCode() > this.GetHashCode() ? DocumentPosition.Following : DocumentPosition.Preceding);
            }
            else if (this.Contains(otherNode))
            {
                return DocumentPosition.ContainedBy | DocumentPosition.Following;
            }
            else if (otherNode.Contains(this))
            {
                return DocumentPosition.Contains | DocumentPosition.Preceding;
            }

            return CompareRelativePositionInNodeList(_owner.ChildNodes, this, otherNode);
        }