Updated UnorderedList to return IHtmlString and changed the relevant <%= blocks to <%:

--HG--
branch : dev
This commit is contained in:
Phil Haack
2010-06-10 00:07:26 -07:00
parent 65772415fd
commit 4bd6fbb866
30 changed files with 62 additions and 35 deletions

View File

@@ -3,6 +3,7 @@ using System.Web.Mvc;
using Moq;
using NUnit.Framework;
using Orchard.Mvc.Html;
using System.Collections.Generic;
namespace Orchard.Tests.Mvc.Html {
[TestFixture]
@@ -133,6 +134,32 @@ namespace Orchard.Tests.Mvc.Html {
Assert.AreEqual(@"<option value=""value"">&lt;br /&gt;</option>", result.ToString());
}
[Test]
public void UnorderedListWithNullItemsReturnsEmptyHtmlString() {
//arrange
var viewContext = new ViewContext();
var viewDataContainer = new Mock<IViewDataContainer>();
var html = new HtmlHelper(viewContext, viewDataContainer.Object);
//act
var result = html.UnorderedList((IEnumerable<string>)null, (a, b) => "", "test");
//assert
Assert.AreEqual(string.Empty, result.ToString());
}
[Test]
public void UnorderedListWithEmptyItemsReturnsEmptyHtmlString() {
//arrange
var viewContext = new ViewContext();
var viewDataContainer = new Mock<IViewDataContainer>();
var html = new HtmlHelper(viewContext, viewDataContainer.Object);
//act
var result = html.UnorderedList(new string[]{}, (a, b) => "", "test");
//assert
Assert.AreEqual(string.Empty, result.ToString());
}
}
}

View File

@@ -1,5 +1,5 @@
<%@ Page Language="C#" Inherits="Orchard.Mvc.ViewPage<Orchard.Core.Contents.ViewModels.DisplayItemViewModel>" %>
<div class="preview">
<%=Html.DisplayForItem(m=>m.Content) %>
<%: Html.DisplayForItem(m=>m.Content) %>
</div>

View File

@@ -1,3 +1,3 @@
<%@ Page Language="C#" Inherits="Orchard.Mvc.ViewPage<Orchard.Core.Contents.ViewModels.DisplayItemViewModel>" %>
<%=Html.DisplayForItem(m=>m.Content) %>
<%: Html.DisplayForItem(m=>m.Content) %>

View File

@@ -1,3 +1,3 @@
<%@ Page Language="C#" Inherits="Orchard.Mvc.ViewPage<Orchard.Core.Routable.ViewModels.RoutableDisplayViewModel>" %>
<% Html.AddTitleParts(Model.Routable.Item.Title); %>
<%=Html.DisplayForItem(m=>m.Routable) %>
<%: Html.DisplayForItem(m=>m.Routable) %>

View File

