From 0d5b04e4dd9a5446dafa61aee75c965389eeb1d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A2=96=E9=80=B8?= <49649786+Zuoqiu-Yingyi@users.noreply.github.com> Date: Mon, 8 May 2023 14:45:45 +0800 Subject: [PATCH] :bug: Fix IAL name checking functionality (#8204) --- app/src/menus/commonMenuItem.ts | 10 +++++----- app/src/util/functions.ts | 4 ++++ 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/app/src/menus/commonMenuItem.ts b/app/src/menus/commonMenuItem.ts index f596bd909..9ac8a8ef6 100644 --- a/app/src/menus/commonMenuItem.ts +++ b/app/src/menus/commonMenuItem.ts @@ -3,7 +3,7 @@ import {shell} from "electron"; /// #endif import {getDockByType} from "../layout/util"; import {confirmDialog} from "../dialog/confirmDialog"; -import {getSearch, isMobile} from "../util/functions"; +import {getSearch, isMobile, isValidAttrName} from "../util/functions"; import {isLocalPath, movePathTo, moveToPath, pathPosix} from "../util/pathName"; import {MenuItem} from "./Menu"; import {hasClosestByClassName} from "../protyle/util/hasClosest"; @@ -310,8 +310,8 @@ export const openFileAttr = (attrs: IObject, id: string, focusName = "bookmark") name = "custom-" + (item.parentElement.querySelector(".b3-text-field") as HTMLInputElement).value; } if (item.value.trim()) { - if (!/^custom-[_.\-0-9a-zA-Z]+$/.test(name)) { - errorTip += name.replace("custom-", "") + ", "; + if (!isValidAttrName(name)) { + errorTip += name.replace(/^custom-/, "") + ", "; return; } attrsResult[name] = item.value; @@ -381,8 +381,8 @@ export const openAttr = (nodeElement: Element, protyle: IProtyle, focusName = "b name = "custom-" + (item.parentElement.querySelector(".b3-text-field") as HTMLInputElement).value; } if (item.value.trim()) { - if (!/^custom-[_.\-0-9a-zA-Z]+$/.test(name)) { - errorTip += name.replace("custom-", "") + ", "; + if (!isValidAttrName(name)) { + errorTip += name.replace(/^custom-/, "") + ", "; return; } if (removeAttrs.includes(name)) { diff --git a/app/src/util/functions.ts b/app/src/util/functions.ts index 3e5c6a440..8ce19efa4 100644 --- a/app/src/util/functions.ts +++ b/app/src/util/functions.ts @@ -42,6 +42,10 @@ export const isFileAnnotation = (text: string) => { return /^<>$/.test(text); }; +export const isValidAttrName = (name: string) => { + return /^[_a-zA-Z][_.\-0-9a-zA-Z]*$/.test(name); +}; + // REF https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/eval export const looseJsonParse = (text: string) => { return Function(`"use strict";return (${text})`)();