text-input-guard 0.2.0 → 0.2.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.
@@ -398,7 +398,7 @@ class SwapState {
398
398
  * @typedef {Object} AttachOptions
399
399
  * @property {Rule[]} [rules] - 適用するルール配列(順番がフェーズ内実行順になる)
400
400
  * @property {boolean} [warn] - 非対応ルールなどを console.warn するか
401
- * @property {string} [invalidClass] - エラー時に付けるclass名
401
+ * @property {string} [invalidClass="is-invalid"] - エラー時に付けるclass名
402
402
  * @property {SeparateValueOptions} [separateValue] - 表示値と内部値の分離設定
403
403
  * @property {(result: ValidateResult) => void} [onValidate] - 評価完了時の通知(input/commitごと)
404
404
  */
@@ -2701,6 +2701,104 @@ comma.fromDataset = function fromDataset(dataset, _el) {
2701
2701
  return comma();
2702
2702
  };
2703
2703
 
2704
+ /**
2705
+ * The script is part of TextInputGuard.
2706
+ *
2707
+ * AUTHOR:
2708
+ * natade-jp (https://github.com/natade-jp)
2709
+ *
2710
+ * LICENSE:
2711
+ * The MIT license https://opensource.org/licenses/MIT
2712
+ */
2713
+
2714
+ /**
2715
+ * IMEオフ入力相当の文字変換テーブル
2716
+ * @type {Record<string, string>}
2717
+ */
2718
+ /* eslint-disable quote-props */
2719
+ const IME_OFF_MAP = {
2720
+ "\u3000": "\u0020", // 全角スペース → space
2721
+ "\u3001": "\u002C", // 、 → ,
2722
+ "\u3002": "\u002E", // 。 → .
2723
+ "\u300C": "\u005B", // 「 → [
2724
+ "\u300D": "\u005D", // 」 → ]
2725
+ "\u301C": "\u007E", // 〜 → ~
2726
+ "\u30FC": "\u002D", // ー → -
2727
+ "\uFFE5": "\u005C" // ¥ → \
2728
+ };
2729
+ /* eslint-enable quote-props */
2730
+
2731
+ /**
2732
+ * ASCII入力欄に日本語IMEで入った文字をASCIIへ矯正する
2733
+ * @param {string} text - 変換したいテキスト
2734
+ * @returns {string} 変換後のテキスト
2735
+ */
2736
+ const toImeOff = function (text) {
2737
+ return Array.from(String(text), (ch) => {
2738
+ // 個別マップ
2739
+ if (ch in IME_OFF_MAP) {
2740
+ return IME_OFF_MAP[ch];
2741
+ }
2742
+
2743
+ const code = ch.charCodeAt(0);
2744
+
2745
+ // 全角ASCII
2746
+ if (code >= 0xFF01 && code <= 0xFF5E) {
2747
+ return String.fromCharCode(code - 0xFEE0);
2748
+ }
2749
+
2750
+ // シングルクォート系
2751
+ if (code >= 0x2018 && code <= 0x201B) {
2752
+ return "'";
2753
+ }
2754
+
2755
+ // ダブルクォート系
2756
+ if (code >= 0x201C && code <= 0x201F) {
2757
+ return '"';
2758
+ }
2759
+
2760
+ return ch;
2761
+ }).join("");
2762
+ };
2763
+
2764
+ /**
2765
+ * ASCII入力欄に日本語IMEで入った文字をASCIIへ矯正する
2766
+ *
2767
+ * 注意:
2768
+ * - これは「半角化」ではなく「IMEオフ入力相当への寄せ」
2769
+ * - ascii() とは責務が異なる
2770
+ *
2771
+ * @returns {Rule}
2772
+ */
2773
+ function imeOff() {
2774
+ return {
2775
+ name: "imeOff",
2776
+ targets: ["input", "textarea"],
2777
+
2778
+ normalizeChar(value, ctx) {
2779
+ return toImeOff(value);
2780
+ }
2781
+ };
2782
+ }
2783
+
2784
+ /**
2785
+ * dataset から imeOff ルールを生成する
2786
+ *
2787
+ * 対応する data 属性
2788
+ * - data-tig-rules-ime-off
2789
+ *
2790
+ * @param {DOMStringMap} dataset
2791
+ * @param {HTMLInputElement|HTMLTextAreaElement} _el
2792
+ * @returns {Rule|null}
2793
+ */
2794
+ imeOff.fromDataset = function fromDataset(dataset, _el) {
2795
+ if (dataset.tigRulesImeOff == null) {
2796
+ return null;
2797
+ }
2798
+
2799
+ return imeOff();
2800
+ };
2801
+
2704
2802
  /**
2705
2803
  * The script is part of Mojix for TextInputGuard.
2706
2804
  *
@@ -5922,7 +6020,7 @@ kana.fromDataset = function fromDataset(dataset, _el) {
5922
6020
  function ascii(options = {}) {
5923
6021
  /** @type {AsciiRuleOptions} */
5924
6022
  const opt = {
5925
- case: options.case ?? null
6023
+ case: options.case ?? "none"
5926
6024
  };
5927
6025
 
5928
6026
  return {
@@ -5935,6 +6033,10 @@ function ascii(options = {}) {
5935
6033
  // まず半角へ正規化
5936
6034
  s = Mojix.toHalfWidthAsciiCode(s);
5937
6035
 
6036
+ // toHalfWidthAsciiCode で対応できていない文字も実施
6037
+ s = s.replace(/\uFFE5/g, "\u005C"); //¥
6038
+ s = s.replace(/[\u2010-\u2015\u2212\u30FC\uFF0D\uFF70]/g, "\u002D"); //ハイフンに似ている記号
6039
+
5938
6040
  // 英字の大文字/小文字統一
5939
6041
  if (opt.case === "upper") {
5940
6042
  s = s.toUpperCase();
@@ -7236,6 +7338,7 @@ const auto = new InputGuardAutoAttach(attach, [
7236
7338
  { name: "numeric", fromDataset: numeric.fromDataset },
7237
7339
  { name: "digits", fromDataset: digits.fromDataset },
7238
7340
  { name: "comma", fromDataset: comma.fromDataset },
7341
+ { name: "imeOff", fromDataset: imeOff.fromDataset },
7239
7342
  { name: "kana", fromDataset: kana.fromDataset },
7240
7343
  { name: "ascii", fromDataset: ascii.fromDataset },
7241
7344
  { name: "filter", fromDataset: filter.fromDataset },
@@ -7260,6 +7363,7 @@ const rules = {
7260
7363
  numeric,
7261
7364
  digits,
7262
7365
  comma,
7366
+ imeOff,
7263
7367
  kana,
7264
7368
  ascii,
7265
7369
  filter,
@@ -7273,11 +7377,11 @@ const rules = {
7273
7377
 
7274
7378
  /**
7275
7379
  * バージョン(ビルド時に置換したいならここを差し替える)
7276
- * 例: rollup replace で ""0.2.0"" を package.json の version に置換
7380
+ * 例: rollup replace で ""0.2.1"" を package.json の version に置換
7277
7381
  */
7278
7382
  // @ts-ignore
7279
7383
  // eslint-disable-next-line no-undef
7280
- const version = "0.2.0" ;
7384
+ const version = "0.2.1" ;
7281
7385
 
7282
7386
  exports.ascii = ascii;
7283
7387
  exports.attach = attach;
@@ -7287,6 +7391,7 @@ exports.bytes = bytes;
7287
7391
  exports.comma = comma;
7288
7392
  exports.digits = digits;
7289
7393
  exports.filter = filter;
7394
+ exports.imeOff = imeOff;
7290
7395
  exports.kana = kana;
7291
7396
  exports.length = length;
7292
7397
  exports.numeric = numeric;