This is an automated email from the ASF dual-hosted git repository.

lukaszlenart pushed a commit to branch 
WW-5639-property-mutually-exclusive-escaping
in repository https://gitbox.apache.org/repos/asf/struts.git

commit e63d20631199663273d203696571f297440071e1
Author: Lukasz Lenart <[email protected]>
AuthorDate: Thu Nov 26 17:34:53 2026 +0100

    WW-5639 feat(tags): pluggable TagEscaper for property and text tags
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 .../java/org/apache/struts2/StrutsConstants.java   |   5 +
 .../org/apache/struts2/components/Property.java    |  37 +++---
 .../java/org/apache/struts2/components/Text.java   |  37 +++---
 .../struts2/components/escape/EscapeConfig.java    | 125 +++++++++++++++++++++
 .../components/escape/StrutsTagEscaper.java        |  64 +++++++++++
 .../struts2/components/escape/TagEscaper.java      |  35 ++++++
 .../struts2/components/escape/TagOrigin.java       |  27 +++++
 .../config/StrutsBeanSelectionProvider.java        |   2 +
 .../org/apache/struts2/default.properties          |   3 +
 core/src/main/resources/struts-beans.xml           |   3 +
 .../struts2/components/IteratorComponentTest.java  |  30 +++--
 .../apache/struts2/components/PropertyTest.java    |  62 +++++++++-
 .../org/apache/struts2/components/TextTest.java    |  54 +++++++++
 .../components/escape/EscapeConfigTest.java        |  54 +++++++++
 .../components/escape/StrutsTagEscaperTest.java    | 103 +++++++++++++++++
 .../config/StrutsBeanSelectionProviderTest.java    |  23 ++++
 16 files changed, 614 insertions(+), 50 deletions(-)

