Add style serialization

This commit is contained in:
Frank A. Krueger 2017-06-18 14:26:32 -07:00
parent 71e21cfa69
commit 04421a00a5
2 changed files with 58 additions and 5 deletions

View File

@ -316,6 +316,10 @@ namespace Ooui
set {
var safeValue = value ?? "inherit";
lock (properties) {
if (value == null) {
properties.Remove (propertyName);
}
else {
Value old;
if (properties.TryGetValue (propertyName, out old)) {
if (EqualityComparer<Value>.Default.Equals (old, safeValue))
@ -323,8 +327,25 @@ namespace Ooui
}
properties[propertyName] = safeValue;
}
}
PropertyChanged?.Invoke (this, new PropertyChangedEventArgs (propertyName));
}
}
public override string ToString ()
{
var o = new System.Text.StringBuilder ();
var head = "";
lock (properties) {
foreach (var p in properties) {
o.Append (head);
o.Append (p.Key);
o.Append (":");
o.Append (String.Format (System.Globalization.CultureInfo.InvariantCulture, "{0}", p.Value));
head = ";";
}
}
return o.ToString ();
}
}
}

View File

@ -38,5 +38,37 @@ namespace Tests
s.BackgroundColor = "blue";
Assert.AreEqual (2, changeCount);
}
[TestMethod]
public void EmptyString ()
{
var s = new Style ();
Assert.AreEqual ("", s.ToString ());
}
[TestMethod]
public void SingleString ()
{
var s = new Style ();
s.BackgroundColor = "red";
Assert.AreEqual ("background-color:red", s.ToString ());
}
[TestMethod]
public void NullString ()
{
var s = new Style ();
s.BackgroundColor = "red";
s.BackgroundColor = null;
Assert.AreEqual ("", s.ToString ());
}
[TestMethod]
public void FloatString ()
{
var s = new Style ();
s.BorderLeftWidth = 3.142;
Assert.AreEqual ("border-left-width:3.142", s.ToString ());
}
}
}