UNPKG

devextreme

Version:

JavaScript/TypeScript Component Suite for Responsive Web Development

252 lines (251 loc) • 8.35 kB
/** * DevExtreme (esm/__internal/ui/text_box/text_editor.mask.rule.js) * Version: 25.2.7 * Build date: Tue May 05 2026 * * Copyright (c) 2012 - 2026 Developer Express Inc. ALL RIGHTS RESERVED * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/ */ import { extend } from "../../../core/utils/extend"; import { isFunction } from "../../../core/utils/type"; const EMPTY_CHAR = " "; class BaseMaskRule { constructor(config) { this._value = " "; extend(this, config) } next(rule) { if (!arguments.length) { return this._next } this._next = rule; return } _prepareHandlingArgs(args, config) { const configuration = config ?? {}; const handlingProperty = Object.prototype.hasOwnProperty.call(args, "value") ? "value" : "text"; const finalConfig = Object.assign({}, args, { start: configuration.start ?? args.start, length: configuration.length ?? args.length, index: (args.index ?? 0) + 1 }); finalConfig[handlingProperty] = configuration.str ?? args[handlingProperty]; return finalConfig } first() { let index = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : 0; const newIndex = index + 1; return this.next().first(newIndex) } isAccepted(caret) { return false } adjustedCaret(caret, isForwardDirection, char) { return isForwardDirection ? this._adjustedForward(caret, 0, char) : this._adjustedBackward(caret, 0, char) } _adjustedForward(caret, index, char) {} _adjustedBackward(caret, index, char) {} isValid(args) {} reset() {} clear(args) {} text() {} value() {} rawValue() {} handle(args) {} } export class EmptyMaskRule extends BaseMaskRule { next() {} handle() { return 0 } text() { return "" } value() { return "" } first() { return 0 } rawValue() { return "" } adjustedCaret() { return 0 } isValid() { return true } } export class MaskRule extends BaseMaskRule { text() { const isValueEqualEmptyChar = " " === this._value; const value = isValueEqualEmptyChar ? this.maskChar : this._value; const finalValue = `${value}${this.next().text()??""}`; return finalValue } value() { const finalValue = `${this._value}${this.next().value()??""}`; return finalValue } rawValue() { const finalValue = `${this._value}${this.next().rawValue()??""}`; return finalValue } handle(args) { const str = Object.prototype.hasOwnProperty.call(args, "value") ? args.value : args.text; if (!str || !str.length || !args.length) { return 0 } if (args.start) { return this.next().handle(this._prepareHandlingArgs(args, { start: args.start - 1 })) } const char = str[0]; const rest = str.substring(1); this._tryAcceptChar(char, args); const isAccepted = this._accepted(); const rule = isAccepted ? this.next() : this; const handlingArgs = this._prepareHandlingArgs(args, { str: rest, length: args.length - 1 }); const handledResult = rule.handle(handlingArgs); const result = isAccepted ? handledResult + 1 : handledResult; return result } clear(args) { this._tryAcceptChar(" ", args); this.next().clear(this._prepareHandlingArgs(args)) } reset() { this._accepted(false); this.next().reset() } _tryAcceptChar(char, args) { var _args$fullText, _args$fullText2; this._accepted(false); if (!this._isAllowed(char, args)) { return } const acceptedChar = " " === char ? this.maskChar ?? "" : char; const fullTextSubstring1 = (null === (_args$fullText = args.fullText) || void 0 === _args$fullText ? void 0 : _args$fullText.substring(0, args.index)) ?? ""; const fullTextSubstring2 = (null === (_args$fullText2 = args.fullText) || void 0 === _args$fullText2 ? void 0 : _args$fullText2.substring((args.index ?? 0) + 1)) ?? ""; args.fullText = `${fullTextSubstring1}${acceptedChar}${fullTextSubstring2}`; this._accepted(true); this._value = char } _accepted(value) { if (!arguments.length) { return !!this._isAccepted } this._isAccepted = !!value } first() { let index = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : 0; return " " === this._value ? index : super.first(index) } _isAllowed(char, args) { if (" " === char) { return true } return this._isValid(char, args) } _isValid(char, args) { const { allowedChars: allowedChars } = this; if (allowedChars instanceof RegExp) { return allowedChars.test(char) } if (isFunction(allowedChars)) { return allowedChars(char, null === args || void 0 === args ? void 0 : args.index, null === args || void 0 === args ? void 0 : args.fullText) } if (Array.isArray(allowedChars)) { return allowedChars.includes(char) } return allowedChars === char } isAccepted(caret) { return Boolean(0 === caret ? this._accepted() : this.next().isAccepted(caret - 1)) } _adjustedForward(caret, index, char) { if (index >= caret) { return index } return this.next()._adjustedForward(caret, index + 1, char) || index + 1 } _adjustedBackward(caret, index) { if (index >= caret - 1) { return caret } return this.next()._adjustedBackward(caret, index + 1) || index + 1 } isValid(args) { return this._isValid(this._value, args) && this.next().isValid(this._prepareHandlingArgs(args)) } } export class StubMaskRule extends MaskRule { value() { return this.next().value() } handle(args) { const hasValueProperty = Object.prototype.hasOwnProperty.call(args, "value"); const str = hasValueProperty ? args.value : args.text; if (!(null !== str && void 0 !== str && str.length) || !args.length) { return 0 } if (args.start || hasValueProperty) { const handlingArgs = this._prepareHandlingArgs(args, { start: args.start && args.start - 1 }); return this.next().handle(handlingArgs) } const char = str[0]; const rest = str.substring(1); this._tryAcceptChar(char); const nextArgs = this._isAllowed(char) ? this._prepareHandlingArgs(args, { str: rest, length: args.length - 1 }) : args; return (this.next().handle(nextArgs) ?? 0) + 1 } clear(args) { this._accepted(false); this.next().clear(this._prepareHandlingArgs(args)) } _tryAcceptChar(char) { this._accepted(this._isValid(char)) } _isValid(char) { return char === this.maskChar } first() { let index = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : 0; const newIndex = index + 1; return this.next().first(newIndex) } _adjustedForward(caret, index, char) { if (index >= caret && char === this.maskChar) { return index } if (caret === index + 1 && this._accepted()) { return caret } return this.next()._adjustedForward(caret, index + 1, char) } _adjustedBackward(caret, index) { if (index >= caret - 1) { return 0 } return this.next()._adjustedBackward(caret, index + 1) } isValid(args) { return Boolean(this.next().isValid(this._prepareHandlingArgs(args))) } }