Exemple #1
0
 public override bool Process(Token t, HtmlTreeBuilder tb)
 {
     if (IsWhitespace(t))
     {
         return(true);
     }
     else if (t.IsComment)
     {
         tb.Insert(t.AsComment());
     }
     else if (t.IsDoctype)
     {
         tb.Error(this);
         return(false);
     }
     else if (t.IsStartTag &&
              t.AsStartTag().Name.Equals("html"))
     {
         return(InBody.Process(t, tb)); // does not transition
     }
     else if (t.IsStartTag &&
              t.AsStartTag().Name.Equals("head"))
     {
         HtmlElement head = tb.Insert(t.AsStartTag());
         tb.HeadElement = head;
         tb.Transition(InHead);
     }
     else if (t.IsEndTag && (StringUtil.In(t.AsEndTag().Name, "head", "body", "html", "br")))
     {
         tb.Process(new Token.StartTag("head"));
         return(tb.Process(t));
     }
     else if (t.IsEndTag)
     {
         tb.Error(this);
         return(false);
     }
     else
     {
         tb.Process(new Token.StartTag("head"));
         return(tb.Process(t));
     }
     return(true);
 }
Exemple #2
0
            public override bool Process(Token t, HtmlTreeBuilder tb)
            {
                if (IsWhitespace(t))
                {
                    tb.Insert(t.AsCharacter());
                    return(true);
                }

                switch (t.Type)
                {
                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(InBody.Process(t, tb));
                    }
                    else if (StringUtil.In(name, "base", "basefont", "bgsound", "command", "link"))
                    {
                        HtmlElement el = tb.InsertEmpty(start);
                        // jsoup special: update base the frist time it is seen
                        if (name.Equals("base") && el.HasAttribute("href"))
                        {
                            tb.MaybeSetBaseUri(el);
                        }
                    }
                    else if (name.Equals("meta"))
                    {
                        HtmlElement meta = tb.InsertEmpty(start);
                        // TODO: charset switches
                    }
                    else if (name.Equals("title"))
                    {
                        HandleRcData(start, tb);
                    }
                    else if (StringUtil.In(name, "noframes", "style"))
                    {
                        HandleRawtext(start, tb);
                    }
                    else if (name.Equals("noscript"))
                    {
                        // else if noscript && scripting flag = true: rawtext (jsoup doesn't run script, to handle as noscript)
                        tb.Insert(start);
                        tb.Transition(InHeadNoscript);
                    }
                    else if (name.Equals("script"))
                    {
                        // skips some script rules as won't execute them
                        tb.Insert(start);
                        tb.tokeniser.Transition(TokeniserState.ScriptData);
                        tb.MarkInsertionMode();
                        tb.Transition(Text);
                    }
                    else if (name.Equals("head"))
                    {
                        tb.Error(this);
                        return(false);
                    }
                    else
                    {
                        return(AnythingElse(t, tb));
                    }
                    break;

                case TokenType.EndTag:
                    Token.EndTag end = t.AsEndTag();
                    name = end.Name;
                    if (name.Equals("head"))
                    {
                        tb.Pop();
                        tb.Transition(AfterHead);
                    }
                    else if (StringUtil.In(name, "body", "html", "br"))
                    {
                        return(AnythingElse(t, tb));
                    }
                    else
                    {
                        tb.Error(this);
                        return(false);
                    }
                    break;

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