Skip to content

Commit

Permalink
Fixed logic for adding empty Placeholder wrappers, updated related tests
Browse files Browse the repository at this point in the history
  • Loading branch information
robearlam committed Aug 7, 2024
1 parent 2b9f5a5 commit aebec3b
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,12 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu
return;
}

bool foundPlaceholderFeatures = false;
if (IsInEditingMode(renderingContext) && IsPlaceHolderEmpty(placeholderFeatures))
{
output.Content.AppendHtml("<div class=\"sc-empty-placeholder\">");
}

bool foundPlaceholderFeatures = false;
foreach (IPlaceholderFeature placeholderFeature in placeholderFeatures.OfType<IPlaceholderFeature>())
{
foundPlaceholderFeatures = true;
Expand All @@ -98,12 +102,27 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu
output.Content.AppendHtml(html);
}

if (IsInEditingMode(renderingContext) && IsPlaceHolderEmpty(placeholderFeatures))
{
output.Content.AppendHtml("</div>");
}

if (!foundPlaceholderFeatures)
{
output.Content.SetHtmlContent($"<div className=\"sc-jss-empty-placeholder\"></div>");
}
}

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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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("<div class=\"sc-empty-placeholder\"></div>");
}

[Theory]
[AutoNSubstituteData]
public async Task ProcessAsync_PlaceholderNameInLayoutServiceResponseAndPlaceholderIsNotEmpty_ContextComponentDoNotChange(
Expand Down

0 comments on commit aebec3b

Please sign in to comment.