LinkOrDefault now returns IHtmlString. Replaced calls to <%= with <%: where relevant to this change.

--HG--
branch : dev
This commit is contained in:
Phil Haack
2010-06-09 23:39:02 -07:00
parent 013638aca8
commit 65772415fd
6 changed files with 96 additions and 24 deletions

View File

@@ -35,6 +35,20 @@ namespace Orchard.Tests.Mvc.Html {
Assert.AreEqual(@"<a href=""http://example.com"">&lt;br /&gt;</a>", result.ToString());
}
[Test]
public void LinkHtmlAttributeEncodesHref() {
//arrange
var viewContext = new ViewContext();
var viewDataContainer = new Mock<IViewDataContainer>();
var html = new HtmlHelper(viewContext, viewDataContainer.Object);
//act
var result = html.Link("test", "<br />");
//assert
Assert.AreEqual(@"<a href=""&lt;br />"">test</a>", result.ToString());
}
[Test]
public void LinkHtmlAttributeEncodesAttributes() {
//arrange
@@ -49,6 +63,62 @@ namespace Orchard.Tests.Mvc.Html {
Assert.AreEqual(@"<a href=""http://example.com"" title=""&lt;br />"">linkText</a>", result.ToString());
}
[Test]
public void LinkOrDefaultReturnsIHtmlString() {
//arrange
var viewContext = new ViewContext();
var viewDataContainer = new Mock<IViewDataContainer>();
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<IViewDataContainer>();
var html = new HtmlHelper(viewContext, viewDataContainer.Object);
//act
var result = html.LinkOrDefault("<br />", "http://example.com");
//assert
Assert.AreEqual(@"<a href=""http://example.com"">&lt;br /&gt;</a>", result.ToString());
}
[Test]
public void LinkOrDefaultWithoutHrefHtmlEncodesLinkText() {
//arrange
var viewContext = new ViewContext();
var viewDataContainer = new Mock<IViewDataContainer>();
var html = new HtmlHelper(viewContext, viewDataContainer.Object);
//act
var result = html.LinkOrDefault("<br />", null);
//assert
Assert.AreEqual(@"&lt;br /&gt;", result.ToString());
}
[Test]
public void LinkOrDefaultWithHrefHtmlAttributeEncodesHref() {
//arrange
var viewContext = new ViewContext();
var viewDataContainer = new Mock<IViewDataContainer>();
var html = new HtmlHelper(viewContext, viewDataContainer.Object);
//act
var result = html.LinkOrDefault("test", "<br />");
//assert
Assert.AreEqual(@"<a href=""&lt;br />"">test</a>", result.ToString());
}
[Test]
public void SelectOptionHtmlEncodesText() {
//arrange

View File

@@ -4,7 +4,7 @@
foreach (var comment in Model) { %>
<li>
<div class="comment">
<span class="who"><%=Html.LinkOrDefault(Html.Encode(comment.Record.UserName), Html.Encode(comment.Record.SiteName), new { rel = "nofollow" })%></span>
<span class="who"><%: Html.LinkOrDefault(comment.Record.UserName, comment.Record.SiteName, new { rel = "nofollow" })%></span>
<%-- todo: (heskew) need comment permalink --%>
<span>said <%: Html.Link(Html.DateTimeRelative(comment.Record.CommentDateUtc.GetValueOrDefault()), "#")%></span>
</div>

View File

@@ -9,7 +9,7 @@ foreach (var comment in Model) { %>
</div>
<div class="commentauthor">
<span class="who"><%=Html.LinkOrDefault(Html.Encode(comment.Record.UserName), Html.Encode(comment.Record.SiteName), new { rel = "nofollow" })%></span>&nbsp;<span>said <%: Html.Link(Html.DateTimeRelative(comment.Record.CommentDateUtc.GetValueOrDefault()), "#")%></span>
<span class="who"><%: Html.LinkOrDefault(comment.Record.UserName, comment.Record.SiteName, new { rel = "nofollow" })%></span>&nbsp;<span>said <%: Html.Link(Html.DateTimeRelative(comment.Record.CommentDateUtc.GetValueOrDefault()), "#")%></span>
</div>
</li><%

View File

@@ -9,7 +9,7 @@ foreach (var comment in Model) { %>
</div>
<div class="commentauthor">
<span class="who"><%=Html.LinkOrDefault(Html.Encode(comment.Record.UserName), Html.Encode(comment.Record.SiteName), new { rel = "nofollow" })%></span>&nbsp;<span>said <%: Html.Link(Html.DateTimeRelative(comment.Record.CommentDateUtc.GetValueOrDefault()), "#")%></span>
<span class="who"><%: Html.LinkOrDefault(comment.Record.UserName, comment.Record.SiteName, new { rel = "nofollow" })%></span>&nbsp;<span>said <%: Html.Link(Html.DateTimeRelative(comment.Record.CommentDateUtc.GetValueOrDefault()), "#")%></span>
</div>
</li><%

View File

@@ -9,7 +9,7 @@ foreach (var comment in Model) { %>
</div>
<div class="commentauthor">
<span class="who"><%=Html.LinkOrDefault(Html.Encode(comment.Record.UserName), Html.Encode(comment.Record.SiteName), new { rel = "nofollow" })%></span>&nbsp;<span>said <%: Html.Link(Html.DateTimeRelative(comment.Record.CommentDateUtc.GetValueOrDefault()), "#")%></span>
<span class="who"><%: Html.LinkOrDefault(comment.Record.UserName, comment.Record.SiteName, new { rel = "nofollow" })%></span>&nbsp;<span>said <%: Html.Link(Html.DateTimeRelative(comment.Record.CommentDateUtc.GetValueOrDefault()), "#")%></span>
</div>
</li><%

View File

@@ -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<string, object> htmlAttributes)
public static IHtmlString Link(this HtmlHelper htmlHelper, string linkContents, string href, IDictionary<string, object> 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<string, object> htmlAttributes)
public static IHtmlString LinkOrDefault(this HtmlHelper htmlHelper, string linkContents, string href, IDictionary<string, object> 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<string, object> htmlAttributes) {
public static IHtmlString AntiForgeryTokenValueOrchardLink(this HtmlHelper htmlHelper, string linkContents, string href, IDictionary<string, object> htmlAttributes) {
return htmlHelper.Link(linkContents, htmlHelper.AntiForgeryTokenGetUrl(href), htmlAttributes);
}