From d8ffa7f7e1836958492d00ad4a1e17dc85f9ab2f Mon Sep 17 00:00:00 2001 From: Fabien Arcellier Date: Mon, 2 Sep 2024 14:47:34 +0200 Subject: [PATCH] fix: custom component name should be constrained explicitely --- src/ui/src/custom_components/core.ts | 34 +++++++++++++++++++++++++++ src/ui/src/custom_components/index.ts | 8 +++---- 2 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 src/ui/src/custom_components/core.ts diff --git a/src/ui/src/custom_components/core.ts b/src/ui/src/custom_components/core.ts new file mode 100644 index 000000000..d3ce15611 --- /dev/null +++ b/src/ui/src/custom_components/core.ts @@ -0,0 +1,34 @@ +/** + * Declare custom components + * + * @param customComponents list of components in key-value form + */ +export function declareCustomComponents( + customComponents: Record, +) { + const validCustomComponents = {}; + Object.entries(customComponents).forEach(([key, customComponent]) => { + const isValid = checkComponentKey(key); + if (isValid) { + validCustomComponents[key] = customComponent; + } + }); + + return validCustomComponents; +} + +/** + * Checks that the key contains only alphanumeric characters and underscores without capital letters + * + * @see https://github.com/writer/writer-framework/issues/517 + */ +function checkComponentKey(key: string): boolean { + const isValidKey = /^[a-z0-9_]+$/.test(key); + if (!isValidKey) { + console.warn( + `custom component '${key}' is ignored. A custom component in 'index.ts' should be declared using only alphanumeric lowercase and _.`, + ); + } + + return isValidKey; +} diff --git a/src/ui/src/custom_components/index.ts b/src/ui/src/custom_components/index.ts index 436ffd20a..1a3244e88 100644 --- a/src/ui/src/custom_components/index.ts +++ b/src/ui/src/custom_components/index.ts @@ -2,10 +2,10 @@ import BubbleMessage from "./BubbleMessage.vue"; import BubbleMessageAdvanced from "./BubbleMessageAdvanced.vue"; - +import { declareCustomComponents } from "./core"; // Export an object with the ids and the templates as default -export default { +export default declareCustomComponents({ bubblemessage: BubbleMessage, - bubblemessageadvanced: BubbleMessageAdvanced, -}; + bubbleMessageadvanced: BubbleMessageAdvanced, +});