Skip to content

Commit

Permalink
* core: optimize runJS helper.
Browse files Browse the repository at this point in the history
  • Loading branch information
catouse committed Jul 22, 2024
1 parent decbf0d commit 0e169d8
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions lib/core/src/dom/run-js.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(`<script id="${id}">${jsCode}</script>`);
if (removeAfterRun) {
Expand All @@ -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();
});
}
Expand Down

0 comments on commit 0e169d8

Please sign in to comment.