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.
@@ -2707,11 +2707,11 @@ var translateCountryNames = (countries, options) => {
2707
2707
  }
2708
2708
  }
2709
2709
  };
2710
- var processDialCodes = (countries, options) => {
2710
+ var processDialCodes = (countries) => {
2711
2711
  const dialCodes = /* @__PURE__ */ new Set();
2712
2712
  let dialCodeMaxLen = 0;
2713
2713
  const dialCodeToIso2Map = {};
2714
- const _addToDialCodeMap = (iso2, dialCode, priority) => {
2714
+ const _addToDialCodeMap = (iso2, dialCode) => {
2715
2715
  if (!iso2 || !dialCode) {
2716
2716
  return;
2717
2717
  }
@@ -2725,10 +2725,10 @@ var processDialCodes = (countries, options) => {
2725
2725
  if (iso2List.includes(iso2)) {
2726
2726
  return;
2727
2727
  }
2728
- const index = priority !== void 0 ? priority : iso2List.length;
2729
- iso2List[index] = iso2;
2728
+ iso2List.push(iso2);
2730
2729
  };
2731
- for (const c of countries) {
2730
+ const countriesSortedByPriority = [...countries].sort((a, b) => a.priority - b.priority);
2731
+ for (const c of countriesSortedByPriority) {
2732
2732
  if (!dialCodes.has(c.dialCode)) {
2733
2733
  dialCodes.add(c.dialCode);
2734
2734
  }
@@ -2736,14 +2736,7 @@ var processDialCodes = (countries, options) => {
2736
2736
  const partialDialCode = c.dialCode.substring(0, k);
2737
2737
  _addToDialCodeMap(c.iso2, partialDialCode);
2738
2738
  }
2739
- _addToDialCodeMap(c.iso2, c.dialCode, c.priority);
2740
- }
2741
- if (options.onlyCountries.length || options.excludeCountries.length) {
2742
- dialCodes.forEach((dialCode) => {
2743
- dialCodeToIso2Map[dialCode] = dialCodeToIso2Map[dialCode].filter(Boolean);
2744
- });
2745
- }
2746
- for (const c of countries) {
2739
+ _addToDialCodeMap(c.iso2, c.dialCode);
2747
2740
  if (c.areaCodes) {
2748
2741
  const rootIso2Code = dialCodeToIso2Map[c.dialCode][0];
2749
2742
  for (const areaCode of c.areaCodes) {
@@ -2878,8 +2871,7 @@ var Iti = class _Iti {
2878
2871
  this.promise = this._createInitPromises();
2879
2872
  this.countries = processAllCountries(this.options);
2880
2873
  const { dialCodes, dialCodeMaxLen, dialCodeToIso2Map } = processDialCodes(
2881
- this.countries,
2882
- this.options
2874
+ this.countries
2883
2875
  );
2884
2876
  this.dialCodes = dialCodes;
2885
2877
  this.dialCodeMaxLen = dialCodeMaxLen;
@@ -2890,6 +2882,45 @@ var Iti = class _Iti {
2890
2882
  static _getIsAndroid() {
2891
2883
  return typeof navigator !== "undefined" ? /Android/i.test(navigator.userAgent) : false;
2892
2884
  }
2885
+ _updateNumeralSet(str) {
2886
+ if (/[\u0660-\u0669]/.test(str)) {
2887
+ this.userNumeralSet = "arabic-indic";
2888
+ } else if (/[\u06F0-\u06F9]/.test(str)) {
2889
+ this.userNumeralSet = "persian";
2890
+ } else {
2891
+ this.userNumeralSet = "ascii";
2892
+ }
2893
+ }
2894
+ _mapAsciiToUserNumerals(str) {
2895
+ if (!this.userNumeralSet) {
2896
+ this._updateNumeralSet(this.ui.telInput.value);
2897
+ }
2898
+ if (this.userNumeralSet === "ascii") {
2899
+ return str;
2900
+ }
2901
+ const base = this.userNumeralSet === "arabic-indic" ? 1632 : 1776;
2902
+ return str.replace(/[0-9]/g, (d) => String.fromCharCode(base + Number(d)));
2903
+ }
2904
+ // Normalize Eastern Arabic (U+0660-0669) and Persian/Extended Arabic-Indic (U+06F0-06F9) numerals to ASCII 0-9
2905
+ _normaliseNumerals(str) {
2906
+ if (!str) {
2907
+ return "";
2908
+ }
2909
+ this._updateNumeralSet(str);
2910
+ if (this.userNumeralSet === "ascii") {
2911
+ return str;
2912
+ }
2913
+ const base = this.userNumeralSet === "arabic-indic" ? 1632 : 1776;
2914
+ const regex = this.userNumeralSet === "arabic-indic" ? /[\u0660-\u0669]/g : /[\u06F0-\u06F9]/g;
2915
+ return str.replace(regex, (ch) => String.fromCharCode(48 + (ch.charCodeAt(0) - base)));
2916
+ }
2917
+ _getTelInputValue() {
2918
+ const inputValue = this.ui.telInput.value.trim();
2919
+ return this._normaliseNumerals(inputValue);
2920
+ }
2921
+ _setTelInputValue(asciiValue) {
2922
+ this.ui.telInput.value = this._mapAsciiToUserNumerals(asciiValue);
2923
+ }
2893
2924
  _createInitPromises() {
2894
2925
  const autoCountryPromise = new Promise((resolve, reject) => {
2895
2926
  this.resolveAutoCountryPromise = resolve;
@@ -2924,8 +2955,9 @@ var Iti = class _Iti {
2924
2955
  //* 1. Extracting a dial code from the given number
2925
2956
  //* 2. Using explicit initialCountry
2926
2957
  _setInitialState(overrideAutoCountry = false) {
2927
- const attributeValue = this.ui.telInput.getAttribute("value");
2928
- const inputValue = this.ui.telInput.value;
2958
+ const attributeValueRaw = this.ui.telInput.getAttribute("value");
2959
+ const attributeValue = this._normaliseNumerals(attributeValueRaw);
2960
+ const inputValue = this._getTelInputValue();
2929
2961
  const useAttribute = attributeValue && attributeValue.startsWith("+") && (!inputValue || !inputValue.startsWith("+"));
2930
2962
  const val = useAttribute ? attributeValue : inputValue;
2931
2963
  const dialCode = this._getDialCode(val);
@@ -3096,35 +3128,34 @@ var Iti = class _Iti {
3096
3128
  countrySearch
3097
3129
  } = this.options;
3098
3130
  let userOverrideFormatting = false;
3099
- if (REGEX.ALPHA_UNICODE.test(this.ui.telInput.value)) {
3131
+ if (REGEX.ALPHA_UNICODE.test(this._getTelInputValue())) {
3100
3132
  userOverrideFormatting = true;
3101
3133
  }
3102
3134
  const handleInputEvent = (e) => {
3135
+ const inputValue = this._getTelInputValue();
3103
3136
  if (this.isAndroid && e?.data === "+" && separateDialCode && allowDropdown && countrySearch) {
3104
3137
  const currentCaretPos = this.ui.telInput.selectionStart || 0;
3105
- const valueBeforeCaret = this.ui.telInput.value.substring(
3106
- 0,
3107
- currentCaretPos - 1
3108
- );
3109
- const valueAfterCaret = this.ui.telInput.value.substring(currentCaretPos);
3110
- this.ui.telInput.value = valueBeforeCaret + valueAfterCaret;
3138
+ const valueBeforeCaret = inputValue.substring(0, currentCaretPos - 1);
3139
+ const valueAfterCaret = inputValue.substring(currentCaretPos);
3140
+ this._setTelInputValue(valueBeforeCaret + valueAfterCaret);
3111
3141
  this._openDropdownWithPlus();
3112
3142
  return;
3113
3143
  }
3114
- if (this._updateCountryFromNumber(this.ui.telInput.value)) {
3144
+ if (this._updateCountryFromNumber(inputValue)) {
3115
3145
  this._triggerCountryChange();
3116
3146
  }
3117
3147
  const isFormattingChar = e?.data && REGEX.NON_PLUS_NUMERIC.test(e.data);
3118
- const isPaste = e?.inputType === INPUT_TYPES.PASTE && this.ui.telInput.value;
3148
+ const isPaste = e?.inputType === INPUT_TYPES.PASTE && inputValue;
3119
3149
  if (isFormattingChar || isPaste && !strictMode) {
3120
3150
  userOverrideFormatting = true;
3121
- } else if (!REGEX.NON_PLUS_NUMERIC.test(this.ui.telInput.value)) {
3151
+ } else if (!REGEX.NON_PLUS_NUMERIC.test(inputValue)) {
3122
3152
  userOverrideFormatting = false;
3123
3153
  }
3124
3154
  const isSetNumber = e?.detail && e.detail["isSetNumber"];
3125
- if (formatAsYouType && !userOverrideFormatting && !isSetNumber) {
3155
+ const isAscii = this.userNumeralSet === "ascii";
3156
+ if (formatAsYouType && !userOverrideFormatting && !isSetNumber && isAscii) {
3126
3157
  const currentCaretPos = this.ui.telInput.selectionStart || 0;
3127
- const valueBeforeCaret = this.ui.telInput.value.substring(
3158
+ const valueBeforeCaret = inputValue.substring(
3128
3159
  0,
3129
3160
  currentCaretPos
3130
3161
  );
@@ -3136,7 +3167,7 @@ var Iti = class _Iti {
3136
3167
  const fullNumber = this._getFullNumber();
3137
3168
  const formattedValue = formatNumberAsYouType(
3138
3169
  fullNumber,
3139
- this.ui.telInput.value,
3170
+ inputValue,
3140
3171
  intlTelInput.utils,
3141
3172
  this.selectedCountryData,
3142
3173
  this.options.separateDialCode
@@ -3147,7 +3178,7 @@ var Iti = class _Iti {
3147
3178
  currentCaretPos,
3148
3179
  isDeleteForwards
3149
3180
  );
3150
- this.ui.telInput.value = formattedValue;
3181
+ this._setTelInputValue(formattedValue);
3151
3182
  this.ui.telInput.setSelectionRange(newCaretPos, newCaretPos);
3152
3183
  }
3153
3184
  };
@@ -3170,12 +3201,18 @@ var Iti = class _Iti {
3170
3201
  return;
3171
3202
  }
3172
3203
  if (strictMode) {
3173
- const value = this.ui.telInput.value;
3174
- const alreadyHasPlus = value.startsWith("+");
3204
+ const inputValue = this._getTelInputValue();
3205
+ const alreadyHasPlus = inputValue.startsWith("+");
3175
3206
  const isInitialPlus = !alreadyHasPlus && this.ui.telInput.selectionStart === 0 && e.key === "+";
3176
- const isNumeric = /^[0-9]$/.test(e.key);
3207
+ const normalisedKey = this._normaliseNumerals(e.key);
3208
+ const isNumeric = /^[0-9]$/.test(normalisedKey);
3177
3209
  const isAllowedChar = separateDialCode ? isNumeric : isInitialPlus || isNumeric;
3178
- const newValue = value.slice(0, this.ui.telInput.selectionStart) + e.key + value.slice(this.ui.telInput.selectionEnd);
3210
+ const input = this.ui.telInput;
3211
+ const selStart = input.selectionStart;
3212
+ const selEnd = input.selectionEnd;
3213
+ const before = inputValue.slice(0, selStart);
3214
+ const after = inputValue.slice(selEnd);
3215
+ const newValue = before + e.key + after;
3179
3216
  const newFullNumber = this._getFullNumber(newValue);
3180
3217
  const coreNumber = intlTelInput.utils.getCoreNumber(
3181
3218
  newFullNumber,
@@ -3202,34 +3239,38 @@ var Iti = class _Iti {
3202
3239
  const input = this.ui.telInput;
3203
3240
  const selStart = input.selectionStart;
3204
3241
  const selEnd = input.selectionEnd;
3205
- const before = input.value.slice(0, selStart);
3206
- const after = input.value.slice(selEnd);
3242
+ const inputValue = this._getTelInputValue();
3243
+ const before = inputValue.slice(0, selStart);
3244
+ const after = inputValue.slice(selEnd);
3207
3245
  const iso2 = this.selectedCountryData.iso2;
3208
- const pasted = e.clipboardData.getData("text");
3246
+ const pastedRaw = e.clipboardData.getData("text");
3247
+ const pasted = this._normaliseNumerals(pastedRaw);
3209
3248
  const initialCharSelected = selStart === 0 && selEnd > 0;
3210
- const allowLeadingPlus = !input.value.startsWith("+") || initialCharSelected;
3249
+ const allowLeadingPlus = !inputValue.startsWith("+") || initialCharSelected;
3211
3250
  const allowedChars = pasted.replace(REGEX.NON_PLUS_NUMERIC_GLOBAL, "");
3212
3251
  const hasLeadingPlus = allowedChars.startsWith("+");
3213
3252
  const numerics = allowedChars.replace(/\+/g, "");
3214
3253
  const sanitised = hasLeadingPlus && allowLeadingPlus ? `+${numerics}` : numerics;
3215
3254
  let newVal = before + sanitised + after;
3216
- let coreNumber = intlTelInput.utils.getCoreNumber(newVal, iso2);
3217
- while (coreNumber.length === 0 && newVal.length > 0) {
3218
- newVal = newVal.slice(0, -1);
3219
- coreNumber = intlTelInput.utils.getCoreNumber(newVal, iso2);
3220
- }
3221
- if (!coreNumber) {
3222
- return;
3223
- }
3224
- if (this.maxCoreNumberLength && coreNumber.length > this.maxCoreNumberLength) {
3225
- if (input.selectionEnd === input.value.length) {
3226
- const trimLength = coreNumber.length - this.maxCoreNumberLength;
3227
- newVal = newVal.slice(0, newVal.length - trimLength);
3228
- } else {
3255
+ if (newVal.length > 5) {
3256
+ let coreNumber = intlTelInput.utils.getCoreNumber(newVal, iso2);
3257
+ while (coreNumber.length === 0 && newVal.length > 0) {
3258
+ newVal = newVal.slice(0, -1);
3259
+ coreNumber = intlTelInput.utils.getCoreNumber(newVal, iso2);
3260
+ }
3261
+ if (!coreNumber) {
3229
3262
  return;
3230
3263
  }
3264
+ if (this.maxCoreNumberLength && coreNumber.length > this.maxCoreNumberLength) {
3265
+ if (input.selectionEnd === inputValue.length) {
3266
+ const trimLength = coreNumber.length - this.maxCoreNumberLength;
3267
+ newVal = newVal.slice(0, newVal.length - trimLength);
3268
+ } else {
3269
+ return;
3270
+ }
3271
+ }
3231
3272
  }
3232
- input.value = newVal;
3273
+ this._setTelInputValue(newVal);
3233
3274
  const caretPos = selStart + sanitised.length;
3234
3275
  input.setSelectionRange(caretPos, caretPos);
3235
3276
  input.dispatchEvent(new InputEvent("input", { bubbles: true }));
@@ -3481,7 +3522,7 @@ var Iti = class _Iti {
3481
3522
  );
3482
3523
  }
3483
3524
  number = this._beforeSetNumber(number);
3484
- this.ui.telInput.value = number;
3525
+ this._setTelInputValue(number);
3485
3526
  }
3486
3527
  //* Check if need to select a new country based on the given number
3487
3528
  //* Note: called from _setInitialState, keyup handler, setNumber.
@@ -3655,7 +3696,8 @@ var Iti = class _Iti {
3655
3696
  const dialCode = listItem.dataset[DATA_KEYS.DIAL_CODE];
3656
3697
  this._updateDialCode(dialCode);
3657
3698
  if (this.options.formatOnDisplay) {
3658
- this._updateValFromNumber(this.ui.telInput.value);
3699
+ const inputValue = this._getTelInputValue();
3700
+ this._updateValFromNumber(inputValue);
3659
3701
  }
3660
3702
  this.ui.telInput.focus();
3661
3703
  if (countryChanged) {
@@ -3686,7 +3728,7 @@ var Iti = class _Iti {
3686
3728
  //* Replace any existing dial code with the new one
3687
3729
  //* Note: called from _selectListItem and setCountry
3688
3730
  _updateDialCode(newDialCodeBare) {
3689
- const inputVal = this.ui.telInput.value;
3731
+ const inputVal = this._getTelInputValue();
3690
3732
  const newDialCode = `+${newDialCodeBare}`;
3691
3733
  let newNumber;
3692
3734
  if (inputVal.startsWith("+")) {
@@ -3696,7 +3738,7 @@ var Iti = class _Iti {
3696
3738
  } else {
3697
3739
  newNumber = newDialCode;
3698
3740
  }
3699
- this.ui.telInput.value = newNumber;
3741
+ this._setTelInputValue(newNumber);
3700
3742
  }
3701
3743
  }
3702
3744
  //* Try and extract a valid international dial code from a full telephone number.
@@ -3733,7 +3775,7 @@ var Iti = class _Iti {
3733
3775
  }
3734
3776
  //* Get the input val, adding the dial code if separateDialCode is enabled.
3735
3777
  _getFullNumber(overrideVal) {
3736
- const val = overrideVal || this.ui.telInput.value.trim();
3778
+ const val = overrideVal ? this._normaliseNumerals(overrideVal) : this._getTelInputValue();
3737
3779
  const { dialCode } = this.selectedCountryData;
3738
3780
  let prefix;
3739
3781
  const numericVal = getNumeric(val);
@@ -3776,8 +3818,9 @@ var Iti = class _Iti {
3776
3818
  //* This is called when the utils request completes.
3777
3819
  handleUtils() {
3778
3820
  if (intlTelInput.utils) {
3779
- if (this.ui.telInput.value) {
3780
- this._updateValFromNumber(this.ui.telInput.value);
3821
+ const inputValue = this._getTelInputValue();
3822
+ if (inputValue) {
3823
+ this._updateValFromNumber(inputValue);
3781
3824
  }
3782
3825
  if (this.selectedCountryData.iso2) {
3783
3826
  this._updatePlaceholder();
@@ -3820,11 +3863,13 @@ var Iti = class _Iti {
3820
3863
  getNumber(format) {
3821
3864
  if (intlTelInput.utils) {
3822
3865
  const { iso2 } = this.selectedCountryData;
3823
- return intlTelInput.utils.formatNumber(
3824
- this._getFullNumber(),
3866
+ const fullNumber = this._getFullNumber();
3867
+ const formattedNumber = intlTelInput.utils.formatNumber(
3868
+ fullNumber,
3825
3869
  iso2,
3826
3870
  format
3827
3871
  );
3872
+ return this._mapAsciiToUserNumerals(formattedNumber);
3828
3873
  }
3829
3874
  return "";
3830
3875
  }
@@ -3912,15 +3957,17 @@ var Iti = class _Iti {
3912
3957
  this._setCountry(iso2Lower);
3913
3958
  this._updateDialCode(this.selectedCountryData.dialCode);
3914
3959
  if (this.options.formatOnDisplay) {
3915
- this._updateValFromNumber(this.ui.telInput.value);
3960
+ const inputValue = this._getTelInputValue();
3961
+ this._updateValFromNumber(inputValue);
3916
3962
  }
3917
3963
  this._triggerCountryChange();
3918
3964
  }
3919
3965
  }
3920
3966
  //* Set the input value and update the country.
3921
3967
  setNumber(number) {
3922
- const countryChanged = this._updateCountryFromNumber(number);
3923
- this._updateValFromNumber(number);
3968
+ const normalisedNumber = this._normaliseNumerals(number);
3969
+ const countryChanged = this._updateCountryFromNumber(normalisedNumber);
3970
+ this._updateValFromNumber(normalisedNumber);
3924
3971
  if (countryChanged) {
3925
3972
  this._triggerCountryChange();
3926
3973
  }
@@ -4005,7 +4052,7 @@ var intlTelInput = Object.assign(
4005
4052
  attachUtils,
4006
4053
  startedLoadingUtilsScript: false,
4007
4054
  startedLoadingAutoCountry: false,
4008
- version: "25.12.6"
4055
+ version: "25.13.1"
4009
4056
  }
4010
4057
  );
4011
4058
  var intl_tel_input_default = intlTelInput;