Forráskód Böngészése

fixed: AddAttributesToRender method calls FillStyleAttributes,
text styles applied correct

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

Igor Zelmanovich 19 éve
szülő
commit
302de1069d

+ 7 - 0
mcs/class/System.Web/System.Web.UI.WebControls/ChangeLog

@@ -1,3 +1,10 @@
+2006-08-23 Igor Zelmanovich <[email protected]>
+
+	* Style.cs:
+	* FontInfo.cs: 
+	fixed: AddAttributesToRender method calls FillStyleAttributes,
+	text styles applied correct.
+
 2006-08-23 Igor Zelmanovich <[email protected]>
 
 	* FontInfo.cs: 

+ 55 - 0
mcs/class/System.Web/System.Web.UI.WebControls/FontInfo.cs

@@ -517,6 +517,61 @@ namespace System.Web.UI.WebControls {
 				fontstyles |= FontStyles.Underline;
 			}
 		}
+
+#if NET_2_0
+		internal void FillStyleAttributes (CssStyleCollection attributes, bool alwaysRenderTextDecoration) {
+			if (IsEmpty) {
+				if(alwaysRenderTextDecoration)
+					attributes.Add (HtmlTextWriterStyle.TextDecoration, "none");
+				return;
+			}
+
+			string s;
+			// Fonts are a bit weird
+			s = String.Join (",", Names);
+			if (s.Length > 0) {
+				attributes.Add (HtmlTextWriterStyle.FontFamily, s);
+			}
+
+			if ((fontstyles & FontStyles.Bold) != 0) {
+				attributes.Add (HtmlTextWriterStyle.FontWeight, Bold ? "bold" : "normal");
+			}
+
+			if ((fontstyles & FontStyles.Italic) != 0) {
+				attributes.Add (HtmlTextWriterStyle.FontStyle, Italic ? "italic" : "normal");
+			}
+
+			if (!Size.IsEmpty) {
+				attributes.Add (HtmlTextWriterStyle.FontSize, Size.ToString ());
+			}
+
+			// These styles are munged into a attribute decoration
+			s = string.Empty;
+			bool hasTextDecoration = false;
+
+			if ((fontstyles & FontStyles.Overline) != 0) {
+				if (Overline)
+					s += "overline ";
+				hasTextDecoration = true;
+			}
+
+			if ((fontstyles & FontStyles.Strikeout) != 0) {
+				if (Strikeout)
+					s += "line-through ";
+				hasTextDecoration = true;
+			}
+
+			if ((fontstyles & FontStyles.Underline) != 0) {
+				if (Underline)
+					s += "underline ";
+				hasTextDecoration = true;
+			}
+
+			s = (s.Length > 0) ? s.Trim () : (alwaysRenderTextDecoration || hasTextDecoration) ? "none" : "";
+			if (s.Length > 0)
+				attributes.Add (HtmlTextWriterStyle.TextDecoration, s);
+		}
+#endif
 		#endregion	// Private Methods
 
 

+ 29 - 61
mcs/class/System.Web/System.Web.UI.WebControls/Style.cs

