Skip to content

Commit

Permalink
chore: rename and document function
Browse files Browse the repository at this point in the history
  • Loading branch information
r00gm committed Sep 13, 2024
1 parent 38babf2 commit b9bb010
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
4 changes: 2 additions & 2 deletions packages/editor-ui/src/composables/useClipboard.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { onBeforeUnmount, onMounted, ref } from 'vue';
import { useClipboard as useClipboardCore } from '@vueuse/core';
import { useDebounce } from '@/composables/useDebounce';
import { sanitizeInput } from '@/utils/htmlUtils';
import { sanitizeIfString } from '@/utils/htmlUtils';

type ClipboardEventFn = (data: string, event?: ClipboardEvent) => void;

Expand Down Expand Up @@ -43,7 +43,7 @@ export function useClipboard(

const clipboardData = event.clipboardData;
if (clipboardData !== null) {
const clipboardValue = sanitizeInput(clipboardData.getData('text/plain'));
const clipboardValue = sanitizeIfString(clipboardData.getData('text/plain'));
onPasteCallback.value(clipboardValue, event);
}
}
Expand Down
18 changes: 9 additions & 9 deletions packages/editor-ui/src/composables/useMessage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ElMessageBoxOptions, Action, MessageBoxInputData } from 'element-plus';
import { ElMessageBox as MessageBox } from 'element-plus';
import { sanitizeInput } from '@/utils/htmlUtils';
import { sanitizeIfString } from '@/utils/htmlUtils';

export type MessageBoxConfirmResult = 'confirm' | 'cancel';

Expand Down Expand Up @@ -29,11 +29,11 @@ export function useMessage() {
};

if (typeof configOrTitle === 'string') {
return await MessageBox.alert(sanitizeInput(message), configOrTitle, resolvedConfig).catch(
return await MessageBox.alert(sanitizeIfString(message), configOrTitle, resolvedConfig).catch(
handleCancelOrClose,
);
}
return await MessageBox.alert(sanitizeInput(message), resolvedConfig).catch(
return await MessageBox.alert(sanitizeIfString(message), resolvedConfig).catch(
handleCancelOrClose,
);
}
Expand All @@ -54,13 +54,13 @@ export function useMessage() {

if (typeof configOrTitle === 'string') {
return await MessageBox.confirm(
sanitizeInput(message),
sanitizeInput(configOrTitle),
sanitizeIfString(message),
sanitizeIfString(configOrTitle),
resolvedConfig,
).catch(handleCancelOrClose);
}

return await MessageBox.confirm(sanitizeInput(message), resolvedConfig).catch(
return await MessageBox.confirm(sanitizeIfString(message), resolvedConfig).catch(
handleCancelOrClose,
);
}
Expand All @@ -78,12 +78,12 @@ export function useMessage() {

if (typeof configOrTitle === 'string') {
return await MessageBox.prompt(
sanitizeInput(message),
sanitizeInput(configOrTitle),
sanitizeIfString(message),
sanitizeIfString(configOrTitle),
resolvedConfig,
).catch(handleCancelOrClosePrompt);
}
return await MessageBox.prompt(sanitizeInput(message), resolvedConfig).catch(
return await MessageBox.prompt(sanitizeIfString(message), resolvedConfig).catch(
handleCancelOrClosePrompt,
);
}
Expand Down
6 changes: 5 additions & 1 deletion packages/editor-ui/src/utils/htmlUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ export function sanitizeHtml(dirtyHtml: string) {
return sanitizedHtml;
}

export const sanitizeInput = <T>(message: T): string | T => {
/**
* Checks if the input is a string and sanitizes it by removing or escaping harmful characters,
* returning the original input if it's not a string.
*/
export const sanitizeIfString = <T>(message: T): string | T => {
if (typeof message === 'string') {
return sanitizeHtml(message);
}
Expand Down

0 comments on commit b9bb010

Please sign in to comment.