text-input-guard 1.0.1 → 1.0.2

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.
@@ -737,6 +737,11 @@ class InputGuard {
737
737
  */
738
738
  this.beforeInputSnapshot = null;
739
739
 
740
+ /**
741
+ * onBeforeInput イベントが発生したか否か
742
+ */
743
+ this.existBeforeInputEvent = false;
744
+
740
745
  /**
741
746
  * ルールからのrevert要求
742
747
  * @type {RevertRequest|null}
@@ -993,12 +998,12 @@ class InputGuard {
993
998
  */
994
999
  createCtx({ useSnapshot = true } = {}) {
995
1000
  const snap = useSnapshot ? this.beforeInputSnapshot : null;
996
- const inputType = snap?.inputType ?? "";
997
- const insertedText = snap?.insertedText ?? "";
1001
+ let inputType = snap?.inputType ?? "";
1002
+ let insertedText = snap?.insertedText ?? "";
998
1003
 
999
1004
  // 受理済み(正規化済み)の全文を「今回の編集の基準」として使う
1000
1005
  // display.value はブラウザ側の編集結果が混ざるので、差分再構成の基準にはしない
1001
- const beforeText = this.lastAcceptedValue ?? "";
1006
+ let beforeText = this.lastAcceptedValue ?? "";
1002
1007
 
1003
1008
  // selection は2系統ある:
1004
1009
  // - snapSel: beforeinput 時点で取得した selection(今回の編集の基準点になり得る)
@@ -1034,6 +1039,38 @@ class InputGuard {
1034
1039
  baseSel = lastSel;
1035
1040
  }
1036
1041
 
1042
+ // beforeinput がない環境では、差分再構成の基準が「前回の受理値」しかないため、そこから今回の編集内容を推測する必要がある。
1043
+ if (beforeText.length === 0 || !this.existBeforeInputEvent) {
1044
+ const display = /** @type {HTMLInputElement|HTMLTextAreaElement} */ (this.displayElement);
1045
+ const current = display.value;
1046
+ // 前回の値がとれないものの、何かしら入力情報がある状態
1047
+ if (current.length > 0) {
1048
+ // 文字列の先頭が前回の受理値と同じなら、末尾に何かしら入力されたと考えられる(オートコンプリート等)
1049
+ if (current.toLocaleLowerCase().startsWith(beforeText.toLocaleLowerCase())) {
1050
+ if (!current.startsWith(beforeText)) {
1051
+ // 文字は同じだが、大文字と小文字の情報が替わっているなどのパターン
1052
+ // 差し代わりが起きているため、前回値は基準にならないと判断して、差分全体を insertedText とする
1053
+ beforeText = "";
1054
+ insertedText = current;
1055
+ } else {
1056
+ // 末尾に追加されたと考えられる部分を insertedText とする
1057
+ // 例: beforeText="abc" → current="abcde" なら、"de" が insertedText
1058
+ insertedText = current.slice(beforeText.length);
1059
+ }
1060
+ // キャレットは前回値の末尾にあると推測する
1061
+ baseSel = /** @type {SelectionState} */ {
1062
+ start: beforeText.length,
1063
+ end: beforeText.length,
1064
+ direction: "none"
1065
+ };
1066
+ inputType = "insertText";
1067
+ }
1068
+ }
1069
+ }
1070
+ // existBeforeInputEvent は、少なくとも1回 beforeinput が発生したかどうかのフラグ
1071
+ // これが false の場合、上記のような「beforeinputがない環境での推測ロジック」を走らせる。
1072
+ this.existBeforeInputEvent = false;
1073
+
1037
1074
  let replaceStart = baseSel.start ?? 0;
1038
1075
  let replaceEnd = baseSel.end ?? 0;
1039
1076
 
@@ -1259,9 +1296,7 @@ class InputGuard {
1259
1296
  try {
1260
1297
  this.evaluateInput();
1261
1298
  } finally {
1262
- // beforeinput が来ない入力経路(autocomplete等)で
1263
- // 古い snapshot を使い回さないよう、1イベントごとに破棄する
1264
- this.beforeInputSnapshot = null;
1299
+ this.existBeforeInputEvent = false;
1265
1300
  }
1266
1301
  }
1267
1302
 
@@ -1282,6 +1317,7 @@ class InputGuard {
1282
1317
  const inputType = typeof e.inputType === "string" ? e.inputType : null;
1283
1318
  /** @type {string|null} */
1284
1319
  const insertedText = typeof e.data === "string" ? e.data : null;
1320
+ this.existBeforeInputEvent = true;
1285
1321
  this.beforeInputSnapshot = { selection, inputType, insertedText };
1286
1322
  }
1287
1323
 
@@ -1421,45 +1457,6 @@ class InputGuard {
1421
1457
  const ctx = this.createCtx();
1422
1458
  ctx.afterText = current;
1423
1459
 
1424
- /**
1425
- * 入力値情報のみを使用するフォールバック
1426
- * @returns {GuardContext}
1427
- */
1428
- const applyFullNormalizeFromCurrent = () => {
1429
- let newText = current;
1430
- ctx.beforeText = "";
1431
- newText = this.runNormalizeChar(newText, ctx);
1432
- newText = this.runNormalizeStructure(newText, ctx);
1433
- this.setDisplayValuePreserveCaret(display, newText, ctx);
1434
- ctx.afterText = newText;
1435
- return ctx;
1436
- };
1437
-
1438
- // beforeinput が取得できない経路(初回評価)では
1439
- // 差分再構成を行うと lastAcceptedValue 基準で値を落とす可能性があるため、
1440
- // 現在の全文を正規化して扱うフォールバックへ切り替える。
1441
- if (!this.beforeInputSnapshot) {
1442
- return applyFullNormalizeFromCurrent();
1443
- }
1444
-
1445
- // オートコンプリート等では beforeinput は来ても data が空のことがあり、
1446
- // 差分情報だけでは再構成不能になる。表示値がすでに変わっている場合は
1447
- // 再構成を諦めて current 全体の正規化に切り替える。
1448
- const isDeleteInput =
1449
- ctx.inputType === "deleteContentBackward" ||
1450
- ctx.inputType === "deleteContentForward";
1451
- const isInsertLikeInput =
1452
- ctx.inputType === "" ||
1453
- ctx.inputType?.startsWith("insert");
1454
- const lacksDelta =
1455
- ctx.insertedText === "" &&
1456
- ctx.beforeText !== current &&
1457
- isInsertLikeInput &&
1458
- !isDeleteInput;
1459
- if (lacksDelta) {
1460
- return applyFullNormalizeFromCurrent();
1461
- }
1462
-
1463
1460
  // 元のテキスト
1464
1461
  const beforeText = ctx.beforeText;
1465
1462
 
@@ -7491,11 +7488,11 @@ const rules = {
7491
7488
 
7492
7489
  /**
7493
7490
  * バージョン(ビルド時に置換したいならここを差し替える)
7494
- * 例: rollup replace で ""1.0.1"" を package.json の version に置換
7491
+ * 例: rollup replace で ""1.0.2"" を package.json の version に置換
7495
7492
  */
7496
7493
  // @ts-ignore
7497
7494
  // eslint-disable-next-line no-undef
7498
- const version = "1.0.1" ;
7495
+ const version = "1.0.2" ;
7499
7496
 
7500
7497
  exports.ascii = ascii;
7501
7498
  exports.attach = attach;