@celljs/mvc 4.1.0 → 4.2.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.
Files changed (42) hide show
  1. package/README.md +13 -1
  2. package/esm/node/module.d.ts +1 -1
  3. package/esm/node/view/file-view.js +4 -1
  4. package/esm/node/view/file-view.js.map +1 -1
  5. package/esm/node/view/html-view.d.ts +8 -1
  6. package/esm/node/view/html-view.js +12 -5
  7. package/esm/node/view/html-view.js.map +1 -1
  8. package/esm/node/view/index.d.ts +1 -0
  9. package/esm/node/view/index.js +1 -0
  10. package/esm/node/view/index.js.map +1 -1
  11. package/esm/node/view/template.d.ts +33 -0
  12. package/esm/node/view/template.js +161 -0
  13. package/esm/node/view/template.js.map +1 -0
  14. package/lib/node/module.d.ts +1 -1
  15. package/lib/node/mvc-http.spec.js +7 -15
  16. package/lib/node/mvc-http.spec.js.map +1 -1
  17. package/lib/node/view/file-view.d.ts.map +1 -1
  18. package/lib/node/view/file-view.js +4 -1
  19. package/lib/node/view/file-view.js.map +1 -1
  20. package/lib/node/view/html-view.d.ts +8 -1
  21. package/lib/node/view/html-view.d.ts.map +1 -1
  22. package/lib/node/view/html-view.js +12 -8
  23. package/lib/node/view/html-view.js.map +1 -1
  24. package/lib/node/view/index.d.ts +1 -0
  25. package/lib/node/view/index.d.ts.map +1 -1
  26. package/lib/node/view/index.js +1 -0
  27. package/lib/node/view/index.js.map +1 -1
  28. package/lib/node/view/template.d.ts +34 -0
  29. package/lib/node/view/template.d.ts.map +1 -0
  30. package/lib/node/view/template.js +165 -0
  31. package/lib/node/view/template.js.map +1 -0
  32. package/lib/node/view/template.spec.d.ts +2 -0
  33. package/lib/node/view/template.spec.d.ts.map +1 -0
  34. package/lib/node/view/template.spec.js +35 -0
  35. package/lib/node/view/template.spec.js.map +1 -0
  36. package/package.json +7 -8
  37. package/src/node/mvc-http.spec.ts +9 -16
  38. package/src/node/view/file-view.ts +4 -1
  39. package/src/node/view/html-view.ts +14 -6
  40. package/src/node/view/index.ts +2 -1
  41. package/src/node/view/template.spec.ts +34 -0
  42. package/src/node/view/template.ts +170 -0
