Skip to content

Commit

Permalink
Encode based on response character encoding
Browse files Browse the repository at this point in the history
Before this commit, characters were always encoded with the default
encoding (i.e. ISO-8859-1). Now, the character encoding of the response
is used.

Closes gh-files
  • Loading branch information
poutsma committed Jun 19, 2024
1 parent c72e31b commit e622555
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ protected final int doStartTagInternal() throws Exception {
* as required. This version is <strong>not</strong> {@link PropertyEditor}-aware.
*/
protected String getDisplayString(@Nullable Object value) {
return ValueFormatter.getDisplayString(value, isHtmlEscape());
String displayString = ValueFormatter.getDisplayString(value, false);
return isHtmlEscape() ? htmlEscape(displayString) : displayString;
}

/**
Expand All @@ -102,7 +103,8 @@ protected String getDisplayString(@Nullable Object value) {
* to obtain the display value.
*/
protected String getDisplayString(@Nullable Object value, @Nullable PropertyEditor propertyEditor) {
return ValueFormatter.getDisplayString(value, propertyEditor, isHtmlEscape());
String displayString = ValueFormatter.getDisplayString(value, propertyEditor, false);
return isHtmlEscape() ? htmlEscape(displayString) : displayString;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ void simpleBindTagWithinForm() throws Exception {

@Test
void simpleBindWithHtmlEscaping() throws Exception {
final String NAME = "Rob \"I Love Mangos\" Harrop";
final String HTML_ESCAPED_NAME = "Rob &quot;I Love Mangos&quot; Harrop";
final String NAME = "Rob \"I Love Cafés\" Harrop";
final String HTML_ESCAPED_NAME = "Rob &quot;I Love Caf&eacute;s&quot; Harrop";

this.tag.setPath("name");
this.rob.setName(NAME);
Expand All @@ -112,6 +112,25 @@ void simpleBindWithHtmlEscaping() throws Exception {
assertValueAttribute(output, HTML_ESCAPED_NAME);
}

@Test
void simpleBindWithHtmlEscapingAndCharacterEncoding() throws Exception {
final String NAME = "Rob \"I Love Cafés\" Harrop";
final String HTML_ESCAPED_NAME = "Rob &quot;I Love Cafés&quot; Harrop";

this.getPageContext().getResponse().setCharacterEncoding("UTF-8");
this.tag.setPath("name");
this.rob.setName(NAME);

assertThat(this.tag.doStartTag()).isEqualTo(Tag.SKIP_BODY);

String output = getOutput();
assertTagOpened(output);
assertTagClosed(output);

assertContainsAttribute(output, "type", getType());
assertValueAttribute(output, HTML_ESCAPED_NAME);
}

protected void assertValueAttribute(String output, String expectedValue) {
assertContainsAttribute(output, "value", expectedValue);
}
Expand Down

1 comment on commit e622555

@poutsma
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit closes #33023.

Please sign in to comment.