* HtmlAnchor.cs: Do not render empty name, target or title attribute.
authorGert Driesen <drieseng@users.sourceforge.net>
Sun, 8 Jun 2008 21:11:28 +0000 (21:11 -0000)
committerGert Driesen <drieseng@users.sourceforge.net>
Sun, 8 Jun 2008 21:11:28 +0000 (21:11 -0000)
Removed extra check for target attribute in RenderAttributes. Use
string.Empty instead of "", and check string's Length instead of
comparison with "" or string.Empty. Minor code formatting.
* HtmlAnchorTest.cs: Added RenderAttributes tests for empty or null
HRef, Name, Target and Title.

svn path=/trunk/mcs/; revision=105270

mcs/class/System.Web/System.Web.UI.HtmlControls/ChangeLog
mcs/class/System.Web/System.Web.UI.HtmlControls/HtmlAnchor.cs
mcs/class/System.Web/Test/System.Web.UI.HtmlControls/ChangeLog
mcs/class/System.Web/Test/System.Web.UI.HtmlControls/HtmlAnchorTest.cs

index d9b9f37d60834c9185fc4a63179c3565bffb29e9..56b0eae8badbb091a6022188c7d8efce2f806522 100644 (file)
@@ -1,3 +1,10 @@
+2008-06-08  Gert Driesen  <drieseng@users.sourceforge.net>
+
+       * HtmlAnchor.cs: Do not render empty name, target or title attribute.
+       Removed extra check for target attribute in RenderAttributes. Use
+       string.Empty instead of "", and check string's Length instead of
+       comparison with "" or string.Empty. Minor code formatting.
+
 2008-06-04  Juraj Skripksy  <js@hotfeet.ch>
 
        * HtmlAnchor.cs (Render): Add missing call to
