Skip to content

Commit

Permalink
Fixed issue rendering nested empty placeholders (#6)
Browse files Browse the repository at this point in the history
Currently, empty Placeholders are not rendering the same HTML that JSS
does. This PR updates the SDK's TagHelper to generate HTML that matches
the same output from JSS.

## Description / Motivation
When a Placeholder is rendered without any components loaded inside of
it, it needs to be wrapped in a div to be correctly rendered in Pages
and Experience Editor. The div being created by for this SDK is `<div
className=\"sc-empty-placeholder\"></div>`

## Testing
- [x] The Unit & Intergration tests are passing.
- [x] I have added the necesary tests to cover my changes.

## Terms
- [x] I agree to follow this project's [Code of
Conduct](CODE_OF_CONDUCT.md).

---------

Co-authored-by: Ivan Lieckens <[email protected]>
  • Loading branch information
robearlam and IvanLieckens authored Aug 8, 2024
1 parent b6387b9 commit 4967f4b
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,13 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu
return;
}

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

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

if (emptyEdit)
{
output.Content.AppendHtml("</div>");
}

if (!foundPlaceholderFeatures)
{
output.Content.SetHtmlContent($"<!-- {string.Format(Resources.Warning_PlaceholderWasEmpty, placeholderName)} -->");
}
}

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 4967f4b

Please sign in to comment.