예제 #1
0
        protected override void OnStart(TextWriter writer)
        {
            // Add the text content as a child
            if (!string.IsNullOrEmpty(TextContent))
            {
                this.AddChild(new Content <THelper>(Helper, TextContent));
            }

            base.OnStart(writer);

            // Pretty print
            if (_prettyPrint && !(writer is SuppressOutputWriter))
            {
                writer.WriteLine();
                int tagIndent = (int)Helper.GetItem(TagIndentKey, 0);
                writer.Write(new String(' ', tagIndent++));
                Helper.AddItem(TagIndentKey, tagIndent);
                Helper.AddItem(LastToWriteKey, this);
            }
            else
            {
                _prettyPrint = false;
            }

            // Merge CSS classes
            if (CssClasses.Count > 0)
            {
                Attributes.Merge("class",
                                 (Attributes.Dictionary.ContainsKey("class") ? Attributes.GetValue("class") + " " : string.Empty)
                                 + string.Join(" ", CssClasses.Reverse())); // TODO: Remove the reverse call
            }

            // Generate the inline CSS style
            if (InlineStyles.Dictionary.Count > 0)
            {
                Attributes.Merge("style", string.Join(" ", InlineStyles.Dictionary.Select(x => x.Key + ": " + x.Value + ";")));
            }

            // Append the start tag
            StringBuilder startTag = new StringBuilder("<" + _tagName);

            foreach (KeyValuePair <string, string> attribute in Attributes.Dictionary.Reverse()) // TODO: Remove the reverse call
            {
                if (string.Equals(attribute.Key, "id", StringComparison.Ordinal) && string.IsNullOrEmpty(attribute.Value))
                {
                    continue;
                }
                string encoded = HttpUtility.HtmlAttributeEncode(attribute.Value);
                startTag.Append(" " + attribute.Key + "=\"" + encoded + "\"");
            }
            startTag.Append(">");
            writer.Write(startTag.ToString());
            _startTagOutput = true;
        }