@@ -426,7 +426,14 @@ namespace System.Web.UI.WebControls {
 
 		void WriteStyleAttributes (HtmlTextWriter writer) 
 		{
-			string		s;
+#if NET_2_0
+			CssStyleCollection col = new CssStyleCollection (new StateBag ());
+			FillStyleAttributes (col, null);
+			foreach (string key in col.Keys) {
+				writer.AddStyleAttribute (key, col [key]);
+			}
+#else
+			string s;
 			Color		color;
 			BorderStyle	bs;
 			Unit		u;
@@ -517,11 +524,12 @@ namespace System.Web.UI.WebControls {
 				if (s != "")
 					writer.AddStyleAttribute (HtmlTextWriterStyle.TextDecoration, s);
 			}
+#endif
 		}
 
+#if NET_2_0
 		void FillStyleAttributes (CssStyleCollection attributes) 
 		{
-			string		s;
 			Color		color;
 			BorderStyle	bs;
 			Unit		u;
@@ -540,18 +548,25 @@ namespace System.Web.UI.WebControls {
 					attributes.Add (HtmlTextWriterStyle.BorderColor, ColorTranslator.ToHtml(color));
 			}
 
-			if ((styles & Styles.BorderStyle) != 0) 
-			{
-				bs = (BorderStyle)viewstate["BorderStyle"];
-				if (bs != BorderStyle.NotSet) 
-					attributes.Add (HtmlTextWriterStyle.BorderStyle, bs.ToString());
+			bool have_width = false;
+			if ((styles & Styles.BorderWidth) != 0) {
+				u = (Unit) viewstate ["BorderWidth"];
+				if (!u.IsEmpty) {
+					if (u.Value > 0)
+						have_width = true;
+					attributes.Add (HtmlTextWriterStyle.BorderWidth, u.ToString ());
+				}
 			}
 
-			if ((styles & Styles.BorderWidth) != 0) 
-			{
-				u = (Unit)viewstate["BorderWidth"];
-				if (!u.IsEmpty)
-					attributes.Add (HtmlTextWriterStyle.BorderWidth, u.ToString());
+			if ((styles & Styles.BorderStyle) != 0) {
+				bs = (BorderStyle) viewstate ["BorderStyle"];
+				if (bs != BorderStyle.NotSet)
+					attributes.Add (HtmlTextWriterStyle.BorderStyle, bs.ToString ());
+				else if (have_width)
+						attributes.Add (HtmlTextWriterStyle.BorderStyle, "solid");
+			}
+			else if (have_width) {
+				attributes.Add (HtmlTextWriterStyle.BorderStyle, "solid");
 			}
 
 			if ((styles & Styles.ForeColor) != 0) 
@@ -575,56 +590,9 @@ namespace System.Web.UI.WebControls {
 					attributes.Add (HtmlTextWriterStyle.Width, u.ToString());
 			}
 
-			if (!Font.IsEmpty) {
-				// Fonts are a bit weird
-				if (fontinfo.Name != string.Empty) 
-				{
-					s = fontinfo.Names[0];
-					for (int i = 1; i < fontinfo.Names.Length; i++) 
-					{
-						s += "," + fontinfo.Names[i];
-					}
-					attributes.Add (HtmlTextWriterStyle.FontFamily, s);
-				}
-
-				if (fontinfo.Bold) 
-				{
-					attributes.Add (HtmlTextWriterStyle.FontWeight, "bold");
-				}
-
-				if (fontinfo.Italic) 
-				{
-					attributes.Add (HtmlTextWriterStyle.FontStyle, "italic");
-				}
-
-				if (!fontinfo.Size.IsEmpty) 
-				{
-					attributes.Add (HtmlTextWriterStyle.FontSize, fontinfo.Size.ToString());
-				}
-
-				// These styles are munged into a attribute decoration
-				s = string.Empty;
-
-				if (fontinfo.Overline) 
-				{
-					s += "overline ";
-				}
-
-				if (fontinfo.Strikeout) 
-				{
-					s += "line-through ";
-				}
-
-				if (fontinfo.Underline) 
-				{
-					s += "underline ";
-				}
-
-				s = (s != "") ? s : AlwaysRenderTextDecoration ? "none" : "";
-				if (s != "")
-					attributes.Add (HtmlTextWriterStyle.TextDecoration, s);
-			}
+			Font.FillStyleAttributes (attributes, AlwaysRenderTextDecoration);
 		}
+#endif
 
 		public virtual void CopyFrom(Style s) 
 		{

+ 2 - 4
mcs/class/System.Web/System.Web.UI.WebControls/TableStyle.cs

@@ -224,17 +224,15 @@ namespace System.Web.UI.WebControls {
 				writer.AddAttribute (HtmlTextWriterAttribute.Border, BorderWidth.Value.ToString (CultureInfo.InvariantCulture));
 			}
 
+#if !NET_2_0
 			string s = BackImageUrl;
 			if (s.Length > 0) {
 				if (owner != null)
 					s = owner.ResolveClientUrl (s);
-#if NET_2_0
-				s = s.Replace (" ", "%20");
-#else
 				s = String.Concat ("url(", s, ")");
-#endif
 				writer.AddStyleAttribute (HtmlTextWriterStyle.BackgroundImage, s);
 			}
+#endif
 		}
 
 		private void Copy (string name, Styles s, Style source)

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

@@ -1,3 +1,9 @@
+2006-08-23 Igor Zelmanovich <[email protected]>
+
+	* StyleTest.cs: 
+	added tests ensure that AddAttributesToRender method calls FillStyleAttributes
+	and style attributes work properly.
+
 2006-08-23 Igor Zelmanovich <[email protected]>
 
 	* FontInfoTest.cs: added test 

+ 96 - 0
mcs/class/System.Web/Test/System.Web.UI.WebControls/StyleTest.cs

@@ -77,6 +77,15 @@ namespace MonoTests.System.Web.UI.WebControls
 				}
 			}
 		}
+
+		public override void AddAttributesToRender (HtmlTextWriter writer, WebControl owner) {
+			base.AddAttributesToRender (writer, owner);
+		}
+
+		protected override void FillStyleAttributes (CssStyleCollection attributes, IUrlResolutionService urlResolver) {
+			base.FillStyleAttributes (attributes, urlResolver);
+			attributes.Add ("FillStyleAttributes", "FillStyleAttributes");
+		}
 #endif
 
 		public string[] KeyValuePairs() {
@@ -369,6 +378,93 @@ namespace MonoTests.System.Web.UI.WebControls
 			p.Header.StyleSheet.RegisterStyle (s, p);
 			Assert.AreEqual (false, s.IsEmpty, "AddRegisteredCssClassAttribute#8");
 		}
