diff --git a/@mizu/eval/deno.jsonc b/@mizu/eval/deno.jsonc new file mode 100644 index 0000000..4300cab --- /dev/null +++ b/@mizu/eval/deno.jsonc @@ -0,0 +1,7 @@ +{ + "name": "@mizu/eval", + "version": "0.1.0", + "exports": { + ".": "./mod.ts" + } +} diff --git a/@mizu/eval/mod.html b/@mizu/eval/mod.html new file mode 100644 index 0000000..93d709a --- /dev/null +++ b/@mizu/eval/mod.html @@ -0,0 +1,17 @@ + + *eval="expression" +

+ Evaluate a JavaScript expression in the context of the element. +

+ +
+ +
+
+ + Usage of this directive is discouraged and it is recommended to use alternative directives whenever possible for improved maintainability and security reasons. It is nevertheless still offered to help covering edge cases. + + + It is executed after the element and all of its children have been completely processed. + +
diff --git a/@mizu/eval/mod.ts b/@mizu/eval/mod.ts new file mode 100644 index 0000000..7c5929a --- /dev/null +++ b/@mizu/eval/mod.ts @@ -0,0 +1,31 @@ +// Imports +import { type Cache, type Directive, Phase } from "@mizu/mizu/core/engine" +export type * from "@mizu/mizu/core/engine" + +/** `*eval` directive. */ +export const _eval = { + name: "*eval", + phase: Phase.CUSTOM_PROCESSING, + init(renderer) { + renderer.cache>(this.name, new WeakMap()) + }, + execute(renderer, element, { attributes: [attribute], cache }) { + if (renderer.isComment(element)) { + return + } + cache.set(element, attribute) + }, + async cleanup(renderer, element, { cache, ...options }) { + if (renderer.isComment(element)) { + return + } + if (cache.has(element)) { + const attribute = cache.get(element)! + cache.delete(element) + await renderer.evaluate(element, attribute.value, options) + } + }, +} as Directive> + +/** Default exports. */ +export default _eval diff --git a/@mizu/eval/mod_test.html b/@mizu/eval/mod_test.html new file mode 100644 index 0000000..438a296 --- /dev/null +++ b/@mizu/eval/mod_test.html @@ -0,0 +1,26 @@ + + + + +
foo
+
+ +
bar
+
+
+ + + +
+
+ +
+ + + +

+
+ + + +
diff --git a/@mizu/eval/mod_test.ts b/@mizu/eval/mod_test.ts new file mode 100644 index 0000000..f676e88 --- /dev/null +++ b/@mizu/eval/mod_test.ts @@ -0,0 +1 @@ +await import("@mizu/mizu/core/testing").then(({ test }) => test(import.meta)) diff --git a/deno.jsonc b/deno.jsonc index 89ec285..84bd952 100644 --- a/deno.jsonc +++ b/deno.jsonc @@ -85,7 +85,7 @@ // "@mizu/clean", // "@mizu/code", "@mizu/custom-element", - // "@mizu/eval", + "@mizu/eval", // "@mizu/event", "@mizu/for", "@mizu/html",