diff --git a/src/Orchard.Tests/Mvc/Html/HtmlHelperExtensionsTests.cs b/src/Orchard.Tests/Mvc/Html/HtmlHelperExtensionsTests.cs index ff50b7d0f..fbaca012f 100644 --- a/src/Orchard.Tests/Mvc/Html/HtmlHelperExtensionsTests.cs +++ b/src/Orchard.Tests/Mvc/Html/HtmlHelperExtensionsTests.cs @@ -35,6 +35,20 @@ namespace Orchard.Tests.Mvc.Html { Assert.AreEqual(@"<br />", result.ToString()); } + [Test] + public void LinkHtmlAttributeEncodesHref() { + //arrange + var viewContext = new ViewContext(); + var viewDataContainer = new Mock(); + var html = new HtmlHelper(viewContext, viewDataContainer.Object); + + //act + var result = html.Link("test", "
"); + + //assert + Assert.AreEqual(@""">test", result.ToString()); + } + [Test] public void LinkHtmlAttributeEncodesAttributes() { //arrange @@ -49,6 +63,62 @@ namespace Orchard.Tests.Mvc.Html { Assert.AreEqual(@""">linkText", result.ToString()); } + [Test] + public void LinkOrDefaultReturnsIHtmlString() { + //arrange + var viewContext = new ViewContext(); + var viewDataContainer = new Mock(); + var html = new HtmlHelper(viewContext, viewDataContainer.Object); + + //act + var result = html.LinkOrDefault("test", "http://example.com") as IHtmlString; + + //assert + Assert.IsNotNull(result); + } + + [Test] + public void LinkOrDefaultHtmlEncodesLinkText() { + //arrange + var viewContext = new ViewContext(); + var viewDataContainer = new Mock(); + var html = new HtmlHelper(viewContext, viewDataContainer.Object); + + //act + var result = html.LinkOrDefault("
", "http://example.com"); + + //assert + Assert.AreEqual(@"<br />", result.ToString()); + } + + [Test] + public void LinkOrDefaultWithoutHrefHtmlEncodesLinkText() { + //arrange + var viewContext = new ViewContext(); + var viewDataContainer = new Mock(); + var html = new HtmlHelper(viewContext, viewDataContainer.Object); + + //act + var result = html.LinkOrDefault("
", null); + + //assert + Assert.AreEqual(@"<br />", result.ToString()); + } + + [Test] + public void LinkOrDefaultWithHrefHtmlAttributeEncodesHref() { + //arrange + var viewContext = new ViewContext(); + var viewDataContainer = new Mock(); + var html = new HtmlHelper(viewContext, viewDataContainer.Object); + + //act + var result = html.LinkOrDefault("test", "
"); + + //assert + Assert.AreEqual(@""">test", result.ToString()); + } + [Test] public void SelectOptionHtmlEncodesText() { //arrange diff --git a/src/Orchard.Web/Modules/Orchard.Comments/Views/ListOfComments.ascx b/src/Orchard.Web/Modules/Orchard.Comments/Views/ListOfComments.ascx index 308e7cabd..7e93f370e 100644 --- a/src/Orchard.Web/Modules/Orchard.Comments/Views/ListOfComments.ascx +++ b/src/Orchard.Web/Modules/Orchard.Comments/Views/ListOfComments.ascx @@ -4,7 +4,7 @@ foreach (var comment in Model) { %>
  • - <%=Html.LinkOrDefault(Html.Encode(comment.Record.UserName), Html.Encode(comment.Record.SiteName), new { rel = "nofollow" })%> + <%: Html.LinkOrDefault(comment.Record.UserName, comment.Record.SiteName, new { rel = "nofollow" })%> <%-- todo: (heskew) need comment permalink --%> said <%: Html.Link(Html.DateTimeRelative(comment.Record.CommentDateUtc.GetValueOrDefault()), "#")%>
    diff --git a/src/Orchard.Web/Themes/Contoso/Views/ListOfComments.ascx b/src/Orchard.Web/Themes/Contoso/Views/ListOfComments.ascx index 30c41d925..c8096dcf6 100644 --- a/src/Orchard.Web/Themes/Contoso/Views/ListOfComments.ascx +++ b/src/Orchard.Web/Themes/Contoso/Views/ListOfComments.ascx @@ -9,7 +9,7 @@ foreach (var comment in Model) { %>
    -<%=Html.LinkOrDefault(Html.Encode(comment.Record.UserName), Html.Encode(comment.Record.SiteName), new { rel = "nofollow" })%> said <%: Html.Link(Html.DateTimeRelative(comment.Record.CommentDateUtc.GetValueOrDefault()), "#")%> +<%: Html.LinkOrDefault(comment.Record.UserName, comment.Record.SiteName, new { rel = "nofollow" })%> said <%: Html.Link(Html.DateTimeRelative(comment.Record.CommentDateUtc.GetValueOrDefault()), "#")%>
  • <% diff --git a/src/Orchard.Web/Themes/Corporate/Views/ListOfComments.ascx b/src/Orchard.Web/Themes/Corporate/Views/ListOfComments.ascx index 30c41d925..c8096dcf6 100644 --- a/src/Orchard.Web/Themes/Corporate/Views/ListOfComments.ascx +++ b/src/Orchard.Web/Themes/Corporate/Views/ListOfComments.ascx @@ -9,7 +9,7 @@ foreach (var comment in Model) { %>
    -<%=Html.LinkOrDefault(Html.Encode(comment.Record.UserName), Html.Encode(comment.Record.SiteName), new { rel = "nofollow" })%> said <%: Html.Link(Html.DateTimeRelative(comment.Record.CommentDateUtc.GetValueOrDefault()), "#")%> +<%: Html.LinkOrDefault(comment.Record.UserName, comment.Record.SiteName, new { rel = "nofollow" })%> said <%: Html.Link(Html.DateTimeRelative(comment.Record.CommentDateUtc.GetValueOrDefault()), "#")%>
    <% diff --git a/src/Orchard.Web/Themes/Green/Views/ListOfComments.ascx b/src/Orchard.Web/Themes/Green/Views/ListOfComments.ascx index 30c41d925..c8096dcf6 100644 --- a/src/Orchard.Web/Themes/Green/Views/ListOfComments.ascx +++ b/src/Orchard.Web/Themes/Green/Views/ListOfComments.ascx @@ -9,7 +9,7 @@ foreach (var comment in Model) { %>
    -<%=Html.LinkOrDefault(Html.Encode(comment.Record.UserName), Html.Encode(comment.Record.SiteName), new { rel = "nofollow" })%> said <%: Html.Link(Html.DateTimeRelative(comment.Record.CommentDateUtc.GetValueOrDefault()), "#")%> +<%: Html.LinkOrDefault(comment.Record.UserName, comment.Record.SiteName, new { rel = "nofollow" })%> said <%: Html.Link(Html.DateTimeRelative(comment.Record.CommentDateUtc.GetValueOrDefault()), "#")%>
    <% diff --git a/src/Orchard/Mvc/Html/HtmlHelperExtensions.cs b/src/Orchard/Mvc/Html/HtmlHelperExtensions.cs index 6ed2c19b7..ba6c95122 100644 --- a/src/Orchard/Mvc/Html/HtmlHelperExtensions.cs +++ b/src/Orchard/Mvc/Html/HtmlHelperExtensions.cs @@ -12,6 +12,7 @@ using Orchard.Services; using Orchard.Settings; using Orchard.Utility; using Orchard.Utility.Extensions; +using System.Web; namespace Orchard.Mvc.Html { public static class HtmlHelperExtensions { @@ -241,53 +242,54 @@ namespace Orchard.Mvc.Html { #region Link - public static MvcHtmlString Link(this HtmlHelper htmlHelper, string linkContents, string href) + public static IHtmlString Link(this HtmlHelper htmlHelper, string linkContents, string href) { return htmlHelper.Link(linkContents, href, null); } - public static MvcHtmlString Link(this HtmlHelper htmlHelper, string linkContents, string href, object htmlAttributes) + public static IHtmlString Link(this HtmlHelper htmlHelper, string linkContents, string href, object htmlAttributes) { return htmlHelper.Link(linkContents, href, new RouteValueDictionary(htmlAttributes)); } - public static MvcHtmlString Link(this HtmlHelper htmlHelper, string linkContents, string href, IDictionary htmlAttributes) + public static IHtmlString Link(this HtmlHelper htmlHelper, string linkContents, string href, IDictionary htmlAttributes) { TagBuilder tagBuilder = new TagBuilder("a") { InnerHtml = htmlHelper.Encode(linkContents) }; tagBuilder.MergeAttributes(htmlAttributes); tagBuilder.MergeAttribute("href", href); - return MvcHtmlString.Create(tagBuilder.ToString(TagRenderMode.Normal)); + return new HtmlString(tagBuilder.ToString(TagRenderMode.Normal)); } #endregion #region LinkOrDefault - public static string LinkOrDefault(this HtmlHelper htmlHelper, string linkContents, string href) + public static IHtmlString LinkOrDefault(this HtmlHelper htmlHelper, string linkContents, string href) { return htmlHelper.LinkOrDefault(linkContents, href, null); } - public static string LinkOrDefault(this HtmlHelper htmlHelper, string linkContents, string href, object htmlAttributes) + public static IHtmlString LinkOrDefault(this HtmlHelper htmlHelper, string linkContents, string href, object htmlAttributes) { return htmlHelper.LinkOrDefault(linkContents, href, new RouteValueDictionary(htmlAttributes)); } - public static string LinkOrDefault(this HtmlHelper htmlHelper, string linkContents, string href, IDictionary htmlAttributes) + public static IHtmlString LinkOrDefault(this HtmlHelper htmlHelper, string linkContents, string href, IDictionary htmlAttributes) { - if (!string.IsNullOrEmpty(href)) - { - TagBuilder tagBuilder = new TagBuilder("a") - { - InnerHtml = linkContents - }; - tagBuilder.MergeAttributes(htmlAttributes); - tagBuilder.MergeAttribute("href", href); - linkContents = tagBuilder.ToString(TagRenderMode.Normal); + string linkText = htmlHelper.Encode(linkContents); + + if (string.IsNullOrEmpty(href)) { + return new HtmlString(linkText); } - return linkContents; + TagBuilder tagBuilder = new TagBuilder("a") + { + InnerHtml = linkText + }; + tagBuilder.MergeAttributes(htmlAttributes); + tagBuilder.MergeAttribute("href", href); + return new HtmlString(tagBuilder.ToString(TagRenderMode.Normal)); } #endregion @@ -336,15 +338,15 @@ namespace Orchard.Mvc.Html { #region AntiForgeryTokenValueOrchardLink - public static MvcHtmlString AntiForgeryTokenValueOrchardLink(this HtmlHelper htmlHelper, string linkContents, string href) { + public static IHtmlString AntiForgeryTokenValueOrchardLink(this HtmlHelper htmlHelper, string linkContents, string href) { return htmlHelper.AntiForgeryTokenValueOrchardLink(linkContents, href, (object)null); } - public static MvcHtmlString AntiForgeryTokenValueOrchardLink(this HtmlHelper htmlHelper, string linkContents, string href, object htmlAttributes) { + public static IHtmlString AntiForgeryTokenValueOrchardLink(this HtmlHelper htmlHelper, string linkContents, string href, object htmlAttributes) { return htmlHelper.AntiForgeryTokenValueOrchardLink(linkContents, href, new RouteValueDictionary(htmlAttributes)); } - public static MvcHtmlString AntiForgeryTokenValueOrchardLink(this HtmlHelper htmlHelper, string linkContents, string href, IDictionary htmlAttributes) { + public static IHtmlString AntiForgeryTokenValueOrchardLink(this HtmlHelper htmlHelper, string linkContents, string href, IDictionary htmlAttributes) { return htmlHelper.Link(linkContents, htmlHelper.AntiForgeryTokenGetUrl(href), htmlAttributes); }