@@ -3,7 +3,7 @@
Html.RegisterStyle("admin.css"); %>
<h1><%:Html.TitleForPage(T("Manage Settings").ToString()) %></h1>
<h2><%:T("Cultures this site supports") %></h2>
<%=Html.UnorderedList(
<%: Html.UnorderedList(
Model.SiteCultures.OrderBy(s => s),
(s, i) => Html.DisplayFor(scvm => s, s == Model.CurrentCulture ? "CurrentCulture" : "RemovableCulture", "").ToString(),
"site-cultures", "culture", "odd")%>

View File

@@ -23,13 +23,13 @@
<li class="previous">
<h4><%=year %> <span>(<%=yearMonths.Sum(ym => ym.Value) %>)</span></h4><%
} %>
<%=Html.UnorderedList(yearMonths, (t, i) => Html.Link(string.Format("{0:MMMM} ({1})", t.Key.ToDateTime(), t.Value), Url.BlogArchiveMonth(Model.Blog.Slug, t.Key.Year, t.Key.Month)), "archiveMonthList") %>
<%: Html.UnorderedList(yearMonths, (t, i) => Html.Link(string.Format("{0:MMMM} ({1})", t.Key.ToDateTime(), t.Value), Url.BlogArchiveMonth(Model.Blog.Slug, t.Key.Year, t.Key.Month)), "archiveMonthList") %>
</li><%
} %>
</ul><%
}
else { %>
<%=Html.UnorderedList(Model.Archives, (t, i) => Html.Link(string.Format("{0:MMMM yyyy} ({1})", t.Key.ToDateTime(), t.Value), Url.BlogArchiveMonth(Model.Blog.Slug, t.Key.Year, t.Key.Month)), "archiveMonthList") %><%
<%: Html.UnorderedList(Model.Archives, (t, i) => Html.Link(string.Format("{0:MMMM yyyy} ({1})", t.Key.ToDateTime(), t.Value), Url.BlogArchiveMonth(Model.Blog.Slug, t.Key.Year, t.Key.Month)), "archiveMonthList") %><%
}
}
else { %>

View File

@@ -1,3 +1,3 @@
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<BlogViewModel>" %>
<%@ Import Namespace="Orchard.Blogs.ViewModels"%>
<%=Html.DisplayForItem(m => m.Blog) %>
<%: Html.DisplayForItem(m => m.Blog) %>

View File

@@ -1,7 +1,7 @@
<%@ Control Language="C#" Inherits="Orchard.Mvc.ViewUserControl<BlogsViewModel>" %>
<%@ Import Namespace="Orchard.Blogs.ViewModels"%>
<%if (Model.Blogs.Count() > 0) { %>
<%=Html.UnorderedList(Model.Blogs, (b, i) => Html.DisplayForItem(b).ToHtmlString(), "blogs contentItems") %><%
<%: Html.UnorderedList(Model.Blogs, (b, i) => Html.DisplayForItem(b).ToHtmlString(), "blogs contentItems") %><%
}
else { %>
<p><%: T("No blogs found.") %></p><%

View File

@@ -1,4 +1,4 @@
<%@ Control Language="C#" Inherits="Orchard.Mvc.ViewUserControl<BlogForAdminViewModel>" %>
<%@ Import Namespace="Orchard.Blogs.ViewModels"%>
<% Html.AddTitleParts(T("Manage Blog").ToString()); %>
<%=Html.DisplayForItem(m => m.Blog) %>
<%: Html.DisplayForItem(m => m.Blog) %>

View File

@@ -5,7 +5,7 @@
<%-- todo: Add helper text here when ready. <p><%: T("Possible text about setting up and managing a blog goes here.") %></p> --%><%
if (Model.Entries.Count() > 0) { %>
<div class="actions"><a class="add button primaryAction" href="<%=Url.BlogCreate() %>"><%: T("New Blog") %></a></div>
<%=Html.UnorderedList(Model.Entries, (entry, i) => {
<%: Html.UnorderedList(Model.Entries, (entry, i) => {
// Add blog post count rendering into "meta" zone
entry.ContentItemViewModel.Zones.AddAction("meta", html => {
int draftCount = entry.TotalPostCount - entry.ContentItemViewModel.Item.PostCount;

View File

@@ -1,4 +1,4 @@
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<BlogPostViewModel>" %>
<%@ Import Namespace="Orchard.Blogs.ViewModels"%>
<% Html.AddTitleParts(Model.Blog.Name); %>
<%=Html.DisplayForItem(m => m.BlogPost) %>
<%: Html.DisplayForItem(m => m.BlogPost) %>

View File

@@ -11,4 +11,4 @@
%>
</div>
<%=Html.UnorderedList(Model.BlogPosts, (c, i) => Html.DisplayForItem(c).ToHtmlString(), "blogPosts contentItems")%>
<%: Html.UnorderedList(Model.BlogPosts, (c, i) => Html.DisplayForItem(c).ToHtmlString(), "blogPosts contentItems")%>

View File

@@ -1,5 +1,5 @@
<%@ Control Language="C#" Inherits="Orchard.Mvc.ViewUserControl<IEnumerable<ContentItemViewModel<BlogPost>>>" %>
<%@ Import Namespace="Orchard.Mvc.ViewModels"%>
<%@ Import Namespace="Orchard.Blogs.Models"%>
<%=Html.UnorderedList(Model, (bp, i) => Html.DisplayForItem(bp).ToHtmlString(), "blogPosts contentItems") %>
<%: Html.UnorderedList(Model, (bp, i) => Html.DisplayForItem(bp).ToHtmlString(), "blogPosts contentItems") %>
<% if (Model.Count() < 1) { %><div class="info message"><%: T("There are no posts for this blog.") %></div><% } %>

View File

@@ -79,7 +79,7 @@
Zone:<%: display.ZoneName ?? "(null)" %>
Position:<%: display.Position ?? "(null)" %>
<div style="margin-left: 20px; border: solid 1px black;">
<%=Html.DisplayFor(x => display.Model, display.TemplateName, display.Prefix)%>
<%: Html.DisplayFor(x => display.Model, display.TemplateName, display.Prefix)%>
</div>
</li>
<%

View File

@@ -41,7 +41,7 @@
if (feature.Descriptor.Dependencies != null) { %>
<div class="dependencies">
<h4><%: T("Depends on:")%></h4>
<%=Html.UnorderedList(
<%: Html.UnorderedList(
feature.Descriptor.Dependencies.OrderBy(s => s),
(s, i) => Html.Link(s, string.Format("#{0}", s.AsFeatureId(n => T(n)))),
"",

View File

@@ -17,7 +17,7 @@
<div class="related"><%
if (!string.Equals(tenant.Name, "default", StringComparison.OrdinalIgnoreCase)) { //todo: (heskew) base this off the view model so logic on what can be removed and have its state changed stays in the controller
var t = tenant; %>
<%=Html.DisplayFor(m => t, string.Format("ActionsFor{0}", tenant.State.CurrentState), "") %><%: T(" | ")%><%
<%: Html.DisplayFor(m => t, string.Format("ActionsFor{0}", tenant.State.CurrentState), "") %><%: T(" | ")%><%
} %>
<%: Html.ActionLink(T("Edit").ToString(), "Edit", new {name = tenant.Name, area = "Orchard.MultiTenancy"}) %><%
if (!string.Equals(tenant.Name, "default", StringComparison.OrdinalIgnoreCase)) { //todo: (heskew) base this off the view model so logic on what can be removed and have its state changed stays in the controller

View File

@@ -1,4 +1,4 @@
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<PageViewModel>" %>
<%@ Import Namespace="Orchard.Mvc.Html"%>
<%@ Import Namespace="Orchard.Pages.ViewModels"%>
<%=Html.DisplayForItem(m=>m.Page) %>
<%: Html.DisplayForItem(m=>m.Page) %>

View File

@@ -2,4 +2,4 @@
<%@ Import Namespace="Orchard.Sandbox.ViewModels" %>
<h1><%: Html.TitleForPage(T("Sandbox Pages").ToString()) %></h1>
<p><%: Html.ActionLink(T("Add new page").ToString(), "create") %></p>
<%=Html.UnorderedList(Model.Pages, (sp, i) => Html.DisplayForItem(sp).ToHtmlString(), "pages contentItems") %>
<%: Html.UnorderedList(Model.Pages, (sp, i) => Html.DisplayForItem(sp).ToHtmlString(), "pages contentItems") %>

View File

@@ -1,3 +1,3 @@
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<PageShowViewModel>" %>
<%@ Import Namespace="Orchard.Sandbox.ViewModels" %>
<%=Html.DisplayForItem(Model.Page) %>
<%: Html.DisplayForItem(Model.Page) %>

View File

@@ -12,6 +12,6 @@ if (!string.IsNullOrWhiteSpace(Model.Query)) {
}
}
if (Model.PageOfResults != null && Model.PageOfResults.Count() > 0) { %>
<%=Html.UnorderedList(Model.PageOfResults, (r, i) => Html.DisplayForItem(r.Content).ToHtmlString() , "search-results contentItems") %>
<%: Html.UnorderedList(Model.PageOfResults, (r, i) => Html.DisplayForItem(r.Content).ToHtmlString() , "search-results contentItems") %>
<%=Html.Pager(Model.PageOfResults, Model.PageOfResults.PageNumber, Model.DefaultPageSize, new {q = Model.Query}) %><%
} %>

View File

@@ -1,7 +1,7 @@
<%@ Control Language="C#" Inherits="Orchard.Mvc.ViewUserControl<TagsIndexViewModel>" %>
<%@ Import Namespace="Orchard.Tags.ViewModels"%>
<h1 class="page-title"><%: Html.TitleForPage(T("Tags").ToString())%></h1>
<%=Html.UnorderedList(
<%: Html.UnorderedList(
Model.Tags,
(t, i) => Html.ActionLink(
Html.Encode(t.TagName),

View File

@@ -2,4 +2,4 @@
<%@ Import Namespace="Orchard.Tags.ViewModels"%>
<% Html.AddTitleParts(T("Tags").ToString(), T("Contents tagged with {0}", Model.TagName).ToString()); %>
<h1 class="page-title"><%=T("Contents tagged with <span>{0}</span>", Html.Encode(Model.TagName)) %></h1>
<%=Html.UnorderedList(Model.Items, (c, i) => Html.DisplayForItem(c).ToHtmlString(), "taggedPosts contentItems") %>
<%: Html.UnorderedList(Model.Items, (c, i) => Html.DisplayForItem(c).ToHtmlString(), "taggedPosts contentItems") %>

View File

@@ -1,5 +1,5 @@
<%@ Control Language="C#" Inherits="Orchard.Mvc.ViewUserControl<IEnumerable<ContentItemViewModel<BlogPost>>>" %>
<%@ Import Namespace="Orchard.Mvc.ViewModels"%>
<%@ Import Namespace="Orchard.Blogs.Models"%>
<%=Html.UnorderedList(Model, (bp, i) => Html.DisplayForItem(bp).ToHtmlString(), "blogPosts contentItems") %>
<%: Html.UnorderedList(Model, (bp, i) => Html.DisplayForItem(bp).ToHtmlString(), "blogPosts contentItems") %>
<% if (Model.Count() < 1) { %><p><%: T("There are no posts for this blog.") %></p><% } %>

View File

@@ -1,5 +1,5 @@
<%@ Control Language="C#" Inherits="Orchard.Mvc.ViewUserControl<IEnumerable<ContentItemViewModel<BlogPost>>>" %>
<%@ Import Namespace="Orchard.Mvc.ViewModels"%>
<%@ Import Namespace="Orchard.Blogs.Models"%>
<%=Html.UnorderedList(Model, (bp, i) => Html.DisplayForItem(bp).ToHtmlString(), "blogPosts contentItems") %>
<%: Html.UnorderedList(Model, (bp, i) => Html.DisplayForItem(bp).ToHtmlString(), "blogPosts contentItems") %>
<% if (Model.Count() < 1) { %><p><%: T("There are no posts for this blog.") %></p><% } %>

View File

@@ -11,4 +11,4 @@
%>
</div>
<%=Html.UnorderedList(Model.BlogPosts, (c, i) => Html.DisplayForItem(c).ToHtmlString(), "blogPosts contentItems")%>
<%: Html.UnorderedList(Model.BlogPosts, (c, i) => Html.DisplayForItem(c).ToHtmlString(), "blogPosts contentItems")%>

View File

@@ -2,5 +2,5 @@
<%@ Import Namespace="Orchard.Mvc.ViewModels"%>
<%@ Import Namespace="Orchard.Blogs.Models"%>
<%=Html.UnorderedList(Model, (bp, i) => Html.DisplayForItem(bp).ToHtmlString(), "blogPosts contentItems") %>
<%: Html.UnorderedList(Model, (bp, i) => Html.DisplayForItem(bp).ToHtmlString(), "blogPosts contentItems") %>
<% if (Model.Count() < 1) { %><p><%: T("There are no posts for this blog.") %></p><% } %>

View File

@@ -11,4 +11,4 @@
%>
</div>
<%=Html.UnorderedList(Model.BlogPosts, (c, i) => Html.DisplayForItem(c).ToHtmlString(), "blogPosts contentItems")%>
<%: Html.UnorderedList(Model.BlogPosts, (c, i) => Html.DisplayForItem(c).ToHtmlString(), "blogPosts contentItems")%>

View File

@@ -2,5 +2,5 @@
<%@ Import Namespace="Orchard.Mvc.ViewModels"%>
<%@ Import Namespace="Orchard.Blogs.Models"%>
<%=Html.UnorderedList(Model, (bp, i) => Html.DisplayForItem(bp).ToHtmlString(), "blogPosts contentItems") %>
<%: Html.UnorderedList(Model, (bp, i) => Html.DisplayForItem(bp).ToHtmlString(), "blogPosts contentItems") %>
<% if (Model.Count() < 1) { %><p><%: T("There are no posts for this blog.") %></p><% } %>

View File

@@ -23,13 +23,13 @@
<li class="previous">
<h4><%=year %> <span>(<%=yearMonths.Sum(ym => ym.Value) %>)</span></h4><%
} %>
<%=Html.UnorderedList(yearMonths, (t, i) => Html.Link(string.Format("{0:MMMM} ({1})", t.Key.ToDateTime(), t.Value), Url.BlogArchiveMonth(Model.Blog.Slug, t.Key.Year, t.Key.Month)), "archiveMonthList") %>
<%: Html.UnorderedList(yearMonths, (t, i) => Html.Link(string.Format("{0:MMMM} ({1})", t.Key.ToDateTime(), t.Value), Url.BlogArchiveMonth(Model.Blog.Slug, t.Key.Year, t.Key.Month)), "archiveMonthList") %>
</li><%
} %>
</ul><%
}
else { %>
<%=Html.UnorderedList(Model.Archives, (t, i) => Html.Link(string.Format("{0:MMMM yyyy} ({1})", t.Key.ToDateTime(), t.Value), Url.BlogArchiveMonth(Model.Blog.Slug, t.Key.Year, t.Key.Month)), "archiveMonthList") %><%
<%: Html.UnorderedList(Model.Archives, (t, i) => Html.Link(string.Format("{0:MMMM yyyy} ({1})", t.Key.ToDateTime(), t.Value), Url.BlogArchiveMonth(Model.Blog.Slug, t.Key.Year, t.Key.Month)), "archiveMonthList") %><%
}
}
else { %>

View File

@@ -112,16 +112,16 @@ namespace Orchard.Mvc.Html {
#region UnorderedList
public static string UnorderedList<T>(this HtmlHelper htmlHelper, IEnumerable<T> items, Func<T, int, string> generateContent, string cssClass) {
public static IHtmlString UnorderedList<T>(this HtmlHelper htmlHelper, IEnumerable<T> items, Func<T, int, string> generateContent, string cssClass) {
return htmlHelper.UnorderedList(items, generateContent, cssClass, null, null);
}
public static string UnorderedList<T>(this HtmlHelper htmlHelper, IEnumerable<T> items, Func<T, int, string> generateContent, string cssClass, string itemCssClass, string alternatingItemCssClass) {
public static IHtmlString UnorderedList<T>(this HtmlHelper htmlHelper, IEnumerable<T> items, Func<T, int, string> generateContent, string cssClass, string itemCssClass, string alternatingItemCssClass) {
return UnorderedList(items, generateContent, cssClass, t => itemCssClass, t => alternatingItemCssClass);
}
private static string UnorderedList<T>(IEnumerable<T> items, Func<T, int, string> generateContent, string cssClass, Func<T, string> generateItemCssClass, Func<T, string> generateAlternatingItemCssClass) {
if (items == null || items.Count() == 0) return "";
private static IHtmlString UnorderedList<T>(IEnumerable<T> items, Func<T, int, string> generateContent, string cssClass, Func<T, string> generateItemCssClass, Func<T, string> generateAlternatingItemCssClass) {
if (items == null || !items.Any()) return new HtmlString(string.Empty);
var sb = new StringBuilder(250);
int counter = 0, count = items.Count() - 1;
@@ -155,7 +155,7 @@ namespace Orchard.Mvc.Html {
sb.Append("</ul>");
return sb.ToString();
return new HtmlString(sb.ToString());
}
#endregion