Ejemplo n.º 1
0
            internal override bool Process(Token t, HtmlTreeBuilder tb)
            {
                switch (t.type)
                {
                    case TokenType.Character:
                    {
                        Token.Character c = t.AsCharacter();
                        if (c.GetData().Equals(HtmlTreeBuilderState.nullString))
                        {
                            // todo confirm that check
                            tb.Error(this);
                            return false;
                        }
                        else if (tb.FramesetOk() && HtmlTreeBuilderState.IsWhitespace(c))
                        {
                            // don't check if whitespace if frames already closed
                            tb.ReconstructFormattingElements();
                            tb.Insert(c);
                        }
                        else
                        {
                            tb.ReconstructFormattingElements();
                            tb.Insert(c);
                            tb.FramesetOk(false);
                        }
                        break;
                    }

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

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

                    case TokenType.StartTag:
                    {
                        Token.StartTag startTag = t.AsStartTag();
                        string name = startTag.Name();
                        if (name.Equals("html"))
                        {
                            tb.Error(this);
                            // merge attributes onto real html
                            Element html = tb.GetStack().First.Value;
                            foreach (Supremes.Nodes.Attribute attribute in startTag.GetAttributes())
                            {
                                if (!html.HasAttr(attribute.Key))
                                {
                                    html.Attributes.Put(attribute);
                                }
                            }
                        }
                        else if (StringUtil.In(name, HtmlTreeBuilderState.Constants.InBodyStartToHead))
                        {
                            return tb.Process(t, HtmlTreeBuilderState.InHead);
                        }
                        else if (name.Equals("body"))
                        {
                            tb.Error(this);
                            IList<Element> stack = tb.GetStack();
                            if (stack.Count == 1 || (stack.Count > 2 && !stack[1].NodeName.Equals("body")))
                            {
                                // only in fragment case
                                return false;
                            }
                            else
                            {
                                // ignore
                                tb.FramesetOk(false);
                                Element body = stack[1];
                                foreach (Supremes.Nodes.Attribute attribute in startTag.GetAttributes())
                                {
                                    if (!body.HasAttr(attribute.Key))
                                    {
                                        body.Attributes.Put(attribute);
                                    }
                                }
                            }
                        }
                        else if (name.Equals("frameset"))
                        {
                            tb.Error(this);
                            var stack = tb.GetStack();
                            if (stack.Count == 1 || (stack.Count > 2 && !stack[1].NodeName.Equals("body")))
                            {
                                // only in fragment case
                                return false;
                                // ignore
                            }
                            else if (!tb.FramesetOk())
                            {
                                return false;
                                // ignore frameset
                            }
                            else
                            {
                                Element second = stack[1];
                                if (second.Parent != null)
                                {
                                    second.Remove();
                                }
                                // pop up to html element
                                while (stack.Count > 1)
                                {
                                    stack.RemoveLast();
                                }
                                tb.Insert(startTag);
                                tb.Transition(HtmlTreeBuilderState.InFrameset);
                            }
                        }
                        else if (StringUtil.In(name, HtmlTreeBuilderState.Constants.InBodyStartPClosers))
                        {
                            if (tb.InButtonScope("p"))
                            {
                                tb.Process(new Token.EndTag("p"));
                            }
                            tb.Insert(startTag);
                        }
                        else if (StringUtil.In(name, HtmlTreeBuilderState.Constants.Headings))
                        {
                            if (tb.InButtonScope("p"))
                            {
                                tb.Process(new Token.EndTag("p"));
                            }
                            if (StringUtil.In(tb.CurrentElement().NodeName, HtmlTreeBuilderState.Constants.Headings))
                            {
                                tb.Error(this);
                                tb.Pop();
                            }
                            tb.Insert(startTag);
                        }
                        else if (StringUtil.In(name, HtmlTreeBuilderState.Constants.InBodyStartPreListing))
                        {
                            if (tb.InButtonScope("p"))
                            {
                                tb.Process(new Token.EndTag("p"));
                            }
                            tb.Insert(startTag);
                            // todo: ignore LF if next token
                            tb.FramesetOk(false);
                        }
                        else if (name.Equals("form"))
                        {
                            if (tb.GetFormElement() != null)
                            {
                                tb.Error(this);
                                return false;
                            }
                            if (tb.InButtonScope("p"))
                            {
                                tb.Process(new Token.EndTag("p"));
                            }
                            tb.InsertForm(startTag, true);
                        }
                        else if (name.Equals("li"))
                        {
                            tb.FramesetOk(false);
                            IList<Element> stack = tb.GetStack();
                            for (int i = stack.Count - 1; i > 0; i--)
                            {
                                Element el = stack[i];
                                if (el.NodeName.Equals("li"))
                                {
                                    tb.Process(new Token.EndTag("li"));
                                    break;
                                }
                                if (tb.IsSpecial(el) && !StringUtil.In(el.NodeName, HtmlTreeBuilderState.Constants.InBodyStartLiBreakers))
                                {
                                    break;
                                }
                            }
                            if (tb.InButtonScope("p"))
                            {
                                tb.Process(new Token.EndTag("p"));
                            }
                            tb.Insert(startTag);
                        }
                        else if (StringUtil.In(name, HtmlTreeBuilderState.Constants.DdDt))
                        {
                            tb.FramesetOk(false);
                            IList<Element> stack = tb.GetStack();
                            for (int i = stack.Count - 1; i > 0; i--)
                            {
                                Element el = stack[i];
                                if (StringUtil.In(el.NodeName, HtmlTreeBuilderState.Constants.DdDt))
                                {
                                    tb.Process(new Token.EndTag(el.NodeName));
                                    break;
                                }
                                if (tb.IsSpecial(el) && !StringUtil.In(el.NodeName, HtmlTreeBuilderState.Constants
                                    .InBodyStartLiBreakers))
                                {
                                    break;
                                }
                            }
                            if (tb.InButtonScope("p"))
                            {
                                tb.Process(new Token.EndTag("p"));
                            }
                            tb.Insert(startTag);
                        }
                        else if (name.Equals("plaintext"))
                        {
                            if (tb.InButtonScope("p"))
                            {
                                tb.Process(new Token.EndTag("p"));
                            }
                            tb.Insert(startTag);
                            tb.tokeniser.Transition(TokeniserState.PLAINTEXT);
                            // once in, never gets out
                        }
                        else if (name.Equals("button"))
                        {
                            if (tb.InButtonScope("button"))
                            {
                                // close and reprocess
                                tb.Error(this);
                                tb.Process(new Token.EndTag("button"));
                                tb.Process(startTag);
                            }
                            else
                            {
                                tb.ReconstructFormattingElements();
                                tb.Insert(startTag);
                                tb.FramesetOk(false);
                            }
                        }
                        else if (name.Equals("a"))
                        {
                            if (tb.GetActiveFormattingElement("a") != null)
                            {
                                tb.Error(this);
                                tb.Process(new Token.EndTag("a"));
                                // still on stack?
                                Element remainingA = tb.GetFromStack("a");
                                if (remainingA != null)
                                {
                                    tb.RemoveFromActiveFormattingElements(remainingA);
                                    tb.RemoveFromStack(remainingA);
                                }
                            }
                            tb.ReconstructFormattingElements();
                            Element a = tb.Insert(startTag);
                            tb.PushActiveFormattingElements(a);
                        }
                        else if (StringUtil.In(name, HtmlTreeBuilderState.Constants.Formatters))
                        {
                            tb.ReconstructFormattingElements();
                            Element el = tb.Insert(startTag);
                            tb.PushActiveFormattingElements(el);
                        }
                        else if (name.Equals("nobr"))
                        {
                            tb.ReconstructFormattingElements();
                            if (tb.InScope("nobr"))
                            {
                                tb.Error(this);
                                tb.Process(new Token.EndTag("nobr"));
                                tb.ReconstructFormattingElements();
                            }
                            Element el = tb.Insert(startTag);
                            tb.PushActiveFormattingElements(el);
                        }
                        else if (StringUtil.In(name, HtmlTreeBuilderState.Constants.InBodyStartApplets))
                        {
                            tb.ReconstructFormattingElements();
                            tb.Insert(startTag);
                            tb.InsertMarkerToFormattingElements();
                            tb.FramesetOk(false);
                        }
                        else if (name.Equals("table"))
                        {
                            if (tb.GetDocument().QuirksMode != DocumentQuirksMode.Quirks && tb.InButtonScope("p"))
                            {
                                tb.Process(new Token.EndTag("p"));
                            }
                            tb.Insert(startTag);
                            tb.FramesetOk(false);
                            tb.Transition(HtmlTreeBuilderState.InTable);
                        }
                        else if (StringUtil.In(name, HtmlTreeBuilderState.Constants.InBodyStartEmptyFormatters))
                        {
                            tb.ReconstructFormattingElements();
                            tb.InsertEmpty(startTag);
                            tb.FramesetOk(false);
                        }
                        else if (name.Equals("input"))
                        {
                            tb.ReconstructFormattingElements();
                            Element el = tb.InsertEmpty(startTag);
                            if (!string.Equals(el.Attr("type"), "hidden", StringComparison.OrdinalIgnoreCase))
                            {
                                tb.FramesetOk(false);
                            }
                        }
                        else if (StringUtil.In(name, HtmlTreeBuilderState.Constants.InBodyStartMedia))
                        {
                            tb.InsertEmpty(startTag);
                        }
                        else if (name.Equals("hr"))
                        {
                            if (tb.InButtonScope("p"))
                            {
                                tb.Process(new Token.EndTag("p"));
                            }
                            tb.InsertEmpty(startTag);
                            tb.FramesetOk(false);
                        }
                        else if (name.Equals("image"))
                        {
                            if (tb.GetFromStack("svg") == null)
                            {
                                return tb.Process(startTag.Name("img"));
                            }
                            else
                            {
                                // change <image> to <img>, unless in svg
                                tb.Insert(startTag);
                            }
                        }
                        else if (name.Equals("isindex"))
                        {
                            // how much do we care about the early 90s?
                            tb.Error(this);
                            if (tb.GetFormElement() != null)
                            {
                                return false;
                            }
                            tb.tokeniser.AcknowledgeSelfClosingFlag();
                            tb.Process(new Token.StartTag("form"));
                            if (startTag.attributes.ContainsKey("action"))
                            {
                                Element form = tb.GetFormElement();
                                form.Attr("action", startTag.attributes["action"]);
                            }
                            tb.Process(new Token.StartTag("hr"));
                            tb.Process(new Token.StartTag("label"));
                            // hope you like english.
                            string prompt = startTag.attributes.ContainsKey("prompt") ? startTag.attributes["prompt"] : "This is a searchable index. Enter search keywords: ";
                            tb.Process(new Token.Character(prompt));
                            // input
                            Attributes inputAttribs = new Attributes();
                            foreach (Supremes.Nodes.Attribute attr in startTag.attributes)
                            {
                                if (!StringUtil.In(attr.Key, HtmlTreeBuilderState.Constants.InBodyStartInputAttribs))
                                {
                                    inputAttribs.Put(attr);
                                }
                            }
                            inputAttribs["name"] = "isindex";
                            tb.Process(new Token.StartTag("input", inputAttribs));
                            tb.Process(new Token.EndTag("label"));
                            tb.Process(new Token.StartTag("hr"));
                            tb.Process(new Token.EndTag("form"));
                        }
                        else if (name.Equals("textarea"))
                        {
                            tb.Insert(startTag);
                            // todo: If the next token is a U+000A LINE FEED (LF) character token, then ignore that token and move on to the next one. (Newlines at the start of textarea elements are ignored as an authoring convenience.)
                            tb.tokeniser.Transition(TokeniserState.Rcdata);
                            tb.MarkInsertionMode();
                            tb.FramesetOk(false);
                            tb.Transition(HtmlTreeBuilderState.Text);
                        }
                        else if (name.Equals("xmp"))
                        {
                            if (tb.InButtonScope("p"))
                            {
                                tb.Process(new Token.EndTag("p"));
                            }
                            tb.ReconstructFormattingElements();
                            tb.FramesetOk(false);
                            HtmlTreeBuilderState.HandleRawtext(startTag, tb);
                        }
                        else if (name.Equals("iframe"))
                        {
                            tb.FramesetOk(false);
                            HtmlTreeBuilderState.HandleRawtext(startTag, tb);
                        }
                        else if (name.Equals("noembed"))
                        {
                            // also handle noscript if script enabled
                            HtmlTreeBuilderState.HandleRawtext(startTag, tb);
                        }
                        else if (name.Equals("select"))
                        {
                            tb.ReconstructFormattingElements();
                            tb.Insert(startTag);
                            tb.FramesetOk(false);
                            HtmlTreeBuilderState state = tb.State();
                            if (state.Equals(HtmlTreeBuilderState.InTable)
                                || state.Equals(HtmlTreeBuilderState.InCaption)
                                || state.Equals(HtmlTreeBuilderState.InTableBody)
                                || state.Equals(HtmlTreeBuilderState.InRow)
                                || state.Equals(HtmlTreeBuilderState.InCell))
                            {
                                tb.Transition(HtmlTreeBuilderState.InSelectInTable);
                            }
                            else
                            {
                                tb.Transition(HtmlTreeBuilderState.InSelect);
                            }
                        }
                        else if (StringUtil.In(name, HtmlTreeBuilderState.Constants.InBodyStartOptions))
                        {
                            if (tb.CurrentElement().NodeName.Equals("option"))
                            {
                                tb.Process(new Token.EndTag("option"));
                            }
                            tb.ReconstructFormattingElements();
                            tb.Insert(startTag);
                        }
                        else if (StringUtil.In(name, HtmlTreeBuilderState.Constants.InBodyStartRuby))
                        {
                            if (tb.InScope("ruby"))
                            {
                                tb.GenerateImpliedEndTags();
                                if (!tb.CurrentElement().NodeName.Equals("ruby"))
                                {
                                    tb.Error(this);
                                    tb.PopStackToBefore("ruby");
                                }
                                // i.e. close up to but not include name
                                tb.Insert(startTag);
                            }
                        }
                        else if (name.Equals("math"))
                        {
                            tb.ReconstructFormattingElements();
                            // todo: handle A start tag whose tag name is "math" (i.e. foreign, mathml)
                            tb.Insert(startTag);
                            tb.tokeniser.AcknowledgeSelfClosingFlag();
                        }
                        else if (name.Equals("svg"))
                        {
                            tb.ReconstructFormattingElements();
                            // todo: handle A start tag whose tag name is "svg" (xlink, svg)
                            tb.Insert(startTag);
                            tb.tokeniser.AcknowledgeSelfClosingFlag();
                        }
                        else if (StringUtil.In(name, HtmlTreeBuilderState.Constants.InBodyStartDrop))
                        {
                            tb.Error(this);
                            return false;
                        }
                        else
                        {
                            tb.ReconstructFormattingElements();
                            tb.Insert(startTag);

                        }
                        break;
                    }

                    case TokenType.EndTag:
                    {
                        Token.EndTag endTag = t.AsEndTag();
                        string name = endTag.Name();
                        if (name.Equals("body"))
                        {
                            if (!tb.InScope("body"))
                            {
                                tb.Error(this);
                                return false;
                            }
                            else
                            {
                                // todo: error if stack contains something not dd, dt, li, optgroup, option, p, rp, rt, tbody, td, tfoot, th, thead, tr, body, html
                                tb.Transition(HtmlTreeBuilderState.AfterBody);
                            }
                        }
                        else if (name.Equals("html"))
                        {
                            bool notIgnored = tb.Process(new Token.EndTag("body"));
                            if (notIgnored)
                            {
                                return tb.Process(endTag);
                            }
                        }
                        else if (StringUtil.In(name, HtmlTreeBuilderState.Constants.InBodyEndClosers))
                        {
                            if (!tb.InScope(name))
                            {
                                // nothing to close
                                tb.Error(this);
                                return false;
                            }
                            else
                            {
                                tb.GenerateImpliedEndTags();
                                if (!tb.CurrentElement().NodeName.Equals(name))
                                {
                                    tb.Error(this);
                                }
                                tb.PopStackToClose(name);
                            }
                        }
                        else if (name.Equals("form"))
                        {
                            Element currentForm = tb.GetFormElement();
                            tb.SetFormElement(null);
                            if (currentForm == null || !tb.InScope(name))
                            {
                                tb.Error(this);
                                return false;
                            }
                            else
                            {
                                tb.GenerateImpliedEndTags();
                                if (!tb.CurrentElement().NodeName.Equals(name))
                                {
                                    tb.Error(this);
                                }
                                // remove currentForm from stack. will shift anything under up.
                                tb.RemoveFromStack(currentForm);
                            }
                        }
                        else if (name.Equals("p"))
                        {
                            if (!tb.InButtonScope(name))
                            {
                                tb.Error(this);
                                tb.Process(new Token.StartTag(name));
                                // if no p to close, creates an empty <p></p>
                                return tb.Process(endTag);
                            }
                            else
                            {
                                tb.GenerateImpliedEndTags(name);
                                if (!tb.CurrentElement().NodeName.Equals(name))
                                {
                                    tb.Error(this);
                                }
                                tb.PopStackToClose(name);
                            }
                        }
                        else if (name.Equals("li"))
                        {
                            if (!tb.InListItemScope(name))
                            {
                                tb.Error(this);
                                return false;
                            }
                            else
                            {
                                tb.GenerateImpliedEndTags(name);
                                if (!tb.CurrentElement().NodeName.Equals(name))
                                {
                                    tb.Error(this);
                                }
                                tb.PopStackToClose(name);
                            }
                        }
                        else if (StringUtil.In(name, HtmlTreeBuilderState.Constants.DdDt))
                        {
                            if (!tb.InScope(name))
                            {
                                tb.Error(this);
                                return false;
                            }
                            else
                            {
                                tb.GenerateImpliedEndTags(name);
                                if (!tb.CurrentElement().NodeName.Equals(name))
                                {
                                    tb.Error(this);
                                }
                                tb.PopStackToClose(name);
                            }
                        }
                        else if (StringUtil.In(name, HtmlTreeBuilderState.Constants.Headings))
                        {
                            if (!tb.InScope(HtmlTreeBuilderState.Constants.Headings))
                            {
                                tb.Error(this);
                                return false;
                            }
                            else
                            {
                                tb.GenerateImpliedEndTags(name);
                                if (!tb.CurrentElement().NodeName.Equals(name))
                                {
                                    tb.Error(this);
                                }
                                tb.PopStackToClose(HtmlTreeBuilderState.Constants.Headings);
                            }
                        }
                        else if (name.Equals("sarcasm"))
                        {
                            // *sigh*
                            return this.AnyOtherEndTag(t, tb);
                        }
                        else if (StringUtil.In(name, HtmlTreeBuilderState.Constants.InBodyEndAdoptionFormatters))
                        {
                            // Adoption Agency Algorithm.
                            // OUTER:
                            for (int i = 0; i < 8; i++)
                            {
                                Element formatEl = tb.GetActiveFormattingElement(name);
                                if (formatEl == null)
                                {
                                    return this.AnyOtherEndTag(t, tb);
                                }
                                else if (!tb.OnStack(formatEl))
                                {
                                    tb.Error(this);
                                    tb.RemoveFromActiveFormattingElements(formatEl);
                                    return true;
                                }
                                else if (!tb.InScope(formatEl.NodeName))
                                {
                                    tb.Error(this);
                                    return false;
                                }
                                else if (tb.CurrentElement() != formatEl)
                                {
                                    tb.Error(this);
                                }
                                Element furthestBlock = null;
                                Element commonAncestor = null;
                                bool seenFormattingElement = false;
                                IList<Element> stack = tb.GetStack();
                                // the spec doesn't limit to < 64, but in degenerate cases (9000+ stack depth) this prevents
                                // run-aways
                                int stackSize = stack.Count;
                                for (int si = 0; si < stackSize && si < 64; si++)
                                {
                                    Element el = stack[si];
                                    if (el == formatEl)
                                    {
                                        commonAncestor = stack[si - 1];
                                        seenFormattingElement = true;
                                    }
                                    else if (seenFormattingElement && tb.IsSpecial(el))
                                    {
                                        furthestBlock = el;
                                        break;
                                    }
                                }
                                if (furthestBlock == null)
                                {
                                    tb.PopStackToClose(formatEl.NodeName);
                                    tb.RemoveFromActiveFormattingElements(formatEl);
                                    return true;
                                }
                                // todo: Let a bookmark note the position of the formatting element in the list of active formatting elements relative to the elements on either side of it in the list.
                                // does that mean: int pos of format el in list?
                                Element node = furthestBlock;
                                Element lastNode = furthestBlock;
                                for (int j = 0; j < 3; j++)
                                {
                                    if (tb.OnStack(node))
                                    {
                                        node = tb.AboveOnStack(node);
                                    }
                                    if (!tb.IsInActiveFormattingElements(node))
                                    {
                                        // note no bookmark check
                                        tb.RemoveFromStack(node);
                                        goto INNER_continue;
                                    }
                                    else
                                    {
                                        if (node == formatEl)
                                        {
                                            goto INNER_break;
                                        }
                                    }
                                    Element replacement = new Element(Tag.ValueOf(node.NodeName), tb.GetBaseUri());
                                    tb.ReplaceActiveFormattingElement(node, replacement);
                                    tb.ReplaceOnStack(node, replacement);
                                    node = replacement;
                                    if (lastNode == furthestBlock)
                                    {
                                    }
                                    // todo: move the aforementioned bookmark to be immediately after the new node in the list of active formatting elements.
                                    // not getting how this bookmark both straddles the element above, but is inbetween here...
                                    if (lastNode.Parent != null)
                                    {
                                        lastNode.Remove();
                                    }
                                    node.AppendChild(lastNode);
                                    lastNode = node;
                                INNER_continue: ;
                                }
                            INNER_break: ;
                                if (StringUtil.In(commonAncestor.NodeName, HtmlTreeBuilderState.Constants.InBodyEndTableFosters))
                                {
                                    if (lastNode.Parent!= null)
                                    {
                                        lastNode.Remove();
                                    }
                                    tb.InsertInFosterParent(lastNode);
                                }
                                else
                                {
                                    if (lastNode.Parent != null)
                                    {
                                        lastNode.Remove();
                                    }
                                    commonAncestor.AppendChild(lastNode);
                                }
                                Element adopter = new Element(formatEl.Tag, tb.GetBaseUri());
                                adopter.Attributes.SetAll(formatEl.Attributes);
                                Node[] childNodes = furthestBlock.ChildNodes.ToArray();
                                foreach (Node childNode in childNodes)
                                {
                                    adopter.AppendChild(childNode);
                                }
                                // append will reparent. thus the clone to avoid concurrent mod.
                                furthestBlock.AppendChild(adopter);
                                tb.RemoveFromActiveFormattingElements(formatEl);
                                // todo: insert the new element into the list of active formatting elements at the position of the aforementioned bookmark.
                                tb.RemoveFromStack(formatEl);
                                tb.InsertOnStackAfter(furthestBlock, adopter);
                            }
                        }
                        else if (StringUtil.In(name, HtmlTreeBuilderState.Constants.InBodyStartApplets))
                        {
                            if (!tb.InScope("name"))
                            {
                                if (!tb.InScope(name))
                                {
                                    tb.Error(this);
                                    return false;
                                }
                                tb.GenerateImpliedEndTags();
                                if (!tb.CurrentElement().NodeName.Equals(name))
                                {
                                    tb.Error(this);
                                }
                                tb.PopStackToClose(name);
                                tb.ClearFormattingElementsToLastMarker();
                            }
                        }
                        else if (name.Equals("br"))
                        {
                            tb.Error(this);
                            tb.Process(new Token.StartTag("br"));
                            return false;
                        }
                        else
                        {
                            return this.AnyOtherEndTag(t, tb);
                        }
                        break;
                    }

                    case TokenType.EOF:
                    {
                        // todo: error if stack contains something not dd, dt, li, p, tbody, td, tfoot, th, thead, tr, body, html
                        // stop parsing
                        break;
                    }
                }
                return true;
            }