Пример #1
0
            public override bool Process(Token t, HtmlTreeBuilder tb)
            {
                switch (t.Type)
                {
                case TokenType.Character:
                    Token.Character c = t.AsCharacter();
                    if (c.Data.Equals(NullString))
                    {
                        tb.Error(this);
                        return(false);
                    }
                    else
                    {
                        tb.Insert(c);
                    }
                    break;

                case TokenType.Comment:
                    tb.Insert(t.AsComment());
                    break;

                case TokenType.Doctype:
                    tb.Error(this);
                    return(false);

                case TokenType.StartTag:
                    Token.StartTag start = t.AsStartTag();
                    string         name  = start.Name;
                    if (name.Equals("html"))
                    {
                        return(tb.Process(start, InBody));
                    }

                    else if (name.Equals("option"))
                    {
                        tb.Process(new Token.EndTag("option"));
                        tb.Insert(start);
                    }
                    else if (name.Equals("optgroup"))
                    {
                        if (tb.CurrentElement.NodeName.Equals("option"))
                        {
                            tb.Process(new Token.EndTag("option"));
                        }

                        else if (tb.CurrentElement.NodeName.Equals("optgroup"))
                        {
                            tb.Process(new Token.EndTag("optgroup"));
                        }

                        tb.Insert(start);
                    }
                    else if (name.Equals("select"))
                    {
                        tb.Error(this);
                        return(tb.Process(new Token.EndTag("select")));
                    }
                    else if (StringUtil.In(name, "input", "keygen", "textarea"))
                    {
                        tb.Error(this);

                        if (!tb.InSelectScope("select"))
                        {
                            return(false);    // frag
                        }
                        tb.Process(new Token.EndTag("select"));
                        return(tb.Process(start));
                    }
                    else if (name.Equals("script"))
                    {
                        return(tb.Process(t, InHead));
                    }
                    else
                    {
                        return(AnythingElse(t, tb));
                    }
                    break;

                case TokenType.EndTag:
                    Token.EndTag end = t.AsEndTag();
                    name = end.Name;

                    if (name.Equals("optgroup"))
                    {
                        if (tb.CurrentElement.NodeName.Equals("option") &&
                            tb.AboveOnStack(tb.CurrentElement) != null &&
                            tb.AboveOnStack(tb.CurrentElement).NodeName.Equals("optgroup"))
                        {
                            tb.Process(new Token.EndTag("option"));
                        }

                        if (tb.CurrentElement.NodeName.Equals("optgroup"))
                        {
                            tb.Pop();
                        }
                        else
                        {
                            tb.Error(this);
                        }
                    }
                    else if (name.Equals("option"))
                    {
                        if (tb.CurrentElement.NodeName.Equals("option"))
                        {
                            tb.Pop();
                        }
                        else
                        {
                            tb.Error(this);
                        }
                    }
                    else if (name.Equals("select"))
                    {
                        if (!tb.InSelectScope(name))
                        {
                            tb.Error(this);
                            return(false);
                        }
                        else
                        {
                            tb.PopStackToClose(name);
                            tb.ResetInsertionMode();
                        }
                    }
                    else
                    {
                        return(AnythingElse(t, tb));
                    }
                    break;

                case TokenType.EOF:
                    if (!tb.CurrentElement.NodeName.Equals("html"))
                    {
                        tb.Error(this);
                    }
                    break;

                default:
                    return(AnythingElse(t, tb));
                }

                return(true);
            }
            public override bool Process(Token t, HtmlTreeBuilder tb)
            {
                if (t.IsCharacter)
                {
                    tb.NewPendingTableCharacters();
                    tb.MarkInsertionMode();
                    tb.Transition(InTableText);
                    return(tb.Process(t));
                }
                else if (t.IsComment)
                {
                    tb.Insert(t.AsComment());
                    return(true);
                }
                else if (t.IsDoctype)
                {
                    tb.Error(this);
                    return(false);
                }
                else if (t.IsStartTag)
                {
                    Token.StartTag startTag = t.AsStartTag();
                    string         name     = startTag.Name;

                    switch (name)
                    {
                    case "caption":
                        tb.ClearStackToTableContext();
                        tb.InsertMarkerToFormattingElements();
                        tb.Insert(startTag);
                        tb.Transition(InCaption);
                        break;

                    case "colgroup":
                        tb.ClearStackToTableContext();
                        tb.Insert(startTag);
                        tb.Transition(InColumnGroup);
                        break;

                    case "col":
                        tb.Process(new Token.StartTag("colgroup"));
                        return(tb.Process(t));

                    case "table":
                        tb.Error(this);
                        bool processed = tb.Process(new Token.EndTag("table"));
                        if (processed)     // only ignored if in fragment
                        {
                            return(tb.Process(t));
                        }
                        break;

                    case "tbody":
                    case "tfoot":
                    case "thead":
                        tb.ClearStackToTableContext();
                        tb.Insert(startTag);
                        tb.Transition(InTableBody);
                        break;

                    case "td":
                    case "th":
                    case "tr":
                        tb.Process(new Token.StartTag("tbody"));
                        return(tb.Process(t));

                    case "style":
                    case "script":
                        return(tb.Process(t, InHead));

                    case "input":
                        if (!startTag.Attributes["type"]
                            .Equals("hidden", StringComparison.OrdinalIgnoreCase))
                        {
                            return(AnythingElse(t, tb));
                        }
                        else
                        {
                            tb.InsertEmpty(startTag);
                        }
                        break;

                    case "form":
                        tb.Error(this);
                        if (tb.FormElement != null)
                        {
                            return(false);
                        }

                        else
                        {
                            HtmlElement form = tb.InsertEmpty(startTag);
                            tb.FormElement = form;
                        }
                        break;

                    default:
                        return(AnythingElse(t, tb));
                    }
                }
                else if (t.IsEndTag)
                {
                    Token.EndTag endTag = t.AsEndTag();
                    string       name   = endTag.Name;

                    switch (name)
                    {
                    case "table":
                        if (!tb.InTableScope(name))
                        {
                            tb.Error(this);
                            return(false);
                        }
                        else
                        {
                            tb.PopStackToClose("table");
                        }
                        tb.ResetInsertionMode();
                        break;

                    case "body":
                    case "caption":
                    case "col":
                    case "colgroup":
                    case "html":
                    case "tbody":
                    case "td":
                    case "tfoot":
                    case "th":
                    case "thead":
                    case "tr":
                        tb.Error(this);
                        return(false);

                    default:
                        return(AnythingElse(t, tb));
                    }
                }
                else if (t.IsEOF)
                {
                    if (tb.CurrentElement.NodeName.Equals("html"))
                    {
                        tb.Error(this);
                    }

                    return(true); // stops parsing
                }

                return(AnythingElse(t, tb));
            }