diff --git a/umbraco.MacroEngines.Juno/RazorDynamicNode/HtmlTagWrapper.cs b/umbraco.MacroEngines.Juno/RazorDynamicNode/HtmlTagWrapper.cs index 86f587f786..d223d552cc 100644 --- a/umbraco.MacroEngines.Juno/RazorDynamicNode/HtmlTagWrapper.cs +++ b/umbraco.MacroEngines.Juno/RazorDynamicNode/HtmlTagWrapper.cs @@ -15,7 +15,7 @@ namespace umbraco.MacroEngines public List> Attributes; public void ReflectAttributesFromAnonymousType(object anonymousAttributes) { - Attributes.AddRange( + var newAttributes = anonymousAttributes .GetType() .GetProperties() @@ -26,9 +26,31 @@ namespace umbraco.MacroEngines prop.Name, string.Format("{0}", prop.GetValue(anonymousAttributes, null)) ) - ) - ); + ); + List> mergedAttributes = + newAttributes + .Concat(Attributes) + .GroupBy(kvp => kvp.Key, kvp => kvp.Value) + .Select(g => new KeyValuePair(g.Key, string.Join(" ", g.ToArray()))) + .ToList(); + + Attributes = mergedAttributes; + } + + //class DistinctKeysComparer : IEqualityComparer> + //{ + // public bool Equals(KeyValuePair x, KeyValuePair y) + // { + // return x.Value == y.Value; + // } + + // public int GetHashCode(KeyValuePair obj) + // { + // return obj.Value.GetHashCode(); + // } + //} + public List CssClasses; public string Tag; @@ -41,21 +63,25 @@ namespace umbraco.MacroEngines } public HtmlString Write() { - using (MemoryStream ms = new MemoryStream()) + if (Children.Count > 0 || Attributes.Count > 0) { - using (TextWriter tw = new StreamWriter(ms)) + using (MemoryStream ms = new MemoryStream()) { - HtmlTextWriter html = new HtmlTextWriter(tw); - this.WriteToHtmlTextWriter(html); - tw.Flush(); - ms.Position = 0; - using (TextReader tr = new StreamReader(ms)) + using (TextWriter tw = new StreamWriter(ms)) { - string result = tr.ReadToEnd(); - return new HtmlString(result); + HtmlTextWriter html = new HtmlTextWriter(tw); + this.WriteToHtmlTextWriter(html); + tw.Flush(); + ms.Position = 0; + using (TextReader tr = new StreamReader(ms)) + { + string result = tr.ReadToEnd(); + return new HtmlString(result); + } } } } + return new HtmlString(string.Empty); } public override string ToString() { @@ -107,6 +133,27 @@ namespace umbraco.MacroEngines className = className.Trim(); return (this.CssClasses.Contains(className)); } + + public HtmlTagWrapper Attr(object newAttributes) + { + this.ReflectAttributesFromAnonymousType(newAttributes); + return this; + } + public HtmlTagWrapper Attr(string name, string value) + { + if (!string.IsNullOrWhiteSpace(value)) + { + List> newAttributes = new List>(); + newAttributes.Add(new KeyValuePair(name, value)); + this.ReflectAttributesFromAnonymousType(newAttributes); + } + else + { + var existingKey = this.Attributes.Find(item => item.Key == name); + Attributes.Remove(existingKey); + } + return this; + } } } diff --git a/umbraco.MacroEngines.Juno/RazorDynamicNode/RazorLibraryCore.cs b/umbraco.MacroEngines.Juno/RazorDynamicNode/RazorLibraryCore.cs index c46bedafe0..561c040e6b 100644 --- a/umbraco.MacroEngines.Juno/RazorDynamicNode/RazorLibraryCore.cs +++ b/umbraco.MacroEngines.Juno/RazorDynamicNode/RazorLibraryCore.cs @@ -167,7 +167,25 @@ namespace umbraco.MacroEngines.Library public HtmlTagWrapper Wrap(string tag, string innerText) { - return Wrap(tag, innerText); + return Wrap(tag, innerText, null); + } + public HtmlTagWrapper Wrap(string tag, object inner, object anonymousAttributes) + { + string innerText = null; + if (inner.GetType() != typeof(DynamicNull) && inner != null) + { + innerText = string.Format("{0}", inner); + } + return Wrap(tag, innerText, anonymousAttributes); + } + public HtmlTagWrapper Wrap(string tag, object inner) + { + string innerText = null; + if (inner.GetType() != typeof(DynamicNull) && inner != null) + { + innerText = string.Format("{0}", inner); + } + return Wrap(tag, innerText, null); } public HtmlTagWrapper Wrap(string tag, string innerText, object anonymousAttributes) { @@ -176,7 +194,10 @@ namespace umbraco.MacroEngines.Library { wrap.ReflectAttributesFromAnonymousType(anonymousAttributes); } - wrap.Children.Add(new HtmlTagWrapperTextNode(innerText)); + if (!string.IsNullOrWhiteSpace(innerText)) + { + wrap.Children.Add(new HtmlTagWrapperTextNode(innerText)); + } return wrap; } }