Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions frontend/__tests__/input/handlers/insert-text.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { describe, expect, it } from "vitest";
import {
getMatchingLigatureOverride,
shouldIgnoreLigatureCompletion,
} from "../../../src/ts/input/helpers/ligatures";

describe("insert-text ligature input overrides", () => {
Comment thread
Dawn-Fighter marked this conversation as resolved.
it.each([
["o", "œ", "œ"],
["O", "Œ", "Œ"],
["a", "æ", "æ"],
["A", "Æ", "Æ"],
])(
"normalizes '%s' to '%s' when target is '%s'",
(data, target, expected) => {
expect(getMatchingLigatureOverride(data, target)).toBe(expected);
},
);

it.each([
["e", "œ", "œuvre"],
["E", "Œ", "ŒUVRE"],
["e", "æ", "æther"],
["E", "Æ", "ÆTHER"],
])("ignores completion '%s' after '%s'", (data, input, word) => {
expect(shouldIgnoreLigatureCompletion(data, input, word)).toBe(true);
});

it("does not normalize unrelated input", () => {
expect(getMatchingLigatureOverride("e", "œ")).toBeNull();
expect(shouldIgnoreLigatureCompletion("u", "œ", "œuvre")).toBe(false);
});
});
22 changes: 22 additions & 0 deletions frontend/src/ts/input/handlers/insert-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ import {
isCharCorrect,
shouldInsertSpaceCharacter,
} from "../helpers/validation";
import {
getMatchingLigatureOverride,
shouldIgnoreLigatureCompletion,
} from "../helpers/ligatures";

const charOverrides = new Map<string, string>([
["…", "..."],
Expand Down Expand Up @@ -82,6 +86,18 @@ export async function onInsertText(options: OnInsertTextParams): Promise<void> {
return;
}

if (
shouldIgnoreLigatureCompletion(
options.data,
TestInput.input.current,
TestWords.words.getCurrentText(),
)
) {
setInputElementValue(inputValue.slice(0, -options.data.length));
TestInput.input.syncWithInputElement();
return;
}
Comment thread
Dawn-Fighter marked this conversation as resolved.
Outdated

const charOverride = charOverrides.get(options.data);
if (
charOverride !== undefined &&
Expand Down Expand Up @@ -301,6 +317,12 @@ function normalizeDataAndUpdateInputIfNeeded(
) {
replaceInputElementLastValueChar(targetChar);
normalizedData = targetChar;
} else {
const ligatureOverride = getMatchingLigatureOverride(data, targetChar);
if (ligatureOverride !== null) {
replaceInputElementLastValueChar(ligatureOverride);
normalizedData = ligatureOverride;
}
}
return normalizedData;
}
Expand Down
32 changes: 32 additions & 0 deletions frontend/src/ts/input/helpers/ligatures.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const ligatureInputOverrides = new Map<string, string>([
["œ", "oe"],
["Œ", "OE"],
["æ", "ae"],
["Æ", "AE"],
]);

export function getMatchingLigatureOverride(
data: string,
targetChar: string | undefined,
): string | null {
if (targetChar === undefined) return null;

const override = ligatureInputOverrides.get(targetChar);
if (override?.[0] !== data) return null;

return targetChar;
}

export function shouldIgnoreLigatureCompletion(
data: string,
testInput: string,
currentWord: string,
): boolean {
const previousTargetChar = currentWord[testInput.length - 1];
if (previousTargetChar === undefined) return false;
Comment thread
Dawn-Fighter marked this conversation as resolved.
Outdated

const override = ligatureInputOverrides.get(previousTargetChar);
if (override === undefined) return false;

return testInput.endsWith(previousTargetChar) && data === override.slice(1);
}
Loading