diff --git a/core/src/main/java/org/apache/struts2/StrutsConstants.java 
b/core/src/main/java/org/apache/struts2/StrutsConstants.java
index ae5d644af..9ab63a9a5 100644
--- a/core/src/main/java/org/apache/struts2/StrutsConstants.java
+++ b/core/src/main/java/org/apache/struts2/StrutsConstants.java
@@ -821,6 +821,11 @@ public final class StrutsConstants {
      */
     public static final String STRUTS_DATE_FORMATTER = "struts.date.formatter";
 
+    /**
+     * See {@link 
org.apache.struts2.components.Property#setTagEscaper(org.apache.struts2.components.escape.TagEscaper)}
+     */
+    public static final String STRUTS_TAG_ESCAPER = "struts.tag.escaper";
+
     public static final String STRUTS_URL_QUERY_STRING_BUILDER = 
"struts.url.queryStringBuilder";
     public static final String STRUTS_URL_QUERY_STRING_PARSER = 
"struts.url.queryStringParser";
     public static final String STRUTS_URL_ENCODER = "struts.url.encoder";
diff --git a/core/src/main/java/org/apache/struts2/components/Property.java 
b/core/src/main/java/org/apache/struts2/components/Property.java
index f57bd1cb4..43d06daff 100644
--- a/core/src/main/java/org/apache/struts2/components/Property.java
+++ b/core/src/main/java/org/apache/struts2/components/Property.java
@@ -19,7 +19,10 @@
 package org.apache.struts2.components;
 
 import org.apache.struts2.util.ValueStack;
-import org.apache.commons.text.StringEscapeUtils;
+import org.apache.struts2.components.escape.EscapeConfig;
+import org.apache.struts2.components.escape.TagEscaper;
+import org.apache.struts2.components.escape.TagOrigin;
+import org.apache.struts2.inject.Inject;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import org.apache.struts2.views.annotations.StrutsTag;
@@ -97,6 +100,8 @@ public class Property extends Component {
     private boolean escapeXml = false;
     private boolean escapeCsv = false;
 
+    private TagEscaper tagEscaper;
+
     @StrutsTagAttribute(description="The default value to be used if 
<u>value</u> attribute is null")
     public void setDefault(String defaultValue) {
         this.defaultValue = defaultValue;
@@ -161,21 +166,21 @@ public class Property extends Component {
         return result;
     }
 
-    private String prepare(String value) {
-        String result = value;
-        if (escapeHtml) {
-            result = StringEscapeUtils.escapeHtml4(result);
-        }
-        if (escapeJavaScript) {
-            result = StringEscapeUtils.escapeEcmaScript(result);
-        }
-        if (escapeXml) {
-            result = StringEscapeUtils.escapeXml10(result);
-        }
-        if (escapeCsv) {
-            result = StringEscapeUtils.escapeCsv(result);
-        }
+    /**
+     * An instance of {@link TagEscaper}, selected with {@link 
org.apache.struts2.StrutsConstants#STRUTS_TAG_ESCAPER}
+     */
+    @Inject
+    public void setTagEscaper(TagEscaper tagEscaper) {
+        this.tagEscaper = tagEscaper;
+    }
 
-        return result;
+    private String prepare(String value) {
+        EscapeConfig config = EscapeConfig.builder(TagOrigin.PROPERTY)
+                .html(escapeHtml)
+                .javaScript(escapeJavaScript)
+                .xml(escapeXml)
+                .csv(escapeCsv)
+                .build();
+        return tagEscaper.escape(config, value);
     }
 }
diff --git a/core/src/main/java/org/apache/struts2/components/Text.java 
b/core/src/main/java/org/apache/struts2/components/Text.java
index 4f9ea316d..fa47d68b4 100644
--- a/core/src/main/java/org/apache/struts2/components/Text.java
+++ b/core/src/main/java/org/apache/struts2/components/Text.java
@@ -19,7 +19,10 @@
 package org.apache.struts2.components;
 
 import org.apache.struts2.util.ValueStack;
-import org.apache.commons.text.StringEscapeUtils;
+import org.apache.struts2.components.escape.EscapeConfig;
+import org.apache.struts2.components.escape.TagEscaper;
+import org.apache.struts2.components.escape.TagOrigin;
+import org.apache.struts2.inject.Inject;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
@@ -133,6 +136,8 @@ public class Text extends ContextBean implements 
Param.UnnamedParametric {
     private boolean escapeXml = false;
     private boolean escapeCsv = false;
 
+    private TagEscaper tagEscaper;
+
     public Text(ValueStack stack) {
         super(stack);
     }
@@ -211,21 +216,21 @@ public class Text extends ContextBean implements 
Param.UnnamedParametric {
         values.add(value);
     }
 
-    private String prepare(String value) {
-        String result = value;
-        if (escapeHtml) {
-            result = StringEscapeUtils.escapeHtml4(result);
-        }
-        if (escapeJavaScript) {
-            result = StringEscapeUtils.escapeEcmaScript(result);
-        }
-        if (escapeXml) {
-            result = StringEscapeUtils.escapeXml10(result);
-        }
-        if (escapeCsv) {
-            result = StringEscapeUtils.escapeCsv(result);
-        }
+    /**
+     * An instance of {@link TagEscaper}, selected with {@link 
org.apache.struts2.StrutsConstants#STRUTS_TAG_ESCAPER}
+     */
+    @Inject
+    public void setTagEscaper(TagEscaper tagEscaper) {
+        this.tagEscaper = tagEscaper;
+    }
 
-        return result;
+    private String prepare(String value) {
+        EscapeConfig config = EscapeConfig.builder(TagOrigin.TEXT)
+                .html(escapeHtml)
+                .javaScript(escapeJavaScript)
+                .xml(escapeXml)
+                .csv(escapeCsv)
+                .build();
+        return tagEscaper.escape(config, value);
     }
 }
diff --git 
a/core/src/main/java/org/apache/struts2/components/escape/EscapeConfig.java 
b/core/src/main/java/org/apache/struts2/components/escape/EscapeConfig.java
new file mode 100644
index 000000000..837905006
--- /dev/null
+++ b/core/src/main/java/org/apache/struts2/components/escape/EscapeConfig.java
@@ -0,0 +1,125 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.struts2.components.escape;
+
+import java.util.Objects;
+
+/**
+ * Immutable escape options of a single tag render, passed to {@link 
TagEscaper}.
+ */
+public final class EscapeConfig {
+
+    private final TagOrigin origin;
+    private final boolean html;
+    private final boolean javaScript;
+    private final boolean xml;
+    private final boolean csv;
+
+    private EscapeConfig(Builder builder) {
+        this.origin = builder.origin;
+        this.html = builder.html;
+        this.javaScript = builder.javaScript;
+        this.xml = builder.xml;
+        this.csv = builder.csv;
+    }
+
+    public static Builder builder(TagOrigin origin) {
+        return new Builder(origin);
+    }
+
+    public TagOrigin origin() {
+        return origin;
+    }
+
+    public boolean html() {
+        return html;
+    }
+
+    public boolean javaScript() {
+        return javaScript;
+    }
+
+    public boolean xml() {
+        return xml;
+    }
+
+    public boolean csv() {
+        return csv;
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) {
+            return true;
+        }
+        if (!(o instanceof EscapeConfig that)) {
+            return false;
+        }
+        return origin == that.origin && html == that.html && javaScript == 
that.javaScript
+                && xml == that.xml && csv == that.csv;
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(origin, html, javaScript, xml, csv);
+    }
+
+    @Override
+    public String toString() {
+        return "EscapeConfig{origin=" + origin + ", html=" + html + ", 
javaScript=" + javaScript
+                + ", xml=" + xml + ", csv=" + csv + '}';
+    }
+
+    public static final class Builder {
+
+        private final TagOrigin origin;
+        private boolean html;
+        private boolean javaScript;
+        private boolean xml;
+        private boolean csv;
+
+        private Builder(TagOrigin origin) {
+            this.origin = Objects.requireNonNull(origin, "origin");
+        }
+
+        public Builder html(boolean html) {
+            this.html = html;
+            return this;
+        }
+
+        public Builder javaScript(boolean javaScript) {
+            this.javaScript = javaScript;
+            return this;
+        }
+
+        public Builder xml(boolean xml) {
+            this.xml = xml;
+            return this;
+        }
+
+        public Builder csv(boolean csv) {
+            this.csv = csv;
+            return this;
+        }
+
+        public EscapeConfig build() {
+            return new EscapeConfig(this);
+        }
+    }
+}
diff --git 
a/core/src/main/java/org/apache/struts2/components/escape/StrutsTagEscaper.java 
b/core/src/main/java/org/apache/struts2/components/escape/StrutsTagEscaper.java
new file mode 100644
index 000000000..95d9ed767
--- /dev/null
+++ 
b/core/src/main/java/org/apache/struts2/components/escape/StrutsTagEscaper.java
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.struts2.components.escape;
+
+import org.apache.commons.text.StringEscapeUtils;
+
+/**
+ * Default {@link TagEscaper}: applies the selected escapers in the order 
HTML, JavaScript, XML, CSV.
+ * <p>
+ * Each step is a protected hook, so an application needing different handling 
for one context (e.g. letting a
+ * safelist of HTML markup through) can extend this class and override that 
hook only.
+ */
+public class StrutsTagEscaper implements TagEscaper {
+
+    @Override
+    public String escape(EscapeConfig config, String value) {
+        String result = value;
+        if (config.html()) {
+            result = escapeHtml(result);
+        }
+        if (config.javaScript()) {
+            result = escapeJavaScript(result);
+        }
+        if (config.xml()) {
+            result = escapeXml(result);
+        }
+        if (config.csv()) {
+            result = escapeCsv(result);
+        }
+        return result;
+    }
+
+    protected String escapeHtml(String value) {
+        return StringEscapeUtils.escapeHtml4(value);
+    }
+
+    protected String escapeJavaScript(String value) {
+        return StringEscapeUtils.escapeEcmaScript(value);
+    }
+
+    protected String escapeXml(String value) {
+        return StringEscapeUtils.escapeXml10(value);
+    }
+
+    protected String escapeCsv(String value) {
+        return StringEscapeUtils.escapeCsv(value);
+    }
+}
diff --git 
a/core/src/main/java/org/apache/struts2/components/escape/TagEscaper.java 
b/core/src/main/java/org/apache/struts2/components/escape/TagEscaper.java
new file mode 100644
index 000000000..179862bda
--- /dev/null
+++ b/core/src/main/java/org/apache/struts2/components/escape/TagEscaper.java
@@ -0,0 +1,35 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.struts2.components.escape;
+
+/**
+ * Escapes the values rendered by the {@code property} and {@code text} tags.
+ * <p>
+ * The instance is selected with {@link 
org.apache.struts2.StrutsConstants#STRUTS_TAG_ESCAPER}; replacing the
+ * default {@link StrutsTagEscaper} makes the application responsible for the 
safety of the rendered output.
+ */
+public interface TagEscaper {
+
+    /**
+     * @param config the escape options set on the tag and the tag they come 
from
+     * @param value  the value to escape, may be {@code null}
+     * @return the escaped value
+     */
+    String escape(EscapeConfig config, String value);
+}
diff --git 
a/core/src/main/java/org/apache/struts2/components/escape/TagOrigin.java 
b/core/src/main/java/org/apache/struts2/components/escape/TagOrigin.java
new file mode 100644
index 000000000..ddd03f11e
--- /dev/null
+++ b/core/src/main/java/org/apache/struts2/components/escape/TagOrigin.java
@@ -0,0 +1,27 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.struts2.components.escape;
+
+/**
+ * The tag asking a {@link TagEscaper} to escape a value.
+ */
+public enum TagOrigin {
+    PROPERTY,
+    TEXT
+}
diff --git 
a/core/src/main/java/org/apache/struts2/config/StrutsBeanSelectionProvider.java 
b/core/src/main/java/org/apache/struts2/config/StrutsBeanSelectionProvider.java
index 866bad44f..da94eb966 100644
--- 
a/core/src/main/java/org/apache/struts2/config/StrutsBeanSelectionProvider.java
+++ 
b/core/src/main/java/org/apache/struts2/config/StrutsBeanSelectionProvider.java
@@ -32,6 +32,7 @@ import org.apache.struts2.UnknownHandlerManager;
 import org.apache.struts2.components.HtmlConstraintProvider;
 import org.apache.struts2.components.UrlRenderer;
 import org.apache.struts2.components.date.DateFormatter;
+import org.apache.struts2.components.escape.TagEscaper;
 import org.apache.struts2.conversion.ConversionAnnotationProcessor;
 import org.apache.struts2.conversion.ConversionFileProcessor;
 import org.apache.struts2.conversion.ConversionPropertiesProcessor;
@@ -453,6 +454,7 @@ public class StrutsBeanSelectionProvider extends 
AbstractBeanSelectionProvider {
         alias(NotExcludedAcceptedPatternsChecker.class, 
StrutsConstants.STRUTS_NOT_EXCLUDED_ACCEPTED_PATTERNS_CHECKER, builder, props, 
Scope.SINGLETON);
 
         alias(DateFormatter.class, StrutsConstants.STRUTS_DATE_FORMATTER, 
builder, props, Scope.SINGLETON);
+        alias(TagEscaper.class, StrutsConstants.STRUTS_TAG_ESCAPER, builder, 
props, Scope.SINGLETON);
 
         alias(ExpressionCacheFactory.class, 
StrutsConstants.STRUTS_OGNL_EXPRESSION_CACHE_FACTORY, builder, props, 
Scope.SINGLETON);
         alias(BeanInfoCacheFactory.class, 
StrutsConstants.STRUTS_OGNL_BEANINFO_CACHE_FACTORY, builder, props, 
Scope.SINGLETON);
diff --git a/core/src/main/resources/org/apache/struts2/default.properties 
b/core/src/main/resources/org/apache/struts2/default.properties
index feb190714..07f1a3189 100644
--- a/core/src/main/resources/org/apache/struts2/default.properties
+++ b/core/src/main/resources/org/apache/struts2/default.properties
@@ -356,6 +356,9 @@ struts.ognl.expressionMaxLength=256
 ### These formatters are using a slightly different patterns, please check 
JavaDocs of both and more details is in WW-5016
 struts.date.formatter=dateTimeFormatter
 
+### Defines which named instance of TagEscaper escapes the values of the 
property and text tags
+struts.tag.escaper=struts
+
 ### When true, ISO date-times converted to java.util.Date honour their offset, 
so a trailing Z means UTC
 ### (e.g. 2026-10-25T05:14:42.000Z from JavaScript's Date.toISOString()). 
Defaults to false, which reads
 ### the Z as server-local time.
diff --git a/core/src/main/resources/struts-beans.xml 
b/core/src/main/resources/struts-beans.xml
index 067bba743..d6743a34c 100644
--- a/core/src/main/resources/struts-beans.xml
+++ b/core/src/main/resources/struts-beans.xml
@@ -244,6 +244,9 @@
     <bean type="org.apache.struts2.components.date.DateFormatter" 
name="dateTimeFormatter"
           class="org.apache.struts2.components.date.DateTimeFormatterAdapter" 
scope="singleton"/>
 
+    <bean type="org.apache.struts2.components.escape.TagEscaper" name="struts"
+          class="org.apache.struts2.components.escape.StrutsTagEscaper" 
scope="singleton"/>
+
     <bean type="org.apache.struts2.ognl.ExpressionCacheFactory" name="struts"
           class="org.apache.struts2.ognl.DefaultOgnlExpressionCacheFactory" 
scope="singleton"/>
     <bean type="org.apache.struts2.ognl.BeanInfoCacheFactory" name="struts"
diff --git 
a/core/src/test/java/org/apache/struts2/components/IteratorComponentTest.java 
b/core/src/test/java/org/apache/struts2/components/IteratorComponentTest.java
index 9b160972c..87fff7203 100644
--- 
a/core/src/test/java/org/apache/struts2/components/IteratorComponentTest.java
+++ 
b/core/src/test/java/org/apache/struts2/components/IteratorComponentTest.java
@@ -54,7 +54,7 @@ public class IteratorComponentTest extends 
StrutsInternalTestCase {
         ic.setValue("items");
         ic.setVar("val");
 
-        Property prop = new Property(stack);
+        Property prop = newProperty(stack);
 
         ic.getComponentStack().push(prop);
         ic.getComponentStack().push(prop);
@@ -88,8 +88,8 @@ public class IteratorComponentTest extends 
StrutsInternalTestCase {
         ic.setStep("2");
         ic.setStatus("status");
 
-        Property prop = new Property(stack);
-        Property status = new Property(stack);
+        Property prop = newProperty(stack);
+        Property status = newProperty(stack);
         status.setValue("#status.index");
 
         ic.getComponentStack().push(prop);
@@ -127,7 +127,7 @@ public class IteratorComponentTest extends 
StrutsInternalTestCase {
         ic.setValue("items");
         ic.setVar("val");
         ic.setBegin("1");
-        Property prop = new Property(stack);
+        Property prop = newProperty(stack);
 
         ic.getComponentStack().push(prop);
         ic.getComponentStack().push(prop);
@@ -162,7 +162,7 @@ public class IteratorComponentTest extends 
StrutsInternalTestCase {
 
         ic.setValue("items");
         ic.setVar("val");
-        Property prop = new Property(stack);
+        Property prop = newProperty(stack);
 
         ic.getComponentStack().push(prop);
         ic.getComponentStack().push(prop);
@@ -198,7 +198,7 @@ public class IteratorComponentTest extends 
StrutsInternalTestCase {
 
         ic.setValue("items");
         ic.setVar("val");
-        Property prop = new Property(stack);
+        Property prop = newProperty(stack);
 
         ic.getComponentStack().push(prop);
         ic.getComponentStack().push(prop);
@@ -231,8 +231,8 @@ public class IteratorComponentTest extends 
StrutsInternalTestCase {
         ic.setEnd("3");
         ic.setStatus("status");
 
-        Property prop = new Property(stack);
-        Property status = new Property(stack);
+        Property prop = newProperty(stack);
+        Property status = newProperty(stack);
         status.setValue("#status.count");
 
         ic.getComponentStack().push(prop);
@@ -275,9 +275,9 @@ public class IteratorComponentTest extends 
StrutsInternalTestCase {
         ic.setValue("list2");
         ic.setStatus("status");
 
-        Property prop = new Property(stack);
+        Property prop = newProperty(stack);
         prop.setValue("name");
-        Property status = new Property(stack);
+        Property status = newProperty(stack);
         status.setValue("#status.indexStr");
 
         ic.getComponentStack().push(status);
@@ -321,9 +321,9 @@ public class IteratorComponentTest extends 
StrutsInternalTestCase {
         ic.setValue("objectArray");
         ic.setStatus("status");
 
-        Property prop = new Property(stack);
+        Property prop = newProperty(stack);
         prop.setValue("name");
-        Property status = new Property(stack);
+        Property status = newProperty(stack);
         status.setValue("#status.countStr");
 
         ic.getComponentStack().push(status);
@@ -352,6 +352,12 @@ public class IteratorComponentTest extends 
StrutsInternalTestCase {
         assertEquals("1 Anton 2 Tym 3 Luk ", out.getBuffer().toString());
     }
 
+    private Property newProperty(ValueStack stack) {
+        Property property = new Property(stack);
+        container.inject(property);
+        return property;
+    }
+
     static class FooAction {
 
         private final List<String> items;
diff --git a/core/src/test/java/org/apache/struts2/components/PropertyTest.java 
b/core/src/test/java/org/apache/struts2/components/PropertyTest.java
index ae0040cde..09df5aa17 100644
--- a/core/src/test/java/org/apache/struts2/components/PropertyTest.java
+++ b/core/src/test/java/org/apache/struts2/components/PropertyTest.java
@@ -22,6 +22,10 @@ import java.io.StringWriter;
 import java.util.Map;
 
 import org.apache.struts2.StrutsInternalTestCase;
+import org.apache.struts2.components.escape.EscapeConfig;
+import org.apache.struts2.components.escape.StrutsTagEscaper;
+import org.apache.struts2.components.escape.TagEscaper;
+import org.apache.struts2.components.escape.TagOrigin;
 import org.apache.struts2.util.StrutsTypeConverter;
 
 import org.apache.struts2.ActionContext;
@@ -46,7 +50,7 @@ public class PropertyTest extends StrutsInternalTestCase {
     public void testNormalBehaviour() {
         final ValueStack stack = ActionContext.getContext().getValueStack();
         stack.push(new FooBar("foo-value", "bar-value"));
-        final Property property = new Property(stack);
+        final Property property = newProperty(stack);
         property.setDefault("default");
         property.setValue("foo");
         assertPropertyOutput("foo-value", property);
@@ -54,7 +58,7 @@ public class PropertyTest extends StrutsInternalTestCase {
 
     public void testDefaultShouldBeOutputIfBeanNotAvailable() {
         final ValueStack stack = ActionContext.getContext().getValueStack();
-        final Property property = new Property(stack);
+        final Property property = newProperty(stack);
         property.setDefault("default");
         property.setValue("foo");
         assertPropertyOutput("default", property);
@@ -63,7 +67,7 @@ public class PropertyTest extends StrutsInternalTestCase {
     public void testDefaultShouldBeOutputIfPropertyIsNull() {
         final ValueStack stack = ActionContext.getContext().getValueStack();
         stack.push(new FooBar(null, "bar-value"));
-        final Property property = new Property(stack);
+        final Property property = newProperty(stack);
         property.setDefault("default");
         property.setValue("foo");
         assertPropertyOutput("default", property);
@@ -72,7 +76,7 @@ public class PropertyTest extends StrutsInternalTestCase {
     public void testTopValueShouldReturnTopOfValueStack() {
         final ValueStack stack = ActionContext.getContext().getValueStack();
         stack.push(new FooBar("foo-value", "bar-value"));
-        final Property property = new Property(stack);
+        final Property property = newProperty(stack);
         property.setDefault("default");
         property.setValue("top");
         assertPropertyOutput("foo-value/bar-value", property);
@@ -82,7 +86,7 @@ public class PropertyTest extends StrutsInternalTestCase {
         final ValueStack stack = ActionContext.getContext().getValueStack();
         
converter.registerConverter("org.apache.struts2.components.PropertyTest$FooBar",
 new FooBarConverter());
         stack.push(new FooBar("foo-value", "bar-value"));
-        final Property property = new Property(stack);
+        final Property property = newProperty(stack);
         property.setDefault("default");
         property.setValue("top");
         assertPropertyOutput("*foo-value + bar-value*", property);
@@ -92,12 +96,58 @@ public class PropertyTest extends StrutsInternalTestCase {
         final ValueStack stack = ActionContext.getContext().getValueStack();
         
converter.registerConverter("org.apache.struts2.components.PropertyTest$FooBar",
 new FooBarConverter());
         stack.push(new FooBar("foo-value", null));
-        final Property property = new Property(stack);
+        final Property property = newProperty(stack);
         property.setDefault("default");
         property.setValue("top");
         assertPropertyOutput("default", property);
     }
 
+    public void testDefaultTagEscaperIsStrutsTagEscaper() {
+        assertTrue(container.getInstance(TagEscaper.class) instanceof 
StrutsTagEscaper);
+    }
+
+    public void testCustomTagEscaperReceivesPropertyConfig() {
+        final ValueStack stack = ActionContext.getContext().getValueStack();
+        stack.push(new FooBar("foo-value", "bar-value"));
+        final Property property = newProperty(stack);
+        RecordingTagEscaper escaper = new RecordingTagEscaper();
+        property.setTagEscaper(escaper);
+        property.setEscapeJavaScript(true);
+        property.setValue("foo");
+
+        assertPropertyOutput("[escaped]", property);
+        
assertEquals(EscapeConfig.builder(TagOrigin.PROPERTY).html(true).javaScript(true).build(),
 escaper.config);
+        assertEquals("foo-value", escaper.value);
+    }
+
+    public void testDefaultHtmlAndJavaScriptStillChain() {
+        final ValueStack stack = ActionContext.getContext().getValueStack();
+        stack.push(new FooBar("x'; alert(1); //\\ <b>", "bar-value"));
+        final Property property = newProperty(stack);
+        property.setEscapeJavaScript(true);
+        property.setValue("foo");
+
+        assertPropertyOutput("x\\'; alert(1); \\/\\/\\\\ &lt;b&gt;", property);
+    }
+
+    private Property newProperty(ValueStack stack) {
+        Property property = new Property(stack);
+        container.inject(property);
+        return property;
+    }
+
+    static final class RecordingTagEscaper implements TagEscaper {
+        EscapeConfig config;
+        String value;
+
+        @Override
+        public String escape(EscapeConfig config, String value) {
+            this.config = config;
+            this.value = value;
+            return "[escaped]";
+        }
+    }
+
     private static void assertPropertyOutput(String expectedOutput, Property 
property) {
         final StringWriter out = new StringWriter();
         assertTrue(property.start(out));
diff --git a/core/src/test/java/org/apache/struts2/components/TextTest.java 
b/core/src/test/java/org/apache/struts2/components/TextTest.java
new file mode 100644
index 000000000..74b723927
--- /dev/null
+++ b/core/src/test/java/org/apache/struts2/components/TextTest.java
@@ -0,0 +1,54 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.struts2.components;
+
+import org.apache.struts2.ActionContext;
+import org.apache.struts2.StrutsInternalTestCase;
+import org.apache.struts2.components.escape.EscapeConfig;
+import org.apache.struts2.components.escape.TagOrigin;
+
+import java.io.StringWriter;
+
+public class TextTest extends StrutsInternalTestCase {
+
+    public void testCustomTagEscaperReceivesTextConfig() {
+        Text text = newText("unknown.text.key");
+        PropertyTest.RecordingTagEscaper escaper = new 
PropertyTest.RecordingTagEscaper();
+        text.setTagEscaper(escaper);
+        text.setEscapeXml(true);
+
+        assertEquals("[escaped]", render(text, ""));
+        assertEquals(EscapeConfig.builder(TagOrigin.TEXT).xml(true).build(), 
escaper.config);
+        assertEquals("unknown.text.key", escaper.value);
+    }
+
+    private Text newText(String name) {
+        Text text = new Text(ActionContext.getContext().getValueStack());
+        container.inject(text);
+        text.setName(name);
+        return text;
+    }
+
+    private static String render(Text text, String body) {
+        StringWriter out = new StringWriter();
+        text.start(out);
+        text.end(out, body);
+        return out.toString();
+    }
+}
diff --git 
a/core/src/test/java/org/apache/struts2/components/escape/EscapeConfigTest.java 
b/core/src/test/java/org/apache/struts2/components/escape/EscapeConfigTest.java
new file mode 100644
index 000000000..6df6a2f8e
--- /dev/null
+++ 
b/core/src/test/java/org/apache/struts2/components/escape/EscapeConfigTest.java
@@ -0,0 +1,54 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.struts2.components.escape;
+
+import org.junit.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class EscapeConfigTest {
+
+    @Test
+    public void builderDefaultsAllFlagsToFalse() {
+        EscapeConfig config = EscapeConfig.builder(TagOrigin.PROPERTY).build();
+
+        assertThat(config.origin()).isEqualTo(TagOrigin.PROPERTY);
+        assertThat(config.html()).isFalse();
+        assertThat(config.javaScript()).isFalse();
+        assertThat(config.xml()).isFalse();
+        assertThat(config.csv()).isFalse();
+    }
+
+    @Test
+    public void builderSetsEachFlag() {
+        
assertThat(EscapeConfig.builder(TagOrigin.TEXT).html(true).build().html()).isTrue();
+        
assertThat(EscapeConfig.builder(TagOrigin.TEXT).javaScript(true).build().javaScript()).isTrue();
+        
assertThat(EscapeConfig.builder(TagOrigin.TEXT).xml(true).build().xml()).isTrue();
+        
assertThat(EscapeConfig.builder(TagOrigin.TEXT).csv(true).build().csv()).isTrue();
+    }
+
+    @Test
+    public void equalConfigsAreEqual() {
+        EscapeConfig first = 
EscapeConfig.builder(TagOrigin.TEXT).html(true).xml(true).build();
+        EscapeConfig second = 
EscapeConfig.builder(TagOrigin.TEXT).html(true).xml(true).build();
+
+        assertThat(first).isEqualTo(second).hasSameHashCodeAs(second);
+        
assertThat(first).isNotEqualTo(EscapeConfig.builder(TagOrigin.PROPERTY).html(true).xml(true).build());
+    }
+}
diff --git 
a/core/src/test/java/org/apache/struts2/components/escape/StrutsTagEscaperTest.java
 
b/core/src/test/java/org/apache/struts2/components/escape/StrutsTagEscaperTest.java
new file mode 100644
index 000000000..a1ddf8d65
--- /dev/null
+++ 
b/core/src/test/java/org/apache/struts2/components/escape/StrutsTagEscaperTest.java
@@ -0,0 +1,103 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.struts2.components.escape;
+
+import org.apache.commons.text.StringEscapeUtils;
+import org.junit.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class StrutsTagEscaperTest {
+
+    private static final String VALUE = "\"x' & <b></b>";
+
+    private final StrutsTagEscaper escaper = new StrutsTagEscaper();
+
+    private static EscapeConfig.Builder config() {
+        return EscapeConfig.builder(TagOrigin.PROPERTY);
+    }
+
+    private static EscapeConfig allFlags() {
+        return 
config().html(true).javaScript(true).xml(true).csv(true).build();
+    }
+
+    @Test
+    public void noFlagsReturnsValueUnchanged() {
+        assertThat(escaper.escape(config().build(), VALUE)).isEqualTo(VALUE);
+    }
+
+    @Test
+    public void htmlOnly() {
+        assertThat(escaper.escape(config().html(true).build(), VALUE))
+                .isEqualTo("&quot;x' &amp; &lt;b&gt;&lt;/b&gt;");
+    }
+
+    @Test
+    public void javaScriptOnly() {
+        assertThat(escaper.escape(config().javaScript(true).build(), VALUE))
+                .isEqualTo("\\\"x\\' & <b><\\/b>");
+    }
+
+    @Test
+    public void htmlThenJavaScript() {
+        
assertThat(escaper.escape(config().html(true).javaScript(true).build(), VALUE))
+                .isEqualTo("&quot;x\\' &amp; &lt;b&gt;&lt;\\/b&gt;");
+    }
+
+    @Test
+    public void htmlThenXmlDoubleEscapes() {
+        assertThat(escaper.escape(config().html(true).xml(true).build(), 
VALUE))
+                .isEqualTo("&amp;quot;x&apos; &amp;amp; 
&amp;lt;b&amp;gt;&amp;lt;/b&amp;gt;");
+    }
+
+    @Test
+    public void htmlThenCsv() {
+        assertThat(escaper.escape(config().html(true).csv(true).build(), 
VALUE))
+                .isEqualTo("&quot;x' &amp; &lt;b&gt;&lt;/b&gt;");
+    }
+
+    @Test
+    public void csvOnly() {
+        assertThat(escaper.escape(config().csv(true).build(), VALUE))
+                .isEqualTo("\"\"\"x' & <b></b>\"");
+    }
+
+    @Test
+    public void nullValueReturnsNull() {
+        assertThat(escaper.escape(allFlags(), null)).isNull();
+    }
+
+    @Test
+    public void emptyValueReturnsEmpty() {
+        assertThat(escaper.escape(allFlags(), "")).isEmpty();
+    }
+
+    @Test
+    public void subclassCanReplaceHtmlStep() {
+        StrutsTagEscaper custom = new StrutsTagEscaper() {
+            @Override
+            protected String escapeHtml(String value) {
+                return "<html:" + value + ">";
+            }
+        };
+
+        assertThat(custom.escape(config().html(true).javaScript(true).build(), 
VALUE))
+                .isEqualTo(StringEscapeUtils.escapeEcmaScript("<html:" + VALUE 
+ ">"));
+    }
+}
diff --git 
a/core/src/test/java/org/apache/struts2/config/StrutsBeanSelectionProviderTest.java
 
b/core/src/test/java/org/apache/struts2/config/StrutsBeanSelectionProviderTest.java
index b5aef2a59..c3d7131ff 100644
--- 
a/core/src/test/java/org/apache/struts2/config/StrutsBeanSelectionProviderTest.java
+++ 
b/core/src/test/java/org/apache/struts2/config/StrutsBeanSelectionProviderTest.java
@@ -25,7 +25,11 @@ import org.apache.struts2.text.LocalizedTextProvider;
 import org.apache.struts2.test.StubConfigurationProvider;
 import org.apache.struts2.StrutsConstants;
 
+import org.apache.struts2.components.escape.EscapeConfig;
+import org.apache.struts2.components.escape.StrutsTagEscaper;
+import org.apache.struts2.components.escape.TagEscaper;
 import org.apache.struts2.inject.ContainerBuilder;
+import org.apache.struts2.inject.Scope;
 import org.apache.struts2.util.location.LocatableProperties;
 
 public class StrutsBeanSelectionProviderTest extends XWorkTestCase {
@@ -48,4 +52,23 @@ public class StrutsBeanSelectionProviderTest extends 
XWorkTestCase {
         assertEquals("Replaced message for token tag", 
localizedTextProvider.findDefaultText("struts.messages.invalid.token", 
Locale.getDefault()));
     }
 
+    public void testTagEscaperIsSelectedByConstant() {
+        ContainerBuilder builder = new ContainerBuilder();
+        builder.factory(TagEscaper.class, "struts", StrutsTagEscaper.class, 
Scope.SINGLETON);
+        builder.factory(TagEscaper.class, "custom", CustomTagEscaper.class, 
Scope.SINGLETON);
+        LocatableProperties props = new LocatableProperties();
+        props.setProperty(StrutsConstants.STRUTS_TAG_ESCAPER, "custom");
+
+        new StrutsBeanSelectionProvider().register(builder, props);
+
+        assertTrue(builder.create(false).getInstance(TagEscaper.class) 
instanceof CustomTagEscaper);
+    }
+
+    public static class CustomTagEscaper implements TagEscaper {
+        @Override
+        public String escape(EscapeConfig config, String value) {
+            return value;
+        }
+    }
+
 }

Reply via email to