+
+		[Test]
+		public void Style_AddAttributesToRender_use_FillStyleAttributes () {
+			StringWriter sw = new StringWriter ();
+			HtmlTextWriter tw = new HtmlTextWriter (sw);
+			StyleTestClass s = new StyleTestClass ();
+			s.AddAttributesToRender (tw);
+			tw.RenderBeginTag ("span");
+			tw.RenderEndTag ();
+			HtmlDiff.AssertAreEqual ("<span style=\"FillStyleAttributes:FillStyleAttributes;\" />", sw.ToString (), "AddAttributesToRender_use_FillStyleAttributes#2");
+		}
+
+		[Test]
+		public void Style_GetStyleAttributes () {
+			Style s;
+			CssStyleCollection css;
+
+			s = new Style ();
+			css = s.GetStyleAttributes (null);
+			Assert.AreEqual (0, css.Count, "GetStyleAttributes#1");
+
+			s.Font.Bold = true;
+			s.Font.Italic = true;
+			s.Font.Size = 10;
+			s.Font.Names = new string [] { "Arial", "Veranda" };
+			s.Font.Overline = true;
+			s.Font.Strikeout = true;
+			s.Font.Underline = true;
+			css = s.GetStyleAttributes (null);
+			Assert.AreEqual ("bold", css ["font-weight"], "GetStyleAttributes#2");
+			Assert.AreEqual ("italic", css ["font-style"], "GetStyleAttributes#3");
+			Assert.AreEqual ("10pt", css ["font-size"], "GetStyleAttributes#4");
+			Assert.AreEqual ("Arial,Veranda", css ["font-family"], "GetStyleAttributes#5");
+			Assert.AreEqual (true, css ["text-decoration"].Contains ("overline"), "GetStyleAttributes#6");
+			Assert.AreEqual (true, css ["text-decoration"].Contains ("line-through"), "GetStyleAttributes#7");
+			Assert.AreEqual (true, css ["text-decoration"].Contains ("underline"), "GetStyleAttributes#8");
+
+			s.Font.Names = null;
+			css = s.GetStyleAttributes (null);
+			Assert.AreEqual (null, css ["font-family"], "GetStyleAttributes#9");
+
+			s.Font.Name = "Arial, Veranda";
+			css = s.GetStyleAttributes (null);
+			Assert.AreEqual ("Arial, Veranda", css ["font-family"], "GetStyleAttributes#10");
+
+			s.Font.Name = "";
+			css = s.GetStyleAttributes (null);
+			Assert.AreEqual (null, css ["font-family"], "GetStyleAttributes#11");
+
+			s.Font.Bold = false;
+			s.Font.Italic = false;
+			s.Font.Size = FontUnit.Empty;
+			s.Font.Overline = false;
+			s.Font.Strikeout = false;
+			s.Font.Underline = false;
+			css = s.GetStyleAttributes (null);
+			Assert.AreEqual ("normal", css ["font-weight"], "GetStyleAttributes#12");
+			Assert.AreEqual ("normal", css ["font-style"], "GetStyleAttributes#13");
+			Assert.AreEqual (null, css ["font-size"], "GetStyleAttributes#14");
+			Assert.AreEqual ("none", css ["text-decoration"], "GetStyleAttributes#15");
+
+			s.Reset ();
+			css = s.GetStyleAttributes (null);
+			Assert.AreEqual (0, css.Count, "GetStyleAttributes#16");
+
+			s.Reset ();
+			s.Font.Underline = false;
+			css = s.GetStyleAttributes (null);
+			Assert.AreEqual ("none", css ["text-decoration"], "GetStyleAttributes#17");
+
+			s.Reset ();
+			s.BorderWidth = 1;
+			s.BorderStyle = BorderStyle.Dashed;
+			css = s.GetStyleAttributes (null);
+			Assert.AreEqual ("Dashed", css ["border-style"], "GetStyleAttributes#18");
+			Assert.AreEqual ("1px", css ["border-width"], "GetStyleAttributes#19");
+
+			s.BorderStyle = BorderStyle.NotSet;
+			css = s.GetStyleAttributes (null);
+			Assert.AreEqual ("solid", css ["border-style"], "GetStyleAttributes#20");
+			Assert.AreEqual ("1px", css ["border-width"], "GetStyleAttributes#21");
+
+			s.BorderWidth = 0;
+			css = s.GetStyleAttributes (null);
+			Assert.AreEqual (null, css ["border-style"], "GetStyleAttributes#22");
+			Assert.AreEqual ("0px", css ["border-width"], "GetStyleAttributes#23");
+		}
 #endif
 
 		[Test]