diff --git a/Ooui/UI.cs b/Ooui/UI.cs index c663766..38e8ec3 100644 --- a/Ooui/UI.cs +++ b/Ooui/UI.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; @@ -16,6 +17,12 @@ namespace Ooui static readonly Dictionary> publishedPaths = new Dictionary> (); + static readonly Dictionary styles = + new Dictionary (); + static readonly StyleSelectors rules = new StyleSelectors (); + + public static StyleSelectors Styles => rules; + static readonly byte[] clientJsBytes; public static string Template { get; set; } = $@" @@ -24,6 +31,7 @@ namespace Ooui @ElementPath +
@@ -177,7 +185,7 @@ namespace Ooui static string RenderTemplate (string elementPath) { - return Template.Replace ("@ElementPath", elementPath); + return Template.Replace ("@ElementPath", elementPath).Replace ("@Styles", rules.ToString ()); } static void WriteElementHtml (string elementPath, HttpListenerResponse response) @@ -444,5 +452,52 @@ namespace Ooui } } } + + public class StyleSelectors + { + public Style this[string selector] { + get { + Style r; + var key = selector ?? ""; + lock (styles) { + if (!styles.TryGetValue (key, out r)) { + r = new Style (); + styles.Add (key, r); + } + return r; + } + } + set { + var key = selector ?? ""; + lock (styles) { + if (value == null) { + styles.Remove (key); + } + else { + styles[key] = value; + } + } + } + } + + public void Clear () + { + lock (styles) { + styles.Clear (); + } + } + + public override string ToString() + { + lock (styles) { + var q = + from s in styles + let v = s.Value.ToString () + where v.Length > 0 + select s.Key + " {" + s.Value.ToString () + "}"; + return String.Join ("\n", q); + } + } + } } } diff --git a/Tests/UITests.cs b/Tests/UITests.cs new file mode 100644 index 0000000..1e480ab --- /dev/null +++ b/Tests/UITests.cs @@ -0,0 +1,57 @@ +using System; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +using Ooui; + +namespace Tests +{ + [TestClass] + public class UITests + { + [TestMethod] + public void UndefinedStylePropertyIsInherit () + { + Assert.AreEqual ("inherit", UI.Styles["something random and made up"].BackgroundColor); + } + + [TestMethod] + public void SetStyleProperty () + { + UI.Styles.Clear (); + UI.Styles[".t1"].BackgroundColor = "red"; + Assert.AreEqual ("red", UI.Styles[".t1"].BackgroundColor); + } + + [TestMethod] + public void ClearWorks () + { + UI.Styles[".t1"].BackgroundColor = "red"; + UI.Styles.Clear (); + Assert.AreEqual ("inherit", UI.Styles[".t1"].BackgroundColor); + Assert.AreEqual ("", UI.Styles.ToString ()); + } + + [TestMethod] + public void SetStyle () + { + UI.Styles.Clear (); + UI.Styles[".t2"] = new Style { + BackgroundColor = "red", + }; + Assert.AreEqual ("red", UI.Styles[".t2"].BackgroundColor); + Assert.AreEqual (".t2 {background-color:red}", UI.Styles.ToString ()); + } + + [TestMethod] + public void SetNullStyle () + { + UI.Styles.Clear (); + UI.Styles[".t3"] = new Style { + BackgroundColor = "red", + }; + UI.Styles[".t3"] = null; + Assert.AreEqual ("inherit", UI.Styles[".t3"].BackgroundColor); + Assert.AreEqual ("", UI.Styles.ToString ()); + } + } +}