index 44994a46ca5209fa6211f6ffb7ec841e7e66a4a0..c6732f31e04f563daab4dabbbb8b53779b097684 100644 (file)
@@ -48,21 +48,20 @@ namespace System.Web.UI.HtmlControls {
                {
                }
 
-
                [DefaultValue ("")]
-                       [WebSysDescription("")]
-                       [WebCategory("Action")]
-                       [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
+               [WebSysDescription("")]
+               [WebCategory("Action")]
+               [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
 #if NET_2_0
-                       [UrlProperty]
+               [UrlProperty]
 #endif
-                       public string HRef {
+               public string HRef {
                        get {
                                string s = Attributes ["href"];
                                return (s == null) ? String.Empty : s;
                        }
                        set {
-                               if (value == null || value == String.Empty) {
+                               if (value == null || value.Length == 0) {
                                        Attributes.Remove ("href");
                                } else {
                                        Attributes ["href"] = value;
@@ -71,16 +70,16 @@ namespace System.Web.UI.HtmlControls {
                }
 
                [DefaultValue ("")]
-                       [WebSysDescription("")]
-                       [WebCategory("Navigation")]
-                       [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
-                       public string Name {
+               [WebSysDescription("")]
+               [WebCategory("Navigation")]
+               [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
+               public string Name {
                        get {
                                string s = Attributes ["name"];
                                return (s == null) ? String.Empty : s;
                        }
                        set {
-                               if (value == null)
+                               if (value == null || value.Length == 0)
                                        Attributes.Remove ("name");
                                else
                                        Attributes ["name"] = value;
@@ -88,16 +87,16 @@ namespace System.Web.UI.HtmlControls {
                }
 
                [DefaultValue ("")]
-                       [WebSysDescription("")]
-                       [WebCategory("Navigation")]
-                       [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
-                       public string Target {
+               [WebSysDescription("")]
+               [WebCategory("Navigation")]
+               [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
+               public string Target {
                        get {
                                string s = Attributes ["target"];
                                return (s == null) ? String.Empty : s;
                        }
                        set {
-                               if (value == null)
+                               if (value == null || value.Length == 0)
                                        Attributes.Remove ("target");
                                else
                                        Attributes ["target"] = value;
@@ -105,19 +104,19 @@ namespace System.Web.UI.HtmlControls {
                }
 
                [DefaultValue ("")]
-                       [WebSysDescription("")]
-                       [WebCategory("Appearance")]
-                       [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
+               [WebSysDescription("")]
+               [WebCategory("Appearance")]
+               [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
 #if NET_2_0
-                       [Localizable (true)]
+               [Localizable (true)]
 #endif
-                       public string Title {
+               public string Title {
                        get {
                                string s = Attributes ["title"];
                                return (s == null) ? String.Empty : s;
                        }
                        set {
-                               if (value == null)
+                               if (value == null || value.Length == 0)
                                        Attributes.Remove ("title");
                                else
                                        Attributes ["title"] = value;
@@ -126,7 +125,7 @@ namespace System.Web.UI.HtmlControls {
 
 #if NET_2_0
                [DefaultValue (true)]
-                       public virtual bool CausesValidation {
+               public virtual bool CausesValidation {
                        get {
                                return ViewState.GetBool ("CausesValidation", true);
                        }
@@ -136,7 +135,7 @@ namespace System.Web.UI.HtmlControls {
                }
 
                [DefaultValue ("")]
-                       public virtual string ValidationGroup {
+               public virtual string ValidationGroup {
                        get {
                                return ViewState.GetString ("ValidationGroup", String.Empty);
                        }
@@ -149,9 +148,9 @@ namespace System.Web.UI.HtmlControls {
 #if NET_2_0
                protected internal
 #else
-                       protected
+               protected
 #endif         
-                       override void OnPreRender (EventArgs e)
+               override void OnPreRender (EventArgs e)
                {
                        base.OnPreRender (e);
                }
@@ -165,10 +164,6 @@ namespace System.Web.UI.HtmlControls {
 
                protected override void RenderAttributes (HtmlTextWriter writer)
                {
-                       string target = Attributes ["target"];
-                       if ((target == null) || (target.Length == 0))
-                               Attributes.Remove("target");
-
                        // we don't want to render the "user" URL, so we either render:
                        EventHandler serverClick = (EventHandler) Events [serverClickEvent];
                        if (serverClick != null) {
@@ -186,12 +181,12 @@ namespace System.Web.UI.HtmlControls {
 #endif
                        } else {
                                string hr = HRef;
-                               if (hr != "")
+                               if (hr != string.Empty)
 #if TARGET_J2EE
                                        // For J2EE portlets we need to genreate a render URL.
-                                       HRef = ResolveClientUrl (hr, String.Compare (target, "_blank", StringComparison.InvariantCultureIgnoreCase) != 0);
+                                       HRef = ResolveClientUrl (hr, String.Compare (Target, "_blank", StringComparison.InvariantCultureIgnoreCase) != 0);
 #else
-                               HRef = ResolveClientUrl (hr);
+                                       HRef = ResolveClientUrl (hr);
 #endif
                        }
 
@@ -201,6 +196,7 @@ namespace System.Web.UI.HtmlControls {
                        // nor is the property available after rendering
                        Attributes.Remove ("href");
                }
+
 #if NET_2_0
                protected virtual void RaisePostBackEvent (string eventArgument)
                {
@@ -210,11 +206,12 @@ namespace System.Web.UI.HtmlControls {
                        OnServerClick (EventArgs.Empty);
                }
        
-               PostBackOptions GetPostBackOptions () {
+               PostBackOptions GetPostBackOptions ()
+               {
                        PostBackOptions options = new PostBackOptions (this);
                        options.ValidationGroup = null;
                        options.ActionUrl = null;
-                       options.Argument = "";
+                       options.Argument = string.Empty;
                        options.RequiresJavaScriptProtocol = true;
                        options.ClientSubmit = true;
                        options.PerformValidation = CausesValidation && Page != null && Page.AreValidatorsUplevel (ValidationGroup);
@@ -234,10 +231,9 @@ namespace System.Web.UI.HtmlControls {
 #endif
                }
 
-
                [WebSysDescription("")]
-                       [WebCategory("Action")]
-                       public event EventHandler ServerClick {
+               [WebCategory("Action")]
+               public event EventHandler ServerClick {
                        add { Events.AddHandler (serverClickEvent, value); }
                        remove { Events.RemoveHandler (serverClickEvent, value); }
                }
index 0d2497f60b3360b6ee5141688d096fd0a1118bdd..fa46176d50d56a5155933d9d4b179200168ea405 100644 (file)
@@ -1,3 +1,8 @@
+2008-06-08  Gert Driesen  <drieseng@users.sourceforge.net>
+
+       * HtmlAnchorTest.cs: Added RenderAttributes tests for empty or null
+       HRef, Name, Target and Title.
+
 2008-05-04 Igor Zelmanovich <igorz@mainsoft.com>
 
        * HtmlInputButtonTest.cs: use HtmlDiff properly.
index 8e1acac87b05d1ab70d19672d196dd53142bd046..560d45f4ea1beed3041a765be9ddafc1d9d02821 100644 (file)
@@ -165,6 +165,125 @@ namespace MonoTests.System.Web.UI.HtmlControls {
                        Assert.AreEqual (" href=\"*1*\" name=\"*2*\" target=\"*3*\" title=\"*4*\"", writer.InnerWriter.ToString (), "HRef is back");
                }
 
+               [Test]
+               public void RenderAttributes_HRef_Empty ()
+               {
+                       TestHtmlAnchor a = new TestHtmlAnchor ();
+                       Assert.AreEqual (0, a.Attributes.Count, "#1");
+                       a.HRef = string.Empty;
+                       a.Name = "*2*";
+                       a.Target = "*3*";
+                       a.Title = "*4*";
+                       Assert.AreEqual (3, a.Attributes.Count, "#2");
+
+                       HtmlTextWriter writer = a.GetWriter ();
+                       Assert.AreEqual (" name=\"*2*\" target=\"*3*\" title=\"*4*\"", writer.InnerWriter.ToString (), "#3");
+               }
+
+               [Test]
+               public void RenderAttributes_HRef_Null ()
+               {
+                       TestHtmlAnchor a = new TestHtmlAnchor ();
+                       Assert.AreEqual (0, a.Attributes.Count, "#1");
+                       a.HRef = null;
+                       a.Name = "*2*";
+                       a.Target = "*3*";
+                       a.Title = "*4*";
+                       Assert.AreEqual (3, a.Attributes.Count, "#2");
+
+                       HtmlTextWriter writer = a.GetWriter ();
+                       Assert.AreEqual (" name=\"*2*\" target=\"*3*\" title=\"*4*\"", writer.InnerWriter.ToString (), "#3");
+               }
+
+               [Test]
+               public void RenderAttributes_Name_Empty ()
+               {
+                       TestHtmlAnchor a = new TestHtmlAnchor ();
+                       Assert.AreEqual (0, a.Attributes.Count, "#1");
+                       a.HRef = "*1*";
+                       a.Name = string.Empty;
+                       a.Target = "*3*";
+                       a.Title = "*4*";
+                       Assert.AreEqual (3, a.Attributes.Count, "#2");
+
+                       HtmlTextWriter writer = a.GetWriter ();
+                       Assert.AreEqual (" href=\"*1*\" target=\"*3*\" title=\"*4*\"", writer.InnerWriter.ToString (), "#3");
+               }
+
+               [Test]
+               public void RenderAttributes_Name_Null ()
+               {
+                       TestHtmlAnchor a = new TestHtmlAnchor ();
+                       Assert.AreEqual (0, a.Attributes.Count, "#1");
+                       a.HRef = "*1*";
+                       a.Name = null;
+                       a.Target = "*3*";
+                       a.Title = "*4*";
+                       Assert.AreEqual (3, a.Attributes.Count, "#2");
+
+                       HtmlTextWriter writer = a.GetWriter ();
+                       Assert.AreEqual (" href=\"*1*\" target=\"*3*\" title=\"*4*\"", writer.InnerWriter.ToString (), "#3");
+               }
+
+               [Test]
+               public void RenderAttributes_Target_Empty ()
+               {
+                       TestHtmlAnchor a = new TestHtmlAnchor ();
+                       Assert.AreEqual (0, a.Attributes.Count, "#1");
+                       a.HRef = "*1*";
+                       a.Name = "*2*";
+                       a.Target = string.Empty;
+                       a.Title = "*4*";
+                       Assert.AreEqual (3, a.Attributes.Count, "#2");
+
+                       HtmlTextWriter writer = a.GetWriter ();
+                       Assert.AreEqual (" href=\"*1*\" name=\"*2*\" title=\"*4*\"", writer.InnerWriter.ToString (), "#3");
+               }
+
+               [Test]
+               public void RenderAttributes_Target_Null ()
+               {
+                       TestHtmlAnchor a = new TestHtmlAnchor ();
+                       Assert.AreEqual (0, a.Attributes.Count, "#1");
+                       a.HRef = "*1*";
+                       a.Name = "*2*";
+                       a.Target = null;
+                       a.Title = "*4*";
+                       Assert.AreEqual (3, a.Attributes.Count, "#2");
+
+                       HtmlTextWriter writer = a.GetWriter ();
+                       Assert.AreEqual (" href=\"*1*\" name=\"*2*\" title=\"*4*\"", writer.InnerWriter.ToString (), "#3");
+               }
+
+               [Test]
+               public void RenderAttributes_Title_Empty ()
+               {
+                       TestHtmlAnchor a = new TestHtmlAnchor ();
+                       Assert.AreEqual (0, a.Attributes.Count, "#1");
+                       a.HRef = "*1*";
+                       a.Name = "*2*";
+                       a.Target = "*3*";
+                       a.Title = string.Empty;
+                       Assert.AreEqual (3, a.Attributes.Count, "#2");
+
+                       HtmlTextWriter writer = a.GetWriter ();
+                       Assert.AreEqual (" href=\"*1*\" name=\"*2*\" target=\"*3*\"", writer.InnerWriter.ToString (), "#3");
+               }
+
+               [Test]
+               public void RenderAttributes_Title_Null ()
+               {
+                       TestHtmlAnchor a = new TestHtmlAnchor ();
+                       Assert.AreEqual (0, a.Attributes.Count, "#1");
+                       a.HRef = "*1*";
+                       a.Name = "*2*";
+                       a.Target = "*3*";
+                       a.Title = null;
+                       Assert.AreEqual (3, a.Attributes.Count, "#2");
+
+                       HtmlTextWriter writer = a.GetWriter ();
+                       Assert.AreEqual (" href=\"*1*\" name=\"*2*\" target=\"*3*\"", writer.InnerWriter.ToString (), "#3");
+               }
 
                private bool serverClick;
                private void ServerClick (object sender, EventArgs e)