Skip to content

Commit

Permalink
add null check for key undefined before calling toLowerCase function
Browse files Browse the repository at this point in the history
  • Loading branch information
Sahejkm committed May 21, 2024
1 parent 6795e91 commit 5ccf58b
Showing 1 changed file with 46 additions and 27 deletions.
73 changes: 46 additions & 27 deletions packages/lexical/src/LexicalUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -772,35 +772,44 @@ export function isTab(
}

export function isBold(
key: string,
key: string | null,
altKey: boolean,
metaKey: boolean,
ctrlKey: boolean,
): boolean {
return (
key.toLowerCase() === 'b' && !altKey && controlOrMeta(metaKey, ctrlKey)
key != null &&
key.toLowerCase() === 'b' &&
!altKey &&
controlOrMeta(metaKey, ctrlKey)
);
}

export function isItalic(
key: string,
key: string | null,
altKey: boolean,
metaKey: boolean,
ctrlKey: boolean,
): boolean {
return (
key.toLowerCase() === 'i' && !altKey && controlOrMeta(metaKey, ctrlKey)
key != null &&
key.toLowerCase() === 'i' &&
!altKey &&
controlOrMeta(metaKey, ctrlKey)
);
}

export function isUnderline(
key: string,
key: string | null,
altKey: boolean,
metaKey: boolean,
ctrlKey: boolean,
): boolean {
return (
key.toLowerCase() === 'u' && !altKey && controlOrMeta(metaKey, ctrlKey)
key != null &&
key.toLowerCase() === 'u' &&
!altKey &&
controlOrMeta(metaKey, ctrlKey)
);
}

Expand All @@ -814,9 +823,9 @@ export function isLineBreak(key: string, shiftKey: boolean): boolean {

// Inserts a new line after the selection

export function isOpenLineBreak(key: string, ctrlKey: boolean): boolean {
export function isOpenLineBreak(key: string | null, ctrlKey: boolean): boolean {
// 79 = KeyO
return IS_APPLE && ctrlKey && key.toLowerCase() === 'o';
return IS_APPLE && ctrlKey && key != null && key.toLowerCase() === 'o';
}

export function isDeleteWordBackward(
Expand Down Expand Up @@ -844,7 +853,7 @@ export function isDeleteLineForward(key: string, metaKey: boolean): boolean {
}

export function isDeleteBackward(
key: string,
key: string | null,
altKey: boolean,
metaKey: boolean,
ctrlKey: boolean,
Expand All @@ -853,7 +862,9 @@ export function isDeleteBackward(
if (altKey || metaKey) {
return false;
}
return isBackspace(key) || (key.toLowerCase() === 'h' && ctrlKey);
return (
isBackspace(key) || (key != null && key.toLowerCase() === 'h' && ctrlKey)
);
}
if (ctrlKey || altKey || metaKey) {
return false;
Expand All @@ -862,7 +873,7 @@ export function isDeleteBackward(
}

export function isDeleteForward(
key: string,
key: string | null,
ctrlKey: boolean,
shiftKey: boolean,
altKey: boolean,
Expand All @@ -872,7 +883,9 @@ export function isDeleteForward(
if (shiftKey || altKey || metaKey) {
return false;
}
return isDelete(key) || (key.toLowerCase() === 'd' && ctrlKey);
return (
isDelete(key) || (key != null && key.toLowerCase() === 'd' && ctrlKey)
);
}
if (ctrlKey || altKey || metaKey) {
return false;
Expand All @@ -881,57 +894,61 @@ export function isDeleteForward(
}

export function isUndo(
key: string,
key: string | null,
shiftKey: boolean,
metaKey: boolean,
ctrlKey: boolean,
): boolean {
return (
key.toLowerCase() === 'z' && !shiftKey && controlOrMeta(metaKey, ctrlKey)
key != null &&
key.toLowerCase() === 'z' &&
!shiftKey &&
controlOrMeta(metaKey, ctrlKey)
);
}

export function isRedo(
key: string,
key: string | null,
shiftKey: boolean,
metaKey: boolean,
ctrlKey: boolean,
): boolean {
if (IS_APPLE) {
return key.toLowerCase() === 'z' && metaKey && shiftKey;
return key != null && key.toLowerCase() === 'z' && metaKey && shiftKey;
}
return (
(key.toLowerCase() === 'y' && ctrlKey) ||
(key.toLowerCase() === 'z' && ctrlKey && shiftKey)
key != null &&
((key.toLowerCase() === 'y' && ctrlKey) ||
(key.toLowerCase() === 'z' && ctrlKey && shiftKey))
);
}

export function isCopy(
key: string,
key: string | null,
shiftKey: boolean,
metaKey: boolean,
ctrlKey: boolean,
): boolean {
if (shiftKey) {
return false;
}
if (key.toLowerCase() === 'c') {
if (key != null && key.toLowerCase() === 'c') {
return IS_APPLE ? metaKey : ctrlKey;
}

return false;
}

export function isCut(
key: string,
key: string | null,
shiftKey: boolean,
metaKey: boolean,
ctrlKey: boolean,
): boolean {
if (shiftKey) {
return false;
}
if (key.toLowerCase() === 'x') {
if (key != null && key.toLowerCase() === 'x') {
return IS_APPLE ? metaKey : ctrlKey;
}

Expand Down Expand Up @@ -1032,24 +1049,26 @@ export function isReturn(key: string): boolean {
return key === 'Enter';
}

export function isBackspace(key: string): boolean {
export function isBackspace(key: string | null): boolean {
return key === 'Backspace';
}

export function isEscape(key: string): boolean {
return key === 'Escape';
}

export function isDelete(key: string): boolean {
return key === 'Delete';
export function isDelete(key: string | null): boolean {
return key != null && key === 'Delete';
}

export function isSelectAll(
key: string,
key: string | null,
metaKey: boolean,
ctrlKey: boolean,
): boolean {
return key.toLowerCase() === 'a' && controlOrMeta(metaKey, ctrlKey);
return (
key != null && key.toLowerCase() === 'a' && controlOrMeta(metaKey, ctrlKey)
);
}

export function $selectAll(): void {
Expand Down

0 comments on commit 5ccf58b

Please sign in to comment.