@griddo/ax 12.13.1 → 12.14.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/config/webpack.config.js +1 -1
- package/package.json +4 -4
- package/src/__tests__/components/Fields/Wysiwyg/Wysiwyg.caption.test.tsx +381 -33
- package/src/__tests__/components/Fields/Wysiwyg/Wysiwyg.test.tsx +1 -0
- package/src/__tests__/containers/PageEditor/accessGrants.test.ts +104 -0
- package/src/__tests__/containers/PageEditor/savePageForbidden.test.ts +66 -0
- package/src/__tests__/forms/validators.test.ts +37 -0
- package/src/__tests__/gainup-trace.test.ts +1 -1
- package/src/__tests__/hooks/permissionsForPageKeys.contract.test.ts +50 -0
- package/src/__tests__/hooks/permissionsPageId.test.ts +17 -0
- package/src/__tests__/hooks/users.test.tsx +167 -8
- package/src/components/Fields/Wysiwyg/helpers.tsx +188 -0
- package/src/components/Fields/Wysiwyg/index.tsx +97 -28
- package/src/components/Fields/Wysiwyg/style.tsx +5 -0
- package/src/containers/PageEditor/actions.tsx +70 -49
- package/src/containers/PageEditor/constants.tsx +4 -0
- package/src/forms/errors.tsx +2 -2
- package/src/forms/validators.tsx +14 -12
- package/src/hooks/index.tsx +12 -6
- package/src/hooks/users.tsx +84 -9
- package/src/modules/ActivityLog/DownloadModal/index.tsx +4 -2
- package/src/modules/ActivityLog/index.tsx +6 -5
- package/src/modules/GlobalEditor/index.tsx +2 -1
- package/src/modules/PageEditor/index.tsx +2 -1
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { pages } from "@ax/api";
|
|
2
|
+
import { pageEditorActions } from "@ax/containers/PageEditor";
|
|
3
|
+
import { PAGE_EDIT_FORBIDDEN_MESSAGE } from "@ax/containers/PageEditor/constants";
|
|
4
|
+
|
|
5
|
+
// A save rejected by the page whitelist (403 since sc-119240) shows a permission message, not the
|
|
6
|
+
// raw API error text.
|
|
7
|
+
const getState = (() => ({
|
|
8
|
+
pageEditor: {
|
|
9
|
+
selectedEditorID: 0,
|
|
10
|
+
isNewTranslation: false,
|
|
11
|
+
editorContent: {
|
|
12
|
+
id: 100,
|
|
13
|
+
draftFromPage: null,
|
|
14
|
+
header: null,
|
|
15
|
+
footer: null,
|
|
16
|
+
templateConfig: {},
|
|
17
|
+
template: { templateType: "BasicTemplate" },
|
|
18
|
+
},
|
|
19
|
+
lastTimeout: null,
|
|
20
|
+
},
|
|
21
|
+
sites: { currentSiteInfo: { id: 7 } },
|
|
22
|
+
app: { lang: 1 },
|
|
23
|
+
})) as any;
|
|
24
|
+
|
|
25
|
+
describe("[UC-AX-save-validate-page] savePage rejected by permissions", () => {
|
|
26
|
+
afterEach(() => {
|
|
27
|
+
vi.restoreAllMocks();
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("AC7 · a 403 shows the permission message", async () => {
|
|
31
|
+
vi.spyOn(pages, "updatePage").mockResolvedValue({
|
|
32
|
+
status: 403,
|
|
33
|
+
data: { code: 403, message: "[ForbiddenError] Cannot update page #100" },
|
|
34
|
+
} as any);
|
|
35
|
+
const setToast = vi.fn();
|
|
36
|
+
|
|
37
|
+
const saved = await pageEditorActions.savePage({ setToast })(vi.fn(), getState);
|
|
38
|
+
|
|
39
|
+
expect(saved).toBe(false);
|
|
40
|
+
expect(setToast).toHaveBeenCalledWith(PAGE_EDIT_FORBIDDEN_MESSAGE);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("AC7 · a 403 from a role permission check (no [ForbiddenError]) keeps the API message", async () => {
|
|
44
|
+
vi.spyOn(pages, "updatePage").mockResolvedValue({
|
|
45
|
+
status: 403,
|
|
46
|
+
data: { code: 403, message: "You don't have enough permissions to edit this type of page." },
|
|
47
|
+
} as any);
|
|
48
|
+
const setToast = vi.fn();
|
|
49
|
+
|
|
50
|
+
await pageEditorActions.savePage({ setToast })(vi.fn(), getState);
|
|
51
|
+
|
|
52
|
+
expect(setToast).toHaveBeenCalledWith("You don't have enough permissions to edit this type of page.");
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("AC7 · any other error keeps the API message", async () => {
|
|
56
|
+
vi.spyOn(pages, "updatePage").mockResolvedValue({
|
|
57
|
+
status: 400,
|
|
58
|
+
data: { code: 400, message: "Slug already exists" },
|
|
59
|
+
} as any);
|
|
60
|
+
const setToast = vi.fn();
|
|
61
|
+
|
|
62
|
+
await pageEditorActions.savePage({ setToast })(vi.fn(), getState);
|
|
63
|
+
|
|
64
|
+
expect(setToast).toHaveBeenCalledWith("Slug already exists");
|
|
65
|
+
});
|
|
66
|
+
});
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { getValidity } from "@ax/forms";
|
|
2
|
+
|
|
3
|
+
describe("getValidity", () => {
|
|
4
|
+
describe("minValue", () => {
|
|
5
|
+
it("should accept the minimum itself", () => {
|
|
6
|
+
expect(getValidity({ minValue: 5 }, "5")).toEqual({ isValid: true, errorText: "" });
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
it("should ask for at least the minimum when the value is below it", () => {
|
|
10
|
+
expect(getValidity({ minValue: 5 }, "3")).toEqual({
|
|
11
|
+
isValid: false,
|
|
12
|
+
errorText: "Please, enter a value of at least 5",
|
|
13
|
+
});
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it("should show a minimum of 0 in the message", () => {
|
|
17
|
+
expect(getValidity({ minValue: 0 }, "-1").errorText).toBe("Please, enter a value of at least 0");
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
describe("maxValue", () => {
|
|
22
|
+
it("should accept the maximum itself", () => {
|
|
23
|
+
expect(getValidity({ maxValue: 10 }, "10")).toEqual({ isValid: true, errorText: "" });
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("should ask for at most the maximum when the value is above it", () => {
|
|
27
|
+
expect(getValidity({ maxValue: 10 }, "12")).toEqual({
|
|
28
|
+
isValid: false,
|
|
29
|
+
errorText: "Please, enter a value of at most 10",
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("should show a maximum of 0 in the message", () => {
|
|
34
|
+
expect(getValidity({ maxValue: 0 }, "1").errorText).toBe("Please, enter a value of at most 0");
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
});
|
|
@@ -17,7 +17,7 @@ const TRACE = join(homedir(), ".claude", "gainup", "scripts", "gainup-trace.mjs"
|
|
|
17
17
|
|
|
18
18
|
// GAINUP se instala como plugin de Claude Code, y su ruta de instalación es versionada
|
|
19
19
|
// (…/plugins/cache/<marketplace>/gainup/<versión>/): el alias de arriba lo mantiene el hook
|
|
20
|
-
// SessionStart del plugin, porque este arnés corre desde `
|
|
20
|
+
// SessionStart del plugin, porque este arnés corre desde `pnpm` y no tiene ${CLAUDE_PLUGIN_ROOT}.
|
|
21
21
|
//
|
|
22
22
|
// «Sin instalar» es un estado legítimo y el guardián se salta. «Instalado pero sin resolver»
|
|
23
23
|
// no lo es: sería un verde por no haber validado nada — el verde falso contra el que está
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { NON_PROTECTED_PERMISSION_KEYS, PROTECTED_PERMISSION_KEYS } from "@ax/hooks";
|
|
2
|
+
|
|
3
|
+
import { readdirSync, readFileSync, statSync } from "node:fs";
|
|
4
|
+
import { join, relative } from "node:path";
|
|
5
|
+
|
|
6
|
+
// Contract between `usePermissionsForPage` and its callers (sc-119240): the page whitelist blocks the
|
|
7
|
+
// keys listed in PROTECTED_PERMISSION_KEYS by NAME. A caller that adds a key nobody classified would
|
|
8
|
+
// silently escape the whitelist (or be blocked by mistake), so every key must be classified, and every
|
|
9
|
+
// protected key must still be used by someone (a stale list is a mirror of the API that drifted).
|
|
10
|
+
const SRC = join(__dirname, "..", "..");
|
|
11
|
+
// The keys object is the first `{` after the call: site and page id arguments contain no braces.
|
|
12
|
+
const HOOK_CALL = /usePermissionsForPage\([^{]*?\{([\s\S]*?)\}/g;
|
|
13
|
+
const KEY = /^\s*(\w+)\s*:/gm;
|
|
14
|
+
|
|
15
|
+
const sourceFiles = (dir: string): string[] =>
|
|
16
|
+
readdirSync(dir).flatMap((name) => {
|
|
17
|
+
const path = join(dir, name);
|
|
18
|
+
|
|
19
|
+
if (name === "__tests__" || name === "node_modules") return [];
|
|
20
|
+
|
|
21
|
+
return statSync(path).isDirectory() ? sourceFiles(path) : /\.tsx?$/.test(name) ? [path] : [];
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
const keysByCaller = sourceFiles(SRC).flatMap((file) =>
|
|
25
|
+
[...readFileSync(file, "utf8").matchAll(HOOK_CALL)].map((call) => ({
|
|
26
|
+
caller: relative(SRC, file),
|
|
27
|
+
keys: [...(call[1] ?? "").matchAll(KEY)].map((match) => match[1] as string),
|
|
28
|
+
})),
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
describe("[UC-AX-restrict-page-access] usePermissionsForPage permission keys contract", () => {
|
|
32
|
+
it("finds the hook callers", () => {
|
|
33
|
+
expect(keysByCaller.length).toBeGreaterThanOrEqual(6);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("AC14 · every key used by a caller is classified as protected or not protected", () => {
|
|
37
|
+
const classified = new Set([...PROTECTED_PERMISSION_KEYS, ...NON_PROTECTED_PERMISSION_KEYS]);
|
|
38
|
+
const unclassified = keysByCaller.flatMap(({ caller, keys }) =>
|
|
39
|
+
keys.filter((key) => !classified.has(key)).map((key) => `${caller}: ${key}`),
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
expect(unclassified).toEqual([]);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("AC14 · every protected key is still used by some caller", () => {
|
|
46
|
+
const used = new Set(keysByCaller.flatMap(({ keys }) => keys));
|
|
47
|
+
|
|
48
|
+
expect(PROTECTED_PERMISSION_KEYS.filter((key: string) => !used.has(key))).toEqual([]);
|
|
49
|
+
});
|
|
50
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { permissionsPageId } from "@ax/hooks";
|
|
2
|
+
|
|
3
|
+
// Page permissions (profile page list, whitelist) belong to the page, not to the version being edited:
|
|
4
|
+
// a draft is checked as its original page (sc-119240, rescue of 11dbbcd2bf).
|
|
5
|
+
describe("[UC-AX-restrict-page-access] permissionsPageId", () => {
|
|
6
|
+
it("AC15 · a draft is checked as its original page", () => {
|
|
7
|
+
expect(permissionsPageId({ draftFromPage: 100 }, 213)).toBe(100);
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it.each([
|
|
11
|
+
["not a draft", { draftFromPage: null }],
|
|
12
|
+
["content without draftFromPage", {}],
|
|
13
|
+
["no content loaded yet", undefined],
|
|
14
|
+
])("AC15 · %s is checked as itself", (_label, editorContent) => {
|
|
15
|
+
expect(permissionsPageId(editorContent, 213)).toBe(213);
|
|
16
|
+
});
|
|
17
|
+
});
|
|
@@ -163,7 +163,7 @@ describe("usePermissionsForPage hook", () => {
|
|
|
163
163
|
});
|
|
164
164
|
});
|
|
165
165
|
|
|
166
|
-
describe("5. Access grants", () => {
|
|
166
|
+
describe("[UC-AX-restrict-page-access] 5. Access grants", () => {
|
|
167
167
|
it("should allow access when no accessGrants are defined", () => {
|
|
168
168
|
setupMockState({ users: { currentUser: mockUser, roles: [mockRole] } });
|
|
169
169
|
|
|
@@ -172,7 +172,7 @@ describe("usePermissionsForPage hook", () => {
|
|
|
172
172
|
expect(result.current.edit).toBe(true);
|
|
173
173
|
});
|
|
174
174
|
|
|
175
|
-
it("should allow access when accessGrants is empty", () => {
|
|
175
|
+
it("AC12 · should allow access when accessGrants is empty", () => {
|
|
176
176
|
const emptyAccessGrants: PageAccessGrantsSummary = { users: [], roles: [] };
|
|
177
177
|
setupMockState({ users: { currentUser: mockUser, roles: [mockRole] } });
|
|
178
178
|
|
|
@@ -212,7 +212,7 @@ describe("usePermissionsForPage hook", () => {
|
|
|
212
212
|
expect(result.current.edit).toBe(false);
|
|
213
213
|
});
|
|
214
214
|
|
|
215
|
-
it("should
|
|
215
|
+
it("AC10 · should deny editing when the page is in pagePermissions but the whitelist excludes the user (sc-119240)", () => {
|
|
216
216
|
const userWithPagePerms = {
|
|
217
217
|
...mockUser,
|
|
218
218
|
pagePermissions: [{ siteId: 1, pageIds: [1] }],
|
|
@@ -220,18 +220,18 @@ describe("usePermissionsForPage hook", () => {
|
|
|
220
220
|
const accessGrants: PageAccessGrantsSummary = { users: [2, 3], roles: [2, 3] };
|
|
221
221
|
setupMockState({ users: { currentUser: userWithPagePerms, roles: [mockRole] } });
|
|
222
222
|
|
|
223
|
-
const { result } = renderHook(() => usePermissionsForPage(1, 1, {
|
|
223
|
+
const { result } = renderHook(() => usePermissionsForPage(1, 1, { editContentPages: "edit_page" }, accessGrants));
|
|
224
224
|
|
|
225
|
-
expect(result.current.
|
|
225
|
+
expect(result.current.editContentPages).toBe(false);
|
|
226
226
|
});
|
|
227
227
|
|
|
228
|
-
it("should
|
|
228
|
+
it("AC10 · should deny editing when user has no pagePermissions and is not in the whitelist (sc-119240)", () => {
|
|
229
229
|
const accessGrants: PageAccessGrantsSummary = { users: [2, 3], roles: [2, 3] };
|
|
230
230
|
setupMockState({ users: { currentUser: mockUser, roles: [mockRole] } });
|
|
231
231
|
|
|
232
|
-
const { result } = renderHook(() => usePermissionsForPage(1, 1, {
|
|
232
|
+
const { result } = renderHook(() => usePermissionsForPage(1, 1, { editContentPages: "edit_page" }, accessGrants));
|
|
233
233
|
|
|
234
|
-
expect(result.current.
|
|
234
|
+
expect(result.current.editContentPages).toBe(false);
|
|
235
235
|
});
|
|
236
236
|
|
|
237
237
|
it("should allow access when user is restricted by pagePermissions but in accessGrants for the page", () => {
|
|
@@ -423,4 +423,163 @@ describe("usePermissionsForPage hook", () => {
|
|
|
423
423
|
expect(result.current.edit).toBe(false);
|
|
424
424
|
});
|
|
425
425
|
});
|
|
426
|
+
|
|
427
|
+
describe("[UC-AX-restrict-page-access] 9. Page whitelist prevails on protected keys (sc-119240)", () => {
|
|
428
|
+
const EDIT = "content.editContentPages";
|
|
429
|
+
const PUBLISH = "content.publishUnpublishPages";
|
|
430
|
+
const DELETE = "content.deletePages";
|
|
431
|
+
const CREATE = "content.createPages";
|
|
432
|
+
const EXEMPTION = "general.setPermissionPerPage";
|
|
433
|
+
const GLOBAL_EXEMPTION = "global.globalData.setPermissionPerPage";
|
|
434
|
+
const GLOBAL_EDIT = "global.globalData.editAllGlobalData";
|
|
435
|
+
|
|
436
|
+
const role = (id: number, sitePermissions: string[], globalPermissions: string[] = []) => ({
|
|
437
|
+
id,
|
|
438
|
+
name: `role ${id}`,
|
|
439
|
+
permissions: {
|
|
440
|
+
sitePermissions: sitePermissions.map((key) => ({ key })),
|
|
441
|
+
globalPermissions: globalPermissions.map((key) => ({ key })),
|
|
442
|
+
},
|
|
443
|
+
});
|
|
444
|
+
const EDITOR = role(10, [EDIT, PUBLISH, DELETE, CREATE], [GLOBAL_EDIT]);
|
|
445
|
+
const REVIEWER = role(11, [EDIT]);
|
|
446
|
+
const ADMIN = role(12, [EDIT, PUBLISH, DELETE, CREATE, EXEMPTION], [GLOBAL_EDIT, GLOBAL_EXEMPTION]);
|
|
447
|
+
const ROLES = [EDITOR, REVIEWER, ADMIN];
|
|
448
|
+
const PAGE_KEYS = { editContentPages: EDIT, publishPages: PUBLISH, deletePages: DELETE, createPages: CREATE };
|
|
449
|
+
const ONLY_USER_7: PageAccessGrantsSummary = { users: [7], roles: [] };
|
|
450
|
+
|
|
451
|
+
const user = (id: number, roles: { siteId: number | "all" | "global"; roles: number[] }[], extra = {}) => ({
|
|
452
|
+
id,
|
|
453
|
+
email: `u${id}@test.com`,
|
|
454
|
+
isSuperAdmin: false,
|
|
455
|
+
roles,
|
|
456
|
+
pagePermissions: undefined,
|
|
457
|
+
...extra,
|
|
458
|
+
});
|
|
459
|
+
|
|
460
|
+
const permissionsOn = (
|
|
461
|
+
currentUser: unknown,
|
|
462
|
+
accessGrants: PageAccessGrantsSummary | undefined,
|
|
463
|
+
siteId: number | "global" = 1,
|
|
464
|
+
keys: Record<string, string> = PAGE_KEYS,
|
|
465
|
+
) => {
|
|
466
|
+
setupMockState({ users: { currentUser, roles: ROLES } });
|
|
467
|
+
|
|
468
|
+
return renderHook(() => usePermissionsForPage(siteId, 1, keys, accessGrants)).result.current;
|
|
469
|
+
};
|
|
470
|
+
|
|
471
|
+
it("AC10 · a site editor out of the whitelist cannot edit, publish nor delete, but can still create", () => {
|
|
472
|
+
const result = permissionsOn(user(9, [{ siteId: 1, roles: [10] }]), ONLY_USER_7);
|
|
473
|
+
|
|
474
|
+
expect(result).toEqual({ editContentPages: false, publishPages: false, deletePages: false, createPages: true });
|
|
475
|
+
});
|
|
476
|
+
|
|
477
|
+
it("AC11 · the user of the whitelist edits", () => {
|
|
478
|
+
expect(permissionsOn(user(7, [{ siteId: 1, roles: [10] }]), ONLY_USER_7).editContentPages).toBe(true);
|
|
479
|
+
});
|
|
480
|
+
|
|
481
|
+
it("AC11 · whitelist ids sent as text still match", () => {
|
|
482
|
+
const asText = { users: ["7"], roles: [] } as unknown as PageAccessGrantsSummary;
|
|
483
|
+
|
|
484
|
+
expect(permissionsOn(user(7, [{ siteId: 1, roles: [10] }]), asText).editContentPages).toBe(true);
|
|
485
|
+
});
|
|
486
|
+
|
|
487
|
+
it("AC11 · a role of the whitelist in the page's site edits", () => {
|
|
488
|
+
const result = permissionsOn(user(9, [{ siteId: 1, roles: [10, 11] }]), { users: [], roles: [11] });
|
|
489
|
+
|
|
490
|
+
expect(result.editContentPages).toBe(true);
|
|
491
|
+
});
|
|
492
|
+
|
|
493
|
+
it('AC11 · a role of the whitelist given in "all" counts', () => {
|
|
494
|
+
const result = permissionsOn(
|
|
495
|
+
user(9, [
|
|
496
|
+
{ siteId: 1, roles: [10] },
|
|
497
|
+
{ siteId: "all", roles: [11] },
|
|
498
|
+
]),
|
|
499
|
+
{ users: [], roles: [11] },
|
|
500
|
+
);
|
|
501
|
+
|
|
502
|
+
expect(result.editContentPages).toBe(true);
|
|
503
|
+
});
|
|
504
|
+
|
|
505
|
+
it("AC11 · a role of the whitelist in another site does not count", () => {
|
|
506
|
+
const result = permissionsOn(
|
|
507
|
+
user(9, [
|
|
508
|
+
{ siteId: 1, roles: [10] },
|
|
509
|
+
{ siteId: 2, roles: [11] },
|
|
510
|
+
]),
|
|
511
|
+
{ users: [], roles: [11] },
|
|
512
|
+
);
|
|
513
|
+
|
|
514
|
+
expect(result.editContentPages).toBe(false);
|
|
515
|
+
});
|
|
516
|
+
|
|
517
|
+
it("AC12 · an empty whitelist restricts nobody", () => {
|
|
518
|
+
const result = permissionsOn(user(9, [{ siteId: 1, roles: [10] }]), { users: [], roles: [] });
|
|
519
|
+
|
|
520
|
+
expect(result.editContentPages).toBe(true);
|
|
521
|
+
});
|
|
522
|
+
|
|
523
|
+
it('AC11 · roles of the site and of "all" are joined for the permissions too', () => {
|
|
524
|
+
const result = permissionsOn(
|
|
525
|
+
user(9, [
|
|
526
|
+
{ siteId: 1, roles: [11] },
|
|
527
|
+
{ siteId: "all", roles: [10] },
|
|
528
|
+
]),
|
|
529
|
+
undefined,
|
|
530
|
+
);
|
|
531
|
+
|
|
532
|
+
expect(result.deletePages).toBe(true);
|
|
533
|
+
});
|
|
534
|
+
|
|
535
|
+
describe("exemption by setPermissionPerPage (Administrator)", () => {
|
|
536
|
+
it("AC13 · an exempt user in the page's site edits a page whose whitelist excludes them", () => {
|
|
537
|
+
expect(permissionsOn(user(9, [{ siteId: 1, roles: [12] }]), ONLY_USER_7).editContentPages).toBe(true);
|
|
538
|
+
});
|
|
539
|
+
|
|
540
|
+
it('AC13 · the exemption given in "all" counts on a site page', () => {
|
|
541
|
+
expect(permissionsOn(user(9, [{ siteId: "all", roles: [12] }]), ONLY_USER_7).editContentPages).toBe(true);
|
|
542
|
+
});
|
|
543
|
+
|
|
544
|
+
it("AC13 · the exemption only counts in its own site", () => {
|
|
545
|
+
const result = permissionsOn(
|
|
546
|
+
user(9, [
|
|
547
|
+
{ siteId: 1, roles: [10] },
|
|
548
|
+
{ siteId: 2, roles: [12] },
|
|
549
|
+
]),
|
|
550
|
+
ONLY_USER_7,
|
|
551
|
+
);
|
|
552
|
+
|
|
553
|
+
expect(result.editContentPages).toBe(false);
|
|
554
|
+
});
|
|
555
|
+
|
|
556
|
+
it("AC13 · an exempt user with other pages in their profile is still limited to them", () => {
|
|
557
|
+
const result = permissionsOn(
|
|
558
|
+
user(9, [{ siteId: 1, roles: [12] }], { pagePermissions: [{ siteId: 1, pageIds: [2] }] }),
|
|
559
|
+
ONLY_USER_7,
|
|
560
|
+
);
|
|
561
|
+
|
|
562
|
+
expect(result.editContentPages).toBe(false);
|
|
563
|
+
});
|
|
564
|
+
|
|
565
|
+
it("AC13 · on a global page only the global-scope exemption counts", () => {
|
|
566
|
+
const keys = { editContentPages: GLOBAL_EDIT };
|
|
567
|
+
|
|
568
|
+
expect(permissionsOn(user(9, [{ siteId: "global", roles: [12] }]), ONLY_USER_7, "global", keys)).toEqual({
|
|
569
|
+
editContentPages: true,
|
|
570
|
+
});
|
|
571
|
+
expect(
|
|
572
|
+
permissionsOn(
|
|
573
|
+
user(9, [
|
|
574
|
+
{ siteId: "global", roles: [10] },
|
|
575
|
+
{ siteId: "all", roles: [12] },
|
|
576
|
+
]),
|
|
577
|
+
ONLY_USER_7,
|
|
578
|
+
"global",
|
|
579
|
+
keys,
|
|
580
|
+
),
|
|
581
|
+
).toEqual({ editContentPages: false });
|
|
582
|
+
});
|
|
583
|
+
});
|
|
584
|
+
});
|
|
426
585
|
});
|
|
@@ -152,13 +152,201 @@ const getImagePercentWidth = (editor: any, $img: any) => {
|
|
|
152
152
|
return percent > 0 && percent < 100 ? `${percent}%` : "100%";
|
|
153
153
|
};
|
|
154
154
|
|
|
155
|
+
// Nothing but blank text, Froala's invisible spaces and its selection markers.
|
|
156
|
+
const isBlank = (fragment: DocumentFragment) => {
|
|
157
|
+
for (const marker of fragment.querySelectorAll(".fr-marker")) marker.remove();
|
|
158
|
+
return !fragment.textContent?.replace(/\u200B/g, "").trim() && !fragment.querySelector("img, iframe, video");
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
// Moves what `paragraph` holds before `first` and after `last` out to
|
|
162
|
+
// paragraphs of their own, around `anchor`, unless it is blank.
|
|
163
|
+
const splitAround = (paragraph: HTMLElement, first: Node, last: Node, anchor: Element) => {
|
|
164
|
+
const before = document.createRange();
|
|
165
|
+
before.setStart(paragraph, 0);
|
|
166
|
+
before.setEndBefore(first);
|
|
167
|
+
const after = document.createRange();
|
|
168
|
+
after.setStartAfter(last);
|
|
169
|
+
after.setEnd(paragraph, paragraph.childNodes.length);
|
|
170
|
+
for (const [range, side] of [
|
|
171
|
+
[before, "before"],
|
|
172
|
+
[after, "after"],
|
|
173
|
+
] as const) {
|
|
174
|
+
if (isBlank(range.cloneContents())) continue;
|
|
175
|
+
const part = paragraph.cloneNode(false) as HTMLElement;
|
|
176
|
+
part.append(range.extractContents());
|
|
177
|
+
anchor[side](part);
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
// The outermost element around `img`, inside `paragraph`, with nothing but
|
|
182
|
+
// blank on that `side` of it: splitting the paragraph there leaves no empty
|
|
183
|
+
// copy of a link or a bold on that side.
|
|
184
|
+
const edgeAround = (img: Element, paragraph: Element, side: "before" | "after") => {
|
|
185
|
+
let edge = img;
|
|
186
|
+
while (edge.parentElement && edge.parentElement !== paragraph) {
|
|
187
|
+
const rest = document.createRange();
|
|
188
|
+
rest.selectNodeContents(edge.parentElement);
|
|
189
|
+
if (side === "before") rest.setEndBefore(edge);
|
|
190
|
+
else rest.setStartAfter(edge);
|
|
191
|
+
if (!isBlank(rest.cloneContents())) break;
|
|
192
|
+
edge = edge.parentElement;
|
|
193
|
+
}
|
|
194
|
+
return edge;
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
// Froala captions an image by wrapping the whole paragraph it is in, so any
|
|
198
|
+
// text sharing it ends up inside the caption, where it can't be edited, and
|
|
199
|
+
// goes away with the caption. Leave the image alone in its paragraph first,
|
|
200
|
+
// out of the bold or the link it may be in too: their text stays around it,
|
|
201
|
+
// still bold or linked. A linked image leaves the paragraph, in its link alone:
|
|
202
|
+
// Froala wraps just the link, and the wrapper div it adds can't sit inside a <p>.
|
|
203
|
+
//
|
|
204
|
+
// Removing a caption leaves the image in that wrapper div, out of any
|
|
205
|
+
// paragraph, and captioning it there nests a second wrapper that Froala then
|
|
206
|
+
// fails to unwrap: it throws, and unwraps another image's. Put the image back
|
|
207
|
+
// in a paragraph before anything else.
|
|
208
|
+
const isolateImage = (img: HTMLElement) => {
|
|
209
|
+
const unit = img.parentElement?.tagName === "A" ? img.parentElement : img;
|
|
210
|
+
const leftoverWrap = unit.parentElement;
|
|
211
|
+
if (leftoverWrap?.classList.contains("fr-img-space-wrap")) {
|
|
212
|
+
const p = document.createElement("p");
|
|
213
|
+
p.append(...leftoverWrap.childNodes);
|
|
214
|
+
leftoverWrap.replaceWith(p);
|
|
215
|
+
}
|
|
216
|
+
const paragraph = img.closest("p");
|
|
217
|
+
if (!paragraph) return;
|
|
218
|
+
splitAround(paragraph, edgeAround(img, paragraph, "before"), edgeAround(img, paragraph, "after"), paragraph);
|
|
219
|
+
const link = img.closest("a");
|
|
220
|
+
if (link && paragraph.contains(link)) {
|
|
221
|
+
link.replaceChildren(img);
|
|
222
|
+
paragraph.replaceWith(link);
|
|
223
|
+
} else {
|
|
224
|
+
paragraph.replaceChildren(img);
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
// Froala keeps the paragraph it wrapped inside the caption, so the caption's
|
|
229
|
+
// text sits in a <p> inside a <span>: it takes the paragraph style of the page,
|
|
230
|
+
// Froala's caption style misses it, and resizing measures the image against the
|
|
231
|
+
// caption itself. Leave the image and its caption text straight in the wrapper.
|
|
232
|
+
const dropCaptionParagraph = (editor: any, img: HTMLElement) => {
|
|
233
|
+
const paragraph = img.parentElement;
|
|
234
|
+
if (!paragraph?.matches(".fr-img-wrap > p")) return;
|
|
235
|
+
// Moving the caption's text drops the selection Froala leaves on it, to type over
|
|
236
|
+
editor.selection.save();
|
|
237
|
+
paragraph.replaceWith(...paragraph.childNodes);
|
|
238
|
+
editor.selection.restore();
|
|
239
|
+
};
|
|
240
|
+
|
|
241
|
+
// The div Froala keeps a captioned image in, and leaves it in once the caption
|
|
242
|
+
// goes. It should hold nothing but the image.
|
|
243
|
+
const imageWrapOf = (img: Element) => img.closest(".fr-img-space-wrap") || img.closest(".fr-img-caption");
|
|
244
|
+
|
|
245
|
+
// Captions put on before isolateImage still hold the text of the paragraph
|
|
246
|
+
// they wrapped, and Froala deletes it along with the caption: take it out to
|
|
247
|
+
// paragraphs around the image first.
|
|
248
|
+
const takeTextOutOfCaption = (img: HTMLElement) => {
|
|
249
|
+
const paragraph = img.parentElement;
|
|
250
|
+
const wrap = imageWrapOf(img);
|
|
251
|
+
if (!paragraph?.matches(".fr-img-wrap > p") || !wrap) return;
|
|
252
|
+
splitAround(paragraph, img, paragraph.querySelector(":scope > .fr-inner") || img, wrap);
|
|
253
|
+
};
|
|
254
|
+
|
|
255
|
+
// Removing an image leaves its wrapper div empty, with the caret in it, so what
|
|
256
|
+
// is typed next would not be in a paragraph: make the div one. Froala hands AX
|
|
257
|
+
// the content only when it saves an undo step, and it saved the removal with
|
|
258
|
+
// the div still there: save the paragraph as well.
|
|
259
|
+
const removeImage = (editor: any) => {
|
|
260
|
+
const wrap = imageWrapOf(editor.image.get().get(0));
|
|
261
|
+
editor.image.remove();
|
|
262
|
+
if (!wrap?.isConnected || wrap.querySelector("img")) return;
|
|
263
|
+
const p = document.createElement("p");
|
|
264
|
+
p.append(...wrap.childNodes);
|
|
265
|
+
wrap.replaceWith(p);
|
|
266
|
+
editor.selection.setAtStart(p);
|
|
267
|
+
editor.selection.restore();
|
|
268
|
+
editor.undo.saveStep();
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
// Leaving a selected image in a wrapper div with the arrows or Esc puts the
|
|
272
|
+
// caret inside the div, after the caption, and what is typed there is not in a
|
|
273
|
+
// paragraph: take the caret to the block next to the image, or to a new one.
|
|
274
|
+
const leaveWrappedImage = (editor: any, wrap: Element, key: string) => {
|
|
275
|
+
if (!["ArrowLeft", "ArrowUp", "ArrowRight", "ArrowDown", "Escape"].includes(key)) return false;
|
|
276
|
+
const forward = key !== "ArrowLeft" && key !== "ArrowUp";
|
|
277
|
+
let block = forward ? wrap.nextElementSibling : wrap.previousElementSibling;
|
|
278
|
+
if (!block?.matches("p, h1, h2, h3, h4, h5, h6, ul, ol, blockquote, pre, table")) {
|
|
279
|
+
block = document.createElement("p");
|
|
280
|
+
block.innerHTML = "<br>";
|
|
281
|
+
wrap[forward ? "after" : "before"](block);
|
|
282
|
+
}
|
|
283
|
+
editor.image.exitEdit(true);
|
|
284
|
+
if (forward) editor.selection.setAtStart(block);
|
|
285
|
+
else editor.selection.setAtEnd(block);
|
|
286
|
+
editor.selection.restore();
|
|
287
|
+
// A new paragraph has to reach AX too (see removeImage). With none, nothing is saved.
|
|
288
|
+
editor.undo.saveStep();
|
|
289
|
+
return true;
|
|
290
|
+
};
|
|
291
|
+
|
|
292
|
+
// Backspace at the start of the block after an image in a wrapper div merges
|
|
293
|
+
// that block into the div, and Delete at the end of the block before merges
|
|
294
|
+
// the caption out into that block. Select the image instead, dropping the
|
|
295
|
+
// block if it is empty: a second press removes the image.
|
|
296
|
+
const selectNeighbourImage = (editor: any, key: string) => {
|
|
297
|
+
const selection = editor.selection.get();
|
|
298
|
+
if ((key !== "Backspace" && key !== "Delete") || !selection.rangeCount || !selection.isCollapsed) return false;
|
|
299
|
+
const forward = key === "Delete";
|
|
300
|
+
const block: Element | undefined = editor.selection.blocks()[0];
|
|
301
|
+
const neighbour = forward ? block?.nextElementSibling : block?.previousElementSibling;
|
|
302
|
+
const img = neighbour?.matches(".fr-img-space-wrap") ? neighbour.querySelector("img") : null;
|
|
303
|
+
if (!block || !img) return false;
|
|
304
|
+
const toEdge = selection.getRangeAt(0).cloneRange();
|
|
305
|
+
if (forward) toEdge.setEnd(block, block.childNodes.length);
|
|
306
|
+
else toEdge.setStart(block, 0);
|
|
307
|
+
if (!isBlank(toEdge.cloneContents())) return false;
|
|
308
|
+
toEdge.selectNodeContents(block);
|
|
309
|
+
// Dropping the block has to reach AX too: see removeImage
|
|
310
|
+
if (isBlank(toEdge.cloneContents())) {
|
|
311
|
+
block.remove();
|
|
312
|
+
editor.undo.saveStep();
|
|
313
|
+
}
|
|
314
|
+
editor.image.edit(editor.$(img));
|
|
315
|
+
return true;
|
|
316
|
+
};
|
|
317
|
+
|
|
318
|
+
// Keeps text out of the div Froala wraps a captioned image in, which the keys
|
|
319
|
+
// around it move text into and the caption out of.
|
|
320
|
+
const handleImageWrapKeys = (editor: any, e: KeyboardEvent) => {
|
|
321
|
+
if (e.isComposing) return;
|
|
322
|
+
const img = editor.image.get()?.get(0);
|
|
323
|
+
const wrap = img && imageWrapOf(img);
|
|
324
|
+
if (!img) {
|
|
325
|
+
if (!selectNeighbourImage(editor, e.key)) return;
|
|
326
|
+
} else if (!wrap) {
|
|
327
|
+
return;
|
|
328
|
+
} else if (e.key === "Backspace" || e.key === "Delete") {
|
|
329
|
+
removeImage(editor);
|
|
330
|
+
} else if (!leaveWrappedImage(editor, wrap, e.key)) {
|
|
331
|
+
return;
|
|
332
|
+
}
|
|
333
|
+
e.preventDefault();
|
|
334
|
+
e.stopPropagation();
|
|
335
|
+
return false;
|
|
336
|
+
};
|
|
337
|
+
|
|
155
338
|
export {
|
|
156
339
|
createPopupFunctions,
|
|
340
|
+
dropCaptionParagraph,
|
|
157
341
|
getImagePercentWidth,
|
|
158
342
|
getLanguageMenuHtml,
|
|
159
343
|
getPopupPosition,
|
|
160
344
|
getRichTextConfig,
|
|
345
|
+
handleImageWrapKeys,
|
|
161
346
|
type IRichTextConfig,
|
|
347
|
+
isolateImage,
|
|
162
348
|
parseClassNames,
|
|
349
|
+
removeImage,
|
|
163
350
|
setLinkAttributes,
|
|
351
|
+
takeTextOutOfCaption,
|
|
164
352
|
};
|