Parcourir la source

2006-10-22 Igor Zelmanovich <[email protected]>

	* CssStyleCollection.cs:
	* AttributeCollection.cs:
	fixed: style collection is synchronized with style attribute 


svn path=/trunk/mcs/; revision=66874
Igor Zelmanovich il y a 19 ans
Parent
commit
62cd905bc9

+ 5 - 0
mcs/class/System.Web/System.Web.UI/AttributeCollection.cs

@@ -89,11 +89,16 @@ namespace System.Web.UI {
 
 		public void Clear ()
 		{
+			CssStyle.Clear ();
 			bag.Clear ();
 		}
 
 		public void Remove (string key)
 		{
+			if (0 == String.Compare (key, "style", true, CultureInfo.InvariantCulture)) {
+				CssStyle.Clear ();
+				return;
+			}
 			bag.Remove (key);
 		}
 

+ 6 - 0
mcs/class/System.Web/System.Web.UI/ChangeLog

@@ -1,3 +1,9 @@
+2006-10-22 Igor Zelmanovich <[email protected]>
+
+	* CssStyleCollection.cs:
+	* AttributeCollection.cs:
+	fixed: style collection is synchronized with style attribute 
+
 2006-10-19 Igor Zelmanovich <[email protected]>
 
 	* ClientScriptManager.cs: fixed: renders id attribute in hidden field

+ 11 - 11
mcs/class/System.Web/System.Web.UI/CssStyleCollection.cs

@@ -32,6 +32,8 @@ using System.IO;
 using System.Collections;
 using System.Security.Permissions;
 using System.Text;
+using System.Collections.Specialized;
+using System.Globalization;
 
 namespace System.Web.UI {
 
@@ -40,7 +42,7 @@ namespace System.Web.UI {
 	public sealed class CssStyleCollection
 	{
 		StateBag bag;
-		StateBag style;
+		HybridDictionary style;
 
 		internal CssStyleCollection (StateBag bag)
 		{
@@ -51,7 +53,11 @@ namespace System.Web.UI {
 
 		void InitFromStyle ()
 		{
-			style = new StateBag ();
+#if NET_2_0
+			style = new HybridDictionary (true);
+#else
+			style = new HybridDictionary (false);
+#endif
 			string att = (string) bag ["style"];
 			if (att != null) {
 				FillStyle (att);
@@ -88,7 +94,7 @@ namespace System.Web.UI {
 		{
 			StringBuilder sb = new StringBuilder ();
 			foreach (string key in style.Keys) {
-				if (key == "background-image")
+				if (key == "background-image" && 0 != String.Compare ("url", ((string) style [key]).Substring (0, 3), true, CultureInfo.InvariantCulture))
 					sb.AppendFormat ("{0}:url({1});", key, HttpUtility.UrlPathEncode ((string) style [key]));
 				else
 					sb.AppendFormat ("{0}:{1};", key, style [key]);
@@ -99,14 +105,12 @@ namespace System.Web.UI {
 
 		public int Count {
 			get {
-				InitFromStyle ();
 				return style.Count;
 			}
 		}
 
 		public string this [string key] {
 			get {
-				InitFromStyle ();
 				return style [key] as string;
 			}
 
@@ -117,14 +121,12 @@ namespace System.Web.UI {
 
 		public ICollection Keys {
 			get {
-				InitFromStyle ();
 				return style.Keys;
 			}
 		}
 
 		public void Add (string key, string value)
 		{
-			InitFromStyle ();
 			style [key] = value;
 			bag ["style"] = BagToString ();
 		}
@@ -141,14 +143,13 @@ namespace System.Web.UI {
 
 		public void Clear ()
 		{
+			style.Clear ();
 			bag.Remove ("style");
-			InitFromStyle ();
 		}
 
 		public void Remove (string key)
 		{
-			InitFromStyle ();
-			if (style == null || style [key] == null)
+			if (style [key] == null)
 				return;
 			style.Remove (key);
 			bag ["style"] = BagToString ();
@@ -156,7 +157,6 @@ namespace System.Web.UI {
 #if NET_2_0
 		public string this [HtmlTextWriterStyle key] {
 			get {
-				InitFromStyle ();
 				return style [HtmlTextWriter.StaticGetStyleName (key)] as string;
 			}
 			set {

+ 4 - 0
mcs/class/System.Web/Test/System.Web.UI/ChangeLog

@@ -1,3 +1,7 @@
+2006-10-22  Igor Zelmanovich   <[email protected]>
+
+	* CssStyleCollectionTest.cs: new tests.
+
 2006-10-19  Igor Zelmanovich   <[email protected]>
 
 	* ClientScriptManagerTest.cs: removed NotWorking attribute.

+ 132 - 3
mcs/class/System.Web/Test/System.Web.UI/CssStyleCollectionTest.cs

@@ -37,8 +37,8 @@ using refl = System.Reflection;
 using System.Web;
 using System.Web.UI;
 using System.Web.UI.WebControls;
-using MonoTests.SystemWeb.Framework;
-using MonoTests.stand_alone.WebHarness;
+using System.Collections.Specialized;
+using System.Text;
 
 namespace MonoTests.System.Web.UI
 {
@@ -57,11 +57,140 @@ namespace MonoTests.System.Web.UI
 			css.Add (HtmlTextWriterStyle.BackgroundImage, url);
 
 			Assert.AreEqual (url, css ["background-image"], "CssStyleCollection_BackgroundImage#1");
-			Assert.AreEqual (url, css [HtmlTextWriterStyle.BackgroundImage], "CssStyleCollection_BackgroundImage#2");
 			Assert.AreEqual ("background-image:url(" + url_encoded + ");", css.Value, "CssStyleCollection_BackgroundImage#3");
+			Assert.AreEqual ("background-image:url(" + url_encoded + ");", c.Attributes["style"], "CssStyleCollection_BackgroundImage#4");
 		}
+#endif
+
+		[Test]
+		public void CssStyleCollection_BackgroundImage2 () {
+			WebControl c = new WebControl (HtmlTextWriterTag.A);
+			CssStyleCollection css = c.Style;
+			string url = "http://www.go-mono.com/space here?key1=val1&key2=val2";
+			string url_encoded = "http://www.go-mono.com/space%20here?key1=val1&key2=val2";
+
+			css.Add ("background-image", url);
+
+			Assert.AreEqual (url, css ["background-image"], "CssStyleCollection_BackgroundImage#1");
+#if NET_2_0
+			Assert.AreEqual ("background-image:url(" + url_encoded + ");", css.Value, "CssStyleCollection_BackgroundImage#3");
+#endif
+			Assert.AreEqual ("background-image:url(" + url_encoded + ");", c.Attributes ["style"], "CssStyleCollection_BackgroundImage#4");
+		}
+
+		[Test]
+		public void CssStyleCollection_BackgroundImage3 () {
+			WebControl c = new WebControl (HtmlTextWriterTag.A);
+			CssStyleCollection css = c.Style;
+			string url = "http://www.go-mono.com/space here?key1=val1&key2=val2";
+			string url_encoded = "http://www.go-mono.com/space%20here?key1=val1&key2=val2";
+
+			css.Add ("background-image", "url(" + url_encoded + ")");
+
+			Assert.AreEqual ("url(" + url_encoded + ")", css ["background-image"], "CssStyleCollection_BackgroundImage#1");
+#if NET_2_0
+			Assert.AreEqual ("background-image:url(" + url_encoded + ");", css.Value, "CssStyleCollection_BackgroundImage#3");
+#endif
+			Assert.AreEqual ("background-image:url(" + url_encoded + ");", c.Attributes ["style"], "CssStyleCollection_BackgroundImage#4");
+		}
+
+		[Test]
+		public void CssStyleCollection_BackgroundImage4 () {
+			WebControl c = new WebControl (HtmlTextWriterTag.A);
+			CssStyleCollection css = c.Style;
+			string url = "http://www.go-mono.com/space here?key1=val1&key2=val2";
+			string url_encoded = "http://www.go-mono.com/space%20here?key1=val1&key2=val2";
+
+			c.Attributes ["style"] = "background-image:url(" + url_encoded + ");";
+
+			Assert.AreEqual ("url(" + url_encoded + ")", css ["background-image"], "CssStyleCollection_BackgroundImage#1");
+#if NET_2_0
+			Assert.AreEqual ("background-image:url(" + url_encoded + ");", css.Value, "CssStyleCollection_BackgroundImage#3");
+#endif
+			Assert.AreEqual ("background-image:url(" + url_encoded + ");", c.Attributes ["style"], "CssStyleCollection_BackgroundImage#4");
+		}
+
+		[Test]
+		public void CssStyleCollection_Enumerator () {
+			WebControl c = new WebControl (HtmlTextWriterTag.A);
+			c.BackColor = Color.Beige;
+			c.ForeColor = Color.Brown;
+			c.Font.Bold = true;
+			c.Attributes ["style"] = "padding: 0px; margin: 0px";
 
+			Assert.AreEqual (2, c.Style.Count, "Style Count");
+#if NET_2_0
+			Assert.AreEqual (3, c.ControlStyle.GetStyleAttributes (c).Count, "ControlStyle Count");
 #endif
 
+			CssStyleCollection col = c.Style;
+			NameValueCollection styles = new NameValueCollection ();
+			foreach (string key in col.Keys) {
+				styles [key] = col [key];
+			}
+			Assert.AreEqual ("0px", styles ["padding"], "Style padding");
+			Assert.AreEqual ("0px",styles ["margin"],  "Style margin");
+		}
+
+		[Test]
+		public void CssStyleCollection_Style_Attribute () {
+			WebControl c = new WebControl (HtmlTextWriterTag.A);
+			Assert.IsTrue (Object.ReferenceEquals (c.Style, c.Attributes.CssStyle));
+
+			// style attribute is parsed to CssStyleCollection
+			c.Attributes.Add ("style", "padding: 1px; margin: 2px");
+			Assert.AreEqual (2, c.Style.Count, "Style Count");
+			Assert.AreEqual ("1px", c.Style ["padding"], "");
+			Assert.AreEqual ("2px", c.Style ["margin"], "");
+
+			// CssStyleCollection is merged to style attribute
+			c.Style.Add ("color", "red");
+			Assert.AreEqual (3, c.Style.Count, "Style Count");
+			Assert.AreEqual ("red", c.Style ["color"], "");
+			Assert.IsTrue (c.Attributes ["style"].IndexOf("color:")>=0);
+
+			// replacing style attribute replaces CssStyleCollection's items
+			c.Attributes ["style"] = "align: center";
+			Assert.AreEqual (1, c.Style.Count, "Style Count");
+			Assert.AreEqual ("center", c.Style ["align"], "");
+
+			// removing style attribute clears CssStyleCollection
+			c.Attributes.Remove("style");
+			Assert.AreEqual (0, c.Style.Count, "Style Count");
+
+			// adding to CssStyleCollection create style attribute
+			c.Style.Add ("color", "red");
+			Assert.AreEqual (1, c.Attributes.Count, "Attributes Count");
+
+			c.Attributes ["style"] = "align: center; color: red;";
+			Assert.AreEqual (2, c.Style.Count, "Style Count");
+			Assert.AreEqual ("center", c.Style ["align"], "");
+			Assert.AreEqual ("red", c.Style ["color"], "");
+
+			// clearing CssStyleCollection removes style attribute
+			c.Style.Clear ();
+			Assert.AreEqual (0, c.Attributes.Count, "Attributes Count");
+		}
+
+		[Test]
+		public void CssStyleCollection_case_sensitive  () {
+			WebControl c = new WebControl (HtmlTextWriterTag.A);
+			c.Style.Add ("color", "red");
+#if NET_2_0
+			Assert.AreEqual ("red", c.Style ["Color"], "");
+#else
+			Assert.AreEqual (null, c.Style ["Color"], "");
+			Assert.AreEqual ("red", c.Style ["color"], "");
+#endif
+			c.Style.Add ("Color", "Blue");
+#if NET_2_0
+			Assert.AreEqual ("Blue", c.Style ["color"], "");
+			Assert.AreEqual (1, c.Style.Count, "Style Count");
+#else
+			Assert.AreEqual ("red", c.Style ["color"], "");
+			Assert.AreEqual ("Blue", c.Style ["Color"], "");
+			Assert.AreEqual (2, c.Style.Count, "Style Count");
+#endif
+		}
 	}
 }