Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issue rendering nested empty placeholders #6

Merged
merged 5 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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