Skip to content

Commit e0a523a

Browse files
committed
chore: exclude zero from the set of allowed codepoints
1 parent 8c728b8 commit e0a523a

File tree

3 files changed

+53
-72
lines changed

3 files changed

+53
-72
lines changed

packages/keybr-keyboard/lib/keycharacters.test.ts

+14-54
Original file line numberDiff line numberDiff line change
@@ -11,62 +11,22 @@ test("codepoint characters", () => {
1111
/* "b" */ 0x0062,
1212
null,
1313
);
14-
assert.strictEqual(
15-
characters.getCodePoint(KeyModifier.None),
16-
/* "a" */ 0x0061,
17-
);
18-
assert.strictEqual(
19-
characters.getCodePoint(KeyModifier.Shift),
20-
/* "a" */ 0x0061,
21-
);
22-
assert.strictEqual(
23-
characters.getCodePoint(KeyModifier.Alt),
24-
/* "b" */ 0x0062,
25-
);
26-
assert.strictEqual(
27-
characters.getCodePoint(KeyModifier.ShiftAlt),
28-
/* "b" */ 0x0062,
29-
);
30-
});
31-
32-
test("dead characters", () => {
33-
const characters = new KeyCharacters(
34-
"KeyA",
35-
{ dead: /* "*" */ 0x002a },
36-
{ dead: /* "*" */ 0x002a },
37-
null,
38-
null,
39-
);
40-
assert.strictEqual(characters.getCodePoint(KeyModifier.None), 0x0000);
41-
assert.strictEqual(characters.getCodePoint(KeyModifier.Shift), 0x0000);
42-
assert.strictEqual(characters.getCodePoint(KeyModifier.Alt), 0x0000);
43-
assert.strictEqual(characters.getCodePoint(KeyModifier.ShiftAlt), 0x0000);
14+
assert.strictEqual(characters.getCodePoint(KeyModifier.None), 0x0061);
15+
assert.strictEqual(characters.getCodePoint(KeyModifier.Shift), 0x0061);
16+
assert.strictEqual(characters.getCodePoint(KeyModifier.Alt), 0x0062);
17+
assert.strictEqual(characters.getCodePoint(KeyModifier.ShiftAlt), 0x0062);
4418
});
4519

46-
test("special characters", () => {
20+
test("dead, special and ligature characters", () => {
4721
const characters = new KeyCharacters(
4822
"KeyA",
49-
{ special: /* "a" */ 0x0061 },
50-
{ special: /* "b" */ 0x0062 },
51-
null,
52-
null,
53-
);
54-
assert.strictEqual(characters.getCodePoint(KeyModifier.None), 0x0000);
55-
assert.strictEqual(characters.getCodePoint(KeyModifier.Shift), 0x0000);
56-
assert.strictEqual(characters.getCodePoint(KeyModifier.Alt), 0x0000);
57-
assert.strictEqual(characters.getCodePoint(KeyModifier.ShiftAlt), 0x0000);
58-
});
59-
60-
test("ligature characters", () => {
61-
const characters = new KeyCharacters(
62-
"KeyA",
63-
{ ligature: "XX" },
64-
{ ligature: "YY" },
65-
null,
66-
null,
67-
);
68-
assert.strictEqual(characters.getCodePoint(KeyModifier.None), 0x0000);
69-
assert.strictEqual(characters.getCodePoint(KeyModifier.Shift), 0x0000);
70-
assert.strictEqual(characters.getCodePoint(KeyModifier.Alt), 0x0000);
71-
assert.strictEqual(characters.getCodePoint(KeyModifier.ShiftAlt), 0x0000);
23+
/* "a" */ 0x0061,
24+
{ dead: /* COMBINING GRAVE ACCENT */ 0x0300 },
25+
{ special: /* ZERO WIDTH NON-JOINER */ 0x200c },
26+
{ ligature: "XYZ" },
27+
);
28+
assert.strictEqual(characters.getCodePoint(KeyModifier.None), 0x0061);
29+
assert.strictEqual(characters.getCodePoint(KeyModifier.Shift), null);
30+
assert.strictEqual(characters.getCodePoint(KeyModifier.Alt), null);
31+
assert.strictEqual(characters.getCodePoint(KeyModifier.ShiftAlt), null);
7232
});

packages/keybr-keyboard/lib/keycharacters.ts

+38-17
Original file line numberDiff line numberDiff line change
@@ -26,34 +26,55 @@ export class KeyCharacters {
2626
return isObject(ch) && "ligature" in ch;
2727
};
2828

29+
readonly id: KeyId;
30+
readonly a: Character | null;
31+
readonly b: Character | null;
32+
readonly c: Character | null;
33+
readonly d: Character | null;
34+
2935
constructor(
30-
readonly id: KeyId,
31-
readonly a: Character | null,
32-
readonly b: Character | null,
33-
readonly c: Character | null,
34-
readonly d: Character | null,
35-
) {}
36-
37-
getCodePoint(modifier: KeyModifier): CodePoint | 0x0000 {
38-
const a = KeyCharacters.isCodePoint(this.a) ? this.a : null;
39-
const b = KeyCharacters.isCodePoint(this.b) ? this.b : null;
40-
const c = KeyCharacters.isCodePoint(this.c) ? this.c : null;
41-
const d = KeyCharacters.isCodePoint(this.d) ? this.d : null;
36+
id: KeyId,
37+
a: Character | null,
38+
b: Character | null,
39+
c: Character | null,
40+
d: Character | null,
41+
) {
42+
this.id = id;
43+
this.a = a || null;
44+
this.b = b || null;
45+
this.c = c || null;
46+
this.d = d || null;
47+
}
48+
49+
getCodePoint(modifier: KeyModifier): CodePoint | null {
4250
switch (modifier) {
4351
case KeyModifier.None:
44-
return a ?? 0x0000;
52+
return select(this.a);
4553
case KeyModifier.Shift:
46-
return b ?? a ?? 0x0000;
54+
return select(this.b, this.a);
4755
case KeyModifier.Alt:
48-
return c ?? b ?? a ?? 0x0000;
56+
return select(this.c, this.b, this.a);
4957
case KeyModifier.ShiftAlt:
50-
return d ?? c ?? b ?? a ?? 0x0000;
58+
return select(this.d, this.c, this.b, this.a);
5159
default:
5260
throw new Error();
5361
}
5462
}
5563

5664
get valid() {
57-
return this.a || this.b || this.c || this.d;
65+
return Boolean(this.a || this.b || this.c || this.d);
66+
}
67+
}
68+
69+
function select(...characters: (Character | null)[]): CodePoint | null {
70+
for (const character of characters) {
71+
if (character != null) {
72+
if (KeyCharacters.isCodePoint(character)) {
73+
return character;
74+
} else {
75+
break;
76+
}
77+
}
5878
}
79+
return null;
5980
}

packages/keybr-textinput-events/lib/emulation.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ function fixKey(
115115
const characters = keyboard.getCharacters(code);
116116
if (characters != null) {
117117
key = String.fromCodePoint(
118-
(codePoint = characters.getCodePoint(toKeyModifier(modifiers))),
118+
(codePoint = characters.getCodePoint(toKeyModifier(modifiers)) ?? 0x0000),
119119
);
120120
}
121121
return [{ type, timeStamp, code, key, modifiers }, codePoint];

0 commit comments

Comments
 (0)