コード例 #1
0
        public void ReadContent()
        {
            string name;

            if (IsWhitespace(Peek()))
            {
                HandleWhitespaces();
            }
            if (Peek() == '<')
            {
                Read();
                switch (Peek())
                {
                case '!':     // declarations
                    Read();
                    if (Peek() == '[')
                    {
                        Read();
                        if (ReadName() != "CDATA")
                        {
                            throw Error("Invalid declaration markup");
                        }
                        Expect('[');
                        ReadCDATASection();
                        return;
                    }
                    else if (Peek() == '-')
                    {
                        ReadComment();
                        return;
                    }
                    else if (ReadName() != "DOCTYPE")
                    {
                        throw Error("Invalid declaration markup.");
                    }
                    else
                    {
                        ReadUntil('>', false);
                        return;
                    }

                case '?':     // PIs
                    buffer.Length = 0;
                    Read();
                    name = ReadName();
                    SkipWhitespaces();
                    string text = String.Empty;
                    if (Peek() != '?')
                    {
                        while (true)
                        {
                            text += ReadUntil('?', false);
                            if (Peek() == '>')
                            {
                                break;
                            }
                            text += "?";
                        }
                    }
                    Expect('>');
                    return;

                case '/':     // end tags
                    buffer.Length = 0;
                    if (elementNames.Count == 0)
                    {
                        throw UnexpectedEndError();
                    }
                    Read();
                    name = ReadName();
                    SkipWhitespaces();
                    string expected = (string)elementNames.Pop();
                    if (name != expected)
                    {
                        throw Error(String.Format("End tag mismatch: expected {0} but found {1}", expected, name));
                    }
                    handler.OnEndElement(name);
                    Expect('>');
                    return;

                default:     // start tags (including empty tags)
                    buffer.Length = 0;
                    name          = ReadName();
                    switch (name.ToLower())
                    {
                    case "style":
                        while (Peek() != '>' && Peek() != '/')
                        {
                            ReadAttribute(ref attributes);
                        }
                        SkipWhitespaces();
                        if (Peek() == '/')
                        {
                            Read();
                        }
                        ReadUntil('>', false);
                        // Begin CSS

                        string cssData = ReadUntil('<', false);
                        cssData = CSSParser.CleanCSS(cssData);
                        if (string.IsNullOrEmpty(cssData))
                        {
                            cssData = ReadUntil('<', false);
                        }

                        if (!string.IsNullOrEmpty(cssData))
                        {
                            cssData = cssData.Replace("![CDATA[", "");
                            cssData = cssData.Replace("]]>", "");
                            cssData = CSSParser.CleanCSS(cssData);
                        }

                        handler.OnStyleElement(name, attributes, cssData);

                        // End CSS
                        attributes.Clear();
                        ReadUntil('>', false);

                        return;
                    }

                    while (Peek() != '>' && Peek() != '/')
                    {
                        ReadAttribute(ref attributes);
                    }
                    SkipWhitespaces();
                    if (Peek() == '/')
                    {
                        handler.OnInlineElement(name, attributes);
                        Read();
                    }
                    else
                    {
                        handler.OnStartElement(name, attributes);
                        elementNames.Push(name);
                    }
                    attributes.Clear();
                    Expect('>');
                    return;
                }
            }
            else
            {
                ReadCharacters();
            }
        }