From 5880df3859cafa9d0ecb4fa3b7bb18300ab2b072 Mon Sep 17 00:00:00 2001 From: Stephan Date: Wed, 19 Apr 2017 18:56:12 +0200 Subject: [PATCH] U4-9764 - add FirstChild with predicate extension --- src/Umbraco.Web/PublishedContentExtensions.cs | 40 +++++++++++-------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/src/Umbraco.Web/PublishedContentExtensions.cs b/src/Umbraco.Web/PublishedContentExtensions.cs index 7972f109f9..36d1306ab6 100644 --- a/src/Umbraco.Web/PublishedContentExtensions.cs +++ b/src/Umbraco.Web/PublishedContentExtensions.cs @@ -540,7 +540,7 @@ namespace Umbraco.Web var filtered = dynamicDocumentList.Where(predicate); return filtered.Count() == 1; } - + #endregion #region AsDynamic @@ -806,7 +806,7 @@ namespace Umbraco.Web public static HtmlString IsOdd(this IPublishedContent content, string valueIfTrue, string valueIfFalse) { return new HtmlString(content.IsOdd() ? valueIfTrue : valueIfFalse); - } + } #endregion @@ -836,7 +836,7 @@ namespace Umbraco.Web { return content.IsNotEqual(other, valueIfTrue, string.Empty); } - + public static HtmlString IsNotEqual(this IPublishedContent content, IPublishedContent other, string valueIfTrue, string valueIfFalse) { return new HtmlString(content.IsNotEqual(other) ? valueIfTrue : valueIfFalse); @@ -1125,7 +1125,7 @@ namespace Umbraco.Web { return content.Ancestors(maxLevel).FirstOrDefault(); } - + /// /// Gets the content or its nearest ancestor. /// @@ -1186,7 +1186,7 @@ namespace Umbraco.Web { return content.AncestorsOrSelf(maxLevel).FirstOrDefault(); } - + internal static IEnumerable AncestorsOrSelf(this IPublishedContent content, bool orSelf, Func func) { var ancestorsOrSelf = content.EnumerateAncestors(orSelf); @@ -1237,7 +1237,7 @@ namespace Umbraco.Web where T : class, IPublishedContent { return parentNodes.SelectMany(x => x.DescendantsOrSelf()); - } + } // as per XPath 1.0 specs §2.2, @@ -1285,7 +1285,7 @@ namespace Umbraco.Web { return content.Descendants(level).OfType(); } - + public static IEnumerable DescendantsOrSelf(this IPublishedContent content) { return content.DescendantsOrSelf(true, null); @@ -1366,7 +1366,7 @@ namespace Umbraco.Web { return content.DescendantOrSelf(level) as T; } - + internal static IEnumerable DescendantsOrSelf(this IPublishedContent content, bool orSelf, Func func) { return content.EnumerateDescendants(orSelf).Where(x => func == null || func(x)); @@ -1390,7 +1390,7 @@ namespace Umbraco.Web foreach (var child2 in child.EnumerateDescendants()) yield return child2; } - + #endregion #region Axes: following-sibling, preceding-sibling, following, preceding + pseudo-axes up, down, next, previous @@ -1413,8 +1413,8 @@ namespace Umbraco.Web public static IPublishedContent Up(this IPublishedContent content, string contentTypeAlias) { - return string.IsNullOrEmpty(contentTypeAlias) - ? content.Parent + return string.IsNullOrEmpty(contentTypeAlias) + ? content.Parent : content.Ancestor(contentTypeAlias); } @@ -1799,13 +1799,19 @@ namespace Umbraco.Web return content.Children().FirstOrDefault(); } - /// - /// Gets the children of the content in a DataTable. - /// + public static IPublishedContent FirstChild(this IPublishedContent content, Func predicate) + where T : class, IPublishedContent + { + return content.Children().FirstOrDefault(predicate); + } + + /// + /// Gets the children of the content in a DataTable. + /// /// The content. /// An optional content type alias. /// The children of the content. - public static DataTable ChildrenAsTable(this IPublishedContent content, string contentTypeAliasFilter = "") + public static DataTable ChildrenAsTable(this IPublishedContent content, string contentTypeAliasFilter = "") { return GenerateDataTable(content, contentTypeAliasFilter); } @@ -1824,7 +1830,7 @@ namespace Umbraco.Web : null : content.Children.FirstOrDefault(x => x.DocumentTypeAlias == contentTypeAliasFilter); if (firstNode == null) - return new DataTable(); //no children found + return new DataTable(); //no children found //use new utility class to create table so that we don't have to maintain code in many places, just one var dt = Core.DataTableExtensions.GenerateDataTable( @@ -1964,7 +1970,7 @@ namespace Umbraco.Web public static CultureInfo GetCulture(this IPublishedContent content, Uri current = null) { return Models.ContentExtensions.GetCulture(UmbracoContext.Current, - ApplicationContext.Current.Services.DomainService, + ApplicationContext.Current.Services.DomainService, ApplicationContext.Current.Services.LocalizationService, ApplicationContext.Current.Services.ContentService, content.Id, content.Path,