示例#1
0
        public HtmlNodeCollection Parse(string html)
        {
            HtmlNodeCollection htmlNodeCollection = new HtmlNodeCollection(null);

            html = this.PreprocessScript(html, "script");
            html = this.PreprocessScript(html, "style");
            html = this.RemoveComments(html);
            html = this.RemoveSGMLComments(html);
            StringCollection tokens        = this.GetTokens(html);
            int                i           = 0;
            HtmlElement        htmlElement = null;
            HtmlNodeCollection result;

            while (i < tokens.Count)
            {
                if ("<".Equals(tokens[i]))
                {
                    i++;
                    if (i >= tokens.Count)
                    {
                        result = htmlNodeCollection;
                        return(result);
                    }
                    string name = tokens[i];
                    i++;
                    htmlElement = new HtmlElement(name);
                    while (i < tokens.Count && !">".Equals(tokens[i]) && !"/>".Equals(tokens[i]))
                    {
                        string name2 = tokens[i];
                        i++;
                        if (i < tokens.Count && "=".Equals(tokens[i]))
                        {
                            i++;
                            string value;
                            if (i < tokens.Count)
                            {
                                value = tokens[i];
                            }
                            else
                            {
                                value = null;
                            }
                            i++;
                            HtmlAttribute attribute = new HtmlAttribute(name2, HtmlEncoder.DecodeValue(value));
                            htmlElement.Attributes.Add(attribute);
                        }
                        else if (i < tokens.Count)
                        {
                            HtmlAttribute attribute = new HtmlAttribute(name2, null);
                            htmlElement.Attributes.Add(attribute);
                        }
                    }
                    htmlNodeCollection.Add(htmlElement);
                    if (i < tokens.Count && "/>".Equals(tokens[i]))
                    {
                        htmlElement.IsTerminated = true;
                        i++;
                        htmlElement = null;
                    }
                    else if (i < tokens.Count && ">".Equals(tokens[i]))
                    {
                        i++;
                    }
                }
                else if (">".Equals(tokens[i]))
                {
                    i++;
                }
                else if ("</".Equals(tokens[i]))
                {
                    i++;
                    if (i >= tokens.Count)
                    {
                        result = htmlNodeCollection;
                        return(result);
                    }
                    string name = tokens[i];
                    i++;
                    int num = this.FindTagOpenNodeIndex(htmlNodeCollection, name);
                    if (num != -1)
                    {
                        this.MoveNodesDown(ref htmlNodeCollection, num + 1, (HtmlElement)htmlNodeCollection[num]);
                    }
                    while (i < tokens.Count && !">".Equals(tokens[i]))
                    {
                        i++;
                    }
                    if (i < tokens.Count && ">".Equals(tokens[i]))
                    {
                        i++;
                    }
                    htmlElement = null;
                }
                else
                {
                    string text = tokens[i];
                    if (this.mRemoveEmptyElementText)
                    {
                        text = this.RemoveWhitespace(text);
                    }
                    text = HtmlParser.DecodeScript(text);
                    if (!this.mRemoveEmptyElementText || text.Length != 0)
                    {
                        if (htmlElement == null || !htmlElement.NoEscaping)
                        {
                            text = HtmlEncoder.DecodeValue(text);
                        }
                        HtmlText node = new HtmlText(text);
                        htmlNodeCollection.Add(node);
                    }
                    i++;
                }
            }
            result = htmlNodeCollection;
            return(result);
        }