intl-tel-input 25.12.6 → 25.13.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2743,11 +2743,11 @@ var translateCountryNames = (countries, options) => {
2743
2743
  }
2744
2744
  }
2745
2745
  };
2746
- var processDialCodes = (countries, options) => {
2746
+ var processDialCodes = (countries) => {
2747
2747
  const dialCodes = /* @__PURE__ */ new Set();
2748
2748
  let dialCodeMaxLen = 0;
2749
2749
  const dialCodeToIso2Map = {};
2750
- const _addToDialCodeMap = (iso2, dialCode, priority) => {
2750
+ const _addToDialCodeMap = (iso2, dialCode) => {
2751
2751
  if (!iso2 || !dialCode) {
2752
2752
  return;
2753
2753
  }
@@ -2761,10 +2761,10 @@ var processDialCodes = (countries, options) => {
2761
2761
  if (iso2List.includes(iso2)) {
2762
2762
  return;
2763
2763
  }
2764
- const index = priority !== void 0 ? priority : iso2List.length;
2765
- iso2List[index] = iso2;
2764
+ iso2List.push(iso2);
2766
2765
  };
2767
- for (const c of countries) {
2766
+ const countriesSortedByPriority = [...countries].sort((a, b) => a.priority - b.priority);
2767
+ for (const c of countriesSortedByPriority) {
2768
2768
  if (!dialCodes.has(c.dialCode)) {
2769
2769
  dialCodes.add(c.dialCode);
2770
2770
  }
@@ -2772,14 +2772,7 @@ var processDialCodes = (countries, options) => {
2772
2772
  const partialDialCode = c.dialCode.substring(0, k);
2773
2773
  _addToDialCodeMap(c.iso2, partialDialCode);
2774
2774
  }
2775
- _addToDialCodeMap(c.iso2, c.dialCode, c.priority);
2776
- }
2777
- if (options.onlyCountries.length || options.excludeCountries.length) {
2778
- dialCodes.forEach((dialCode) => {
2779
- dialCodeToIso2Map[dialCode] = dialCodeToIso2Map[dialCode].filter(Boolean);
2780
- });
2781
- }
2782
- for (const c of countries) {
2775
+ _addToDialCodeMap(c.iso2, c.dialCode);
2783
2776
  if (c.areaCodes) {
2784
2777
  const rootIso2Code = dialCodeToIso2Map[c.dialCode][0];
2785
2778
  for (const areaCode of c.areaCodes) {
@@ -2914,8 +2907,7 @@ var Iti = class _Iti {
2914
2907
  this.promise = this._createInitPromises();
2915
2908
  this.countries = processAllCountries(this.options);
2916
2909
  const { dialCodes, dialCodeMaxLen, dialCodeToIso2Map } = processDialCodes(
2917
- this.countries,
2918
- this.options
2910
+ this.countries
2919
2911
  );
2920
2912
  this.dialCodes = dialCodes;
2921
2913
  this.dialCodeMaxLen = dialCodeMaxLen;
@@ -2926,6 +2918,45 @@ var Iti = class _Iti {
2926
2918
  static _getIsAndroid() {
2927
2919
  return typeof navigator !== "undefined" ? /Android/i.test(navigator.userAgent) : false;
2928
2920
  }
2921
+ _updateNumeralSet(str) {
2922
+ if (/[\u0660-\u0669]/.test(str)) {
2923
+ this.userNumeralSet = "arabic-indic";
2924
+ } else if (/[\u06F0-\u06F9]/.test(str)) {
2925
+ this.userNumeralSet = "persian";
2926
+ } else {
2927
+ this.userNumeralSet = "ascii";
2928
+ }
2929
+ }
2930
+ _mapAsciiToUserNumerals(str) {
2931
+ if (!this.userNumeralSet) {
2932
+ this._updateNumeralSet(this.ui.telInput.value);
2933
+ }
2934
+ if (this.userNumeralSet === "ascii") {
2935
+ return str;
2936
+ }
2937
+ const base = this.userNumeralSet === "arabic-indic" ? 1632 : 1776;
2938
+ return str.replace(/[0-9]/g, (d) => String.fromCharCode(base + Number(d)));
2939
+ }
2940
+ // Normalize Eastern Arabic (U+0660-0669) and Persian/Extended Arabic-Indic (U+06F0-06F9) numerals to ASCII 0-9
2941
+ _normaliseNumerals(str) {
2942
+ if (!str) {
2943
+ return "";
2944
+ }
2945
+ this._updateNumeralSet(str);
2946
+ if (this.userNumeralSet === "ascii") {
2947
+ return str;
2948
+ }
2949
+ const base = this.userNumeralSet === "arabic-indic" ? 1632 : 1776;
2950
+ const regex = this.userNumeralSet === "arabic-indic" ? /[\u0660-\u0669]/g : /[\u06F0-\u06F9]/g;
2951
+ return str.replace(regex, (ch) => String.fromCharCode(48 + (ch.charCodeAt(0) - base)));
2952
+ }
2953
+ _getTelInputValue() {
2954
+ const inputValue = this.ui.telInput.value.trim();
2955
+ return this._normaliseNumerals(inputValue);
2956
+ }
2957
+ _setTelInputValue(asciiValue) {
2958
+ this.ui.telInput.value = this._mapAsciiToUserNumerals(asciiValue);
2959
+ }
2929
2960
  _createInitPromises() {
2930
2961
  const autoCountryPromise = new Promise((resolve, reject) => {
2931
2962
  this.resolveAutoCountryPromise = resolve;
@@ -2960,8 +2991,9 @@ var Iti = class _Iti {
2960
2991
  //* 1. Extracting a dial code from the given number
2961
2992
  //* 2. Using explicit initialCountry
2962
2993
  _setInitialState(overrideAutoCountry = false) {
2963
- const attributeValue = this.ui.telInput.getAttribute("value");
2964
- const inputValue = this.ui.telInput.value;
2994
+ const attributeValueRaw = this.ui.telInput.getAttribute("value");
2995
+ const attributeValue = this._normaliseNumerals(attributeValueRaw);
2996
+ const inputValue = this._getTelInputValue();
2965
2997
  const useAttribute = attributeValue && attributeValue.startsWith("+") && (!inputValue || !inputValue.startsWith("+"));
2966
2998
  const val = useAttribute ? attributeValue : inputValue;
2967
2999
  const dialCode = this._getDialCode(val);
@@ -3132,35 +3164,34 @@ var Iti = class _Iti {
3132
3164
  countrySearch
3133
3165
  } = this.options;
3134
3166
  let userOverrideFormatting = false;
3135
- if (REGEX.ALPHA_UNICODE.test(this.ui.telInput.value)) {
3167
+ if (REGEX.ALPHA_UNICODE.test(this._getTelInputValue())) {
3136
3168
  userOverrideFormatting = true;
3137
3169
  }
3138
3170
  const handleInputEvent = (e) => {
3171
+ const inputValue = this._getTelInputValue();
3139
3172
  if (this.isAndroid && e?.data === "+" && separateDialCode && allowDropdown && countrySearch) {
3140
3173
  const currentCaretPos = this.ui.telInput.selectionStart || 0;
3141
- const valueBeforeCaret = this.ui.telInput.value.substring(
3142
- 0,
3143
- currentCaretPos - 1
3144
- );
3145
- const valueAfterCaret = this.ui.telInput.value.substring(currentCaretPos);
3146
- this.ui.telInput.value = valueBeforeCaret + valueAfterCaret;
3174
+ const valueBeforeCaret = inputValue.substring(0, currentCaretPos - 1);
3175
+ const valueAfterCaret = inputValue.substring(currentCaretPos);
3176
+ this._setTelInputValue(valueBeforeCaret + valueAfterCaret);
3147
3177
  this._openDropdownWithPlus();
3148
3178
  return;
3149
3179
  }
3150
- if (this._updateCountryFromNumber(this.ui.telInput.value)) {
3180
+ if (this._updateCountryFromNumber(inputValue)) {
3151
3181
  this._triggerCountryChange();
3152
3182
  }
3153
3183
  const isFormattingChar = e?.data && REGEX.NON_PLUS_NUMERIC.test(e.data);
3154
- const isPaste = e?.inputType === INPUT_TYPES.PASTE && this.ui.telInput.value;
3184
+ const isPaste = e?.inputType === INPUT_TYPES.PASTE && inputValue;
3155
3185
  if (isFormattingChar || isPaste && !strictMode) {
3156
3186
  userOverrideFormatting = true;
3157
- } else if (!REGEX.NON_PLUS_NUMERIC.test(this.ui.telInput.value)) {
3187
+ } else if (!REGEX.NON_PLUS_NUMERIC.test(inputValue)) {
3158
3188
  userOverrideFormatting = false;
3159
3189
  }
3160
3190
  const isSetNumber = e?.detail && e.detail["isSetNumber"];
3161
- if (formatAsYouType && !userOverrideFormatting && !isSetNumber) {
3191
+ const isAscii = this.userNumeralSet === "ascii";
3192
+ if (formatAsYouType && !userOverrideFormatting && !isSetNumber && isAscii) {
3162
3193
  const currentCaretPos = this.ui.telInput.selectionStart || 0;
3163
- const valueBeforeCaret = this.ui.telInput.value.substring(
3194
+ const valueBeforeCaret = inputValue.substring(
3164
3195
  0,
3165
3196
  currentCaretPos
3166
3197
  );
@@ -3172,7 +3203,7 @@ var Iti = class _Iti {
3172
3203
  const fullNumber = this._getFullNumber();
3173
3204
  const formattedValue = formatNumberAsYouType(
3174
3205
  fullNumber,
3175
- this.ui.telInput.value,
3206
+ inputValue,
3176
3207
  intlTelInput.utils,
3177
3208
  this.selectedCountryData,
3178
3209
  this.options.separateDialCode
@@ -3183,7 +3214,7 @@ var Iti = class _Iti {
3183
3214
  currentCaretPos,
3184
3215
  isDeleteForwards
3185
3216
  );
3186
- this.ui.telInput.value = formattedValue;
3217
+ this._setTelInputValue(formattedValue);
3187
3218
  this.ui.telInput.setSelectionRange(newCaretPos, newCaretPos);
3188
3219
  }
3189
3220
  };
@@ -3206,12 +3237,18 @@ var Iti = class _Iti {
3206
3237
  return;
3207
3238
  }
3208
3239
  if (strictMode) {
3209
- const value = this.ui.telInput.value;
3210
- const alreadyHasPlus = value.startsWith("+");
3240
+ const inputValue = this._getTelInputValue();
3241
+ const alreadyHasPlus = inputValue.startsWith("+");
3211
3242
  const isInitialPlus = !alreadyHasPlus && this.ui.telInput.selectionStart === 0 && e.key === "+";
3212
- const isNumeric = /^[0-9]$/.test(e.key);
3243
+ const normalisedKey = this._normaliseNumerals(e.key);
3244
+ const isNumeric = /^[0-9]$/.test(normalisedKey);
3213
3245
  const isAllowedChar = separateDialCode ? isNumeric : isInitialPlus || isNumeric;
3214
- const newValue = value.slice(0, this.ui.telInput.selectionStart) + e.key + value.slice(this.ui.telInput.selectionEnd);
3246
+ const input = this.ui.telInput;
3247
+ const selStart = input.selectionStart;
3248
+ const selEnd = input.selectionEnd;
3249
+ const before = inputValue.slice(0, selStart);
3250
+ const after = inputValue.slice(selEnd);
3251
+ const newValue = before + e.key + after;
3215
3252
  const newFullNumber = this._getFullNumber(newValue);
3216
3253
  const coreNumber = intlTelInput.utils.getCoreNumber(
3217
3254
  newFullNumber,
@@ -3238,34 +3275,38 @@ var Iti = class _Iti {
3238
3275
  const input = this.ui.telInput;
3239
3276
  const selStart = input.selectionStart;
3240
3277
  const selEnd = input.selectionEnd;
3241
- const before = input.value.slice(0, selStart);
3242
- const after = input.value.slice(selEnd);
3278
+ const inputValue = this._getTelInputValue();
3279
+ const before = inputValue.slice(0, selStart);
3280
+ const after = inputValue.slice(selEnd);
3243
3281
  const iso2 = this.selectedCountryData.iso2;
3244
- const pasted = e.clipboardData.getData("text");
3282
+ const pastedRaw = e.clipboardData.getData("text");
3283
+ const pasted = this._normaliseNumerals(pastedRaw);
3245
3284
  const initialCharSelected = selStart === 0 && selEnd > 0;
3246
- const allowLeadingPlus = !input.value.startsWith("+") || initialCharSelected;
3285
+ const allowLeadingPlus = !inputValue.startsWith("+") || initialCharSelected;
3247
3286
  const allowedChars = pasted.replace(REGEX.NON_PLUS_NUMERIC_GLOBAL, "");
3248
3287
  const hasLeadingPlus = allowedChars.startsWith("+");
3249
3288
  const numerics = allowedChars.replace(/\+/g, "");
3250
3289
  const sanitised = hasLeadingPlus && allowLeadingPlus ? `+${numerics}` : numerics;
3251
3290
  let newVal = before + sanitised + after;
3252
- let coreNumber = intlTelInput.utils.getCoreNumber(newVal, iso2);
3253
- while (coreNumber.length === 0 && newVal.length > 0) {
3254
- newVal = newVal.slice(0, -1);
3255
- coreNumber = intlTelInput.utils.getCoreNumber(newVal, iso2);
3256
- }
3257
- if (!coreNumber) {
3258
- return;
3259
- }
3260
- if (this.maxCoreNumberLength && coreNumber.length > this.maxCoreNumberLength) {
3261
- if (input.selectionEnd === input.value.length) {
3262
- const trimLength = coreNumber.length - this.maxCoreNumberLength;
3263
- newVal = newVal.slice(0, newVal.length - trimLength);
3264
- } else {
3291
+ if (newVal.length > 5) {
3292
+ let coreNumber = intlTelInput.utils.getCoreNumber(newVal, iso2);
3293
+ while (coreNumber.length === 0 && newVal.length > 0) {
3294
+ newVal = newVal.slice(0, -1);
3295
+ coreNumber = intlTelInput.utils.getCoreNumber(newVal, iso2);
3296
+ }
3297
+ if (!coreNumber) {
3265
3298
  return;
3266
3299
  }
3300
+ if (this.maxCoreNumberLength && coreNumber.length > this.maxCoreNumberLength) {
3301
+ if (input.selectionEnd === inputValue.length) {
3302
+ const trimLength = coreNumber.length - this.maxCoreNumberLength;
3303
+ newVal = newVal.slice(0, newVal.length - trimLength);
3304
+ } else {
3305
+ return;
3306
+ }
3307
+ }
3267
3308
  }
3268
- input.value = newVal;
3309
+ this._setTelInputValue(newVal);
3269
3310
  const caretPos = selStart + sanitised.length;
3270
3311
  input.setSelectionRange(caretPos, caretPos);
3271
3312
  input.dispatchEvent(new InputEvent("input", { bubbles: true }));
@@ -3517,7 +3558,7 @@ var Iti = class _Iti {
3517
3558
  );
3518
3559
  }
3519
3560
  number = this._beforeSetNumber(number);
3520
- this.ui.telInput.value = number;
3561
+ this._setTelInputValue(number);
3521
3562
  }
3522
3563
  //* Check if need to select a new country based on the given number
3523
3564
  //* Note: called from _setInitialState, keyup handler, setNumber.
@@ -3691,7 +3732,8 @@ var Iti = class _Iti {
3691
3732
  const dialCode = listItem.dataset[DATA_KEYS.DIAL_CODE];
3692
3733
  this._updateDialCode(dialCode);
3693
3734
  if (this.options.formatOnDisplay) {
3694
- this._updateValFromNumber(this.ui.telInput.value);
3735
+ const inputValue = this._getTelInputValue();
3736
+ this._updateValFromNumber(inputValue);
3695
3737
  }
3696
3738
  this.ui.telInput.focus();
3697
3739
  if (countryChanged) {
@@ -3722,7 +3764,7 @@ var Iti = class _Iti {
3722
3764
  //* Replace any existing dial code with the new one
3723
3765
  //* Note: called from _selectListItem and setCountry
3724
3766
  _updateDialCode(newDialCodeBare) {
3725
- const inputVal = this.ui.telInput.value;
3767
+ const inputVal = this._getTelInputValue();
3726
3768
  const newDialCode = `+${newDialCodeBare}`;
3727
3769
  let newNumber;
3728
3770
  if (inputVal.startsWith("+")) {
@@ -3732,7 +3774,7 @@ var Iti = class _Iti {
3732
3774
  } else {
3733
3775
  newNumber = newDialCode;
3734
3776
  }
3735
- this.ui.telInput.value = newNumber;
3777
+ this._setTelInputValue(newNumber);
3736
3778
  }
3737
3779
  }
3738
3780
  //* Try and extract a valid international dial code from a full telephone number.
@@ -3769,7 +3811,7 @@ var Iti = class _Iti {
3769
3811
  }
3770
3812
  //* Get the input val, adding the dial code if separateDialCode is enabled.
3771
3813
  _getFullNumber(overrideVal) {
3772
- const val = overrideVal || this.ui.telInput.value.trim();
3814
+ const val = overrideVal ? this._normaliseNumerals(overrideVal) : this._getTelInputValue();
3773
3815
  const { dialCode } = this.selectedCountryData;
3774
3816
  let prefix;
3775
3817
  const numericVal = getNumeric(val);
@@ -3812,8 +3854,9 @@ var Iti = class _Iti {
3812
3854
  //* This is called when the utils request completes.
3813
3855
  handleUtils() {
3814
3856
  if (intlTelInput.utils) {
3815
- if (this.ui.telInput.value) {
3816
- this._updateValFromNumber(this.ui.telInput.value);
3857
+ const inputValue = this._getTelInputValue();
3858
+ if (inputValue) {
3859
+ this._updateValFromNumber(inputValue);
3817
3860
  }
3818
3861
  if (this.selectedCountryData.iso2) {
3819
3862
  this._updatePlaceholder();
@@ -3856,11 +3899,13 @@ var Iti = class _Iti {
3856
3899
  getNumber(format) {
3857
3900
  if (intlTelInput.utils) {
3858
3901
  const { iso2 } = this.selectedCountryData;
3859
- return intlTelInput.utils.formatNumber(
3860
- this._getFullNumber(),
3902
+ const fullNumber = this._getFullNumber();
3903
+ const formattedNumber = intlTelInput.utils.formatNumber(
3904
+ fullNumber,
3861
3905
  iso2,
3862
3906
  format
3863
3907
  );
3908
+ return this._mapAsciiToUserNumerals(formattedNumber);
3864
3909
  }
3865
3910
  return "";
3866
3911
  }
@@ -3948,15 +3993,17 @@ var Iti = class _Iti {
3948
3993
  this._setCountry(iso2Lower);
3949
3994
  this._updateDialCode(this.selectedCountryData.dialCode);
3950
3995
  if (this.options.formatOnDisplay) {
3951
- this._updateValFromNumber(this.ui.telInput.value);
3996
+ const inputValue = this._getTelInputValue();
3997
+ this._updateValFromNumber(inputValue);
3952
3998
  }
3953
3999
  this._triggerCountryChange();
3954
4000
  }
3955
4001
  }
3956
4002
  //* Set the input value and update the country.
3957
4003
  setNumber(number) {
3958
- const countryChanged = this._updateCountryFromNumber(number);
3959
- this._updateValFromNumber(number);
4004
+ const normalisedNumber = this._normaliseNumerals(number);
4005
+ const countryChanged = this._updateCountryFromNumber(normalisedNumber);
4006
+ this._updateValFromNumber(normalisedNumber);
3960
4007
  if (countryChanged) {
3961
4008
  this._triggerCountryChange();
3962
4009
  }
@@ -4041,7 +4088,7 @@ var intlTelInput = Object.assign(
4041
4088
  attachUtils,
4042
4089
  startedLoadingUtilsScript: false,
4043
4090
  startedLoadingAutoCountry: false,
4044
- version: "25.12.6"
4091
+ version: "25.13.1"
4045
4092
  }
4046
4093
  );
4047
4094
  var intl_tel_input_default = intlTelInput;