Got Wrap methods on UmbracoHelper, deprecated old classes in the macroEngines project that Wrap utilizes and proxies

all calles in RazorLibraryCore for Wrap to UmbracoHelper. Fixed up the HtmlTagWrapper classes so they have immutable properties and replaced
public fields with Properties.
This commit is contained in:
Shannon Deminick
2012-09-20 13:16:38 +07:00
parent fd166389c4
commit ae4cb00bbc
11 changed files with 193 additions and 325 deletions

View File

@@ -1,9 +0,0 @@
using System.Web.UI;
namespace Umbraco.Core
{
internal interface IHtmlTagWrapper
{
void WriteToHtmlTextWriter(HtmlTextWriter html);
}
}

View File

@@ -92,9 +92,6 @@
<Compile Include="NameValueCollectionExtensions.cs" />
<Compile Include="PropertyEditors\DatePickerPropertyEditorValueConverter.cs" />
<Compile Include="PropertyEditors\IPropertyEditorValueConverter.cs" />
<Compile Include="HtmlTagWrapper.cs" />
<Compile Include="HtmlTagWrapperBase.cs" />
<Compile Include="HtmlTagWrapperTextNode.cs" />
<Compile Include="Dynamics\IDynamicDocumentDataSource.cs" />
<Compile Include="Dynamics\ParseException.cs" />
<Compile Include="Dynamics\PropertyResult.cs" />

View File

