@selfhelp/shared 1.2.5 → 1.3.1
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/CHANGELOG.md +341 -326
- package/LICENSE +381 -381
- package/README.md +57 -57
- package/dist/index.d.mts +25 -1
- package/dist/index.d.ts +25 -1
- package/dist/index.js +2 -0
- package/dist/index.mjs +2 -0
- package/package.json +102 -102
- package/src/api/__tests__/endpoints.test.ts +45 -0
- package/src/api/endpoints.ts +2 -0
- package/src/api/index.ts +5 -5
- package/src/assets/__tests__/resolveAssetUrl.test.ts +61 -61
- package/src/assets/index.ts +5 -5
- package/src/assets/resolveAssetUrl.ts +44 -44
- package/src/cms-classes/__tests__/classify.test.ts +63 -63
- package/src/cms-classes/allow-list.ts +158 -158
- package/src/cms-classes/classify.ts +90 -90
- package/src/cms-classes/index.ts +7 -7
- package/src/cms-classes/remap.ts +44 -44
- package/src/condition/__tests__/evaluate.test.ts +54 -54
- package/src/condition/evaluate.ts +121 -121
- package/src/condition/index.ts +5 -5
- package/src/index.ts +25 -25
- package/src/interpolation/__tests__/replaceCalcedValues.test.ts +41 -41
- package/src/interpolation/index.ts +5 -5
- package/src/interpolation/replaceCalcedValues.ts +27 -27
- package/src/plugin-sdk/define.ts +89 -89
- package/src/plugin-sdk/index.ts +103 -103
- package/src/plugin-sdk/lock.ts +124 -124
- package/src/plugin-sdk/manifest.ts +307 -307
- package/src/plugin-sdk/realtime.ts +277 -277
- package/src/plugin-sdk/registry.ts +105 -105
- package/src/plugin-sdk/runtime-shim.ts +140 -140
- package/src/plugin-sdk/types.ts +315 -315
- package/src/plugin-sdk/version.ts +209 -209
- package/src/registry/__tests__/registry.test.ts +135 -135
- package/src/registry/index.ts +94 -94
- package/src/registry/styles.registry.ts +295 -295
- package/src/testing/__tests__/testing-kit.test.ts +228 -228
- package/src/testing/index.ts +483 -483
- package/src/theme/index.ts +6 -6
- package/src/theme/tailwind.ts +73 -73
- package/src/theme/tokens.ts +177 -177
- package/src/types/api/auth.ts +22 -0
- package/src/types/api/envelope.ts +34 -34
- package/src/types/api/forms.ts +76 -76
- package/src/types/api/index.ts +9 -9
- package/src/types/api/language.ts +12 -12
- package/src/types/api/page.ts +9 -9
- package/src/types/index.ts +17 -17
- package/src/types/mantine/common.ts +142 -142
- package/src/types/pages.ts +107 -107
- package/src/types/styles/auth.ts +184 -184
- package/src/types/styles/base.ts +72 -72
- package/src/types/styles/composite.ts +131 -131
- package/src/types/styles/forms.ts +439 -439
- package/src/types/styles/interactive.ts +165 -165
- package/src/types/styles/layout.ts +184 -184
- package/src/types/styles/media.ts +68 -68
- package/src/types/styles/typography.ts +118 -118
- package/src/types/styles/unknown.ts +220 -220
- package/src/utils/__tests__/transformPageData.test.ts +69 -69
- package/src/utils/spacing.ts +64 -64
- package/src/utils/transformPageData.ts +99 -99
|
@@ -1,90 +1,90 @@
|
|
|
1
|
-
/*
|
|
2
|
-
SPDX-FileCopyrightText: 2026 Humdek, University of Bern
|
|
3
|
-
SPDX-License-Identifier: MPL-2.0
|
|
4
|
-
*/
|
|
5
|
-
/**
|
|
6
|
-
* `classifyClass()` — one-shot decision for a single CMS class on mobile.
|
|
7
|
-
*
|
|
8
|
-
* Returns a tuple describing what the renderer should do:
|
|
9
|
-
* - 'allow' — pass the class through unchanged
|
|
10
|
-
* - 'remap' — pass the rewritten class through (`to` is the new value)
|
|
11
|
-
* - 'drop' — do not pass the class through; renderer should warn
|
|
12
|
-
* in development.
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
import { CLASS_PATTERNS, LITERAL_CLASSES } from './allow-list';
|
|
16
|
-
import { findRemap } from './remap';
|
|
17
|
-
|
|
18
|
-
export type TClassDecision =
|
|
19
|
-
| { kind: 'allow'; className: string }
|
|
20
|
-
| { kind: 'remap'; from: string; to: string; reason?: string }
|
|
21
|
-
| { kind: 'drop'; className: string; reason?: string };
|
|
22
|
-
|
|
23
|
-
function matchesPattern(className: string): boolean {
|
|
24
|
-
for (const pattern of CLASS_PATTERNS) {
|
|
25
|
-
if (!className.startsWith(pattern.prefix)) continue;
|
|
26
|
-
const tail = className.slice(pattern.prefix.length);
|
|
27
|
-
if (pattern.values.includes(tail)) return true;
|
|
28
|
-
}
|
|
29
|
-
return false;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export function classifyClass(rawClass: string): TClassDecision {
|
|
33
|
-
const className = rawClass.trim();
|
|
34
|
-
if (!className) return { kind: 'drop', className, reason: 'empty class' };
|
|
35
|
-
|
|
36
|
-
// Variant prefixes (`md:`, `dark:`, `hover:`) — strip the prefix
|
|
37
|
-
// before matching, but the remap table can also intercept them.
|
|
38
|
-
const remap = findRemap(className);
|
|
39
|
-
if (remap) {
|
|
40
|
-
if (remap.to === null) {
|
|
41
|
-
return { kind: 'drop', className, reason: remap.reason };
|
|
42
|
-
}
|
|
43
|
-
return { kind: 'remap', from: className, to: remap.to, reason: remap.reason };
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
// arbitrary value tail like `w-[120px]` — pass through, Uniwind handles it.
|
|
47
|
-
if (className.includes('[') && className.endsWith(']')) {
|
|
48
|
-
return { kind: 'allow', className };
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
if (LITERAL_CLASSES.has(className)) {
|
|
52
|
-
return { kind: 'allow', className };
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
if (matchesPattern(className)) {
|
|
56
|
-
return { kind: 'allow', className };
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
return { kind: 'drop', className, reason: 'not in mobile allow-list' };
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Run classifyClass over a whitespace-separated class string and return
|
|
64
|
-
* the resulting list of allowed classes. The optional `onWarn` callback
|
|
65
|
-
* fires for every drop/remap (use this in dev to spot CMS-side issues).
|
|
66
|
-
*/
|
|
67
|
-
export function classifyClassString(
|
|
68
|
-
raw: string | null | undefined,
|
|
69
|
-
onWarn?: (decision: Exclude<TClassDecision, { kind: 'allow' }>) => void
|
|
70
|
-
): string[] {
|
|
71
|
-
if (!raw) return [];
|
|
72
|
-
const out: string[] = [];
|
|
73
|
-
for (const cls of raw.split(/\s+/)) {
|
|
74
|
-
if (!cls) continue;
|
|
75
|
-
const decision = classifyClass(cls);
|
|
76
|
-
switch (decision.kind) {
|
|
77
|
-
case 'allow':
|
|
78
|
-
out.push(decision.className);
|
|
79
|
-
break;
|
|
80
|
-
case 'remap':
|
|
81
|
-
out.push(decision.to);
|
|
82
|
-
if (onWarn) onWarn(decision);
|
|
83
|
-
break;
|
|
84
|
-
case 'drop':
|
|
85
|
-
if (onWarn) onWarn(decision);
|
|
86
|
-
break;
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
return out;
|
|
90
|
-
}
|
|
1
|
+
/*
|
|
2
|
+
SPDX-FileCopyrightText: 2026 Humdek, University of Bern
|
|
3
|
+
SPDX-License-Identifier: MPL-2.0
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* `classifyClass()` — one-shot decision for a single CMS class on mobile.
|
|
7
|
+
*
|
|
8
|
+
* Returns a tuple describing what the renderer should do:
|
|
9
|
+
* - 'allow' — pass the class through unchanged
|
|
10
|
+
* - 'remap' — pass the rewritten class through (`to` is the new value)
|
|
11
|
+
* - 'drop' — do not pass the class through; renderer should warn
|
|
12
|
+
* in development.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { CLASS_PATTERNS, LITERAL_CLASSES } from './allow-list';
|
|
16
|
+
import { findRemap } from './remap';
|
|
17
|
+
|
|
18
|
+
export type TClassDecision =
|
|
19
|
+
| { kind: 'allow'; className: string }
|
|
20
|
+
| { kind: 'remap'; from: string; to: string; reason?: string }
|
|
21
|
+
| { kind: 'drop'; className: string; reason?: string };
|
|
22
|
+
|
|
23
|
+
function matchesPattern(className: string): boolean {
|
|
24
|
+
for (const pattern of CLASS_PATTERNS) {
|
|
25
|
+
if (!className.startsWith(pattern.prefix)) continue;
|
|
26
|
+
const tail = className.slice(pattern.prefix.length);
|
|
27
|
+
if (pattern.values.includes(tail)) return true;
|
|
28
|
+
}
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function classifyClass(rawClass: string): TClassDecision {
|
|
33
|
+
const className = rawClass.trim();
|
|
34
|
+
if (!className) return { kind: 'drop', className, reason: 'empty class' };
|
|
35
|
+
|
|
36
|
+
// Variant prefixes (`md:`, `dark:`, `hover:`) — strip the prefix
|
|
37
|
+
// before matching, but the remap table can also intercept them.
|
|
38
|
+
const remap = findRemap(className);
|
|
39
|
+
if (remap) {
|
|
40
|
+
if (remap.to === null) {
|
|
41
|
+
return { kind: 'drop', className, reason: remap.reason };
|
|
42
|
+
}
|
|
43
|
+
return { kind: 'remap', from: className, to: remap.to, reason: remap.reason };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// arbitrary value tail like `w-[120px]` — pass through, Uniwind handles it.
|
|
47
|
+
if (className.includes('[') && className.endsWith(']')) {
|
|
48
|
+
return { kind: 'allow', className };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (LITERAL_CLASSES.has(className)) {
|
|
52
|
+
return { kind: 'allow', className };
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (matchesPattern(className)) {
|
|
56
|
+
return { kind: 'allow', className };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return { kind: 'drop', className, reason: 'not in mobile allow-list' };
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Run classifyClass over a whitespace-separated class string and return
|
|
64
|
+
* the resulting list of allowed classes. The optional `onWarn` callback
|
|
65
|
+
* fires for every drop/remap (use this in dev to spot CMS-side issues).
|
|
66
|
+
*/
|
|
67
|
+
export function classifyClassString(
|
|
68
|
+
raw: string | null | undefined,
|
|
69
|
+
onWarn?: (decision: Exclude<TClassDecision, { kind: 'allow' }>) => void
|
|
70
|
+
): string[] {
|
|
71
|
+
if (!raw) return [];
|
|
72
|
+
const out: string[] = [];
|
|
73
|
+
for (const cls of raw.split(/\s+/)) {
|
|
74
|
+
if (!cls) continue;
|
|
75
|
+
const decision = classifyClass(cls);
|
|
76
|
+
switch (decision.kind) {
|
|
77
|
+
case 'allow':
|
|
78
|
+
out.push(decision.className);
|
|
79
|
+
break;
|
|
80
|
+
case 'remap':
|
|
81
|
+
out.push(decision.to);
|
|
82
|
+
if (onWarn) onWarn(decision);
|
|
83
|
+
break;
|
|
84
|
+
case 'drop':
|
|
85
|
+
if (onWarn) onWarn(decision);
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return out;
|
|
90
|
+
}
|
package/src/cms-classes/index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
/*
|
|
2
|
-
SPDX-FileCopyrightText: 2026 Humdek, University of Bern
|
|
3
|
-
SPDX-License-Identifier: MPL-2.0
|
|
4
|
-
*/
|
|
5
|
-
export * from './allow-list';
|
|
6
|
-
export * from './remap';
|
|
7
|
-
export * from './classify';
|
|
1
|
+
/*
|
|
2
|
+
SPDX-FileCopyrightText: 2026 Humdek, University of Bern
|
|
3
|
+
SPDX-License-Identifier: MPL-2.0
|
|
4
|
+
*/
|
|
5
|
+
export * from './allow-list';
|
|
6
|
+
export * from './remap';
|
|
7
|
+
export * from './classify';
|
package/src/cms-classes/remap.ts
CHANGED
|
@@ -1,44 +1,44 @@
|
|
|
1
|
-
/*
|
|
2
|
-
SPDX-FileCopyrightText: 2026 Humdek, University of Bern
|
|
3
|
-
SPDX-License-Identifier: MPL-2.0
|
|
4
|
-
*/
|
|
5
|
-
/**
|
|
6
|
-
* Web -> mobile class remap. Some Tailwind classes are valid on web but
|
|
7
|
-
* have no mobile equivalent (or have a different name in Uniwind). This
|
|
8
|
-
* table runs BEFORE the allow-list check.
|
|
9
|
-
*
|
|
10
|
-
* Rules of thumb:
|
|
11
|
-
* - drop classes that don't make sense on mobile (`select-text` is a no-op on RN, `cursor-*`, `hover:*`).
|
|
12
|
-
* - rewrite logical pairs (e.g. mobile prefers `text-center` over `text-left` defaults — leave alone).
|
|
13
|
-
* - alias common Mantine-flavored utility names users may type.
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
export interface IClassRemap {
|
|
17
|
-
/** Class to rewrite. */
|
|
18
|
-
from: string;
|
|
19
|
-
/** Replacement class, or null to drop the class entirely. */
|
|
20
|
-
to: string | null;
|
|
21
|
-
/** Optional reason surfaced via dev warning. */
|
|
22
|
-
reason?: string;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export const CLASS_REMAP: readonly IClassRemap[] = [
|
|
26
|
-
// hover/focus on mobile only fires for keyboard nav; safe to drop.
|
|
27
|
-
{ from: 'hover:bg-gray-100', to: null, reason: 'hover not applicable on mobile' },
|
|
28
|
-
{ from: 'hover:underline', to: null, reason: 'hover not applicable on mobile' },
|
|
29
|
-
// cursor classes are no-ops on RN.
|
|
30
|
-
{ from: 'cursor-pointer', to: null, reason: 'no cursor on RN' },
|
|
31
|
-
{ from: 'cursor-default', to: null, reason: 'no cursor on RN' },
|
|
32
|
-
{ from: 'cursor-not-allowed', to: null, reason: 'no cursor on RN' },
|
|
33
|
-
// select-text behaves slightly differently on RN; keep but as a soft no-op.
|
|
34
|
-
{ from: 'select-text', to: null, reason: 'unsupported on RN' },
|
|
35
|
-
// legacy aliases
|
|
36
|
-
{ from: 'mantine-fw', to: 'w-full' },
|
|
37
|
-
{ from: 'mantine-h-full', to: 'h-full' },
|
|
38
|
-
];
|
|
39
|
-
|
|
40
|
-
const REMAP_MAP = new Map<string, IClassRemap>(CLASS_REMAP.map((r) => [r.from, r]));
|
|
41
|
-
|
|
42
|
-
export function findRemap(className: string): IClassRemap | undefined {
|
|
43
|
-
return REMAP_MAP.get(className);
|
|
44
|
-
}
|
|
1
|
+
/*
|
|
2
|
+
SPDX-FileCopyrightText: 2026 Humdek, University of Bern
|
|
3
|
+
SPDX-License-Identifier: MPL-2.0
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Web -> mobile class remap. Some Tailwind classes are valid on web but
|
|
7
|
+
* have no mobile equivalent (or have a different name in Uniwind). This
|
|
8
|
+
* table runs BEFORE the allow-list check.
|
|
9
|
+
*
|
|
10
|
+
* Rules of thumb:
|
|
11
|
+
* - drop classes that don't make sense on mobile (`select-text` is a no-op on RN, `cursor-*`, `hover:*`).
|
|
12
|
+
* - rewrite logical pairs (e.g. mobile prefers `text-center` over `text-left` defaults — leave alone).
|
|
13
|
+
* - alias common Mantine-flavored utility names users may type.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
export interface IClassRemap {
|
|
17
|
+
/** Class to rewrite. */
|
|
18
|
+
from: string;
|
|
19
|
+
/** Replacement class, or null to drop the class entirely. */
|
|
20
|
+
to: string | null;
|
|
21
|
+
/** Optional reason surfaced via dev warning. */
|
|
22
|
+
reason?: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export const CLASS_REMAP: readonly IClassRemap[] = [
|
|
26
|
+
// hover/focus on mobile only fires for keyboard nav; safe to drop.
|
|
27
|
+
{ from: 'hover:bg-gray-100', to: null, reason: 'hover not applicable on mobile' },
|
|
28
|
+
{ from: 'hover:underline', to: null, reason: 'hover not applicable on mobile' },
|
|
29
|
+
// cursor classes are no-ops on RN.
|
|
30
|
+
{ from: 'cursor-pointer', to: null, reason: 'no cursor on RN' },
|
|
31
|
+
{ from: 'cursor-default', to: null, reason: 'no cursor on RN' },
|
|
32
|
+
{ from: 'cursor-not-allowed', to: null, reason: 'no cursor on RN' },
|
|
33
|
+
// select-text behaves slightly differently on RN; keep but as a soft no-op.
|
|
34
|
+
{ from: 'select-text', to: null, reason: 'unsupported on RN' },
|
|
35
|
+
// legacy aliases
|
|
36
|
+
{ from: 'mantine-fw', to: 'w-full' },
|
|
37
|
+
{ from: 'mantine-h-full', to: 'h-full' },
|
|
38
|
+
];
|
|
39
|
+
|
|
40
|
+
const REMAP_MAP = new Map<string, IClassRemap>(CLASS_REMAP.map((r) => [r.from, r]));
|
|
41
|
+
|
|
42
|
+
export function findRemap(className: string): IClassRemap | undefined {
|
|
43
|
+
return REMAP_MAP.get(className);
|
|
44
|
+
}
|
|
@@ -1,54 +1,54 @@
|
|
|
1
|
-
/*
|
|
2
|
-
SPDX-FileCopyrightText: 2026 Humdek, University of Bern
|
|
3
|
-
SPDX-License-Identifier: MPL-2.0
|
|
4
|
-
*/
|
|
5
|
-
import { describe, it, expect } from 'vitest';
|
|
6
|
-
import { buildConditionContext, evaluateCondition } from '../evaluate';
|
|
7
|
-
|
|
8
|
-
describe('evaluateCondition', () => {
|
|
9
|
-
const ctx = buildConditionContext('web');
|
|
10
|
-
|
|
11
|
-
it('treats a null/empty/whitespace condition as visible (no condition)', () => {
|
|
12
|
-
expect(evaluateCondition(null, ctx).visible).toBe(true);
|
|
13
|
-
expect(evaluateCondition('', ctx).visible).toBe(true);
|
|
14
|
-
expect(evaluateCondition(' ', ctx).visible).toBe(true);
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
it('evaluates a true JSON-Logic rule to visible', () => {
|
|
18
|
-
expect(evaluateCondition('{"==":[1,1]}', ctx).visible).toBe(true);
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
it('evaluates a false JSON-Logic rule to not visible', () => {
|
|
22
|
-
expect(evaluateCondition('{"==":[1,2]}', ctx).visible).toBe(false);
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
it('resolves context variables (platform) from the condition context', () => {
|
|
26
|
-
const rule = '{"==":[{"var":"platform"},"web"]}';
|
|
27
|
-
expect(evaluateCondition(rule, buildConditionContext('web')).visible).toBe(true);
|
|
28
|
-
expect(evaluateCondition(rule, buildConditionContext('mobile')).visible).toBe(false);
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
it('treats invalid JSON as not visible (fail closed, never throws)', () => {
|
|
32
|
-
expect(evaluateCondition('{not json', ctx).visible).toBe(false);
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
it('exposes a debug payload with the error when withDebug is set', () => {
|
|
36
|
-
const result = evaluateCondition('{not json', ctx, { withDebug: true });
|
|
37
|
-
expect(result.visible).toBe(false);
|
|
38
|
-
expect(result.debug?.error).toContain('Invalid JSON');
|
|
39
|
-
expect(result.debug?.condition).toBe('{not json');
|
|
40
|
-
});
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
describe('buildConditionContext', () => {
|
|
44
|
-
it('builds deterministic date/time vars from a fixed "now" and includes the platform', () => {
|
|
45
|
-
const now = new Date(2026, 4, 22, 9, 5, 3); // 2026-05-22 09:05:03 (month is 0-based)
|
|
46
|
-
const ctx = buildConditionContext('mobile', { custom: 'x' }, now);
|
|
47
|
-
|
|
48
|
-
expect(ctx.platform).toBe('mobile');
|
|
49
|
-
expect(ctx.current_date).toBe('2026-05-22');
|
|
50
|
-
expect(ctx.current_time).toBe('09:05:03');
|
|
51
|
-
expect(ctx.current_datetime).toBe('2026-05-22 09:05:03');
|
|
52
|
-
expect(ctx.custom).toBe('x');
|
|
53
|
-
});
|
|
54
|
-
});
|
|
1
|
+
/*
|
|
2
|
+
SPDX-FileCopyrightText: 2026 Humdek, University of Bern
|
|
3
|
+
SPDX-License-Identifier: MPL-2.0
|
|
4
|
+
*/
|
|
5
|
+
import { describe, it, expect } from 'vitest';
|
|
6
|
+
import { buildConditionContext, evaluateCondition } from '../evaluate';
|
|
7
|
+
|
|
8
|
+
describe('evaluateCondition', () => {
|
|
9
|
+
const ctx = buildConditionContext('web');
|
|
10
|
+
|
|
11
|
+
it('treats a null/empty/whitespace condition as visible (no condition)', () => {
|
|
12
|
+
expect(evaluateCondition(null, ctx).visible).toBe(true);
|
|
13
|
+
expect(evaluateCondition('', ctx).visible).toBe(true);
|
|
14
|
+
expect(evaluateCondition(' ', ctx).visible).toBe(true);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it('evaluates a true JSON-Logic rule to visible', () => {
|
|
18
|
+
expect(evaluateCondition('{"==":[1,1]}', ctx).visible).toBe(true);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('evaluates a false JSON-Logic rule to not visible', () => {
|
|
22
|
+
expect(evaluateCondition('{"==":[1,2]}', ctx).visible).toBe(false);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('resolves context variables (platform) from the condition context', () => {
|
|
26
|
+
const rule = '{"==":[{"var":"platform"},"web"]}';
|
|
27
|
+
expect(evaluateCondition(rule, buildConditionContext('web')).visible).toBe(true);
|
|
28
|
+
expect(evaluateCondition(rule, buildConditionContext('mobile')).visible).toBe(false);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('treats invalid JSON as not visible (fail closed, never throws)', () => {
|
|
32
|
+
expect(evaluateCondition('{not json', ctx).visible).toBe(false);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('exposes a debug payload with the error when withDebug is set', () => {
|
|
36
|
+
const result = evaluateCondition('{not json', ctx, { withDebug: true });
|
|
37
|
+
expect(result.visible).toBe(false);
|
|
38
|
+
expect(result.debug?.error).toContain('Invalid JSON');
|
|
39
|
+
expect(result.debug?.condition).toBe('{not json');
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
describe('buildConditionContext', () => {
|
|
44
|
+
it('builds deterministic date/time vars from a fixed "now" and includes the platform', () => {
|
|
45
|
+
const now = new Date(2026, 4, 22, 9, 5, 3); // 2026-05-22 09:05:03 (month is 0-based)
|
|
46
|
+
const ctx = buildConditionContext('mobile', { custom: 'x' }, now);
|
|
47
|
+
|
|
48
|
+
expect(ctx.platform).toBe('mobile');
|
|
49
|
+
expect(ctx.current_date).toBe('2026-05-22');
|
|
50
|
+
expect(ctx.current_time).toBe('09:05:03');
|
|
51
|
+
expect(ctx.current_datetime).toBe('2026-05-22 09:05:03');
|
|
52
|
+
expect(ctx.custom).toBe('x');
|
|
53
|
+
});
|
|
54
|
+
});
|
|
@@ -1,121 +1,121 @@
|
|
|
1
|
-
/*
|
|
2
|
-
SPDX-FileCopyrightText: 2026 Humdek, University of Bern
|
|
3
|
-
SPDX-License-Identifier: MPL-2.0
|
|
4
|
-
*/
|
|
5
|
-
/**
|
|
6
|
-
* Client-side mirror of the backend `ConditionService::evaluateCondition`.
|
|
7
|
-
* Uses `json-logic-js` so the same JSON-Logic expressions evaluated on
|
|
8
|
-
* the server are evaluated identically here.
|
|
9
|
-
*
|
|
10
|
-
* Backend system variables (see ConditionService docblock):
|
|
11
|
-
* - language: ISO locale code, e.g. 'de-CH'
|
|
12
|
-
* - current_date / current_datetime / current_time
|
|
13
|
-
* - page_keyword
|
|
14
|
-
* - platform: 'web' | 'mobile'
|
|
15
|
-
*
|
|
16
|
-
* Anything else passed in `extraVars` is merged on top.
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
import jsonLogic from 'json-logic-js';
|
|
20
|
-
|
|
21
|
-
export type TPlatform = 'web' | 'mobile';
|
|
22
|
-
|
|
23
|
-
export interface IConditionContext {
|
|
24
|
-
platform: TPlatform;
|
|
25
|
-
language?: string;
|
|
26
|
-
page_keyword?: string;
|
|
27
|
-
current_date?: string;
|
|
28
|
-
current_datetime?: string;
|
|
29
|
-
current_time?: string;
|
|
30
|
-
[extraKey: string]: unknown;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export interface IEvaluatedCondition {
|
|
34
|
-
/** Whether the condition allowed the section to render. */
|
|
35
|
-
visible: boolean;
|
|
36
|
-
/** Optional debug payload (only set when `withDebug` is true). */
|
|
37
|
-
debug?: {
|
|
38
|
-
condition: string | null;
|
|
39
|
-
result: boolean;
|
|
40
|
-
variables: Record<string, unknown>;
|
|
41
|
-
error?: string;
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Build a default context with current date/time + the platform
|
|
47
|
-
* variable. Pass any extra vars (data-bound section variables, etc).
|
|
48
|
-
*/
|
|
49
|
-
export function buildConditionContext(
|
|
50
|
-
platform: TPlatform,
|
|
51
|
-
extraVars: Record<string, unknown> = {},
|
|
52
|
-
now: Date = new Date()
|
|
53
|
-
): IConditionContext {
|
|
54
|
-
const pad = (n: number): string => String(n).padStart(2, '0');
|
|
55
|
-
const date = `${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())}`;
|
|
56
|
-
const time = `${pad(now.getHours())}:${pad(now.getMinutes())}:${pad(now.getSeconds())}`;
|
|
57
|
-
return {
|
|
58
|
-
platform,
|
|
59
|
-
current_date: date,
|
|
60
|
-
current_time: time,
|
|
61
|
-
current_datetime: `${date} ${time}`,
|
|
62
|
-
...extraVars,
|
|
63
|
-
};
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Evaluate a JSON-Logic condition string. A null/empty/whitespace
|
|
68
|
-
* condition is treated as `visible: true` (no condition).
|
|
69
|
-
*/
|
|
70
|
-
export function evaluateCondition(
|
|
71
|
-
condition: string | null | undefined,
|
|
72
|
-
context: IConditionContext,
|
|
73
|
-
options: { withDebug?: boolean } = {}
|
|
74
|
-
): IEvaluatedCondition {
|
|
75
|
-
const trimmed = (condition ?? '').toString().trim();
|
|
76
|
-
if (!trimmed) {
|
|
77
|
-
return options.withDebug
|
|
78
|
-
? { visible: true, debug: { condition: null, result: true, variables: { ...context } } }
|
|
79
|
-
: { visible: true };
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
let parsed: unknown;
|
|
83
|
-
try {
|
|
84
|
-
parsed = JSON.parse(trimmed);
|
|
85
|
-
} catch (err) {
|
|
86
|
-
return options.withDebug
|
|
87
|
-
? {
|
|
88
|
-
visible: false,
|
|
89
|
-
debug: {
|
|
90
|
-
condition: trimmed,
|
|
91
|
-
result: false,
|
|
92
|
-
variables: { ...context },
|
|
93
|
-
error: `Invalid JSON: ${(err as Error).message}`,
|
|
94
|
-
},
|
|
95
|
-
}
|
|
96
|
-
: { visible: false };
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
try {
|
|
100
|
-
// jsonLogic typings are loose; the result of any boolean rule is
|
|
101
|
-
// a boolean, but rules like {var: 'x'} can return raw values, so
|
|
102
|
-
// we coerce.
|
|
103
|
-
const raw = jsonLogic.apply(parsed as jsonLogic.RulesLogic, context as Record<string, unknown>);
|
|
104
|
-
const visible = Boolean(raw);
|
|
105
|
-
return options.withDebug
|
|
106
|
-
? { visible, debug: { condition: trimmed, result: visible, variables: { ...context } } }
|
|
107
|
-
: { visible };
|
|
108
|
-
} catch (err) {
|
|
109
|
-
return options.withDebug
|
|
110
|
-
? {
|
|
111
|
-
visible: false,
|
|
112
|
-
debug: {
|
|
113
|
-
condition: trimmed,
|
|
114
|
-
result: false,
|
|
115
|
-
variables: { ...context },
|
|
116
|
-
error: (err as Error).message,
|
|
117
|
-
},
|
|
118
|
-
}
|
|
119
|
-
: { visible: false };
|
|
120
|
-
}
|
|
121
|
-
}
|
|
1
|
+
/*
|
|
2
|
+
SPDX-FileCopyrightText: 2026 Humdek, University of Bern
|
|
3
|
+
SPDX-License-Identifier: MPL-2.0
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Client-side mirror of the backend `ConditionService::evaluateCondition`.
|
|
7
|
+
* Uses `json-logic-js` so the same JSON-Logic expressions evaluated on
|
|
8
|
+
* the server are evaluated identically here.
|
|
9
|
+
*
|
|
10
|
+
* Backend system variables (see ConditionService docblock):
|
|
11
|
+
* - language: ISO locale code, e.g. 'de-CH'
|
|
12
|
+
* - current_date / current_datetime / current_time
|
|
13
|
+
* - page_keyword
|
|
14
|
+
* - platform: 'web' | 'mobile'
|
|
15
|
+
*
|
|
16
|
+
* Anything else passed in `extraVars` is merged on top.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import jsonLogic from 'json-logic-js';
|
|
20
|
+
|
|
21
|
+
export type TPlatform = 'web' | 'mobile';
|
|
22
|
+
|
|
23
|
+
export interface IConditionContext {
|
|
24
|
+
platform: TPlatform;
|
|
25
|
+
language?: string;
|
|
26
|
+
page_keyword?: string;
|
|
27
|
+
current_date?: string;
|
|
28
|
+
current_datetime?: string;
|
|
29
|
+
current_time?: string;
|
|
30
|
+
[extraKey: string]: unknown;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface IEvaluatedCondition {
|
|
34
|
+
/** Whether the condition allowed the section to render. */
|
|
35
|
+
visible: boolean;
|
|
36
|
+
/** Optional debug payload (only set when `withDebug` is true). */
|
|
37
|
+
debug?: {
|
|
38
|
+
condition: string | null;
|
|
39
|
+
result: boolean;
|
|
40
|
+
variables: Record<string, unknown>;
|
|
41
|
+
error?: string;
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Build a default context with current date/time + the platform
|
|
47
|
+
* variable. Pass any extra vars (data-bound section variables, etc).
|
|
48
|
+
*/
|
|
49
|
+
export function buildConditionContext(
|
|
50
|
+
platform: TPlatform,
|
|
51
|
+
extraVars: Record<string, unknown> = {},
|
|
52
|
+
now: Date = new Date()
|
|
53
|
+
): IConditionContext {
|
|
54
|
+
const pad = (n: number): string => String(n).padStart(2, '0');
|
|
55
|
+
const date = `${now.getFullYear()}-${pad(now.getMonth() + 1)}-${pad(now.getDate())}`;
|
|
56
|
+
const time = `${pad(now.getHours())}:${pad(now.getMinutes())}:${pad(now.getSeconds())}`;
|
|
57
|
+
return {
|
|
58
|
+
platform,
|
|
59
|
+
current_date: date,
|
|
60
|
+
current_time: time,
|
|
61
|
+
current_datetime: `${date} ${time}`,
|
|
62
|
+
...extraVars,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Evaluate a JSON-Logic condition string. A null/empty/whitespace
|
|
68
|
+
* condition is treated as `visible: true` (no condition).
|
|
69
|
+
*/
|
|
70
|
+
export function evaluateCondition(
|
|
71
|
+
condition: string | null | undefined,
|
|
72
|
+
context: IConditionContext,
|
|
73
|
+
options: { withDebug?: boolean } = {}
|
|
74
|
+
): IEvaluatedCondition {
|
|
75
|
+
const trimmed = (condition ?? '').toString().trim();
|
|
76
|
+
if (!trimmed) {
|
|
77
|
+
return options.withDebug
|
|
78
|
+
? { visible: true, debug: { condition: null, result: true, variables: { ...context } } }
|
|
79
|
+
: { visible: true };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
let parsed: unknown;
|
|
83
|
+
try {
|
|
84
|
+
parsed = JSON.parse(trimmed);
|
|
85
|
+
} catch (err) {
|
|
86
|
+
return options.withDebug
|
|
87
|
+
? {
|
|
88
|
+
visible: false,
|
|
89
|
+
debug: {
|
|
90
|
+
condition: trimmed,
|
|
91
|
+
result: false,
|
|
92
|
+
variables: { ...context },
|
|
93
|
+
error: `Invalid JSON: ${(err as Error).message}`,
|
|
94
|
+
},
|
|
95
|
+
}
|
|
96
|
+
: { visible: false };
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
try {
|
|
100
|
+
// jsonLogic typings are loose; the result of any boolean rule is
|
|
101
|
+
// a boolean, but rules like {var: 'x'} can return raw values, so
|
|
102
|
+
// we coerce.
|
|
103
|
+
const raw = jsonLogic.apply(parsed as jsonLogic.RulesLogic, context as Record<string, unknown>);
|
|
104
|
+
const visible = Boolean(raw);
|
|
105
|
+
return options.withDebug
|
|
106
|
+
? { visible, debug: { condition: trimmed, result: visible, variables: { ...context } } }
|
|
107
|
+
: { visible };
|
|
108
|
+
} catch (err) {
|
|
109
|
+
return options.withDebug
|
|
110
|
+
? {
|
|
111
|
+
visible: false,
|
|
112
|
+
debug: {
|
|
113
|
+
condition: trimmed,
|
|
114
|
+
result: false,
|
|
115
|
+
variables: { ...context },
|
|
116
|
+
error: (err as Error).message,
|
|
117
|
+
},
|
|
118
|
+
}
|
|
119
|
+
: { visible: false };
|
|
120
|
+
}
|
|
121
|
+
}
|
package/src/condition/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
/*
|
|
2
|
-
SPDX-FileCopyrightText: 2026 Humdek, University of Bern
|
|
3
|
-
SPDX-License-Identifier: MPL-2.0
|
|
4
|
-
*/
|
|
5
|
-
export * from './evaluate';
|
|
1
|
+
/*
|
|
2
|
+
SPDX-FileCopyrightText: 2026 Humdek, University of Bern
|
|
3
|
+
SPDX-License-Identifier: MPL-2.0
|
|
4
|
+
*/
|
|
5
|
+
export * from './evaluate';
|