Exemple #1
0
        /// <summary>
        /// Creates a copy of the comment.
        /// </summary>
        /// <returns>The comment's copy.</returns>
        public override object Clone()
        {
            HtmlComment result = new HtmlComment(text);

            result.SetParentNode(ParentNode);
            result.SetDocument(Document);
            return(result);
        }
Exemple #2
0
        /// <summary>
        /// Loads and parses an HTML file from a <see cref="HtmlReader"/>.
        /// </summary>
        /// <param name="reader">The <see cref="HtmlReader"/> to read HTML
        /// text from.</param>
        /// <remarks>
        /// <para>This method is the one that actually loads and parses
        /// the HTML text.</para>
        /// <para>The caller is responsible for disposing
        /// the <paramref name="reader"/> if required.</para>
        /// </remarks>
        public void Load(HtmlReader reader)
        {
            HtmlNode toAdd;
            Stack    nodeStack = new Stack();

            nodeStack.Push(this);
            while (reader.Read())
            {
                toAdd = null;

                switch (reader.NodeType)
                {
                case HtmlNodeType.Comment:
                    toAdd = new HtmlComment(reader.Value);
                    break;

                case HtmlNodeType.DocumentType:
                    toAdd = new HtmlDocumentType();
                    break;

                case HtmlNodeType.Tag:
                {
                    HtmlElement element = new HtmlElement(reader.Name);
                    if (Tag.IsAtomic(reader.Name))
                    {
                        toAdd = element;
                    }
                    else
                    {
                        nodeStack.Push(element);
                    }
                    break;
                }

                case HtmlNodeType.EndTag:
                {
                    HtmlElement element = (HtmlElement)nodeStack.Pop();
                    if (0 != nodeStack.Count)
                    {
                        toAdd = element;
                    }
                    // And if the stack is empty  we, probably, reached
                    // the end of the document
                    break;
                }

                case HtmlNodeType.ClosedTag:
                    toAdd = new HtmlElement(reader.Name);
                    break;

                case HtmlNodeType.Text:
                    toAdd = new HtmlText(reader.Value);
                    break;

                case HtmlNodeType.Whitespace:
                    toAdd = new HtmlWhitespace(reader.Value);
                    break;
                }
                if (null != toAdd)
                {
                    ((HtmlNode)nodeStack.Peek()).ChildNodes.Add(toAdd);
                }
            }
        }