@@ -3,15 +3,27 @@ using System.Linq;
using System.Web.UI;
using System.IO;
using System.Web;
using Umbraco.Core;
namespace Umbraco.Core
namespace Umbraco.Web.Mvc
{
internal class HtmlTagWrapper : IHtmlTagWrapper, IHtmlString
public class HtmlTagWrapper : IHtmlTagWrapper, IHtmlString
{
public HtmlTagWrapper Parent;
public List<IHtmlTagWrapper> Children;
public List<KeyValuePair<string, string>> Attributes;
public void ReflectAttributesFromAnonymousType(List<KeyValuePair<string, string>> newAttributes)
private readonly List<IHtmlTagWrapper> _children;
public IEnumerable<IHtmlTagWrapper> Children
{
get { return _children; }
}
private List<KeyValuePair<string, string>> _attributes;
public IEnumerable<KeyValuePair<string, string>> Attributes
{
get { return _attributes; }
}
public void ReflectAttributesFromAnonymousType(List<KeyValuePair<string, string>> newAttributes)
{
List<KeyValuePair<string, string>> mergedAttributes =
newAttributes
@@ -20,7 +32,7 @@ namespace Umbraco.Core
.Select(g => new KeyValuePair<string, string>(g.Key, string.Join(" ", g.ToArray())))
.ToList();
Attributes = mergedAttributes;
_attributes = mergedAttributes;
}
public void ReflectAttributesFromAnonymousType(object anonymousAttributes)
{
@@ -47,14 +59,14 @@ namespace Umbraco.Core
public HtmlTagWrapper(string tag)
{
this.Tag = tag;
this.Children = new List<IHtmlTagWrapper>();
this._children = new List<IHtmlTagWrapper>();
this.CssClasses = new List<string>();
this.Attributes = new List<KeyValuePair<string, string>>();
this._attributes = new List<KeyValuePair<string, string>>();
this.Visible = true;
}
public HtmlString Write()
{
if ((Children.Count > 0 || Attributes.Count > 0 || CssClasses.Count > 0) && Visible)
if ((Children.Any() || Attributes.Any() || CssClasses.Count > 0) && Visible)
{
using (MemoryStream ms = new MemoryStream())
{
@@ -137,46 +149,45 @@ namespace Umbraco.Core
{
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);
var newAttributes = new List<KeyValuePair<string, string>> {new KeyValuePair<string, string>(name, value)};
this.ReflectAttributesFromAnonymousType(newAttributes);
}
else
{
var existingKey = this.Attributes.Find(item => item.Key == name);
Attributes.Remove(existingKey);
var existingKey = this._attributes.Find(item => item.Key == name);
_attributes.Remove(existingKey);
}
return this;
}
public HtmlTagWrapper AddChild(IHtmlTagWrapper newChild)
{
Children.Add(newChild);
_children.Add(newChild);
return this;
}
public HtmlTagWrapper AddChildren(params IHtmlTagWrapper[] collection)
{
Children.AddRange(collection);
_children.AddRange(collection);
return this;
}
public HtmlTagWrapper AddChild(string text)
{
Children.Add(new HtmlTagWrapperTextNode(text));
_children.Add(new HtmlTagWrapperTextNode(text));
return this;
}
public HtmlTagWrapper AddChildAt(int index, IHtmlTagWrapper newChild)
{
Children.Insert(index, newChild);
_children.Insert(index, newChild);
return this;
}
public HtmlTagWrapper AddChildAt(int index, string text)
{
Children.Insert(index, new HtmlTagWrapperTextNode(text));
_children.Insert(index, new HtmlTagWrapperTextNode(text));
return this;
}
public HtmlTagWrapper AddChildrenAt(int index, params IHtmlTagWrapper[] collection)
{
Children.InsertRange(index, collection);
_children.InsertRange(index, collection);
return this;
}
public HtmlTagWrapper RemoveChildAt(int index)
@@ -185,7 +196,7 @@ namespace Umbraco.Core
}
public int CountChildren()
{
return this.Children.Count;
return this.Children.Count();
}
public HtmlTagWrapper ClearChildren()
{

View File

@@ -1,8 +1,10 @@
namespace Umbraco.Core
using Umbraco.Core;
namespace Umbraco.Web.Mvc
{
internal class HtmlTagWrapperTextNode : IHtmlTagWrapper
public class HtmlTagWrapperTextNode : IHtmlTagWrapper
{
public string Content { get; set; }
public string Content { get; private set; }
public HtmlTagWrapperTextNode(string content)
{
this.Content = content;

View File

@@ -0,0 +1,9 @@
using System.Web.UI;
namespace Umbraco.Web.Mvc
{
public interface IHtmlTagWrapper
{
void WriteToHtmlTextWriter(HtmlTextWriter html);
}
}

View File

@@ -264,6 +264,9 @@
<Compile Include="Media\EmbedProviders\Settings\String.cs" />
<Compile Include="Media\EmbedProviders\Twitgoo.cs" />
<Compile Include="ModelStateExtensions.cs" />
<Compile Include="Mvc\HtmlTagWrapper.cs" />
<Compile Include="Mvc\HtmlTagWrapperTextNode.cs" />
<Compile Include="Mvc\IHtmlTagWrapper.cs" />
<Compile Include="PropertyEditors\RteMacroRenderingPropertyEditorValueConverter.cs" />
<Compile Include="PublishedMediaStoreResolver.cs" />
<Compile Include="RenderFieldCaseType.cs" />

View File

@@ -12,9 +12,11 @@ using HtmlAgilityPack;
using Umbraco.Core;
using Umbraco.Core.Dynamics;
using Umbraco.Core.Models;
using Umbraco.Web.Mvc;
using umbraco;
using System.Collections.Generic;
using umbraco.presentation.templateControls;
using HtmlTagWrapper = Umbraco.Web.Mvc.HtmlTagWrapper;
namespace Umbraco.Web
{
@@ -642,5 +644,103 @@ namespace Umbraco.Web
}
#endregion
#region Wrap
public HtmlTagWrapper Wrap(string tag, string innerText, params IHtmlTagWrapper[] children)
{
var item = Wrap(tag, innerText, (object)null);
foreach (var child in children)
{
item.AddChild(child);
}
return item;
}
public HtmlTagWrapper Wrap(string tag, string innerText)
{
return Wrap(tag, innerText, (object)null);
}
public HtmlTagWrapper Wrap(string tag, object inner, object anonymousAttributes)
{
string innerText = null;
if (inner != null && inner.GetType() != typeof(DynamicNull))
{
innerText = string.Format("{0}", inner);
}
return Wrap(tag, innerText, anonymousAttributes);
}
public HtmlTagWrapper Wrap(string tag, object inner, object anonymousAttributes, params IHtmlTagWrapper[] children)
{
string innerText = null;
if (inner != null && inner.GetType() != typeof(DynamicNull))
{
innerText = string.Format("{0}", inner);
}
var item = Wrap(tag, innerText, anonymousAttributes);
foreach (var child in children)
{
item.AddChild(child);
}
return item;
}
public HtmlTagWrapper Wrap(string tag, object inner)
{
string innerText = null;
if (inner != null && inner.GetType() != typeof(DynamicNull))
{
innerText = string.Format("{0}", inner);
}
return Wrap(tag, innerText, null);
}
public HtmlTagWrapper Wrap(string tag, string innerText, object anonymousAttributes)
{
var wrap = new HtmlTagWrapper(tag);
if (anonymousAttributes != null)
{
wrap.ReflectAttributesFromAnonymousType(anonymousAttributes);
}
if (!string.IsNullOrWhiteSpace(innerText))
{
wrap.AddChild(new HtmlTagWrapperTextNode(innerText));
}
return wrap;
}
public HtmlTagWrapper Wrap(string tag, string innerText, object anonymousAttributes, params IHtmlTagWrapper[] children)
{
var wrap = new HtmlTagWrapper(tag);
if (anonymousAttributes != null)
{
wrap.ReflectAttributesFromAnonymousType(anonymousAttributes);
}
if (!string.IsNullOrWhiteSpace(innerText))
{
wrap.AddChild(new HtmlTagWrapperTextNode(innerText));
}
foreach (var child in children)
{
wrap.AddChild(child);
}
return wrap;
}
public HtmlTagWrapper Wrap(bool visible, string tag, string innerText, object anonymousAttributes)
{
var item = Wrap(tag, innerText, anonymousAttributes);
item.Visible = visible;
return item;
}
public HtmlTagWrapper Wrap(bool visible, string tag, string innerText, object anonymousAttributes, params IHtmlTagWrapper[] children)
{
var item = Wrap(tag, innerText, anonymousAttributes, children);
item.Visible = visible;
foreach (var child in children)
{
item.AddChild(child);
}
return item;
}
#endregion
}
}

View File

@@ -9,200 +9,15 @@ using System.Web;
namespace umbraco.MacroEngines
{
[Obsolete("This class has been superceded by Umbraco.Core.HtmlTagWrapper")]
public class HtmlTagWrapper : HtmlTagWrapperBase, IHtmlString
[Obsolete("This class has been superceded by Umbraco.Web.Mvc.HtmlTagWrapper")]
public class HtmlTagWrapper : Umbraco.Web.Mvc.HtmlTagWrapper
{
//NOTE: We could not actually wrap these methods to proxy to the superceded class because many of these methods return
//instances of itself.
public HtmlTagWrapper Parent;
public List<HtmlTagWrapperBase> Children;
public List<KeyValuePair<string, string>> Attributes;
public void ReflectAttributesFromAnonymousType(List<KeyValuePair<string, string>> newAttributes)
public HtmlTagWrapper(string tag)
: base(tag)
{
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;
}
public void ReflectAttributesFromAnonymousType(object anonymousAttributes)
{
var newAttributes =
anonymousAttributes
.GetType()
.GetProperties()
.Where(prop => !string.IsNullOrWhiteSpace(string.Format("{0}", prop.GetValue(anonymousAttributes, null))))
.ToList()
.ConvertAll(prop =>
new KeyValuePair<string, string>(
prop.Name,
string.Format("{0}", prop.GetValue(anonymousAttributes, null))
)
);
ReflectAttributesFromAnonymousType(newAttributes);
}
public List<string> CssClasses;
public string Tag;
public bool Visible;
public HtmlTagWrapper(string Tag)
{
this.Tag = Tag;
this.Children = new List<HtmlTagWrapperBase>();
this.CssClasses = new List<string>();
this.Attributes = new List<KeyValuePair<string, string>>();
this.Visible = true;
}
public HtmlString Write()
{
if ((Children.Count > 0 || Attributes.Count > 0 || CssClasses.Count > 0) && Visible)
{
using (MemoryStream ms = new MemoryStream())
{
using (TextWriter tw = new StreamWriter(ms))
{
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()
{
return "Use @item.Write() to emit the HTML rather than @item";
}
public IHtmlString ToHtml()
{
return this.Write();
}
public void WriteToHtmlTextWriter(HtmlTextWriter html)
{
html.WriteBeginTag(Tag);
string cssClassNames = string.Join(" ", CssClasses.ToArray()).Trim();
foreach (var attribute in Attributes)
{
html.WriteAttribute(attribute.Key, attribute.Value);
}
if (!string.IsNullOrWhiteSpace(cssClassNames))
{
html.WriteAttribute("class", cssClassNames);
}
html.Write(HtmlTextWriter.TagRightChar);
foreach (var child in Children)
{
child.WriteToHtmlTextWriter(html);
}
html.WriteEndTag(Tag);
}
public HtmlTagWrapper AddClassName(string className)
{
className = className.Trim();
if (!this.CssClasses.Contains(className))
{
this.CssClasses.Add(className);
}
return this;
}
public HtmlTagWrapper RemoveClassName(string className)
{
className = className.Trim();
if (this.CssClasses.Contains(className))
{
this.CssClasses.Remove(className);
}
return this;
}
public bool HasClassName(string className)
{
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;
}
public HtmlTagWrapper AddChild(HtmlTagWrapperBase newChild)
{
Children.Add(newChild);
return this;
}
public HtmlTagWrapper AddChildren(params HtmlTagWrapperBase[] collection)
{
Children.AddRange(collection);
return this;
}
public HtmlTagWrapper AddChild(string text)
{
Children.Add(new HtmlTagWrapperTextNode(text));
return this;
}
public HtmlTagWrapper AddChildAt(int index, HtmlTagWrapperBase newChild)
{
Children.Insert(index, newChild);
return this;
}
public HtmlTagWrapper AddChildAt(int index, string text)
{
Children.Insert(index, new HtmlTagWrapperTextNode(text));
return this;
}
public HtmlTagWrapper AddChildrenAt(int index, params HtmlTagWrapperBase[] collection)
{
Children.InsertRange(index, collection);
return this;
}
public HtmlTagWrapper RemoveChildAt(int index)
{
return this;
}
public int CountChildren()
{
return this.Children.Count;
}
public HtmlTagWrapper ClearChildren()
{
return this;
}
public string ToHtmlString()
{
return this.Write().ToHtmlString();
}
}
}
}

View File

@@ -8,9 +8,8 @@ using System.Web;
namespace umbraco.MacroEngines
{
[Obsolete("This interface has been superceded by Umbraco.Core.IHtmlTagWrapper")]
public interface HtmlTagWrapperBase
[Obsolete("This interface has been superceded by Umbraco.Web.Mvc.IHtmlTagWrapper")]
public interface HtmlTagWrapperBase : Umbraco.Web.Mvc.IHtmlTagWrapper
{
void WriteToHtmlTextWriter(HtmlTextWriter html);
}
}

View File

@@ -6,18 +6,15 @@ using System.Text;
namespace umbraco.MacroEngines
{
[Obsolete("This interface has been superceded by Umbraco.Core.HtmlTagWrapperTextNode")]
public class HtmlTagWrapperTextNode : HtmlTagWrapperBase
[Obsolete("This interface has been superceded by Umbraco.Web.Mvc.HtmlTagWrapperTextNode")]
public class HtmlTagWrapperTextNode : Umbraco.Web.Mvc.HtmlTagWrapperTextNode
{
public string content;
public HtmlTagWrapperTextNode(string content)
: base(content)
{
this.content = content;
}
public void WriteToHtmlTextWriter(System.Web.UI.HtmlTextWriter html)
{
html.Write(content);
}
}
}

View File

@@ -193,99 +193,43 @@ namespace umbraco.MacroEngines.Library
return _umbracoHelper.If(test, valueIfTrue);
}
public HtmlTagWrapper Wrap(string tag, string innerText, params HtmlTagWrapperBase[] Children)
public Umbraco.Web.Mvc.HtmlTagWrapper Wrap(string tag, string innerText, params Umbraco.Web.Mvc.IHtmlTagWrapper[] children)
{
var item = Wrap(tag, innerText, (object)null);
foreach (var child in Children)
{
item.AddChild(child);
}
return item;
return _umbracoHelper.Wrap(tag, innerText, children);
}
public HtmlTagWrapper Wrap(string tag, string innerText)
public Umbraco.Web.Mvc.HtmlTagWrapper Wrap(string tag, string innerText)
{
return Wrap(tag, innerText, (object)null);
}
public HtmlTagWrapper Wrap(string tag, object inner, object anonymousAttributes)
{
string innerText = null;
if (inner != null && inner.GetType() != typeof(DynamicNull))
{
innerText = string.Format("{0}", inner);
}
return Wrap(tag, innerText, anonymousAttributes);
}
public Umbraco.Web.Mvc.HtmlTagWrapper Wrap(string tag, object inner, object anonymousAttributes)
{
return _umbracoHelper.Wrap(tag, inner, anonymousAttributes);
}
public Umbraco.Web.Mvc.HtmlTagWrapper Wrap(string tag, object inner, object anonymousAttributes, params Umbraco.Web.Mvc.IHtmlTagWrapper[] children)
{
return _umbracoHelper.Wrap(tag, inner, anonymousAttributes, children);
}
public Umbraco.Web.Mvc.HtmlTagWrapper Wrap(string tag, object inner)
{
return _umbracoHelper.Wrap(tag, inner);
}
public Umbraco.Web.Mvc.HtmlTagWrapper Wrap(string tag, string innerText, object anonymousAttributes)
{
return _umbracoHelper.Wrap(tag, innerText, anonymousAttributes);
}
public Umbraco.Web.Mvc.HtmlTagWrapper Wrap(string tag, string innerText, object anonymousAttributes, params Umbraco.Web.Mvc.IHtmlTagWrapper[] children)
{
return _umbracoHelper.Wrap(tag, innerText, anonymousAttributes, children);
}
public Umbraco.Web.Mvc.HtmlTagWrapper Wrap(bool visible, string tag, string innerText, object anonymousAttributes)
{
return _umbracoHelper.Wrap(visible, tag, innerText, anonymousAttributes);
}
public Umbraco.Web.Mvc.HtmlTagWrapper Wrap(bool visible, string tag, string innerText, object anonymousAttributes, params Umbraco.Web.Mvc.IHtmlTagWrapper[] children)
{
return _umbracoHelper.Wrap(visible, tag, innerText, anonymousAttributes, children);
}
public HtmlTagWrapper Wrap(string tag, object inner, object anonymousAttributes, params HtmlTagWrapperBase[] Children)
{
string innerText = null;
if (inner != null && inner.GetType() != typeof(DynamicNull))
{
innerText = string.Format("{0}", inner);
}
var item = Wrap(tag, innerText, anonymousAttributes);
foreach (var child in Children)
{
item.AddChild(child);
}
return item;
}
public HtmlTagWrapper Wrap(string tag, object inner)
{
string innerText = null;
if (inner != null && inner.GetType() != typeof(DynamicNull))
{
innerText = string.Format("{0}", inner);
}
return Wrap(tag, innerText, null);
}
public HtmlTagWrapper Wrap(string tag, string innerText, object anonymousAttributes)
{
HtmlTagWrapper wrap = new HtmlTagWrapper(tag);
if (anonymousAttributes != null)
{
wrap.ReflectAttributesFromAnonymousType(anonymousAttributes);
}
if (!string.IsNullOrWhiteSpace(innerText))
{
wrap.Children.Add(new HtmlTagWrapperTextNode(innerText));
}
return wrap;
}
public HtmlTagWrapper Wrap(string tag, string innerText, object anonymousAttributes, params HtmlTagWrapperBase[] Children)
{
HtmlTagWrapper wrap = new HtmlTagWrapper(tag);
if (anonymousAttributes != null)
{
wrap.ReflectAttributesFromAnonymousType(anonymousAttributes);
}
if (!string.IsNullOrWhiteSpace(innerText))
{
wrap.Children.Add(new HtmlTagWrapperTextNode(innerText));
}
foreach (var child in Children)
{
wrap.AddChild(child);
}
return wrap;
}
public HtmlTagWrapper Wrap(bool visible, string tag, string innerText, object anonymousAttributes)
{
var item = Wrap(tag, innerText, anonymousAttributes);
item.Visible = visible;
return item;
}
public HtmlTagWrapper Wrap(bool visible, string tag, string innerText, object anonymousAttributes, params HtmlTagWrapperBase[] Children)
{
var item = Wrap(tag, innerText, anonymousAttributes, Children);
item.Visible = visible;
foreach (var child in Children)
{
item.AddChild(child);
}
return item;
}
public IHtmlString Truncate(IHtmlString html, int length)
{
return Truncate(html.ToHtmlString(), length, true, false);