sjabloon 0.11.0 → 0.12.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.
- package/EMBEDDING.md +209 -0
- package/README.md +150 -117
- package/lib/core.js +149 -211
- package/lib/html.d.ts +3 -12
- package/lib/html.js +8 -6
- package/lib/index.d.ts +3 -12
- package/lib/index.js +11 -15
- package/lib/text.d.ts +3 -12
- package/lib/text.js +6 -3
- package/lib/types.d.ts +37 -7
- package/package.json +8 -3
package/lib/index.js
CHANGED
|
@@ -9,15 +9,12 @@ import { display, make } from "./core.js";
|
|
|
9
9
|
export { display, isDiagnostic, relocate } from "./core.js";
|
|
10
10
|
|
|
11
11
|
export const { template, render } = make([
|
|
12
|
-
// Static text is a compile-time constant:
|
|
13
|
-
// text node rather than allocating
|
|
14
|
-
(text) =>
|
|
15
|
-
(
|
|
16
|
-
acc.push(token)
|
|
17
|
-
|
|
18
|
-
(expr) => (scope, acc) => {
|
|
19
|
-
acc.push({ value: expr(scope) });
|
|
20
|
-
},
|
|
12
|
+
// Static text is a compile-time constant: the default parameter hoists and
|
|
13
|
+
// freezes one token per text node rather than allocating per loop iteration.
|
|
14
|
+
(text, token = Object.freeze({ literal: text })) =>
|
|
15
|
+
(scope, acc) =>
|
|
16
|
+
acc.push(token),
|
|
17
|
+
(expr) => (scope, acc) => acc.push({ value: expr(scope) }),
|
|
21
18
|
0,
|
|
22
19
|
() => /** @type {import('./types.js').Token[]} */ ([]),
|
|
23
20
|
(acc) => acc,
|
|
@@ -31,12 +28,11 @@ export const { template, render } = make([
|
|
|
31
28
|
* @param {readonly import('./types.js').Token[]} tokens A render's output.
|
|
32
29
|
* @returns {string} The joined text.
|
|
33
30
|
*/
|
|
34
|
-
export const text = (tokens) =>
|
|
35
|
-
let s = "";
|
|
31
|
+
export const text = (tokens) =>
|
|
36
32
|
// A literal never stringifies and a nullish value still renders empty.
|
|
37
33
|
// Widened here because each token carries one key or the other, which the
|
|
38
34
|
// public union deliberately does not model.
|
|
39
|
-
|
|
40
|
-
s
|
|
41
|
-
|
|
42
|
-
|
|
35
|
+
/** @type {readonly { literal?: string, value?: unknown }[]} */ (tokens).reduce(
|
|
36
|
+
(s, t) => s + (t.literal ?? display(t.value)),
|
|
37
|
+
"",
|
|
38
|
+
);
|
package/lib/text.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export * from "./types.js";
|
|
2
|
-
import type {
|
|
2
|
+
import type { SjabloonRender, SjabloonTemplate } from "./types.js";
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Compile a template once, render it many times to a plain string.
|
|
@@ -7,15 +7,6 @@ import type { SjabloonFunctions, SjabloonRenderer, SjabloonValues } from "./type
|
|
|
7
7
|
* `{{ expr }}` interpolates unescaped — escaping belongs at the output edge —
|
|
8
8
|
* so `{{{ expr }}}` has no meaning here and is a compile-time
|
|
9
9
|
* `SJABLOON_RAW_TAG` error.
|
|
10
|
-
*
|
|
11
|
-
* @see SjabloonRenderer for `names`/`functions`, SjabloonScope for `$` and `@`.
|
|
12
|
-
* @throws {SyntaxError} On malformed tags, unclosed blocks, or bad expressions.
|
|
13
10
|
*/
|
|
14
|
-
export
|
|
15
|
-
|
|
16
|
-
funcs?: SjabloonFunctions,
|
|
17
|
-
opts?: { bound?: Iterable<string> },
|
|
18
|
-
): SjabloonRenderer<string>;
|
|
19
|
-
|
|
20
|
-
/** Compile and render in one go. Shorthand for `template(str, funcs)(values)`. */
|
|
21
|
-
export function render(str: string, values?: SjabloonValues, funcs?: SjabloonFunctions): string;
|
|
11
|
+
export const template: SjabloonTemplate<string>;
|
|
12
|
+
export const render: SjabloonRender<string>;
|
package/lib/text.js
CHANGED
|
@@ -6,13 +6,16 @@
|
|
|
6
6
|
* Definitionally `text(template(str)(values))` from the root entry, but built
|
|
7
7
|
* as a string accumulator so casual string users never allocate tokens.
|
|
8
8
|
*/
|
|
9
|
-
import { display,
|
|
9
|
+
import { display, make } from "./core.js";
|
|
10
10
|
|
|
11
11
|
export { isDiagnostic, relocate } from "./core.js";
|
|
12
12
|
|
|
13
|
+
// The string accumulator is this edition's own shape, so its profile is stated
|
|
14
|
+
// here rather than named in the core: every node is shorter than an import of
|
|
15
|
+
// it, and the core stays the parser alone.
|
|
13
16
|
export const { template, render } = make([
|
|
14
|
-
|
|
15
|
-
(expr) => (scope, acc
|
|
17
|
+
(text) => (scope, acc) => (acc.text += text),
|
|
18
|
+
(expr) => (scope, acc) => (acc.text += display(expr(scope))),
|
|
16
19
|
0,
|
|
17
20
|
() => ({ text: "" }),
|
|
18
21
|
(acc) => acc.text,
|
package/lib/types.d.ts
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
|
+
import type { Diagnostic, Relocation } from "waarmerk";
|
|
1
2
|
import type { XprsnErrorCode, XprsnRead } from "xprsn";
|
|
2
3
|
|
|
4
|
+
/**
|
|
5
|
+
* The codes sjabloon itself mints. An expression fault carries xprsn's instead,
|
|
6
|
+
* which is why the diagnostic's `code` is the wider union — the same split
|
|
7
|
+
* padvinder draws between its own codes and treffer's.
|
|
8
|
+
*/
|
|
3
9
|
export type SjabloonErrorCode =
|
|
4
|
-
| XprsnErrorCode
|
|
5
10
|
| "SJABLOON_EACH_SYNTAX"
|
|
6
11
|
| "SJABLOON_BLOCKED_BINDING"
|
|
7
12
|
| "SJABLOON_UNEXPECTED_TAG"
|
|
@@ -16,8 +21,17 @@ export interface SjabloonBlock {
|
|
|
16
21
|
readonly end: number;
|
|
17
22
|
}
|
|
18
23
|
|
|
19
|
-
|
|
20
|
-
|
|
24
|
+
/**
|
|
25
|
+
* The fields and their meanings are waarmerk's; this names the code union they are
|
|
26
|
+
* checked against, narrows the three this module always carries from optional
|
|
27
|
+
* to required, and adds the block context a template fault sits in.
|
|
28
|
+
*
|
|
29
|
+
* `code` is widened with `XprsnErrorCode` because most diagnostics here are
|
|
30
|
+
* xprsn errors translated into template coordinates: a host can still tell a
|
|
31
|
+
* malformed tag from a fault in the expression inside it.
|
|
32
|
+
*/
|
|
33
|
+
export interface SjabloonDiagnostic extends Diagnostic<SjabloonErrorCode | XprsnErrorCode> {
|
|
34
|
+
readonly code: SjabloonErrorCode | XprsnErrorCode;
|
|
21
35
|
readonly start: number;
|
|
22
36
|
readonly end: number;
|
|
23
37
|
readonly blocks: readonly SjabloonBlock[];
|
|
@@ -81,6 +95,25 @@ export interface SjabloonRenderer<T> {
|
|
|
81
95
|
scoped(values: SjabloonValues): T;
|
|
82
96
|
}
|
|
83
97
|
|
|
98
|
+
/**
|
|
99
|
+
* `template` in every edition: compile once, render many times. `T` is what
|
|
100
|
+
* one render returns.
|
|
101
|
+
*
|
|
102
|
+
* @throws {SyntaxError} On malformed tags, unclosed blocks, or bad expressions.
|
|
103
|
+
*/
|
|
104
|
+
export type SjabloonTemplate<T> = (
|
|
105
|
+
str: string,
|
|
106
|
+
funcs?: SjabloonFunctions,
|
|
107
|
+
opts?: { bound?: Iterable<string> },
|
|
108
|
+
) => SjabloonRenderer<T>;
|
|
109
|
+
|
|
110
|
+
/** Compile and render in one go. Shorthand for `template(str, funcs)(values)`. */
|
|
111
|
+
export type SjabloonRender<T> = (
|
|
112
|
+
str: string,
|
|
113
|
+
values?: SjabloonValues,
|
|
114
|
+
funcs?: SjabloonFunctions,
|
|
115
|
+
) => T;
|
|
116
|
+
|
|
84
117
|
/** One static text run of the template, verbatim. */
|
|
85
118
|
export interface LiteralToken {
|
|
86
119
|
literal: string;
|
|
@@ -113,7 +146,4 @@ export function isDiagnostic(error: unknown): error is SjabloonDiagnostic;
|
|
|
113
146
|
*
|
|
114
147
|
* @throws {TypeError} When `diag` is not a sjabloon diagnostic.
|
|
115
148
|
*/
|
|
116
|
-
export function relocate(
|
|
117
|
-
diag: unknown,
|
|
118
|
-
opts?: { prefix?: string; offset?: number },
|
|
119
|
-
): SjabloonDiagnostic;
|
|
149
|
+
export function relocate(diag: unknown, opts?: Relocation): SjabloonDiagnostic;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sjabloon",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.0",
|
|
4
4
|
"description": "Tiny, CSP-safe template engine for JavaScript, powered by xprsn expressions. No eval, no new Function.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"csp",
|
|
@@ -17,7 +17,8 @@
|
|
|
17
17
|
},
|
|
18
18
|
"repository": "getquario/sjabloon",
|
|
19
19
|
"files": [
|
|
20
|
-
"lib"
|
|
20
|
+
"lib",
|
|
21
|
+
"EMBEDDING.md"
|
|
21
22
|
],
|
|
22
23
|
"type": "module",
|
|
23
24
|
"module": "lib/index.js",
|
|
@@ -61,7 +62,8 @@
|
|
|
61
62
|
"test:unit": "c8 --100 --src lib/ node --disallow-code-generation-from-strings --test --test-concurrency=1 test/*.test.js"
|
|
62
63
|
},
|
|
63
64
|
"dependencies": {
|
|
64
|
-
"
|
|
65
|
+
"waarmerk": "^0.1.0",
|
|
66
|
+
"xprsn": "^0.11.1"
|
|
65
67
|
},
|
|
66
68
|
"devDependencies": {
|
|
67
69
|
"@arethetypeswrong/cli": "^0.18.3",
|
|
@@ -83,6 +85,7 @@
|
|
|
83
85
|
"name": "sjabloon",
|
|
84
86
|
"path": "lib/index.js",
|
|
85
87
|
"ignore": [
|
|
88
|
+
"waarmerk",
|
|
86
89
|
"xprsn"
|
|
87
90
|
],
|
|
88
91
|
"limit": "2.35 kB"
|
|
@@ -91,6 +94,7 @@
|
|
|
91
94
|
"name": "sjabloon/text",
|
|
92
95
|
"path": "lib/text.js",
|
|
93
96
|
"ignore": [
|
|
97
|
+
"waarmerk",
|
|
94
98
|
"xprsn"
|
|
95
99
|
],
|
|
96
100
|
"limit": "2.35 kB"
|
|
@@ -99,6 +103,7 @@
|
|
|
99
103
|
"name": "sjabloon/html",
|
|
100
104
|
"path": "lib/html.js",
|
|
101
105
|
"ignore": [
|
|
106
|
+
"waarmerk",
|
|
102
107
|
"xprsn"
|
|
103
108
|
],
|
|
104
109
|
"limit": "2.4 kB"
|