diff --git a/src/Umbraco.Tests/HtmlHelperExtensionMethodsTests.cs b/src/Umbraco.Tests/HtmlHelperExtensionMethodsTests.cs new file mode 100644 index 0000000000..5efabd891e --- /dev/null +++ b/src/Umbraco.Tests/HtmlHelperExtensionMethodsTests.cs @@ -0,0 +1,33 @@ +using System.Web.Mvc; +using NUnit.Framework; +using Umbraco.Web; + +namespace Umbraco.Tests +{ + [TestFixture] + public class HtmlHelperExtensionMethodsTests + { + [SetUp] + public virtual void Initialize() + { + //create an empty htmlHelper + _htmlHelper = new HtmlHelper(new ViewContext(), new ViewPage()); + } + + private HtmlHelper _htmlHelper; + + [Test] + public void Wrap_Simple() + { + var output = _htmlHelper.Wrap("div", "hello world"); + Assert.AreEqual("
hello world
", output.ToHtmlString()); + } + + [Test] + public void Wrap_Object_Attributes() + { + var output = _htmlHelper.Wrap("div", "hello world", new {style = "color:red;", onclick = "void();"}); + Assert.AreEqual("
hello world
", output.ToHtmlString()); + } + } +} \ No newline at end of file diff --git a/src/Umbraco.Tests/StringExtensionsTests.cs b/src/Umbraco.Tests/StringExtensionsTests.cs index 73e828ac73..49f8f830d7 100644 --- a/src/Umbraco.Tests/StringExtensionsTests.cs +++ b/src/Umbraco.Tests/StringExtensionsTests.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.Security; using System.Text; -using System.Web.Mvc; using NUnit.Framework; using Umbraco.Core; diff --git a/src/Umbraco.Tests/Umbraco.Tests.csproj b/src/Umbraco.Tests/Umbraco.Tests.csproj index 2d5c4776d1..b924753e6c 100644 --- a/src/Umbraco.Tests/Umbraco.Tests.csproj +++ b/src/Umbraco.Tests/Umbraco.Tests.csproj @@ -55,6 +55,7 @@ + diff --git a/src/Umbraco.Web.UI/Web.config b/src/Umbraco.Web.UI/Web.config index 3dd62de184..a860662208 100644 --- a/src/Umbraco.Web.UI/Web.config +++ b/src/Umbraco.Web.UI/Web.config @@ -36,7 +36,7 @@ - + diff --git a/src/Umbraco.Web/HtmlHelperRenderExtensions.cs b/src/Umbraco.Web/HtmlHelperRenderExtensions.cs index e91f754140..f353a07214 100644 --- a/src/Umbraco.Web/HtmlHelperRenderExtensions.cs +++ b/src/Umbraco.Web/HtmlHelperRenderExtensions.cs @@ -6,6 +6,7 @@ using System.Text; using System.Web.Mvc; using System.Web.Mvc.Html; using Umbraco.Core; +using Umbraco.Core.Dynamics; using Umbraco.Web.Mvc; using umbraco; @@ -16,59 +17,6 @@ namespace Umbraco.Web /// public static class HtmlHelperRenderExtensions { - /// - /// Used for rendering out the Form for BeginUmbracoForm - /// - internal class UmbracoForm : MvcForm - { - /// - /// Creates an UmbracoForm - /// - /// - /// - /// - /// - /// - public UmbracoForm( - ViewContext viewContext, - string surfaceController, - string surfaceAction, - string area, - object additionalRouteVals = null) - : base(viewContext) - { - //need to create a params string as Base64 to put into our hidden field to use during the routes - var surfaceRouteParams = string.Format("c={0}&a={1}&ar={2}", - viewContext.HttpContext.Server.UrlEncode(surfaceController), - viewContext.HttpContext.Server.UrlEncode(surfaceAction), - area); - - var additionalRouteValsAsQuery = additionalRouteVals.ToDictionary().ToQueryString(); - if (!additionalRouteValsAsQuery.IsNullOrWhiteSpace()) - surfaceRouteParams = "&" + additionalRouteValsAsQuery; - - _base64String = Convert.ToBase64String(Encoding.UTF8.GetBytes(surfaceRouteParams)); - - _textWriter = viewContext.Writer; - } - - - private bool _disposed; - private readonly string _base64String; - private readonly TextWriter _textWriter; - - protected override void Dispose(bool disposing) - { - if (this._disposed) - return; - this._disposed = true; - - //write out the hidden surface form routes - _textWriter.Write(""); - - base.Dispose(disposing); - } - } public static MvcHtmlString EditorFor(this HtmlHelper htmlHelper, string templateName = "", string htmlFieldName = "", object additionalViewData = null) where T : new() @@ -108,6 +56,63 @@ namespace Umbraco.Web return filteredHtmlHelper.ValidationSummary(excludePropertyErrors, message, htmlAttributes); } + #region BeginUmbracoForm + + /// + /// Used for rendering out the Form for BeginUmbracoForm + /// + internal class UmbracoForm : MvcForm + { + /// + /// Creates an UmbracoForm + /// + /// + /// + /// + /// + /// + public UmbracoForm( + ViewContext viewContext, + string surfaceController, + string surfaceAction, + string area, + object additionalRouteVals = null) + : base(viewContext) + { + //need to create a params string as Base64 to put into our hidden field to use during the routes + var surfaceRouteParams = string.Format("c={0}&a={1}&ar={2}", + viewContext.HttpContext.Server.UrlEncode(surfaceController), + viewContext.HttpContext.Server.UrlEncode(surfaceAction), + area); + + var additionalRouteValsAsQuery = additionalRouteVals.ToDictionary().ToQueryString(); + if (!additionalRouteValsAsQuery.IsNullOrWhiteSpace()) + surfaceRouteParams = "&" + additionalRouteValsAsQuery; + + _base64String = Convert.ToBase64String(Encoding.UTF8.GetBytes(surfaceRouteParams)); + + _textWriter = viewContext.Writer; + } + + + private bool _disposed; + private readonly string _base64String; + private readonly TextWriter _textWriter; + + protected override void Dispose(bool disposing) + { + if (this._disposed) + return; + this._disposed = true; + + //write out the hidden surface form routes + _textWriter.Write(""); + + base.Dispose(disposing); + } + } + + /// /// Helper method to create a new form to execute in the Umbraco request pipeline against a locally declared controller /// @@ -143,8 +148,8 @@ namespace Umbraco.Web /// /// public static MvcForm BeginUmbracoForm(this HtmlHelper html, string action, string controllerName, - object additionalRouteVals, - object htmlAttributes) + object additionalRouteVals, + object htmlAttributes) { return html.BeginUmbracoForm(action, controllerName, additionalRouteVals, htmlAttributes.ToDictionary()); } @@ -159,11 +164,11 @@ namespace Umbraco.Web /// /// public static MvcForm BeginUmbracoForm(this HtmlHelper html, string action, string controllerName, - object additionalRouteVals, - IDictionary htmlAttributes) + object additionalRouteVals, + IDictionary htmlAttributes) { Mandate.ParameterNotNullOrEmpty(action, "action"); - Mandate.ParameterNotNullOrEmpty(controllerName, "controllerName"); + Mandate.ParameterNotNullOrEmpty(controllerName, "controllerName"); var area = Umbraco.Core.Configuration.GlobalSettings.UmbracoMvcArea; return html.BeginUmbracoForm(action, controllerName, area, additionalRouteVals, htmlAttributes); @@ -189,9 +194,9 @@ namespace Umbraco.Web /// /// public static MvcForm BeginUmbracoForm(this HtmlHelper html, string action) - where T: SurfaceController + where T : SurfaceController { - return html.BeginUmbracoForm(action, typeof (T)); + return html.BeginUmbracoForm(action, typeof(T)); } /// @@ -203,7 +208,7 @@ namespace Umbraco.Web /// /// public static MvcForm BeginUmbracoForm(this HtmlHelper html, string action, Type surfaceType, - object additionalRouteVals) + object additionalRouteVals) { return html.BeginUmbracoForm(action, surfaceType, additionalRouteVals, new Dictionary()); } @@ -219,7 +224,7 @@ namespace Umbraco.Web public static MvcForm BeginUmbracoForm(this HtmlHelper html, string action, object additionalRouteVals) where T : SurfaceController { - return html.BeginUmbracoForm(action, typeof (T), additionalRouteVals); + return html.BeginUmbracoForm(action, typeof(T), additionalRouteVals); } /// @@ -232,8 +237,8 @@ namespace Umbraco.Web /// /// public static MvcForm BeginUmbracoForm(this HtmlHelper html, string action, Type surfaceType, - object additionalRouteVals, - object htmlAttributes) + object additionalRouteVals, + object htmlAttributes) { return html.BeginUmbracoForm(action, surfaceType, additionalRouteVals, htmlAttributes.ToDictionary()); } @@ -247,12 +252,12 @@ namespace Umbraco.Web /// /// /// - public static MvcForm BeginUmbracoForm(this HtmlHelper html, string action, - object additionalRouteVals, - object htmlAttributes) - where T: SurfaceController + public static MvcForm BeginUmbracoForm(this HtmlHelper html, string action, + object additionalRouteVals, + object htmlAttributes) + where T : SurfaceController { - return html.BeginUmbracoForm(action, typeof (T), additionalRouteVals, htmlAttributes); + return html.BeginUmbracoForm(action, typeof(T), additionalRouteVals, htmlAttributes); } /// @@ -265,9 +270,9 @@ namespace Umbraco.Web /// /// public static MvcForm BeginUmbracoForm(this HtmlHelper html, string action, Type surfaceType, - object additionalRouteVals, - IDictionary htmlAttributes) - { + object additionalRouteVals, + IDictionary htmlAttributes) + { Mandate.ParameterNotNullOrEmpty(action, "action"); Mandate.ParameterNotNull(surfaceType, "surfaceType"); @@ -279,7 +284,7 @@ namespace Umbraco.Web if (!surfaceController.Metadata.AreaName.IsNullOrWhiteSpace()) { //set the area to the plugin area - area = surfaceController.Metadata.AreaName; + area = surfaceController.Metadata.AreaName; } return html.BeginUmbracoForm(action, surfaceController.Metadata.ControllerName, area, additionalRouteVals, htmlAttributes); } @@ -293,12 +298,12 @@ namespace Umbraco.Web /// /// /// - public static MvcForm BeginUmbracoForm(this HtmlHelper html, string action, - object additionalRouteVals, - IDictionary htmlAttributes) - where T: SurfaceController + public static MvcForm BeginUmbracoForm(this HtmlHelper html, string action, + object additionalRouteVals, + IDictionary htmlAttributes) + where T : SurfaceController { - return html.BeginUmbracoForm(action, typeof (T), additionalRouteVals, htmlAttributes); + return html.BeginUmbracoForm(action, typeof(T), additionalRouteVals, htmlAttributes); } /// @@ -339,13 +344,13 @@ namespace Umbraco.Web /// This code is pretty much the same as the underlying MVC code that writes out the form /// private static MvcForm RenderForm(this HtmlHelper htmlHelper, - string formAction, - FormMethod method, - IDictionary htmlAttributes, - string surfaceController, - string surfaceAction, - string area, - object additionalRouteVals = null) + string formAction, + FormMethod method, + IDictionary htmlAttributes, + string surfaceController, + string surfaceAction, + string area, + object additionalRouteVals = null) { var tagBuilder = new TagBuilder("form"); @@ -372,6 +377,75 @@ namespace Umbraco.Web return theForm; } + #endregion + + + #region Wrap + + public static HtmlTagWrapper Wrap(this HtmlHelper html, string tag, string innerText, params IHtmlTagWrapper[] children) + { + var item = html.Wrap(tag, innerText, (object)null); + foreach (var child in children) + { + item.AddChild(child); + } + return item; + } + + public static HtmlTagWrapper Wrap(this HtmlHelper html, 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 = html.Wrap(tag, innerText, anonymousAttributes); + foreach (var child in children) + { + item.AddChild(child); + } + return item; + } + public static HtmlTagWrapper Wrap(this HtmlHelper html, string tag, object inner) + { + string innerText = null; + if (inner != null && inner.GetType() != typeof(DynamicNull)) + { + innerText = string.Format("{0}", inner); + } + return html.Wrap(tag, innerText, (object)null); + } + + public static HtmlTagWrapper Wrap(this HtmlHelper html, 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 static HtmlTagWrapper Wrap(this HtmlHelper html, bool visible, string tag, string innerText, object anonymousAttributes, params IHtmlTagWrapper[] children) + { + var item = html.Wrap(tag, innerText, anonymousAttributes, children); + item.Visible = visible; + foreach (var child in children) + { + item.AddChild(child); + } + return item; + } + + #endregion } } \ No newline at end of file diff --git a/src/Umbraco.Web/IPublishedContentStore.cs b/src/Umbraco.Web/IPublishedContentStore.cs index c1ea212343..9195e8bc7d 100644 --- a/src/Umbraco.Web/IPublishedContentStore.cs +++ b/src/Umbraco.Web/IPublishedContentStore.cs @@ -5,7 +5,7 @@ namespace Umbraco.Web /// /// Defines the methods to access published content /// - public interface IPublishedContentStore : IPublishedStore + internal interface IPublishedContentStore : IPublishedStore { IDocument GetDocumentByRoute(UmbracoContext umbracoContext, string route, bool? hideTopLevelNode = null); IDocument GetDocumentByUrlAlias(UmbracoContext umbracoContext, int rootNodeId, string alias); diff --git a/src/Umbraco.Web/IPublishedMediaStore.cs b/src/Umbraco.Web/IPublishedMediaStore.cs index bedb5cb459..82f8b8e15b 100644 --- a/src/Umbraco.Web/IPublishedMediaStore.cs +++ b/src/Umbraco.Web/IPublishedMediaStore.cs @@ -3,7 +3,7 @@ namespace Umbraco.Web /// /// Defines the methods to access published media /// - public interface IPublishedMediaStore : IPublishedStore + internal interface IPublishedMediaStore : IPublishedStore { } } \ No newline at end of file diff --git a/src/Umbraco.Web/Mvc/PluginController.cs b/src/Umbraco.Web/Mvc/PluginController.cs index 62c51e2172..dd08ba08a7 100644 --- a/src/Umbraco.Web/Mvc/PluginController.cs +++ b/src/Umbraco.Web/Mvc/PluginController.cs @@ -25,16 +25,7 @@ namespace Umbraco.Web.Mvc if (umbracoContext == null) throw new ArgumentNullException("umbracoContext"); UmbracoContext = umbracoContext; InstanceId = Guid.NewGuid(); - } - - public IPublishedContentStore PublishedContentStore - { - get { return PublishedContentStoreResolver.Current.PublishedContentStore; } - } - - public IPublishedMediaStore PublishedMediaStore - { - get { return PublishedMediaStoreResolver.Current.PublishedMediaStore; } + Umbraco = new UmbracoHelper(umbracoContext); } /// @@ -42,6 +33,11 @@ namespace Umbraco.Web.Mvc /// internal Guid InstanceId { get; private set; } + /// + /// Returns an UmbracoHelper object + /// + public UmbracoHelper Umbraco { get; private set; } + /// /// Returns the current UmbracoContext /// diff --git a/src/Umbraco.Web/Mvc/RenderViewPage.cs b/src/Umbraco.Web/Mvc/RenderViewPage.cs index 101d7fba5e..8dcf775672 100644 --- a/src/Umbraco.Web/Mvc/RenderViewPage.cs +++ b/src/Umbraco.Web/Mvc/RenderViewPage.cs @@ -51,8 +51,6 @@ namespace Umbraco.Web.Mvc /// public dynamic CurrentPage { get; private set; } - private ICultureDictionary _cultureDictionary; - /// /// Returns the dictionary value for the key specified /// @@ -60,12 +58,7 @@ namespace Umbraco.Web.Mvc /// public string GetDictionaryValue(string key) { - if (_cultureDictionary == null) - { - var factory = CultureDictionaryFactoryResolver.Current.Factory; - _cultureDictionary = factory.CreateDictionary(); - } - return _cultureDictionary[key]; + return Umbraco.GetDictionaryValue(key); } private UmbracoHelper _helper; diff --git a/src/Umbraco.Web/UmbracoContext.cs b/src/Umbraco.Web/UmbracoContext.cs index 2804c00dc6..916daddc1c 100644 --- a/src/Umbraco.Web/UmbracoContext.cs +++ b/src/Umbraco.Web/UmbracoContext.cs @@ -199,6 +199,22 @@ namespace Umbraco.Web get { return DocumentRequest != null; } } + /// + /// A shortcut to the UmbracoContext's RoutingContext's NiceUrlProvider + /// + /// + /// If the RoutingContext is null, this will throw an exception. + /// + internal NiceUrlProvider NiceUrlProvider + { + get + { + if (RoutingContext == null) + throw new InvalidOperationException("Cannot access the NiceUrlProvider when the UmbracoContext's RoutingContext is null"); + return RoutingContext.NiceUrlProvider; + } + } + /// /// Gets/sets the RoutingContext object /// diff --git a/src/Umbraco.Web/UmbracoHelper.cs b/src/Umbraco.Web/UmbracoHelper.cs index 30f88c5014..3ba60902ca 100644 --- a/src/Umbraco.Web/UmbracoHelper.cs +++ b/src/Umbraco.Web/UmbracoHelper.cs @@ -5,22 +5,27 @@ using System.Linq; using System.Text; using System.Web; using System.Web.Configuration; +using System.Web.Security; using System.Web.UI; using System.Xml.Linq; using System.Xml.XPath; using HtmlAgilityPack; using Umbraco.Core; +using Umbraco.Core.Dictionary; using Umbraco.Core.Dynamics; +using Umbraco.Core.IO; using Umbraco.Core.Models; using Umbraco.Web.Mvc; using umbraco; using System.Collections.Generic; +using umbraco.cms.businesslogic.member; +using umbraco.cms.businesslogic.web; using umbraco.presentation.templateControls; using HtmlTagWrapper = Umbraco.Web.Mvc.HtmlTagWrapper; namespace Umbraco.Web { - + /// /// A helper class that provides many useful methods and functionality for using Umbraco in templates /// @@ -191,7 +196,7 @@ namespace Umbraco.Web var attributesForItem = new AttributeCollectionAdapter( new AttributeCollection( new StateBag())); - foreach(var i in attributes) + foreach (var i in attributes) { attributesForItem.Add(i.Key, i.Value); } @@ -201,7 +206,7 @@ namespace Umbraco.Web Field = fieldAlias, TextIfEmpty = altText, LegacyAttributes = attributesForItem - }; + }; var containerPage = new FormlessPage(); containerPage.Controls.Add(item); @@ -217,8 +222,120 @@ namespace Umbraco.Web #endregion + #region Dictionary + + private ICultureDictionary _cultureDictionary; + + /// + /// Returns the dictionary value for the key specified + /// + /// + /// + public string GetDictionaryValue(string key) + { + if (_cultureDictionary == null) + { + var factory = CultureDictionaryFactoryResolver.Current.Factory; + _cultureDictionary = factory.CreateDictionary(); + } + return _cultureDictionary[key]; + } + + #endregion + + #region Membership + + /// + /// Check if a document object is protected by the "Protect Pages" functionality in umbraco + /// + /// The identifier of the document object to check + /// The full path of the document object to check + /// True if the document object is protected + public bool IsProtected(int documentId, string path) + { + return Access.IsProtected(documentId, path); + } + + /// + /// Check if the current user has access to a document + /// + /// The identifier of the document object to check + /// The full path of the document object to check + /// True if the current user has access or if the current document isn't protected + public bool MemberHasAccess(int nodeId, string path) + { + if (IsProtected(nodeId, path)) + { + return Member.IsLoggedOn() && Access.HasAccess(nodeId, path, Membership.GetUser()); + } + return true; + } + + /// + /// Whether or not the current member is logged in (based on the membership provider) + /// + /// True is the current user is logged in + public bool MemberIsLoggedOn() + { + /* + MembershipUser u = Membership.GetUser(); + return u != null; + */ + return Member.IsLoggedOn(); + } + + #endregion + + #region NiceUrls + + /// + /// Returns a string with a friendly url from a node. + /// IE.: Instead of having /482 (id) as an url, you can have + /// /screenshots/developer/macros (spoken url) + /// + /// Identifier for the node that should be returned + /// String with a friendly url from a node + public string NiceUrl(int nodeId) + { + var niceUrlsProvider = UmbracoContext.Current.NiceUrlProvider; + return niceUrlsProvider.GetNiceUrl(nodeId); + } + + /// + /// This method will always add the domain to the path if the hostnames are set up correctly. + /// + /// Identifier for the node that should be returned + /// String with a friendly url with full domain from a node + public string NiceUrlWithDomain(int nodeId) + { + var niceUrlsProvider = UmbracoContext.Current.NiceUrlProvider; + return niceUrlsProvider.GetNiceUrl(nodeId, UmbracoContext.Current.UmbracoUrl, true); + } + + #endregion + #region Content + public IDocument GetContentById(int id) + { + return GetDocumentById(id, PublishedContentStoreResolver.Current.PublishedContentStore); + } + + public IDocument GetContentById(string id) + { + return GetDocumentById(id, PublishedContentStoreResolver.Current.PublishedContentStore); + } + + public IEnumerable GetContentByIds(params int[] ids) + { + return GetDocumentByIds(PublishedContentStoreResolver.Current.PublishedContentStore, ids); + } + + public IEnumerable GetContentByIds(params string[] ids) + { + return GetDocumentByIds(PublishedContentStoreResolver.Current.PublishedContentStore, ids); + } + public dynamic ContentById(int id) { return DocumentById(id, PublishedContentStoreResolver.Current.PublishedContentStore); @@ -238,11 +355,31 @@ namespace Umbraco.Web { return DocumentByIds(PublishedContentStoreResolver.Current.PublishedContentStore, ids); } - + #endregion #region Media + public IDocument GetMediaById(int id) + { + return GetDocumentById(id, PublishedMediaStoreResolver.Current.PublishedMediaStore); + } + + public IDocument GetMediaById(string id) + { + return GetDocumentById(id, PublishedMediaStoreResolver.Current.PublishedMediaStore); + } + + public IEnumerable GetMediaByIds(params int[] ids) + { + return GetDocumentByIds(PublishedMediaStoreResolver.Current.PublishedMediaStore, ids); + } + + public IEnumerable GetMediaByIds(params string[] ids) + { + return GetDocumentByIds(PublishedMediaStoreResolver.Current.PublishedMediaStore, ids); + } + public dynamic MediaById(int id) { return DocumentById(id, PublishedMediaStoreResolver.Current.PublishedMediaStore); @@ -267,6 +404,29 @@ namespace Umbraco.Web #region Used by Content/Media + private IDocument GetDocumentById(int id, IPublishedStore store) + { + return store.GetDocumentById(UmbracoContext.Current, id); + } + + private IDocument GetDocumentById(string id, IPublishedStore store) + { + int docId; + return int.TryParse(id, out docId) + ? DocumentById(docId, store) + : null; + } + + private IEnumerable GetDocumentByIds(IPublishedStore store, params int[] ids) + { + return ids.Select(eachId => GetDocumentById(eachId, store)); + } + + private IEnumerable GetDocumentByIds(IPublishedStore store, params string[] ids) + { + return ids.Select(eachId => GetDocumentById(eachId, store)); + } + private dynamic DocumentById(int id, IPublishedStore store) { var doc = store.GetDocumentById(UmbracoContext.Current, id); @@ -278,8 +438,8 @@ namespace Umbraco.Web private dynamic DocumentById(string id, IPublishedStore store) { int docId; - return int.TryParse(id, out docId) - ? DocumentById(docId, store) + return int.TryParse(id, out docId) + ? DocumentById(docId, store) : new DynamicNull(); } @@ -361,6 +521,29 @@ namespace Umbraco.Web #region Strings + /// + /// Replaces text line breaks with html line breaks + /// + /// The text. + /// The text with text line breaks replaced with html linebreaks (
)
+ public string ReplaceLineBreaksForHtml(string text) + { + if (bool.Parse(Umbraco.Core.Configuration.GlobalSettings.EditXhtmlMode)) + return text.Replace("\n", "
\n"); + else + return text.Replace("\n", "
\n"); + } + + /// + /// Returns an MD5 hash of the string specified + /// + /// The text to create a hash from + /// Md5 has of the string + public string CreateMd5Hash(string text) + { + return text.ToMd5(); + } + public HtmlString StripHtml(IHtmlString html, params string[] tags) { return StripHtml(html.ToHtmlString(), tags); @@ -445,7 +628,7 @@ namespace Umbraco.Web internal string Join(string seperator, params object[] args) { - var results = args.Where(arg => arg != null && arg.GetType() != typeof (TIgnore)).Select(arg => string.Format("{0}", arg)).Where(sArg => !string.IsNullOrWhiteSpace(sArg)).ToList(); + var results = args.Where(arg => arg != null && arg.GetType() != typeof(TIgnore)).Select(arg => string.Format("{0}", arg)).Where(sArg => !string.IsNullOrWhiteSpace(sArg)).ToList(); return string.Join(seperator, results); } @@ -645,71 +828,6 @@ 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, 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, (object)null); - } - - 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, 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.Web/umbraco.presentation/library.cs b/src/Umbraco.Web/umbraco.presentation/library.cs index fb4ebc887b..53049cf2c3 100644 --- a/src/Umbraco.Web/umbraco.presentation/library.cs +++ b/src/Umbraco.Web/umbraco.presentation/library.cs @@ -9,8 +9,8 @@ using System.Web; using System.Web.UI; using System.Xml; using System.Xml.XPath; - - +using Umbraco.Core; +using Umbraco.Web; using umbraco.BusinessLogic; using umbraco.cms.businesslogic; using umbraco.cms.businesslogic.media; @@ -25,11 +25,11 @@ using umbraco.DataLayer; using System.Web.Security; using umbraco.cms.businesslogic.language; using umbraco.IO; -using umbraco.presentation; using System.Collections; using System.Collections.Generic; using umbraco.cms.businesslogic.cache; using umbraco.NodeFactory; +using UmbracoContext = umbraco.presentation.UmbracoContext; namespace umbraco { @@ -42,6 +42,14 @@ namespace umbraco ///
public class library { + /// + /// Returns a new UmbracoHelper so that we can start moving the logic from some of these methods to it + /// + /// + private static UmbracoHelper GetUmbracoHelper() + { + return new UmbracoHelper(Umbraco.Web.UmbracoContext.Current); + } #region Declarations @@ -370,8 +378,7 @@ namespace umbraco /// String with a friendly url from a node public static string NiceUrl(int nodeID) { - var niceUrlsProvider = Umbraco.Web.UmbracoContext.Current.RoutingContext.NiceUrlProvider; - return niceUrlsProvider.GetNiceUrl(nodeID); + return GetUmbracoHelper().NiceUrl(nodeID); } /// @@ -393,8 +400,7 @@ namespace umbraco /// String with a friendly url with full domain from a node public static string NiceUrlWithDomain(int nodeID) { - var niceUrlsProvider = Umbraco.Web.UmbracoContext.Current.RoutingContext.NiceUrlProvider; - return niceUrlsProvider.GetNiceUrl(nodeID, Umbraco.Web.UmbracoContext.Current.UmbracoUrl, true); + return GetUmbracoHelper().NiceUrlWithDomain(nodeID); } /// @@ -413,7 +419,7 @@ namespace umbraco public static string ResolveVirtualPath(string path) { - return IOHelper.ResolveUrl(path); + return Umbraco.Core.IO.IOHelper.ResolveUrl(path); } @@ -659,12 +665,7 @@ namespace umbraco /// True is the current user is logged in public static bool IsLoggedOn() { - /* - MembershipUser u = Membership.GetUser(); - return u != null; - */ - - return Member.IsLoggedOn(); + return GetUmbracoHelper().MemberIsLoggedOn(); } public static XPathNodeIterator AllowedGroups(int documentId, string path) @@ -684,7 +685,7 @@ namespace umbraco /// True if the document object is protected public static bool IsProtected(int DocumentId, string Path) { - return Access.IsProtected(DocumentId, Path); + return GetUmbracoHelper().IsProtected(DocumentId, Path); } /// @@ -695,39 +696,18 @@ namespace umbraco /// True if the current user has access or if the current document isn't protected public static bool HasAccess(int NodeId, string Path) { - if (IsProtected(NodeId, Path)) - { - if (Member.IsLoggedOn()) - return Access.HasAccess(NodeId, Path, Membership.GetUser()); - else - return false; - } - else - return true; - + return GetUmbracoHelper().MemberHasAccess(NodeId, Path); } /// - /// Encrypts the string using md5 + /// Returns an MD5 hash of the string specified /// - /// The text. - /// Md5 encrupted string + /// The text to create a hash from + /// Md5 has of the string public static string md5(string text) { - System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider(); - byte[] bs = System.Text.Encoding.UTF8.GetBytes(text); - - bs = x.ComputeHash(bs); - - System.Text.StringBuilder s = new System.Text.StringBuilder(); - - foreach (byte b in bs) - { - s.Append(b.ToString("x2").ToLower()); - } - - return s.ToString(); + return text.ToMd5(); } /// @@ -1060,10 +1040,7 @@ namespace umbraco /// The text with text line breaks replaced with html linebreaks (
)
public static string ReplaceLineBreaks(string text) { - if (bool.Parse(GlobalSettings.EditXhtmlMode)) - return text.Replace("\n", "
\n"); - else - return text.Replace("\n", "
\n"); + return GetUmbracoHelper().ReplaceLineBreaksForHtml(text); } /// diff --git a/src/umbraco.MacroEngines/RazorCore/RazorMacroEngine.cs b/src/umbraco.MacroEngines/RazorCore/RazorMacroEngine.cs index 7ea5cc9928..e30d8c5dca 100644 --- a/src/umbraco.MacroEngines/RazorCore/RazorMacroEngine.cs +++ b/src/umbraco.MacroEngines/RazorCore/RazorMacroEngine.cs @@ -6,6 +6,7 @@ using System.Threading; using System.Web; using System.Web.Compilation; using System.Web.WebPages; +using Umbraco.Core; using umbraco.cms.businesslogic.macro; using umbraco.interfaces; using umbraco.IO; @@ -24,14 +25,7 @@ namespace umbraco.MacroEngines } public string GetMd5(string text) { - var x = new System.Security.Cryptography.MD5CryptoServiceProvider(); - var bs = System.Text.Encoding.UTF8.GetBytes(text); - bs = x.ComputeHash(bs); - var s = new System.Text.StringBuilder(); - foreach (var b in bs) { - s.Append(b.ToString("x2").ToLower()); - } - return s.ToString(); + return text.ToMd5(); } /// diff --git a/src/umbraco.MacroEngines/RazorDynamicNode/RazorLibraryCore.cs b/src/umbraco.MacroEngines/RazorDynamicNode/RazorLibraryCore.cs index 6c59eb4244..7239330fb9 100644 --- a/src/umbraco.MacroEngines/RazorDynamicNode/RazorLibraryCore.cs +++ b/src/umbraco.MacroEngines/RazorDynamicNode/RazorLibraryCore.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using System.Web.Mvc; using Umbraco.Core.Dynamics; using Umbraco.Web; using umbraco.interfaces; @@ -17,6 +18,12 @@ namespace umbraco.MacroEngines.Library { private readonly INode _node; private readonly UmbracoHelper _umbracoHelper; + + /// + /// An empty HtmlHelper with a blank ViewContext, used only to access some htmlHelper extension methods + /// + private readonly HtmlHelper _htmlHelper; + public INode Node { get { return _node; } @@ -25,6 +32,7 @@ namespace umbraco.MacroEngines.Library { this._node = node; _umbracoHelper = new UmbracoHelper(UmbracoContext.Current); + _htmlHelper = new HtmlHelper(new ViewContext(), new ViewPage()); } public dynamic NodeById(int Id) @@ -194,24 +202,24 @@ namespace umbraco.MacroEngines.Library } public Umbraco.Web.Mvc.HtmlTagWrapper Wrap(string tag, string innerText, params Umbraco.Web.Mvc.IHtmlTagWrapper[] children) - { - return _umbracoHelper.Wrap(tag, innerText, children); + { + return _htmlHelper.Wrap(tag, innerText, children); } 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); + return _htmlHelper.Wrap(tag, inner, anonymousAttributes, children); } public Umbraco.Web.Mvc.HtmlTagWrapper Wrap(string tag, object inner) { - return _umbracoHelper.Wrap(tag, inner); + return _htmlHelper.Wrap(tag, inner); } 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); + return _htmlHelper.Wrap(tag, innerText, anonymousAttributes, children); } 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); + return _htmlHelper.Wrap(visible, tag, innerText, anonymousAttributes, children); } public IHtmlString Truncate(IHtmlString html, int length)