Skip to content

Commit

Permalink
Fix #11958: InputMask only strip slotChar if using optional mask (#11959
Browse files Browse the repository at this point in the history
)
  • Loading branch information
melloware committed May 19, 2024
1 parent 8ad14ae commit 6fa609a
Showing 1 changed file with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,11 @@ public void decode(FacesContext context, UIComponent component) {
String submittedValue = context.getExternalContext().getRequestParameterMap().get(clientId);

if (submittedValue != null) {
// strip mask characters in case of optional values
submittedValue = submittedValue.replace(inputMask.getSlotChar(), Constants.EMPTY_STRING);
// #6469/#11958 strip mask characters in case of optional values
String mask = inputMask.getMask();
if (isMaskOptional(mask)) {
submittedValue = submittedValue.replace(inputMask.getSlotChar(), Constants.EMPTY_STRING);
}

if (inputMask.isValidateMask() && !LangUtils.isEmpty(submittedValue) && LangUtils.isNotBlank(mask)) {
Pattern pattern = translateMaskIntoRegex(context, mask);
Expand Down Expand Up @@ -150,6 +152,17 @@ else if (REGEX_METACHARS.indexOf(c) >= 0) {
return optional ? (translated + "?") : translated;
}

/**
* Checks if the given mask string contains any optional mask characters.
* In this context, an optional mask character is defined as either '[' or ']'.
*
* @param mask the mask string to check
* @return {@code true} if the mask contains either '[' or ']'; {@code false} otherwise
*/
protected boolean isMaskOptional(String mask) {
return mask.contains("[") || mask.contains("]");
}

@Override
public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
InputMask inputMask = (InputMask) component;
Expand All @@ -166,7 +179,7 @@ protected void encodeScript(FacesContext context, InputMask inputMask) throws IO
if (mask != null) {
// autoclear must be false when using optional mask
boolean autoClear = inputMask.isAutoClear();
if (mask.contains("[") || mask.contains("]")) {
if (isMaskOptional(mask)) {
autoClear = false;
}
wb.attr("mask", mask)
Expand Down

0 comments on commit 6fa609a

Please sign in to comment.