@@ -0,0 +1,170 @@
1
+ const ESCAPES: Record<string, string> = {
2
+ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', '\'': '&#39;', '/': '&#x2F;', '`': '&#x60;', '=': '&#x3D;'
3
+ };
4
+
5
+ const UNSAFE = /[&<>"'`=/]/;
6
+
7
+ function escapeHtml(text: string): string {
8
+ return UNSAFE.test(text) ? text.replace(/[&<>"'`=/]/g, c => ESCAPES[c]) : text;
9
+ }
10
+
11
+ /** `path` is the name split at dots; empty for `.`. */
12
+ type Token =
13
+ | { type: 'text', value: string }
14
+ | { type: 'name' | '&', path: string[] }
15
+ | { type: '#' | '^', path: string[], children: Token[] };
16
+
17
+ function toPath(name: string): string[] {
18
+ return name === '.' ? [] : name.split('.');
19
+ }
20
+
21
+ /** Section, inverted section, end of section and comment tags alone on a line take the line with them. */
22
+ const STANDALONE = new Set(['#', '^', '/', '!']);
23
+
24
+ /**
25
+ * A logic-less template with the syntax of Mustache (the subset Cell supports):
26
+ *
27
+ * - `{{name}}` writes a value HTML-escaped, `{{{name}}}` and `{{& name}}` as it is; `name` can be a path (`user.name`)
28
+ * or `.` for the current item; a missing value writes nothing;
29
+ * - `{{#name}}...{{/name}}` repeats for each item of a list, enters an object, or shows when the value is truthy;
30
+ * - `{{^name}}...{{/name}}` shows when the value is falsy or an empty list;
31
+ * - `{{! comment }}` writes nothing.
32
+ *
33
+ * Partials (`{{> name}}`) and other delimiters (`{{=<% %>=}}`) are not supported and fail when the template is compiled.
34
+ */
35
+ export class Template {
36
+
37
+ protected readonly tokens: Token[];
38
+
39
+ constructor(protected readonly source: string) {
40
+ this.tokens = this.parse(source);
41
+ }
42
+
43
+ render(model: unknown): string {
44
+ return this.renderTokens(this.tokens, [model]);
45
+ }
46
+
47
+ protected parse(source: string): Token[] {
48
+ const root: Token[] = [];
49
+ const stack: { name: string, children: Token[] }[] = [];
50
+ let current = root;
51
+ let i = 0;
52
+ while (i < source.length) {
53
+ const open = source.indexOf('{{', i);
54
+ if (open < 0) {
55
+ current.push({ type: 'text', value: source.slice(i) });
56
+ break;
57
+ }
58
+ let text = source.slice(i, open);
59
+ let type: string;
60
+ let name: string;
61
+ let end: number;
62
+ if (source[open + 2] === '{') {
63
+ const close = source.indexOf('}}}', open + 3);
64
+ if (close < 0) {
65
+ throw new Error(`Unclosed tag at ${open}`);
66
+ }
67
+ type = '&';
68
+ name = source.slice(open + 3, close).trim();
69
+ end = close + 3;
70
+ } else {
71
+ const close = source.indexOf('}}', open + 2);
72
+ if (close < 0) {
73
+ throw new Error(`Unclosed tag at ${open}`);
74
+ }
75
+ const content = source.slice(open + 2, close).trim();
76
+ type = '#^/!&>='.includes(content[0]) ? content[0] : 'name';
77
+ name = type === 'name' ? content : content.slice(1).trim();
78
+ end = close + 2;
79
+ }
80
+ if (type === '>' || type === '=') {
81
+ throw new Error(`${type === '>' ? 'Partials' : 'Custom delimiters'} are not supported: ${source.slice(open, end)}`);
82
+ }
83
+ if (STANDALONE.has(type)) {
84
+ const lineStart = source.lastIndexOf('\n', open - 1) + 1;
85
+ const lineEnd = source.indexOf('\n', end);
86
+ const before = source.slice(Math.max(lineStart, i), open);
87
+ const after = source.slice(end, lineEnd < 0 ? source.length : lineEnd);
88
+ if (lineStart >= i && /^[ \t]*$/.test(before) && /^[ \t]*\r?$/.test(after)) {
89
+ text = text.slice(0, text.length - before.length);
90
+ end = lineEnd < 0 ? source.length : lineEnd + 1;
91
+ }
92
+ }
93
+ if (text) {
94
+ current.push({ type: 'text', value: text });
95
+ }
96
+ if (type === '#' || type === '^') {
97
+ const section: Token = { type, path: toPath(name), children: [] };
98
+ current.push(section);
99
+ stack.push({ name, children: current });
100
+ current = section.children;
101
+ } else if (type === '/') {
102
+ const section = stack.pop();
103
+ if (!section || section.name !== name) {
104
+ throw new Error(`Unopened section "${name}" at ${open}`);
105
+ }
106
+ current = section.children;
107
+ } else if (type !== '!') {
108
+ current.push({ type: type as 'name' | '&', path: toPath(name) });
109
+ }
110
+ i = end;
111
+ }
112
+ if (stack.length) {
113
+ throw new Error(`Unclosed section "${stack[stack.length - 1].name}"`);
114
+ }
115
+ return root;
116
+ }
117
+
118
+ protected lookup(path: string[], contexts: unknown[]): unknown {
119
+ if (path.length === 0) {
120
+ return contexts[contexts.length - 1];
121
+ }
122
+ const first = path[0];
123
+ for (let i = contexts.length - 1; i >= 0; i--) {
124
+ const context = contexts[i];
125
+ if (context !== null && typeof context === 'object' && first in context) {
126
+ let value = (context as Record<string, unknown>)[first];
127
+ for (let j = 1; j < path.length; j++) {
128
+ value = value === null || value === undefined ? undefined : (value as Record<string, unknown>)[path[j]];
129
+ }
130
+ return typeof value === 'function' ? value.call(context) : value;
131
+ }
132
+ }
133
+ return undefined;
134
+ }
135
+
136
+ protected renderTokens(tokens: Token[], contexts: unknown[]): string {
137
+ let out = '';
138
+ for (const token of tokens) {
139
+ if (token.type === 'text') {
140
+ out += token.value;
141
+ continue;
142
+ }
143
+ const value = this.lookup(token.path, contexts);
144
+ if (token.type === '#') {
145
+ if (Array.isArray(value)) {
146
+ for (const item of value) {
147
+ contexts.push(item);
148
+ out += this.renderTokens(token.children, contexts);
149
+ contexts.pop();
150
+ }
151
+ } else if (typeof value === 'object' || typeof value === 'string' || typeof value === 'number') {
152
+ if (value) {
153
+ contexts.push(value);
154
+ out += this.renderTokens(token.children, contexts);
155
+ contexts.pop();
156
+ }
157
+ } else if (value) {
158
+ out += this.renderTokens(token.children, contexts);
159
+ }
160
+ } else if (token.type === '^') {
161
+ if (!value || (Array.isArray(value) && value.length === 0)) {
162
+ out += this.renderTokens(token.children, contexts);
163
+ }
164
+ } else if (value !== null && value !== undefined) {
165
+ out += token.type === '&' ? String(value) : escapeHtml(String(value));
166
+ }
167
+ }
168
+ return out;
169
+ }
170
+ }