HtmlTagWrapper don't write if empty, class methods, attribute methods
This commit is contained in:
@@ -15,7 +15,7 @@ namespace umbraco.MacroEngines
|
||||
public List<KeyValuePair<string, string>> 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<KeyValuePair<string, string>> mergedAttributes =
|
||||
newAttributes
|
||||
.Concat(Attributes)
|
||||
.GroupBy(kvp => kvp.Key, kvp => kvp.Value)
|
||||
.Select(g => new KeyValuePair<string, string>(g.Key, string.Join(" ", g.ToArray())))
|
||||
.ToList();
|
||||
|
||||
Attributes = mergedAttributes;
|
||||
|
||||
}
|
||||
|
||||
//class DistinctKeysComparer : IEqualityComparer<KeyValuePair<string, string>>
|
||||
//{
|
||||
// public bool Equals(KeyValuePair<string, string> x, KeyValuePair<string, string> y)
|
||||
// {
|
||||
// return x.Value == y.Value;
|
||||
// }
|
||||
|
||||
// public int GetHashCode(KeyValuePair obj)
|
||||
// {
|
||||
// return obj.Value.GetHashCode();
|
||||
// }
|
||||
//}
|
||||
|
||||
public List<string> 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<KeyValuePair<string, string>> newAttributes = new List<KeyValuePair<string, string>>();
|
||||
newAttributes.Add(new KeyValuePair<string, string>(name, value));
|
||||
this.ReflectAttributesFromAnonymousType(newAttributes);
|
||||
}
|
||||
else
|
||||
{
|
||||
var existingKey = this.Attributes.Find(item => item.Key == name);
|
||||
Attributes.Remove(existingKey);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user