Пример #1
0
        /// <summary>
        ///   Converts current HTML component to a HTML encoded string
        /// </summary>
        /// <returns></returns>
        public virtual string ToHtml()
        {
            PreRender();

            if (Tag == EHtmlTag.Empty)
            {
                return(string.Empty);
            }

            if (Attributes.ContainsKey("class"))
            {
                CssClasses.Add(Attributes["class"]);
            }

            if (CssClasses.Count > 0)
            {
                Attributes["class"] = string.Join(" ", CssClasses.Distinct());
            }

            string innerHtml = string.Empty;

            if (Tag != EHtmlTag.Img)
            {
                innerHtml =
                    string.Join("", PrependTags.Select(f => f.ToHtml())) +
                    InnerHtml +
                    string.Join("", AppendTags.Select(f => f.ToHtml()));
            }

            List <string> parts = new List <string> {
                "<" + Tag.ToString().ToLowerInvariant()
            };

            string attribs = " " + string.Join(" ",
                                               Attributes.OrderBy(f => f.Key).Select(f => string.Format("{0}=\"{1}\"", f.Key, f.Value)));

            parts.Add(attribs);

            if (Tag == EHtmlTag.Img || Tag == EHtmlTag.Input)
            {
                parts.Add("/>");
            }
            else
            {
                parts.Add(">");

                if (!string.IsNullOrWhiteSpace(innerHtml))
                {
                    parts.Add(innerHtml);
                }

                parts.Add("</" + Tag.ToString().ToLowerInvariant() + ">");
            }

            string result = string.Join(string.Empty, parts);

            return(result);
        }