Skip to content

Commit

Permalink
Merge pull request quarkusio#38255 from mkouba/issue-38251
Browse files Browse the repository at this point in the history
Qute: fix UserTagSectionHelper.Arguments.asHtmlAttributes()
  • Loading branch information
ia3andy authored Jan 17, 2024
2 parents c648a3b + 8b445f1 commit 621c22c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ public class UserTagSectionHelper extends IncludeSectionHelper implements Sectio

protected final boolean isNestedContentNeeded;

private final HtmlEscaper htmlEscaper;

UserTagSectionHelper(Supplier<Template> templateSupplier, Map<String, SectionBlock> extendingBlocks,
Map<String, Expression> parameters, boolean isIsolated, boolean isNestedContentNeeded) {
Map<String, Expression> parameters, boolean isIsolated, boolean isNestedContentNeeded, HtmlEscaper htmlEscaper) {
super(templateSupplier, extendingBlocks, parameters, isIsolated);
this.isNestedContentNeeded = isNestedContentNeeded;
this.htmlEscaper = htmlEscaper;
}

@Override
Expand All @@ -29,7 +32,7 @@ protected boolean optimizeIfNoParams() {

@Override
protected void addAdditionalEvaluatedParams(SectionResolutionContext context, Map<String, Object> evaluatedParams) {
evaluatedParams.put(Factory.ARGS, new Arguments(evaluatedParams));
evaluatedParams.put(Factory.ARGS, new Arguments(evaluatedParams, htmlEscaper));
if (isNestedContentNeeded) {
// If needed then add the {nested-content} to the evaluated params
Expression nestedContent = ((TemplateImpl) template.get()).findExpression(this::isNestedContent);
Expand All @@ -56,6 +59,7 @@ public static class Factory extends IncludeSectionHelper.AbstractIncludeFactory<

private final String name;
private final String templateId;
private final HtmlEscaper htmlEscaper;

/**
*
Expand All @@ -65,6 +69,7 @@ public static class Factory extends IncludeSectionHelper.AbstractIncludeFactory<
public Factory(String name, String templateId) {
this.name = name;
this.templateId = templateId;
this.htmlEscaper = new HtmlEscaper(List.of());
}

@Override
Expand Down Expand Up @@ -108,7 +113,7 @@ protected UserTagSectionHelper newHelper(Supplier<Template> template, Map<String
return new UserTagSectionHelper(template, extendingBlocks, params,
isolatedValue != null ? isolatedValue
: Boolean.parseBoolean(context.getParameterOrDefault(ISOLATED, ISOLATED_DEFAULT_VALUE)),
isNestedContentNeeded);
isNestedContentNeeded, htmlEscaper);
}

@Override
Expand All @@ -129,16 +134,19 @@ protected void handleParamInit(String key, String value, SectionInitContext cont
public static class Arguments implements Iterable<Entry<String, Object>> {

private final List<Entry<String, Object>> args;
private final HtmlEscaper htmlEscaper;

Arguments(Map<String, Object> map) {
Arguments(Map<String, Object> map, HtmlEscaper htmlEscaper) {
this.args = new ArrayList<>(Objects.requireNonNull(map).size());
map.entrySet().forEach(args::add);
// sort by key
this.args.sort(Comparator.comparing(Entry::getKey));
this.htmlEscaper = htmlEscaper;
}

private Arguments(List<Entry<String, Object>> args) {
private Arguments(List<Entry<String, Object>> args, HtmlEscaper htmlEscaper) {
this.args = args;
this.htmlEscaper = htmlEscaper;
}

public boolean isEmpty() {
Expand Down Expand Up @@ -171,7 +179,7 @@ public Arguments skip(String... keys) {
newArgs.add(e);
}
}
return new Arguments(newArgs);
return new Arguments(newArgs, htmlEscaper);
}

public Arguments filter(String... keys) {
Expand All @@ -182,23 +190,23 @@ public Arguments filter(String... keys) {
newArgs.add(e);
}
}
return new Arguments(newArgs);
return new Arguments(newArgs, htmlEscaper);
}

// foo="1" bar="true"
public String asHtmlAttributes() {
public RawString asHtmlAttributes() {
StringBuilder builder = new StringBuilder();
for (Iterator<Entry<String, Object>> it = args.iterator(); it.hasNext();) {
Entry<String, Object> e = it.next();
builder.append(e.getKey());
builder.append("=\"");
builder.append(e.getValue());
builder.append(htmlEscaper.escape(String.valueOf(e.getValue())));
builder.append("\"");
if (it.hasNext()) {
builder.append(" ");
}
}
return builder.toString();
return new RawString(builder.toString());
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,12 @@ public void testArguments() {
.addSectionHelper(new UserTagSectionHelper.Factory("myTag1", "my-tag-1"))
.addSectionHelper(new UserTagSectionHelper.Factory("myTag2", "my-tag-2"))
.addSectionHelper(new UserTagSectionHelper.Factory("gravatar", "gravatar-tag"))
.addResultMapper(new HtmlEscaper(ImmutableList.of("text/html")))
.strictRendering(false)
.build();
Template tag1 = engine.parse(
"{_args.size}::{_args.empty}::{_args.get('foo').or('bar')}::{_args.asHtmlAttributes}::{_args.skip('foo','baz').size}::{#each _args.filter('foo')}{it.value}{/each}");
"{_args.size}::{_args.empty}::{_args.get('foo').or('bar')}::{_args.asHtmlAttributes}::{_args.skip('foo','baz').size}::{#each _args.filter('foo')}{it.value}{/each}",
Variant.forContentType(Variant.TEXT_HTML));
engine.putTemplate("my-tag-1", tag1);
Template tag2 = engine.parse(
"{#each _args}{it.key}=\"{it.value}\"{#if it_hasNext} {/if}{/each}");
Expand All @@ -178,7 +180,8 @@ public void testArguments() {

Template template = engine.parse("{#myTag1 /}");
assertEquals("0::true::bar::::0::", template.render());
assertEquals("2::false::1::bar=\"true\" foo=\"1\"::1::1", engine.parse("{#myTag1 foo=1 bar=true /}").render());
assertEquals("3::false::1::bar=\"true\" baz=\"&quot;\" foo=\"1\"::1::1",
engine.parse("{#myTag1 foo=1 bar=true baz=quotationMark /}").data("quotationMark", "\"").render());

assertEquals("baz=\"false\" foo=\"1\"", engine.parse("{#myTag2 foo=1 baz=false /}").render());

Expand Down

0 comments on commit 621c22c

Please sign in to comment.