2009-02-26 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System.Web / System.Web.UI / HtmlTextWriter.cs
index 76ef166f48cd1cfe2670e942c21132a0d845af63..ab2e7b7dc7a774c8410a9077d2d6ead78916a762 100644 (file)
@@ -47,6 +47,11 @@ namespace System.Web.UI {
 
                static HtmlTextWriter ()
                {
+#if NET_2_0
+                       _tagTable = new Hashtable (tags.Length, StringComparer.OrdinalIgnoreCase);
+                       _attributeTable = new Hashtable (htmlattrs.Length, StringComparer.OrdinalIgnoreCase);
+                       _styleTable = new Hashtable (htmlstyles.Length, StringComparer.OrdinalIgnoreCase);
+#else
                        _tagTable = new Hashtable (tags.Length, 
                                CaseInsensitiveHashCodeProvider.Default, CaseInsensitiveComparer.Default);
                        
@@ -55,7 +60,7 @@ namespace System.Web.UI {
                        
                        _styleTable = new Hashtable (htmlstyles.Length, 
                                CaseInsensitiveHashCodeProvider.Default, CaseInsensitiveComparer.Default);
-
+#endif
                        foreach (HtmlTag tag in tags)
                                _tagTable.Add (tag.name, tag);
 
@@ -103,15 +108,18 @@ namespace System.Web.UI {
                public virtual void AddAttribute (HtmlTextWriterAttribute key, string value, bool fEncode)
                {
                        if (fEncode)
-                               value = EncodeAttributeValue (key, value);
+                               value = HttpUtility.HtmlAttributeEncode (value);
+
                        AddAttribute (GetAttributeName (key), value, key);
                }
 
 
                public virtual void AddAttribute (HtmlTextWriterAttribute key, string value)
                {
-                       bool fEncode = key != HtmlTextWriterAttribute.Name;
-                       AddAttribute (key, value, fEncode);
+                       if ((key != HtmlTextWriterAttribute.Name) && (key != HtmlTextWriterAttribute.Id))
+                               value = HttpUtility.HtmlAttributeEncode (value);
+
+                       AddAttribute (GetAttributeName (key), value, key);
                }
 
 
@@ -125,8 +133,12 @@ namespace System.Web.UI {
 
                public virtual void AddAttribute (string name, string value)
                {
-                       bool fEncode = String.Compare ("name", name, true, CultureInfo.InvariantCulture) != 0;
-                       AddAttribute (name, value, fEncode);
+                       HtmlTextWriterAttribute key = GetAttributeKey (name);
+
+                       if ((key != HtmlTextWriterAttribute.Name) && (key != HtmlTextWriterAttribute.Id))
+                               value = HttpUtility.HtmlAttributeEncode (value);
+
+                       AddAttribute (name, value, key);
                }
 
                protected virtual void AddAttribute (string name, string value, HtmlTextWriterAttribute key)
@@ -403,22 +415,20 @@ namespace System.Web.UI {
 
                public virtual void RenderBeginTag (string tagName)
                {
-                       if (!OnTagRender (tagName, GetTagKey (tagName)))
-                               return;
+                       bool ignore = !OnTagRender (tagName, GetTagKey (tagName));
 
                        PushEndTag (tagName);
-
+                       TagIgnore = ignore;
                        DoBeginTag ();
                }
 
                public virtual void RenderBeginTag (HtmlTextWriterTag tagKey)
                {
-                       if (!OnTagRender (GetTagName (tagKey), tagKey))
-                               return;
+                       bool ignore = !OnTagRender (GetTagName (tagKey), tagKey);
 
                        PushEndTag (tagKey);
-
                        DoBeginTag ();
+                       TagIgnore = ignore;
                }
 
                void WriteIfNotNull (string s)
@@ -431,25 +441,27 @@ namespace System.Web.UI {
                void DoBeginTag ()
                {
                        WriteIfNotNull (RenderBeforeTag ());
-                       WriteBeginTag (TagName);
-                       FilterAttributes ();
-
-                       HtmlTextWriterTag key = (int) TagKey < tags.Length ? TagKey : HtmlTextWriterTag.Unknown;
-
-                       switch (tags [(int) key].tag_type) {
-                               case TagType.Inline:
-                                       Write (TagRightChar);
-                                       break;
-                               case TagType.Block:
-                                       Write (TagRightChar);
-                                       WriteLine ();
-                                       Indent++;
-                                       break;
-                               case TagType.SelfClosing:
-                                       Write (SelfClosingTagEnd);
-                                       break;
+                       if (!TagIgnore) {
+                               WriteBeginTag (TagName);
+                               FilterAttributes ();
+
+                               HtmlTextWriterTag key = (int) TagKey < tags.Length ? TagKey : HtmlTextWriterTag.Unknown;
+
+                               switch (tags [(int) key].tag_type) {
+                                       case TagType.Inline:
+                                               Write (TagRightChar);
+                                               break;
+                                       case TagType.Block:
+                                               Write (TagRightChar);
+                                               WriteLine ();
+                                               Indent++;
+                                               break;
+                                       case TagType.SelfClosing:
+                                               Write (SelfClosingTagEnd);
+                                               break;
+                               }
                        }
-
+                       
                        // FIXME what do i do for self close here?
                        WriteIfNotNull (RenderBeforeContent ());
                }
@@ -457,25 +469,29 @@ namespace System.Web.UI {
 
                public virtual void RenderEndTag ()
                {
+                       
                        // FIXME what do i do for self close here?
                        WriteIfNotNull (RenderAfterContent ());
 
-                       HtmlTextWriterTag key = (int) TagKey < tags.Length ? TagKey : HtmlTextWriterTag.Unknown;
-
-                       switch (tags [(int) key].tag_type) {
-                               case TagType.Inline:
-                                       WriteEndTag (TagName);
-                                       break;
-                               case TagType.Block:
-                                       Indent--;
-                                       WriteLineNoTabs ("");
-                                       WriteEndTag (TagName);
-
-                                       break;
-                               case TagType.SelfClosing:
-                                       // NADA
-                                       break;
+                       if (!TagIgnore) {
+                               HtmlTextWriterTag key = (int) TagKey < tags.Length ? TagKey : HtmlTextWriterTag.Unknown;
+
+                               switch (tags [(int) key].tag_type) {
+                                       case TagType.Inline:
+                                               WriteEndTag (TagName);
+                                               break;
+                                       case TagType.Block:
+                                               Indent--;
+                                               WriteLineNoTabs ("");
+                                               WriteEndTag (TagName);
+
+                                               break;
+                                       case TagType.SelfClosing:
+                                               // NADA
+                                               break;
+                               }
                        }
+                       
                        WriteIfNotNull (RenderAfterTag ());
 
                        PopEndTag ();
@@ -793,6 +809,26 @@ namespace System.Web.UI {
                        }
                }
 
+               bool TagIgnore {
+                       get {
+                               if (tagstack_pos == -1)
+                                       throw new InvalidOperationException ();
+
+                               return tagstack [tagstack_pos].ignore;
+                       }
+
+                       set {
+                               if (tagstack_pos == -1)
+                                       throw new InvalidOperationException ();
+                               
+                               tagstack [tagstack_pos].ignore = value;
+                       }
+               }
+               
+               internal HttpWriter GetHttpWriter ()
+               {
+                       return b as HttpWriter;
+               }
 
                TextWriter b;
                string tab_string;
@@ -816,6 +852,7 @@ namespace System.Web.UI {
                AddedTag {
                        public string name;
                        public HtmlTextWriterTag key;
+                       public bool ignore;
                }
 
 #if TARGET_JVM
@@ -1212,7 +1249,14 @@ namespace System.Web.UI {
                {
                        throw new NotImplementedException ();
                }
+               
+               public virtual void BeginRender ()
+               {
+               }
 
+               public virtual void EndRender ()
+               {
+               }
 #endif
        }
-}
\ No newline at end of file
+}