示例#1
0
        public static void Parse(string aSource, TextFormat defaultFormat, List<HtmlElement> elements)
        {
            _elements = elements;

            int pos = 0, pos2, length = 0, tagType = 0;
            string tagName = null;
            string tagSource;
            int skipText = 0;

            sTextFormatStackTop = 0;
            _format.CopyFrom(defaultFormat);

            while ((pos2 = EnumTag(aSource, pos, ref tagName, ref length, ref tagType)) != -1)
            {
                if (pos != pos2 && skipText == 0)
                    AppendText(aSource.Substring(pos, pos2 - pos), true);

                tagSource = aSource.Substring(pos2, length);
                switch (tagName)
                {
                    case "b":
                        if (tagType == 0)
                        {
                            PushTextFormat(_format);
                            _format.bold = true;
                        }
                        else
                            PopTextFormat(_format);
                        break;

                    case "i":
                        if (tagType == 0)
                        {
                            PushTextFormat(_format);
                            _format.italic = true;
                        }
                        else
                            PopTextFormat(_format);
                        break;

                    case "u":
                        if (tagType == 0)
                        {
                            PushTextFormat(_format);
                            _format.underline = true;
                        }
                        else
                            PopTextFormat(_format);
                        break;

                    case "sub":
                        break;

                    case "sup":
                        break;

                    case "font":
                        if (tagType == 0)
                        {
                            PushTextFormat(_format);
                            string[] values = GetAttributeValue(tagSource, fontAttrNames);
                            string size = values[0];
                            if (size != null)
                                _format.size = Convert.ToInt32(size);
                            string color = values[1];
                            if (color != null)
                                _format.color = ToolSet.ConvertFromHtmlColor(color);
                        }
                        else if (tagType == 1)
                            PopTextFormat(_format);
                        break;

                    case "br":
                        AppendText("\n", false);
                        break;

                    case "img":
                        if (tagType == 0 || tagType == 2)
                        {
                            string[] values = GetAttributeValue(tagSource, imgAttrNames);
                            HtmlElement_Img element;
                            if (sImgElementPool.Count > 0)
                                element = sImgElementPool.Pop();
                            else
                                element = new HtmlElement_Img();
                            element.src = values[0];
                            if (values[1] != null)
                                element.width = Convert.ToInt32(values[1]);
                            else
                                element.width = 0;
                            if (values[2] != null)
                                element.height = Convert.ToInt32(values[2]);
                            else
                                element.height = 0;
                            _elements.Add(element);
                        }
                        break;

                    case "a":
                        if (tagType == 0)
                        {
                            string[] values = GetAttributeValue(tagSource, aAttrNames);
                            HtmlElement_A element;
                            if (sAElementPool.Count > 0)
                                element = sAElementPool.Pop();
                            else
                                element = new HtmlElement_A();
                            element.href = values[0];
                            element.target = values[1];
                            element.end = false;
                            _elements.Add(element);
                        }
                        else if (tagType == 1)
                        {
                            HtmlElement_A element;
                            if (sAElementPool.Count > 0)
                                element = sAElementPool.Pop();
                            else
                                element = new HtmlElement_A();
                            element.end = true;
                            _elements.Add(element);
                        }
                        break;
                }

                pos = pos2 + length;
            }

            if (pos != aSource.Length)
                AppendText(aSource.Substring(pos, aSource.Length - pos), true);

            _elements = null;
        }
示例#2
0
        static public void Parse(string aSource, TextFormat defaultFormat, List <HtmlElement> elements)
        {
            _elements = elements;

            int    pos = 0, pos2, length = 0, tagType = 0;
            string tagName = null;
            string tagSource;
            int    skipText = 0;

            sTextFormatStackTop = 0;
            _format.CopyFrom(defaultFormat);

            while ((pos2 = EnumTag(aSource, pos, ref tagName, ref length, ref tagType)) != -1)
            {
                if (pos != pos2 && skipText == 0)
                {
                    AppendText(aSource.Substring(pos, pos2 - pos), true);
                }

                tagSource = aSource.Substring(pos2, length);
                switch (tagName)
                {
                case "b":
                    if (tagType == 0)
                    {
                        PushTextFormat(_format);
                        _format.bold = true;
                    }
                    else
                    {
                        PopTextFormat(_format);
                    }
                    break;

                case "i":
                    if (tagType == 0)
                    {
                        PushTextFormat(_format);
                        _format.italic = true;
                    }
                    else
                    {
                        PopTextFormat(_format);
                    }
                    break;

                case "u":
                    if (tagType == 0)
                    {
                        PushTextFormat(_format);
                        _format.underline = true;
                    }
                    else
                    {
                        PopTextFormat(_format);
                    }
                    break;

                case "sub":
                    break;

                case "sup":
                    break;

                case "font":
                    if (tagType == 0)
                    {
                        PushTextFormat(_format);
                        string[] values = GetAttributeValue(tagSource, fontAttrNames);
                        string   size   = values[0];
                        if (size != null)
                        {
                            _format.size = Convert.ToInt32(size);
                        }
                        string color = values[1];
                        if (color != null)
                        {
                            _format.color = ToolSet.ConvertFromHtmlColor(color);
                        }
                    }
                    else if (tagType == 1)
                    {
                        PopTextFormat(_format);
                    }
                    break;

                case "br":
                    AppendText("\n", false);
                    break;

                case "img":
                    if (tagType == 0 || tagType == 2)
                    {
                        string[]        values = GetAttributeValue(tagSource, imgAttrNames);
                        HtmlElement_Img element;
                        if (sImgElementPool.Count > 0)
                        {
                            element = sImgElementPool.Pop();
                        }
                        else
                        {
                            element = new HtmlElement_Img();
                        }
                        element.src = values[0];
                        if (values[1] != null)
                        {
                            element.width = Convert.ToInt32(values[1]);
                        }
                        else
                        {
                            element.width = 0;
                        }
                        if (values[2] != null)
                        {
                            element.height = Convert.ToInt32(values[2]);
                        }
                        else
                        {
                            element.height = 0;
                        }
                        _elements.Add(element);
                    }
                    break;

                case "a":
                    if (tagType == 0)
                    {
                        string[]      values = GetAttributeValue(tagSource, aAttrNames);
                        HtmlElement_A element;
                        if (sAElementPool.Count > 0)
                        {
                            element = sAElementPool.Pop();
                        }
                        else
                        {
                            element = new HtmlElement_A();
                        }
                        element.href   = values[0];
                        element.target = values[1];
                        element.end    = false;
                        _elements.Add(element);
                    }
                    else if (tagType == 1)
                    {
                        HtmlElement_A element;
                        if (sAElementPool.Count > 0)
                        {
                            element = sAElementPool.Pop();
                        }
                        else
                        {
                            element = new HtmlElement_A();
                        }
                        element.end = true;
                        _elements.Add(element);
                    }
                    break;
                }

                pos = pos2 + length;
            }

            if (pos != aSource.Length)
            {
                AppendText(aSource.Substring(pos, aSource.Length - pos), true);
            }

            _elements = null;
        }