diff --git a/src/Umbraco.Core/HtmlTagWrapperBase.cs b/src/Umbraco.Core/HtmlTagWrapperBase.cs deleted file mode 100644 index c6c3172708..0000000000 --- a/src/Umbraco.Core/HtmlTagWrapperBase.cs +++ /dev/null @@ -1,9 +0,0 @@ -using System.Web.UI; - -namespace Umbraco.Core -{ - internal interface IHtmlTagWrapper - { - void WriteToHtmlTextWriter(HtmlTextWriter html); - } -} diff --git a/src/Umbraco.Core/Umbraco.Core.csproj b/src/Umbraco.Core/Umbraco.Core.csproj index 9a56378674..20bd19baf1 100644 --- a/src/Umbraco.Core/Umbraco.Core.csproj +++ b/src/Umbraco.Core/Umbraco.Core.csproj @@ -92,9 +92,6 @@ - - - diff --git a/src/Umbraco.Core/HtmlTagWrapper.cs b/src/Umbraco.Web/Mvc/HtmlTagWrapper.cs similarity index 77% rename from src/Umbraco.Core/HtmlTagWrapper.cs rename to src/Umbraco.Web/Mvc/HtmlTagWrapper.cs index 9543f276c3..7a187afb57 100644 --- a/src/Umbraco.Core/HtmlTagWrapper.cs +++ b/src/Umbraco.Web/Mvc/HtmlTagWrapper.cs @@ -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 Children; - public List> Attributes; - public void ReflectAttributesFromAnonymousType(List> newAttributes) + + private readonly List _children; + public IEnumerable Children + { + get { return _children; } + } + + private List> _attributes; + public IEnumerable> Attributes + { + get { return _attributes; } + } + + public void ReflectAttributesFromAnonymousType(List> newAttributes) { List> mergedAttributes = newAttributes @@ -20,7 +32,7 @@ namespace Umbraco.Core .Select(g => new KeyValuePair(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(); + this._children = new List(); this.CssClasses = new List(); - this.Attributes = new List>(); + this._attributes = new List>(); 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> newAttributes = new List>(); - newAttributes.Add(new KeyValuePair(name, value)); - this.ReflectAttributesFromAnonymousType(newAttributes); + var newAttributes = new List> {new KeyValuePair(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() { diff --git a/src/Umbraco.Core/HtmlTagWrapperTextNode.cs b/src/Umbraco.Web/Mvc/HtmlTagWrapperTextNode.cs similarity index 59% rename from src/Umbraco.Core/HtmlTagWrapperTextNode.cs rename to src/Umbraco.Web/Mvc/HtmlTagWrapperTextNode.cs index f70fd3e577..d6d82512d0 100644 --- a/src/Umbraco.Core/HtmlTagWrapperTextNode.cs +++ b/src/Umbraco.Web/Mvc/HtmlTagWrapperTextNode.cs @@ -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; diff --git a/src/Umbraco.Web/Mvc/IHtmlTagWrapper.cs b/src/Umbraco.Web/Mvc/IHtmlTagWrapper.cs new file mode 100644 index 0000000000..2535b78d90 --- /dev/null +++ b/src/Umbraco.Web/Mvc/IHtmlTagWrapper.cs @@ -0,0 +1,9 @@ +using System.Web.UI; + +namespace Umbraco.Web.Mvc +{ + public interface IHtmlTagWrapper + { + void WriteToHtmlTextWriter(HtmlTextWriter html); + } +} diff --git a/src/Umbraco.Web/Umbraco.Web.csproj b/src/Umbraco.Web/Umbraco.Web.csproj index e9b3d54ddd..5b9357b022 100644 --- a/src/Umbraco.Web/Umbraco.Web.csproj +++ b/src/Umbraco.Web/Umbraco.Web.csproj @@ -264,6 +264,9 @@ + + + diff --git a/src/Umbraco.Web/UmbracoHelper.cs b/src/Umbraco.Web/UmbracoHelper.cs index e4bc97ba8a..8f95267b1a 100644 --- a/src/Umbraco.Web/UmbracoHelper.cs +++ b/src/Umbraco.Web/UmbracoHelper.cs @@ -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 } } diff --git a/src/umbraco.MacroEngines/RazorDynamicNode/HtmlTagWrapper.cs b/src/umbraco.MacroEngines/RazorDynamicNode/HtmlTagWrapper.cs index d7debac607..d1f7f05df5 100644 --- a/src/umbraco.MacroEngines/RazorDynamicNode/HtmlTagWrapper.cs +++ b/src/umbraco.MacroEngines/RazorDynamicNode/HtmlTagWrapper.cs @@ -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 Children; - public List> Attributes; - public void ReflectAttributesFromAnonymousType(List> newAttributes) + public HtmlTagWrapper(string tag) + : base(tag) { - 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; - } - 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( - prop.Name, - string.Format("{0}", prop.GetValue(anonymousAttributes, null)) - ) - ); - ReflectAttributesFromAnonymousType(newAttributes); - - } - - public List CssClasses; - public string Tag; - public bool Visible; - - public HtmlTagWrapper(string Tag) - { - this.Tag = Tag; - this.Children = new List(); - this.CssClasses = new List(); - this.Attributes = new List>(); - 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> 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; - } - - 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(); - } + + } } } diff --git a/src/umbraco.MacroEngines/RazorDynamicNode/HtmlTagWrapperBase.cs b/src/umbraco.MacroEngines/RazorDynamicNode/HtmlTagWrapperBase.cs index 3b98b999d9..05e58a4dfa 100644 --- a/src/umbraco.MacroEngines/RazorDynamicNode/HtmlTagWrapperBase.cs +++ b/src/umbraco.MacroEngines/RazorDynamicNode/HtmlTagWrapperBase.cs @@ -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); } } diff --git a/src/umbraco.MacroEngines/RazorDynamicNode/HtmlTagWrapperTextNode.cs b/src/umbraco.MacroEngines/RazorDynamicNode/HtmlTagWrapperTextNode.cs index 7d50309a33..03520c8e56 100644 --- a/src/umbraco.MacroEngines/RazorDynamicNode/HtmlTagWrapperTextNode.cs +++ b/src/umbraco.MacroEngines/RazorDynamicNode/HtmlTagWrapperTextNode.cs @@ -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); - } } } diff --git a/src/umbraco.MacroEngines/RazorDynamicNode/RazorLibraryCore.cs b/src/umbraco.MacroEngines/RazorDynamicNode/RazorLibraryCore.cs index e2983c2685..0e8b082d7d 100644 --- a/src/umbraco.MacroEngines/RazorDynamicNode/RazorLibraryCore.cs +++ b/src/umbraco.MacroEngines/RazorDynamicNode/RazorLibraryCore.cs @@ -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);