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