From 0e169d8f82e6acbee609542b2558afa8cbbfc3a6 Mon Sep 17 00:00:00 2001 From: sunhao Date: Mon, 22 Jul 2024 17:08:27 +0800 Subject: [PATCH] * core: optimize runJS helper. --- lib/core/src/dom/run-js.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/core/src/dom/run-js.ts b/lib/core/src/dom/run-js.ts index 736be3b97c..eb1e36f88c 100644 --- a/lib/core/src/dom/run-js.ts +++ b/lib/core/src/dom/run-js.ts @@ -9,10 +9,10 @@ import type {Selector} from '../cash'; * @param selector Element selector to run. * @param jsCode If not set, run all scripts in the element. */ -export function runJS(selector: Selector, jsCode?: string, removeAfterRun = false) { +export function runJS(selector: Selector, jsCode?: string | null, removeAfterRun = false) { const $element = $(selector); if (jsCode !== undefined) { - if (jsCode.length) { + if (typeof jsCode === 'string' && jsCode.length) { const id = `zui-runjs-${nextGid()}`; $element.append(``); if (removeAfterRun) { @@ -21,8 +21,15 @@ export function runJS(selector: Selector, jsCode?: string, removeAfterRun = fals } return; } + if ($element.is('script')) { + const code = $element[0]?.textContent; + if (code) { + runJS($element.parent(), code); + } + return; + } $element.find('script').each((_, script) => { - runJS($element, (script as HTMLScriptElement).innerHTML); + runJS($element, (script as HTMLScriptElement).textContent); script.remove(); }); }