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

Qute: fix rendered=false if a fragment includes nested fragment #44298

Merged
merged 1 commit into from
Nov 5, 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 @@ -19,12 +19,15 @@ public class FragmentSectionHelper implements SectionHelper {

private static final String ID = "id";

// the generated id of the template that declares this fragment section
private final String generatedTemplateId;
private final String identifier;
private final Expression rendered;

FragmentSectionHelper(String identifier, Expression isVisible) {
FragmentSectionHelper(String identifier, Expression rendered, String generatedTemplateId) {
this.identifier = identifier;
this.rendered = isVisible;
this.rendered = rendered;
this.generatedTemplateId = generatedTemplateId;
}

public String getIdentifier() {
Expand All @@ -33,18 +36,25 @@ public String getIdentifier() {

@Override
public CompletionStage<ResultNode> resolve(SectionResolutionContext context) {
if (rendered == null
// executed from an include section
|| context.getParameters().containsKey(Template.Fragment.ATTRIBUTE)
// the attribute is set if executed separately via Template.Fragment
|| context.resolutionContext().getAttribute(Fragment.ATTRIBUTE) != null) {
if (isAlwaysExecuted(context)) {
return context.execute();
}
return context.resolutionContext().evaluate(rendered).thenCompose(r -> {
return Booleans.isFalsy(r) ? ResultNode.NOOP : context.execute();
});
}

private boolean isAlwaysExecuted(SectionResolutionContext context) {
if (rendered == null
// executed from an include section
|| context.getParameters().containsKey(Fragment.ATTRIBUTE)) {
return true;
}
Object attribute = context.resolutionContext().getAttribute(Fragment.ATTRIBUTE);
// the attribute is set if executed separately via Template.Fragment
return attribute != null && attribute.equals(generatedTemplateId + identifier);
}

public static class Factory implements SectionHelperFactory<FragmentSectionHelper> {

static final Pattern FRAGMENT_PATTERN = Pattern.compile("[a-zA-Z0-9_]+");
Expand Down Expand Up @@ -99,7 +109,7 @@ public FragmentSectionHelper initialize(SectionInitContext context) {
.build();
}
}
return new FragmentSectionHelper(id, context.getExpression(RENDERED));
return new FragmentSectionHelper(id, context.getExpression(RENDERED), generatedId);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,9 @@ public Template getOriginalTemplate() {
@Override
public TemplateInstance instance() {
TemplateInstance instance = super.instance();
instance.setAttribute(Fragment.ATTRIBUTE, true);
// when a fragment is executed separately we need a way to instruct FragmentSectionHelper to ignore the "renreded" parameter
// Fragment.ATTRIBUTE contains the generated id of the template that declares the fragment section and the fragment identifier
instance.setAttribute(Fragment.ATTRIBUTE, TemplateImpl.this.getGeneratedId() + FragmentImpl.this.getId());
return instance;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,41 @@ public void testInvalidId() {
expected.getMessage());
}

@Test
public void testNestedFragmentRendered() {
Engine engine = Engine.builder().addDefaults().build();
Template alpha = engine.parse("""
OK
{#fragment id=\"nested\" rendered=false}
NOK
{/}
{#fragment id=\"visible\"}
01
{/fragment}
""");
engine.putTemplate("alpha", alpha);
assertEquals("OK01", alpha.render().replaceAll("\\s", ""));
assertEquals("NOK", alpha.getFragment("nested").render().trim());

Template bravo = engine.parse("""
{#include $nested}
{#fragment id=\"nested\" rendered=false}
OK
{/}
""");
assertEquals("OK", bravo.render().trim());
assertEquals("OK", bravo.getFragment("nested").render().trim());

assertEquals("NOK", engine.parse("{#include alpha$nested /}").render().trim());
Template charlie = engine.parse("{#include alpha /}");
assertEquals("OK01", charlie.render().replaceAll("\\s", ""));

Template delta = engine.parse("""
{#fragment id=\"nested\" rendered=false}
{#include alpha /}
{/}
""");
assertEquals("OK01", delta.getFragment("nested").render().replaceAll("\\s", ""));
}

}
Loading