|
| 1 | +/** |
| 2 | + * Built-in directives for the webjs `html` tagged template system. |
| 3 | + * |
| 4 | + * webjs follows a "less is more" philosophy: only directives that solve |
| 5 | + * problems with NO native alternative are included. AI agents don't need |
| 6 | + * syntax sugar — they can write ternaries, string concatenation, and |
| 7 | + * lifecycle hooks just fine. |
| 8 | + * |
| 9 | + * **What's here:** |
| 10 | + * - `unsafeHTML(str)` — render trusted raw HTML (no alternative in templates) |
| 11 | + * |
| 12 | + * **What's NOT here (and why):** |
| 13 | + * - classMap → use `class=${'btn ' + (active ? 'active' : '')}` |
| 14 | + * - styleMap → use `style=${'color:' + color}` |
| 15 | + * - ifDefined → use `attr=${val ?? null}` (null removes the attribute) |
| 16 | + * - when/choose → use ternary `${cond ? a : b}` or if/else before the template |
| 17 | + * - guard → memoize in `willUpdate()` lifecycle hook |
| 18 | + * - ref → use `this.query('#el')` in `firstUpdated()` or `updated()` |
| 19 | + * - cache → use CSS `display:none` to preserve DOM instead of removing |
| 20 | + * - until → use the `Task` controller for component-scoped async data |
| 21 | + * - live → set `.value` via property binding `.value=${val}` and handle |
| 22 | + * input events with `@input=${e => this.setState({val: e.target.value})}` |
| 23 | + * |
| 24 | + * `repeat()` is in its own file (`./repeat.js`) — it's essential for keyed |
| 25 | + * list reconciliation and has no native alternative. |
| 26 | + * |
| 27 | + * @module directives |
| 28 | + */ |
| 29 | + |
| 30 | +/* ================================================================ |
| 31 | + * unsafeHTML |
| 32 | + * ================================================================ */ |
| 33 | + |
| 34 | +/** |
| 35 | + * Render a raw HTML string without escaping. The string is injected |
| 36 | + * directly into the DOM as parsed HTML nodes. |
| 37 | + * |
| 38 | + * **When to use (AI hint):** Use ONLY for trusted HTML — CMS content, |
| 39 | + * markdown-to-HTML output, or sanitized rich text. NEVER use for |
| 40 | + * user-supplied input — this is an XSS vector. |
| 41 | + * |
| 42 | + * ```js |
| 43 | + * import { html } from 'webjs'; |
| 44 | + * import { unsafeHTML } from 'webjs/directives'; |
| 45 | + * |
| 46 | + * // Good: trusted markdown output |
| 47 | + * html`<article>${unsafeHTML(markdownToHtml(post.body))}</article>`; |
| 48 | + * |
| 49 | + * // DANGEROUS: user input — use ${text} instead (auto-escaped) |
| 50 | + * // html`<p>${unsafeHTML(userInput)}</p>`; // ← XSS! |
| 51 | + * ``` |
| 52 | + * |
| 53 | + * @param {string | null | undefined} htmlString |
| 54 | + * Trusted HTML string to render without escaping. |
| 55 | + * @returns {{ _$webjs: 'unsafe-html', value: string }} |
| 56 | + */ |
| 57 | +export function unsafeHTML(htmlString) { |
| 58 | + return { _$webjs: 'unsafe-html', value: String(htmlString ?? '') }; |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * Type guard: returns `true` if `x` is a marker produced by `unsafeHTML()`. |
| 63 | + * @param {unknown} x |
| 64 | + * @returns {x is { _$webjs: 'unsafe-html', value: string }} |
| 65 | + */ |
| 66 | +export function isUnsafeHTML(x) { |
| 67 | + return !!x && typeof x === 'object' && /** @type {any} */ (x)._$webjs === 'unsafe-html'; |
| 68 | +} |
| 69 | + |
| 70 | +/* ================================================================ |
| 71 | + * live |
| 72 | + * ================================================================ */ |
| 73 | + |
| 74 | +/** |
| 75 | + * Dirty-check a value against the **live DOM value** instead of the |
| 76 | + * last rendered value. Essential for `<input>` two-way binding where |
| 77 | + * the user can modify the DOM value between renders. |
| 78 | + * |
| 79 | + * **When to use (AI hint):** Use `live()` on `.value` or `.checked` |
| 80 | + * bindings for `<input>`, `<textarea>`, `<select>` elements where the |
| 81 | + * user types/selects between renders. Without `live()`, the renderer |
| 82 | + * skips the update because its cached value matches — even though the |
| 83 | + * DOM value has changed. |
| 84 | + * |
| 85 | + * ```js |
| 86 | + * import { html } from 'webjs'; |
| 87 | + * import { live } from 'webjs/directives'; |
| 88 | + * |
| 89 | + * html`<input .value=${live(this.state.query)} |
| 90 | + * @input=${e => this.setState({ query: e.target.value })}>`; |
| 91 | + * ``` |
| 92 | + * |
| 93 | + * On the server, `live()` is a no-op — it unwraps to the inner value. |
| 94 | + * |
| 95 | + * @param {unknown} value The value to set on the element. |
| 96 | + * @returns {{ _$webjs: 'live', value: unknown }} |
| 97 | + */ |
| 98 | +export function live(value) { |
| 99 | + return { _$webjs: 'live', value }; |
| 100 | +} |
| 101 | + |
| 102 | +/** |
| 103 | + * Type guard: returns `true` if `x` is a marker produced by `live()`. |
| 104 | + * @param {unknown} x |
| 105 | + * @returns {x is { _$webjs: 'live', value: unknown }} |
| 106 | + */ |
| 107 | +export function isLive(x) { |
| 108 | + return !!x && typeof x === 'object' && /** @type {any} */ (x)._$webjs === 'live'; |
| 109 | +} |
0 commit comments