Skip to content

Commit

Permalink
#255 empty String in attribute value does not remove the attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
casid committed Aug 2, 2023
1 parent ebb6ba2 commit 3e4c084
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ For rendering HTML documents, `ContentType.Html` is highly recommended for [secu

### Smart Attributes

Expressions in HTML attributes are evaluated, so that optimal output is generated. This means attributes with a single output that evaluates to an empty string, null, or false, are not rendered. For instance:
Expressions in HTML attributes are evaluated, so that optimal output is generated. This means attributes with a single output that evaluates to null, or false, are not rendered. For instance:

```html
<span data-title="${null}">Info</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ void attributes_String_empty() {

templateEngine.render("template.kte", "", output);

assertThat(output.toString()).isEqualTo("<button>Click</button>");
assertThat(output.toString()).isEqualTo("<button data-value=\"\">Click</button>");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static boolean isAttributeRendered(double value) {
}

public static boolean isAttributeRendered(String value) {
return value != null && !value.isEmpty();
return value != null;
}

public static boolean isAttributeRendered(Content value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,20 @@ void attributes_String_empty() {

templateEngine.render("template.jte", "", output);

assertThat(output.toString()).isEqualTo("<button>Click</button>");
assertThat(output.toString()).isEqualTo("<button data-value=\"\">Click</button>");
}

/**
* This is essentially the same test as gg.jte.TemplateEngine_HtmlOutputEscapingTest#attributes_String_empty()
* But it especially shows that there is a semantic difference between an empty String and null.
*/
@Test
void attributes_String_empty_option() {
codeResolver.givenCode("template.jte", "@param String value\n<option value=\"${value}\">Empty</option>");

templateEngine.render("template.jte", "", output);

assertThat(output.toString()).isEqualTo("<option value=\"\">Empty</option>");
}

@Test
Expand Down

0 comments on commit 3e4c084

Please sign in to comment.