From e62255512a6657e468ae5fc019b4ba5f3d7cc223 Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Wed, 19 Jun 2024 11:14:27 +0200 Subject: [PATCH] Encode based on response character encoding 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 --- .../servlet/tags/form/AbstractFormTag.java | 6 +++-- .../web/servlet/tags/form/InputTagTests.java | 23 +++++++++++++++++-- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/tags/form/AbstractFormTag.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/tags/form/AbstractFormTag.java index 958744af130..c9dc294c054 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/tags/form/AbstractFormTag.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/tags/form/AbstractFormTag.java @@ -92,7 +92,8 @@ public abstract class AbstractFormTag extends HtmlEscapingAwareTag { * as required. This version is not {@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; } /** @@ -102,7 +103,8 @@ public abstract class AbstractFormTag extends HtmlEscapingAwareTag { * 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; } /** diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/tags/form/InputTagTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/tags/form/InputTagTests.java index 54a04354518..c93f9fdb7d0 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/tags/form/InputTagTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/tags/form/InputTagTests.java @@ -96,8 +96,8 @@ class InputTagTests extends AbstractFormTagTests { @Test void simpleBindWithHtmlEscaping() throws Exception { - final String NAME = "Rob \"I Love Mangos\" Harrop"; - final String HTML_ESCAPED_NAME = "Rob "I Love Mangos" Harrop"; + final String NAME = "Rob \"I Love Cafés\" Harrop"; + final String HTML_ESCAPED_NAME = "Rob "I Love Cafés" Harrop"; this.tag.setPath("name"); this.rob.setName(NAME); @@ -112,6 +112,25 @@ class InputTagTests extends AbstractFormTagTests { 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 "I Love Cafés" 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); }