Beispiel #1
0
        public virtual INode InsertBefore(INode newChild, INode refChild)
        {
            // TODO - handle newChild already in tree

            if (newChild == null)
            {
                return(null);
            }
            if (!(newChild is DomNodeImpl))
            {
                throw new DomException(DomException.WrongDocument, "newChild not instanceof DomNodeImpl");
            }
            DomNodeImpl newCh = (DomNodeImpl)newChild;

            if (this.Adaptee.Type == Node.RootNode)
            {
                if (newCh.Adaptee.Type != Node.DocTypeTag && newCh.Adaptee.Type != Node.ProcInsTag)
                {
                    throw new DomException(DomException.HierarchyRequest, "newChild cannot be a child of this node");
                }
            }
            else if (this.Adaptee.Type == Node.StartTag)
            {
                if (newCh.Adaptee.Type != Node.StartTag && newCh.Adaptee.Type != Node.StartEndTag && newCh.Adaptee.Type != Node.CommentTag && newCh.Adaptee.Type != Node.TextNode && newCh.Adaptee.Type != Node.CDATATag)
                {
                    throw new DomException(DomException.HierarchyRequest, "newChild cannot be a child of this node");
                }
            }
            if (refChild == null)
            {
                Node.InsertNodeAtEnd(this.Adaptee, newCh.Adaptee);
                if (this.Adaptee.Type == Node.StartEndTag)
                {
                    this.Adaptee.Type = Node.StartTag;
                }
            }
            else
            {
                Node refNode = this.Adaptee.Content;
                while (refNode != null)
                {
                    if (refNode.Adapter == refChild)
                    {
                        break;
                    }
                    refNode = refNode.Next;
                }
                if (refNode == null)
                {
                    throw new DomException(DomException.NotFound, "refChild not found");
                }
                Node.InsertNodeBeforeElement(refNode, newCh.Adaptee);
            }
            return(newChild);
        }
Beispiel #2
0
        public virtual INode AppendChild(INode newChild)
        {
            // TODO - handle newChild already in tree

            if (newChild == null)
            {
                return(null);
            }

            if (!(newChild is DomNodeImpl))
            {
                throw new DomException(DomException.WrongDocument, "newChild not instanceof DomNodeImpl");
            }

            DomNodeImpl newCh = (DomNodeImpl)newChild;

            if (this.Adaptee.Type == Node.RootNode)
            {
                if (newCh.Adaptee.Type != Node.DocTypeTag && newCh.Adaptee.Type != Node.ProcInsTag)
                {
                    throw new DomException(DomException.HierarchyRequest, "newChild cannot be a child of this node");
                }
            }
            else if (this.Adaptee.Type == Node.StartTag)
            {
                if (newCh.Adaptee.Type != Node.StartTag && newCh.Adaptee.Type != Node.StartEndTag && newCh.Adaptee.Type != Node.CommentTag && newCh.Adaptee.Type != Node.TextNode && newCh.Adaptee.Type != Node.CDATATag)
                {
                    throw new DomException(DomException.HierarchyRequest, "newChild cannot be a child of this node");
                }
            }

            Node.InsertNodeAtEnd(Adaptee, newCh.Adaptee);

            if (Adaptee.Type == Node.StartEndTag)
            {
                Adaptee.Type = Node.StartTag;
            }

            return(newChild);
        }
Beispiel #3
0
        public virtual INode ReplaceChild(INode newChild, INode oldChild)
        {
            // TODO - handle newChild already in tree

            if (newChild == null)
            {
                return(null);
            }
            if (!(newChild is DomNodeImpl))
            {
                throw new DomException(DomException.WrongDocument, "newChild not instanceof DomNodeImpl");
            }
            DomNodeImpl newCh = (DomNodeImpl)newChild;

            if (this.Adaptee.Type == Node.RootNode)
            {
                if (newCh.Adaptee.Type != Node.DocTypeTag && newCh.Adaptee.Type != Node.ProcInsTag)
                {
                    throw new DomException(DomException.HierarchyRequest, "newChild cannot be a child of this node");
                }
            }
            else if (this.Adaptee.Type == Node.StartTag)
            {
                if (newCh.Adaptee.Type != Node.StartTag && newCh.Adaptee.Type != Node.StartEndTag && newCh.Adaptee.Type != Node.CommentTag && newCh.Adaptee.Type != Node.TextNode && newCh.Adaptee.Type != Node.CDATATag)
                {
                    throw new DomException(DomException.HierarchyRequest, "newChild cannot be a child of this node");
                }
            }
            if (oldChild == null)
            {
                throw new DomException(DomException.NotFound, "oldChild not found");
            }
            else
            {
                Node n;
                Node refNode = this.Adaptee.Content;
                while (refNode != null)
                {
                    if (refNode.Adapter == oldChild)
                    {
                        break;
                    }
                    refNode = refNode.Next;
                }
                if (refNode == null)
                {
                    throw new DomException(DomException.NotFound, "oldChild not found");
                }
                newCh.Adaptee.Next    = refNode.Next;
                newCh.Adaptee.Prev    = refNode.Prev;
                newCh.Adaptee.Last    = refNode.Last;
                newCh.Adaptee.Parent  = refNode.Parent;
                newCh.Adaptee.Content = refNode.Content;
                if (refNode.Parent != null)
                {
                    if (refNode.Parent.Content == refNode)
                    {
                        refNode.Parent.Content = newCh.Adaptee;
                    }
                    if (refNode.Parent.Last == refNode)
                    {
                        refNode.Parent.Last = newCh.Adaptee;
                    }
                }
                if (refNode.Prev != null)
                {
                    refNode.Prev.Next = newCh.Adaptee;
                }
                if (refNode.Next != null)
                {
                    refNode.Next.Prev = newCh.Adaptee;
                }
                for (n = refNode.Content; n != null; n = n.Next)
                {
                    if (n.Parent == refNode)
                    {
                        n.Parent = newCh.Adaptee;
                    }
                }
            }
            return(oldChild);
        }