Esempio n. 1
0
        protected virtual void writeHtml(HtmlTextWriter html)
        {
            if (!WillBeRendered())
            {
                return;
            }

            _htmlAttributes.Each((key, val) =>
            {
                if (val != null)
                {
                    var stringValue = !(val is string) && key.StartsWith(DataPrefix) ? JsonUtil.ToJson(val) : val.ToString();
                    html.AddAttribute(key, stringValue);
                }
            });

            if (_cssClasses.Count > 0)
            {
                var classValue = _cssClasses.Join(" ");
                html.AddAttribute(CssClassAttribute, classValue);
            }

            if (_metaData.Count > 0)
            {
                var metadataValue = JsonUtil.ToUnsafeJson(_metaData.Inner);
                html.AddAttribute(MetadataAttribute, metadataValue);
            }

            if (_customStyles.Count > 0)
            {
                var attValue = _customStyles
                               .Select(x => x.Key + ":" + x.Value)
                               .ToArray().Join(";");

                html.AddAttribute(CssStyleAttribute, attValue);
            }

            html.RenderBeginTag(_tag);

            writeInnerText(html);

            _children.Each(x => x.writeHtml(html));

            if (HasClosingTag())
            {
                html.RenderEndTag();
            }

            if (_next != null)
            {
                _next.writeHtml(html);
            }
        }
Esempio n. 2
0
        protected virtual void writeHtml(HtmlTextWriter html)
        {
            if (!WillBeRendered())
            {
                return;
            }

            if (_tag == "br")
            {
                new BrTag().writeHtml(html);
                return;
            }

            if (HasTag())
            {
                _htmlAttributes.Each((key, attribute) =>
                {
                    if (attribute != null)
                    {
                        var value       = attribute.Value;
                        var stringValue = !(value is string) && key.StartsWith(DataPrefix)
                            ? JsonUtil.ToJson(value)
                            : value.ToString();
                        html.AddAttribute(key, stringValue, attribute.IsEncoded);
                    }
                    else
                    {
                        // HtmlTextWriter treats a null value as an attribute with no value (e.g., <input required />).
                        html.AddAttribute(key, null, false);
                    }
                });

                if (_cssClasses.Count > 0)
                {
                    var classValue = _cssClasses.Join(" ");
                    html.AddAttribute(CssClassAttribute, classValue);
                }

                if (_metaData.Count > 0)
                {
                    var metadataValue = JsonUtil.ToUnsafeJson(_metaData.Inner);
                    html.AddAttribute(MetadataAttribute, metadataValue);
                }

                if (_customStyles.Count > 0)
                {
                    var attValue = _customStyles
                                   .Select(x => x.Key + ":" + x.Value)
                                   .ToArray().Join(";");

                    html.AddAttribute(CssStyleAttribute, attValue);
                }

                html.RenderBeginTag(_tag);
            }

            writeInnerText(html);

            _children.Each(x => x.writeHtml(html));

            if (HasClosingTag())
            {
                html.RenderEndTag();
            }
            else
            {
                var currentInner = html.InnerWriter;
                html.InnerWriter = new StringWriter();
                if (!HasTag())
                {
                    html.RenderBeginTag("");
                }
                html.RenderEndTag();
                html.InnerWriter = currentInner;
            }

            if (_next != null)
            {
                _next.writeHtml(html);
            }
        }