mono / mono

Mono open source ECMA CLI, C# and .NET implementation.
https://www.mono-project.com
Other
11.13k stars 3.82k forks source link

WebControl CSS style collection causes exception on duplicate keys #7455

Open hannoquest opened 6 years ago

hannoquest commented 6 years ago

Steps to Reproduce

The following sample code fails:

var c = new System.Web.UI.HtmlControls.HtmlGenericControl();
c.Attributes["style"] = "display: block; display: none;";

Current Behavior

Exception occurs because of duplicate keys in the CSS code string; see stack trace below. Additionally, the key is not formatted correctly in the error message.

Expected Behavior

The call should succeed as the supplied value is valid CSS code. Note that the call does not fail when running against .NET Framework.

The bug can be fixed by allowing for duplicate keys in the CssStyleCollection.ParseStyle method.

On which platforms did you notice this

[ ] macOS [x] Linux [ ] Windows

Version Used: 5.8.0.127

Stacktrace

System.ArgumentException: An item with the same key has already been added. Key: {0}
  at System.Collections.Specialized.ListDictionary.Add (System.Object key, System.Object value) [0x00065] in <1a0168907d2042858c12ff7b2c118a26>:0 
  at System.Web.UI.CssStyleCollection.ParseStyle (System.Int32 startIndex) [0x000d3] in <41e0250b34284bbbab746b210d492610>:0 
  at System.Web.UI.CssStyleCollection.InitFromStyle () [0x0001d] in <41e0250b34284bbbab746b210d492610>:0 
  at System.Web.UI.CssStyleCollection.set_Value (System.String value) [0x00007] in <41e0250b34284bbbab746b210d492610>:0 
  at System.Web.UI.AttributeCollection.Add (System.String key, System.String value) [0x00019] in <41e0250b34284bbbab746b210d492610>:0 
  at System.Web.UI.AttributeCollection.set_Item (System.String key, System.String value) [0x00000] in <41e0250b34284bbbab746b210d492610>:0 
hannoquest commented 6 years ago

Fix suggestion: Change CssStyleCollection.cs line 99 to: style[key] = value;

akoeplinger commented 6 years ago

@hannoquest feel free to send a pull request with this change 😄