diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/eval.ts | 19 | ||||
| -rw-r--r-- | src/index.ts | 44 |
2 files changed, 35 insertions, 28 deletions
diff --git a/src/eval.ts b/src/eval.ts index 79eeb79..a70c760 100644 --- a/src/eval.ts +++ b/src/eval.ts @@ -8,7 +8,11 @@ export interface EvalActions { jsEval(js: string): Promise<any> loadCssx(id: string, url: string): Promise<string> getVariable(name: string): Promise<string | undefined> - updateVariable(id: string, varName: string, value: string): Promise<void> + updateVariable( + id: string | undefined, + varName: string, + value: string, + ): Promise<void> getAttribute( id: string | undefined, name: string, @@ -89,11 +93,12 @@ const getFunctions = (name: string, args: Expr[], actions: EvalActions) => return varName && (actions.getVariable(varName) ?? defaultValue) }, update: async () => { - const id = await evalExpr(args[0], actions) - const varName = await evalExpr(args[1], actions) - const value = await evalExpr(args[2], actions) - if (id && varName && value) { - actions.updateVariable(id, varName, value) + const [id, name, value] = + args.length >= 3 + ? await evalArgs(args, 3, actions) + : [undefined, ...(await evalArgs(args, 2, actions))] + if (name) { + actions.updateVariable(id ?? undefined, name, value ?? '') } }, @@ -103,7 +108,7 @@ const getFunctions = (name: string, args: Expr[], actions: EvalActions) => ? await evalArgs(args, 3, actions) : [undefined, ...(await evalArgs(args, 2, actions))] if (name) { - actions.setAttribute(id as string | undefined, name, value ?? '') + actions.setAttribute(id ?? undefined, name, value ?? '') } }, attr: async () => { diff --git a/src/index.ts b/src/index.ts index acbb360..af8eb23 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,7 +2,6 @@ import { EvalActions, evalExpr } from './eval' import { extractDeclaration, DeclarationEval, - Declaration, expressionsToDeclrs, } from './declarations' import { parse } from './parser' @@ -61,30 +60,32 @@ const getEvalActions = ( removeClass: async (id, cls) => document.getElementById(id)?.classList.remove(cls), delay: delay => new Promise(res => setTimeout(res, delay)), - jsEval: async js => (0, eval)(js), + jsEval: async js => !pure && (0, eval)(js), loadCssx: async (id, url) => - new Promise((resolve, reject) => { - const $link = Object.assign(document.createElement('link'), { - href: url, - rel: 'stylesheet', - }) - $link.onload = () => { - const $el = document.getElementById(id) - if ($el) { - manageElement($el) - resolve(id) - } else { - console.error(`[CSSX] Unable to find root for ${id}`) - reject(`[CSSX] Unable to find root for ${id}`) - } - } - document.body.appendChild($link) - }), + pure + ? '' + : new Promise((resolve, reject) => { + const $link = Object.assign(document.createElement('link'), { + href: url, + rel: 'stylesheet', + }) + $link.onload = () => { + const $el = document.getElementById(id) + if ($el) { + manageElement($el) + resolve(id) + } else { + console.error(`[CSSX] Unable to find root for ${id}`) + reject(`[CSSX] Unable to find root for ${id}`) + } + } + document.body.appendChild($link) + }), getVariable: async varName => getPropertyValue($element, varName), updateVariable: async (targetId, varName, value) => { - const $el = document.getElementById(targetId) + const $el = targetId ? document.getElementById(targetId) : $element if ($el) { - $el.style.setProperty(varName, JSON.stringify(value)) + ;($el as any).style.setProperty(varName, JSON.stringify(value)) } }, setAttribute: async (id, name, value) => { @@ -108,6 +109,7 @@ const getEvalActions = ( ? new FormData($element as HTMLFormElement) : undefined, sendRequest: async ({ url, method, data }) => { + if (pure) return await fetch(url, { method, body: data }) // TODO: Handle response? }, |
