From aebec3bfedc26c24aa31da0c04c3239372fd9281 Mon Sep 17 00:00:00 2001 From: Rob Earlam Date: Wed, 7 Aug 2024 16:44:54 +1000 Subject: [PATCH] Fixed logic for adding empty Placeholder wrappers, updated related tests --- .../TagHelpers/PlaceholderTagHelper.cs | 21 ++++++++- .../TagHelpers/PlaceholderTagHelperFixture.cs | 47 +++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/src/Sitecore.AspNetCore.SDK.RenderingEngine/TagHelpers/PlaceholderTagHelper.cs b/src/Sitecore.AspNetCore.SDK.RenderingEngine/TagHelpers/PlaceholderTagHelper.cs index 93b876a..529b11e 100644 --- a/src/Sitecore.AspNetCore.SDK.RenderingEngine/TagHelpers/PlaceholderTagHelper.cs +++ b/src/Sitecore.AspNetCore.SDK.RenderingEngine/TagHelpers/PlaceholderTagHelper.cs @@ -70,8 +70,12 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu return; } - bool foundPlaceholderFeatures = false; + if (IsInEditingMode(renderingContext) && IsPlaceHolderEmpty(placeholderFeatures)) + { + output.Content.AppendHtml("
"); + } + bool foundPlaceholderFeatures = false; foreach (IPlaceholderFeature placeholderFeature in placeholderFeatures.OfType()) { foundPlaceholderFeatures = true; @@ -98,12 +102,27 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu output.Content.AppendHtml(html); } + if (IsInEditingMode(renderingContext) && IsPlaceHolderEmpty(placeholderFeatures)) + { + output.Content.AppendHtml("
"); + } + if (!foundPlaceholderFeatures) { output.Content.SetHtmlContent($"
"); } } + private static bool IsInEditingMode(ISitecoreRenderingContext renderingContext) + { + return renderingContext?.Response?.Content?.Sitecore?.Context?.IsEditing ?? false; + } + + private static bool IsPlaceHolderEmpty(Placeholder placeholderFeatures) + { + return !placeholderFeatures.Exists(x => x is Component); + } + private static Placeholder? GetPlaceholderFeatures(string placeholderName, ISitecoreRenderingContext renderingContext) { Placeholder? placeholderFeatures = null; diff --git a/tests/Sitecore.AspNetCore.SDK.RenderingEngine.Tests/TagHelpers/PlaceholderTagHelperFixture.cs b/tests/Sitecore.AspNetCore.SDK.RenderingEngine.Tests/TagHelpers/PlaceholderTagHelperFixture.cs index 99ab560..17612e2 100644 --- a/tests/Sitecore.AspNetCore.SDK.RenderingEngine.Tests/TagHelpers/PlaceholderTagHelperFixture.cs +++ b/tests/Sitecore.AspNetCore.SDK.RenderingEngine.Tests/TagHelpers/PlaceholderTagHelperFixture.cs @@ -476,6 +476,53 @@ public async Task ProcessAsync_PlaceholderContainsUnknownPlaceholderFeature_Outp tagHelperOutput.Content.GetContent().Should().BeEmpty(); } + [Theory] + [AutoNSubstituteData] + public async Task ProcessAsync_PlaceholderContainsUnknownPlaceholderFeature_IsInEditingMode_OutputIsEditingWrapperTag( + PlaceholderTagHelper sut, + ViewContext viewContext, + TagHelperContext tagHelperContext, + TagHelperOutput tagHelperOutput) + { + // Arrange + SitecoreRenderingContext context = new() + { + Response = new SitecoreLayoutResponse([]) + { + Content = new SitecoreLayoutResponseContent + { + Sitecore = new SitecoreData + { + Context = new Context { IsEditing = true }, + Route = new Route + { + Placeholders = + { + [PlaceHolderWithComponentsName] = + [ + new TestPlaceholderFeature + { + Content = TestComponentRenderer.HtmlContent + } + ] + } + } + } + } + } + }; + + viewContext.HttpContext.SetSitecoreRenderingContext(context); + sut.Name = PlaceHolderWithComponentsName; + sut.ViewContext = viewContext; + + // Act + await sut.ProcessAsync(tagHelperContext, tagHelperOutput); + + // Assert + tagHelperOutput.Content.GetContent().Should().Be("
"); + } + [Theory] [AutoNSubstituteData] public async Task ProcessAsync_PlaceholderNameInLayoutServiceResponseAndPlaceholderIsNotEmpty_ContextComponentDoNotChange(