Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 50 additions & 41 deletions lib/extensions/inputmask.numeric.extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,19 +185,26 @@ function genMask(opts) {
opts.numericInput = true;
}

// Register the groupSeparator as a definition *before* escaping prefix/suffix
// so that a groupSeparator character appearing inside the prefix or suffix
// (e.g. prefix: "€ " with groupSeparator: " ") is escaped as a literal instead
// of being picked up by the lexer as a dynamic placeholder. Fixes #2262.
if (
opts.groupSeparator !== "" &&
opts.definitions[opts.groupSeparator] === undefined
) {
opts.definitions[opts.groupSeparator] = {
validator: "[" + opts.groupSeparator + "]",
placeholder: opts.groupSeparator,
static: true,
generated: true // forced marker as generated input
};
}

let mask = "[+]",
altMask;
mask += autoEscape(opts.prefix, opts);
if (opts.groupSeparator !== "") {
if (opts.definitions[opts.groupSeparator] === undefined) {
// update separator definition
opts.definitions[opts.groupSeparator] = {};
opts.definitions[opts.groupSeparator].validator =
"[" + opts.groupSeparator + "]";
opts.definitions[opts.groupSeparator].placeholder = opts.groupSeparator;
opts.definitions[opts.groupSeparator].static = true;
opts.definitions[opts.groupSeparator].generated = true; // forced marker as generated input
}
mask += opts._mask(opts);
} else {
mask += "9{+}";
Expand Down Expand Up @@ -274,22 +281,27 @@ function decimalValidator(chrs, maskset, pos, strict, opts) {
return result;
}

// Match the numeric body of the (reversed) buffer against the prefix/suffix
// wrapper, returning the captured "number" group or null if the match fails.
function matchNumberInWrapper(buffer, opts) {
const match = new RegExp(
"(^" +
(opts.negationSymbol.front !== ""
? escapeRegex(opts.negationSymbol.front) + "?"
: "") +
escapeRegex(opts.prefix) +
")(.*)(" +
escapeRegex(opts.suffix) +
(opts.negationSymbol.back !== ""
? escapeRegex(opts.negationSymbol.back) + "?"
: "") +
"$)"
).exec(buffer.slice().reverse().join(""));
return match ? match[2] : null;
}

function checkForLeadingZeroes(buffer, opts) {
// check leading zeros
let numberMatches = new RegExp(
"(^" +
(opts.negationSymbol.front !== ""
? escapeRegex(opts.negationSymbol.front) + "?"
: "") +
escapeRegex(opts.prefix) +
")(.*)(" +
escapeRegex(opts.suffix) +
(opts.negationSymbol.back != ""
? escapeRegex(opts.negationSymbol.back) + "?"
: "") +
"$)"
).exec(buffer.slice().reverse().join("")),
number = numberMatches ? numberMatches[2] : "",
let number = matchNumberInWrapper(buffer, opts) ?? "",
leadingzeroes = false;
if (number) {
number = number.split(opts.radixPoint.charAt(0))[0];
Expand Down Expand Up @@ -729,23 +741,20 @@ Inputmask.extendAliases({
}
}
if (buffer[buffer.length - 1] === opts.negationSymbol.front) {
// strip negation symbol on blur when value is 0
const nmbrMtchs = new RegExp(
"(^" +
(opts.negationSymbol.front != ""
? escapeRegex(opts.negationSymbol.front) + "?"
: "") +
escapeRegex(opts.prefix) +
")(.*)(" +
escapeRegex(opts.suffix) +
(opts.negationSymbol.back != ""
? escapeRegex(opts.negationSymbol.back) + "?"
: "") +
"$)"
).exec(stripBuffer(buffer.slice(), true).reverse().join("")),
number = nmbrMtchs ? nmbrMtchs[2] : "";
if (number == 0) {
result = { refreshFromBuffer: true, buffer: [0] };
// strip negation symbol on blur when value is 0. Match on the
// raw buffer so a groupSeparator-colliding char inside the
// prefix/suffix (e.g. space in "€ ") is preserved; normalize
// only the extracted number group. Fixes #2262.
const rawNumber = matchNumberInWrapper(buffer, opts);
if (rawNumber !== null) {
let number = rawNumber
.split(opts.groupSeparator)
.join("");
if (opts.radixPoint)
number = number.replace(opts.radixPoint, ".");
if (number === "" || number == 0) {
result = { refreshFromBuffer: true, buffer: [0] };
}
}
} else if (opts.radixPoint !== "") {
// strip radixpoint on blur when it is the latest char
Expand Down
Loading