@webstir-io/module-contract 0.5.0 → 0.6.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.
@@ -73,6 +73,14 @@ function run(nodes, data, scopes, options, out) {
73
73
  }
74
74
  case 'attr': {
75
75
  const value = read(node.path, data, scopes);
76
+ if (node.json) {
77
+ // An island's props: the value as JSON, escaped like any attribute.
78
+ if (value !== undefined) {
79
+ const json = toJson(value, node.loc, `data-props="${node.path.source}"`);
80
+ out.push(` ${node.name}="${escapeAttribute(json)}"`);
81
+ }
82
+ break;
83
+ }
76
84
  // ARIA states are the words "true" and "false", not an attribute's presence.
77
85
  if (typeof value === 'boolean' && ARIA_TRUE_FALSE.has(node.name.toLowerCase())) {
78
86
  out.push(` ${node.name}="${value}"`);
@@ -133,6 +141,19 @@ function toText(value, loc, label) {
133
141
  }
134
142
  throw new RenderProgramError(loc, `${label} needs a string or number, got ${describe(value)}`);
135
143
  }
144
+ function toJson(value, loc, label) {
145
+ let json;
146
+ try {
147
+ json = JSON.stringify(value);
148
+ }
149
+ catch (error) {
150
+ throw new RenderProgramError(loc, `${label} needs JSON data: ${error.message}`);
151
+ }
152
+ if (json === undefined) {
153
+ throw new RenderProgramError(loc, `${label} needs JSON data, got ${describe(value)}`);
154
+ }
155
+ return json;
156
+ }
136
157
  /** Every URL in the value is checked: `srcset` lists candidates, `ping` lists URLs. */
137
158
  function safeAttributeUrl(name, value) {
138
159
  if (name === 'srcset' || name === 'imagesrcset') {
@@ -33,6 +33,8 @@ export type RenderNode = string | {
33
33
  readonly op: 'attr';
34
34
  readonly name: string;
35
35
  readonly url: boolean;
36
+ /** Writes the value as JSON (an island's `data-props`) rather than as text. */
37
+ readonly json?: boolean;
36
38
  readonly path: RenderPath;
37
39
  readonly loc: RenderSourceLocation;
38
40
  } | {
@@ -52,19 +54,19 @@ export type RenderNode = string | {
52
54
  };
53
55
  export declare const renderNodeSchema: z.ZodType<RenderNode>;
54
56
  export declare const renderProgramSchema: z.ZodObject<{
55
- version: z.ZodLiteral<1>;
57
+ version: z.ZodLiteral<2>;
56
58
  page: z.ZodString;
57
59
  source: z.ZodString;
58
60
  bindings: z.ZodNumber;
59
61
  nodes: z.ZodArray<z.ZodType<RenderNode, z.ZodTypeDef, RenderNode>, "many">;
60
62
  }, "strip", z.ZodTypeAny, {
61
- version: 1;
63
+ version: 2;
62
64
  page: string;
63
65
  source: string;
64
66
  bindings: number;
65
67
  nodes: RenderNode[];
66
68
  }, {
67
- version: 1;
69
+ version: 2;
68
70
  page: string;
69
71
  source: string;
70
72
  bindings: number;
@@ -17,6 +17,7 @@ export const renderNodeSchema = z.lazy(() => z.union([
17
17
  op: z.literal('attr'),
18
18
  name: z.string().min(1),
19
19
  url: z.boolean(),
20
+ json: z.boolean().optional(),
20
21
  path: renderPathSchema,
21
22
  loc: renderSourceLocationSchema,
22
23
  }),
@@ -1,2 +1,2 @@
1
- export declare const RENDER_PROGRAM_VERSION = 1;
1
+ export declare const RENDER_PROGRAM_VERSION = 2;
2
2
  export declare const RENDER_PROGRAM_FILE = "index.program.json";
@@ -1,2 +1,2 @@
1
- export const RENDER_PROGRAM_VERSION = 1;
1
+ export const RENDER_PROGRAM_VERSION = 2;
2
2
  export const RENDER_PROGRAM_FILE = 'index.program.json';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webstir-io/module-contract",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "description": "Public contract definitions for Webstir frontend and backend module providers.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",