clarusjs 0.17.0 → 0.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -19,6 +19,8 @@ export function htmlExpr(nodes, rw = asWritten, top) {
19
19
  return `__cl_passedHtml(__cl_attrs, ${JSON.stringify((n.skip ?? []).filter((x) => x !== 'engine' && x !== 'use' && !x.includes('.')))})`;
20
20
  if (n.kind === 'attr' && n.name === '...')
21
21
  return `__cl_spreadHtml(${attrValueExpr(n.value, rw.expr)}, ${JSON.stringify(n.skip ?? [])}, ${JSON.stringify(n.tag)}${rw === asWritten && n.where ? `, ${JSON.stringify(n.where)}` : ''})`;
22
+ if (n.kind === 'attr' && n.content)
23
+ return '""';
22
24
  if (n.kind === 'attr')
23
25
  return `__cl_attrHtml(${JSON.stringify(n.name)}, ${attrValueExpr(n.value, rw.expr)}${tagArg(n)})`;
24
26
  if (n.kind === 'if') {
@@ -62,6 +62,7 @@ export type IRNode = {
62
62
  tag: string;
63
63
  skip?: string[];
64
64
  where?: string;
65
+ content?: true;
65
66
  } | {
66
67
  kind: 'connect';
67
68
  expr: string;
@@ -455,6 +455,7 @@ export function lower(parsed, file, source, options = {}) {
455
455
  emitStatic(`<${node.tag}`);
456
456
  const elementEvents = attrs.filter((a) => a.kind === 'event');
457
457
  const dynamic = [];
458
+ let textareaValue;
458
459
  let engine = false;
459
460
  for (const a of attrs) {
460
461
  if (a.kind === 'static')
@@ -516,7 +517,10 @@ export function lower(parsed, file, source, options = {}) {
516
517
  checkExpr(e.expr, file, source, e.start);
517
518
  const reactive = stateful && exprs.some((e) => referencesNames(e.expr, scriptScope) || referencesNames(e.expr, loopNames));
518
519
  const value = a.kind === 'prop' ? { expr: a.expr } : { parts: a.parts.map((p) => (typeof p === 'string' ? p : { expr: p.expr })) };
519
- const node_ = { kind: 'attr', name: a.name, value, reactive, index: -1, tag: node.tag };
520
+ const content = node.tag === 'textarea' && a.name === 'value';
521
+ const node_ = { kind: 'attr', name: a.name, value, reactive, index: -1, tag: node.tag, ...(content ? { content: true } : {}) };
522
+ if (content)
523
+ textareaValue = a.kind === 'prop' ? a.expr : `[${a.parts.map((p) => (typeof p === 'string' ? JSON.stringify(p) : `(${p.expr})`)).join(', ')}].join('')`;
520
524
  if (reactive)
521
525
  dynamic.push(node_);
522
526
  out.push(node_);
@@ -537,6 +541,12 @@ export function lower(parsed, file, source, options = {}) {
537
541
  }
538
542
  }
539
543
  emitStatic('>');
544
+ if (textareaValue !== undefined) {
545
+ emitStatic('\n');
546
+ out.push({ kind: 'hole', expr: textareaValue, reactive: false, index: -1 });
547
+ emitStatic('</textarea>');
548
+ return;
549
+ }
540
550
  const iconBody = iconBodies.get(node);
541
551
  if (iconBody !== undefined)
542
552
  emitStatic(iconBody);
@@ -1,3 +1,9 @@
1
+ /**
2
+ * What `attrValue()` puts in place of a URL it refuses (a `javascript:` link, say): an address that goes nowhere.
3
+ * @example
4
+ * attrValue('href', 'javascript:alert(1)') === UNSAFE_URL // true
5
+ * @see https://clarusjs.com/reference/runtime#attrvalue
6
+ */
1
7
  export declare const UNSAFE_URL = "about:invalid#unsafe";
2
8
  /**
3
9
  * A class from a string (as written), an object (its truthy keys) or an array (flattened, falsy entries dropped), like
@@ -18,6 +24,13 @@ export declare function classValue(value: unknown): string;
18
24
  export declare function styleValue(value: unknown): string;
19
25
  /**
20
26
  * What an attribute should become: `null` removes it, `true` makes it a bare boolean attribute, a string is its value.
27
+ * URL attributes (`href`, `src`…) are checked: a relative URL or a safe scheme passes, anything else becomes
28
+ * `UNSAFE_URL`. The same check templates use, for packages that write attributes themselves (`@clarusjs/editor`).
29
+ * @example
30
+ * attrValue('href', '/docs') // '/docs'
31
+ * attrValue('href', 'javascript:alert(1)') // UNSAFE_URL
32
+ * attrValue('disabled', true) // true
33
+ * @see https://clarusjs.com/reference/runtime#attrvalue
21
34
  */
22
35
  export declare function attrValue(name: string, value: unknown, tag?: string): string | true | null;
23
36
  /**
@@ -13,6 +13,12 @@ const ANIMATION_ELEMENTS = new Set(['animate', 'set', 'animatemotion', 'animatet
13
13
  const IMAGE_ATTRS = new Set(['src', 'srcset', 'poster']);
14
14
  const SAFE_SCHEMES = new Set(['http', 'https', 'mailto', 'tel']);
15
15
  const RASTER_DATA = /^data:image\/(png|gif|jpe?g|webp|avif|bmp);/i;
16
+ /**
17
+ * What `attrValue()` puts in place of a URL it refuses (a `javascript:` link, say): an address that goes nowhere.
18
+ * @example
19
+ * attrValue('href', 'javascript:alert(1)') === UNSAFE_URL // true
20
+ * @see https://clarusjs.com/reference/runtime#attrvalue
21
+ */
16
22
  export const UNSAFE_URL = 'about:invalid#unsafe';
17
23
  /**
18
24
  * Allowlist: a relative URL, a safe scheme, or (for image attributes) a raster data: image or an object URL.
@@ -60,6 +66,13 @@ export function styleValue(value) {
60
66
  }
61
67
  /**
62
68
  * What an attribute should become: `null` removes it, `true` makes it a bare boolean attribute, a string is its value.
69
+ * URL attributes (`href`, `src`…) are checked: a relative URL or a safe scheme passes, anything else becomes
70
+ * `UNSAFE_URL`. The same check templates use, for packages that write attributes themselves (`@clarusjs/editor`).
71
+ * @example
72
+ * attrValue('href', '/docs') // '/docs'
73
+ * attrValue('href', 'javascript:alert(1)') // UNSAFE_URL
74
+ * attrValue('disabled', true) // true
75
+ * @see https://clarusjs.com/reference/runtime#attrvalue
63
76
  */
64
77
  export function attrValue(name, value, tag) {
65
78
  if (value == null)
@@ -586,8 +586,13 @@ export function attr(ctx, index, name, fn, tag) {
586
586
  const el = marked(ctx, `[data-cl-a="${index}"]`);
587
587
  if (!el)
588
588
  return;
589
+ let first = true;
589
590
  effect(() => {
590
591
  const raw = fn();
592
+ // At attach, a field already typed in (before the island's code loaded) keeps what was typed: the loader's
593
+ // replayed input event then writes it to state. Its value differs from the one the server rendered.
594
+ const typed = first && name === 'value' && (el.tagName === 'INPUT' || el.tagName === 'TEXTAREA') && el.value !== el.defaultValue;
595
+ first = false;
591
596
  // <select multiple> bound to an array: each option selected when the array has its value.
592
597
  if (name === 'value' && Array.isArray(raw) && el.tagName === 'SELECT' && el.multiple) {
593
598
  const values = raw.map(String); // option values are text: [1, 2] selects "1" and "2"
@@ -601,7 +606,7 @@ export function attr(ctx, index, name, fn, tag) {
601
606
  else
602
607
  el.setAttribute(name, v === true ? '' : v);
603
608
  // A form field shows its property once someone has typed or clicked in it, not the attribute: set both.
604
- if (name === 'value' && 'value' in el) {
609
+ if (name === 'value' && 'value' in el && !typed) {
605
610
  const field = el;
606
611
  const next = v === null || v === true ? '' : v;
607
612
  // A number field showing the same number keeps what was typed ("1.0" isn't rewritten to "1" mid-typing).
@@ -2,5 +2,5 @@ export { state, derived, shared, effect, scope, onDispose, untrack, batch, type
2
2
  export { holes, blocks, text, on, attr, spread, block, awaited, forBlock, region, model, modelInput, emit, listen, declare, use, values, provideOn, type Hole, type Block, type Ctx } from './dom.ts';
3
3
  export { escape, attrHtml, spreadHtml, blockHtml, forHtml, regionHtml } from './html.ts';
4
4
  export { parse as parseIslandData } from './serialize.ts';
5
- export { classValue, styleValue } from './attrs.ts';
5
+ export { attrValue, classValue, styleValue, UNSAFE_URL } from './attrs.ts';
6
6
  export { browser } from './env.ts';
@@ -3,5 +3,5 @@ export { state, derived, shared, effect, scope, onDispose, untrack, batch } from
3
3
  export { holes, blocks, text, on, attr, spread, block, awaited, forBlock, region, model, modelInput, emit, listen, declare, use, values, provideOn } from './dom.js';
4
4
  export { escape, attrHtml, spreadHtml, blockHtml, forHtml, regionHtml } from './html.js';
5
5
  export { parse as parseIslandData } from './serialize.js';
6
- export { classValue, styleValue } from './attrs.js';
6
+ export { attrValue, classValue, styleValue, UNSAFE_URL } from './attrs.js';
7
7
  export { browser } from './env.js';
@@ -162,8 +162,15 @@ async function attachOne(el, load, recorded) {
162
162
  loading.delete(el);
163
163
  }
164
164
  recorded.stop();
165
- if (el.hasAttribute('data-cl-ready'))
165
+ if (el.hasAttribute('data-cl-ready')) {
166
+ // Fields filled before attach with no input to replay (form restore, autofill: the value differs from the
167
+ // server's) send one now, before a replayed submit reads state; a field typed in gets its recorded input, once.
168
+ for (const f of el.querySelectorAll('input,textarea'))
169
+ // the value check first: owner() walks siblings, too slow to run for every row's field
170
+ if (f.value !== f.defaultValue && owner(f) === el && !recorded.events.some((e) => e.target === f && e.type === 'input'))
171
+ f.dispatchEvent(new Event('input', { bubbles: true }));
166
172
  replay(recorded.events);
173
+ }
167
174
  }
168
175
  /**
169
176
  * Attach every island under `root` that isn't attached yet. Resolves once the islands that attach right away
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clarusjs",
3
- "version": "0.17.0",
3
+ "version": "0.18.0",
4
4
  "description": "Clear, compiled views: templates compiled to server HTML, with JavaScript only for interactive islands. SSR, SSG and CSR from one component model, built on Vite.",
5
5
  "license": "MIT",
6
6
  "keywords": [