mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 11:44:58 +08:00
LinkOrDefault now returns IHtmlString. Replaced calls to <%= with <%: where relevant to this change.
--HG-- branch : dev
This commit is contained in:
@@ -35,6 +35,20 @@ namespace Orchard.Tests.Mvc.Html {
|
||||
Assert.AreEqual(@"<a href=""http://example.com""><br /></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=""<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=""<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""><br /></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(@"<br />", 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=""<br />"">test</a>", result.ToString());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SelectOptionHtmlEncodesText() {
|
||||
//arrange
|
||||
|
@@ -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>
|
||||
|
@@ -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> <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> <span>said <%: Html.Link(Html.DateTimeRelative(comment.Record.CommentDateUtc.GetValueOrDefault()), "#")%></span>
|
||||
</div>
|
||||
|
||||
</li><%
|
||||
|
@@ -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> <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> <span>said <%: Html.Link(Html.DateTimeRelative(comment.Record.CommentDateUtc.GetValueOrDefault()), "#")%></span>
|
||||
</div>
|
||||
|
||||
</li><%
|
||||
|
@@ -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> <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> <span>said <%: Html.Link(Html.DateTimeRelative(comment.Record.CommentDateUtc.GetValueOrDefault()), "#")%></span>
|
||||
</div>
|
||||
|
||||
</li><%
|
||||
|
@@ -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);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user