Skip to content

Commit 66e4f4d

Browse files
fix(a11y): preserve read-only editor semantics and caret navigation (#3223)
Expose document semantics in read-only editors and keep caret navigation out of non-editable checkbox controls.
1 parent 99aff4b commit 66e4f4d

8 files changed

Lines changed: 152 additions & 19 deletions

File tree

‎packages/core/src/blocks/Heading/Heading.browser.test.tsx‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ describe("Heading", () => {
434434
initialContent: toggleHeadingDocument,
435435
props: { editable: false },
436436
});
437-
const first = page.getByRole("textbox").nth(0);
437+
const first = page.getByRole("document").nth(0);
438438
const toggle = first.getByRole("button", {
439439
name: editor.dictionary.toggle_blocks.toggle_button,
440440
exact: true,
@@ -470,7 +470,7 @@ describe("Heading", () => {
470470
props: { editable: false },
471471
});
472472
expect(recreated).not.toBe(editor);
473-
const restored = page.getByRole("textbox").nth(1);
473+
const restored = page.getByRole("document").nth(1);
474474
await expect
475475
.element(
476476
restored.getByRole("button", {
@@ -719,22 +719,22 @@ describe("Heading", () => {
719719
name: blockNoteEditor.dictionary.toggle_blocks.toggle_button,
720720
exact: true,
721721
});
722-
const editor = page.getByRole("textbox");
722+
const editor = page.getByRole("document");
723723
await expect.element(editor).toMatchAriaInlineSnapshot(`
724-
- textbox:
724+
- document:
725725
- button "Expand or collapse"
726726
- heading "Details" [level=2]
727727
`);
728728
await toggleButton.click();
729729
await expect.element(editor).toMatchAriaInlineSnapshot(`
730-
- textbox:
730+
- document:
731731
- button "Expand or collapse" [expanded]
732732
- heading "Details" [level=2]
733733
- paragraph: Nested read-only content
734734
`);
735735
await toggleButton.click();
736736
await expect.element(editor).toMatchAriaInlineSnapshot(`
737-
- textbox:
737+
- document:
738738
- button "Expand or collapse"
739739
- heading "Details" [level=2]
740740
`);
@@ -825,9 +825,9 @@ describe("Heading", () => {
825825
initialContent: toggleHeadingDocument,
826826
props: { editable: false, theme: "light" },
827827
});
828-
const textbox = page.getByRole("textbox");
829-
await expect.element(textbox).toBeVisible();
830-
expect((await runAxe(textbox)).violations).toEqual([]);
828+
const readOnlyDocument = page.getByRole("document");
829+
await expect.element(readOnlyDocument).toBeVisible();
830+
expect((await runAxe(readOnlyDocument)).violations).toEqual([]);
831831

832832
await page
833833
.getByRole("button", {
@@ -838,7 +838,7 @@ describe("Heading", () => {
838838
await expect
839839
.element(page.getByText("Nested read-only content", { exact: true }))
840840
.toBeVisible();
841-
expect((await runAxe(textbox)).violations).toEqual([]);
841+
expect((await runAxe(readOnlyDocument)).violations).toEqual([]);
842842
});
843843
});
844844

‎packages/core/src/blocks/ListItem/CheckListItem/block.browser.test.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,41 @@ it("renders checkbox as disabled when editor is not editable", async ({
6060
const checkbox = getCheckboxFromView(view);
6161
expect(checkbox.disabled).toBe(true);
6262
});
63+
64+
it("moves the caret from the start of a checklist item to the line above", async ({
65+
setupEditor,
66+
page,
67+
pressKey,
68+
}) => {
69+
const { editor } = await setupEditor({
70+
initialContent: [
71+
{
72+
type: "heading",
73+
props: { level: 2, isToggleable: true },
74+
content: "Toggle heading",
75+
children: [
76+
{ id: "above", type: "paragraph", content: "Heading child" },
77+
],
78+
},
79+
{
80+
id: "task",
81+
type: "checkListItem",
82+
props: { checked: true },
83+
content: "Completed task",
84+
},
85+
],
86+
});
87+
const toggle = page.getByRole("button", {
88+
name: editor.dictionary.toggle_blocks.toggle_button,
89+
});
90+
await toggle.click();
91+
await expect.element(toggle).toHaveAttribute("aria-expanded", "true");
92+
editor.setTextCursorPosition("task", "start");
93+
editor.focus();
94+
95+
await pressKey("ArrowUp");
96+
97+
await expect
98+
.poll(() => editor.getTextCursorPosition().block.id)
99+
.toBe("above");
100+
});

‎packages/core/src/editor/Block.css‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,8 @@ NESTED BLOCKS
372372

373373
/* Checked */
374374
.bn-block-content[data-content-type="checkListItem"] > div:has(> input) {
375+
/* Keep vertical caret navigation out of the non-editable checkbox wrapper. */
376+
user-select: none;
375377
height: 24px;
376378
}
377379

‎packages/core/src/editor/BlockNoteEditor.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,10 @@ export class BlockNoteEditor<
548548
scrollMargin: { top: 72, bottom: 72, left: 0, right: 0 },
549549
...newOptions._tiptapOptions?.editorProps,
550550
attributes: {
551+
// Let StateManager supply the role dynamically instead of
552+
// inheriting TipTap's unconditional textbox role. Explicit consumer
553+
// roles below still take precedence.
554+
role: "",
551555
// As of TipTap v2.5.0 the tabIndex is removed when the editor is not
552556
// editable, so you can't focus it. We want to revert this as we have
553557
// UI behaviour that relies on it.

‎packages/core/src/editor/managers/StateManager.browser.test.ts‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,67 @@ describe("StateManager editability", () => {
1111
prosemirrorPlugins: [new Plugin({ props: { editable: () => false } })],
1212
}));
1313

14+
it("uses document semantics while an editing lock is held", async ({
15+
setupEditor,
16+
page,
17+
onTestFinished,
18+
}) => {
19+
const { editor } = await setupEditor({ mountType: "dom" });
20+
const textbox = page.getByRole("textbox");
21+
const readOnlyDocument = page.getByRole("document");
22+
await expect.element(textbox).toHaveAttribute("aria-autocomplete", "list");
23+
await expect.element(textbox).toHaveAttribute("aria-haspopup", "listbox");
24+
25+
const release = editor.acquireEditingLock();
26+
onTestFinished(release);
27+
28+
await expect
29+
.element(readOnlyDocument)
30+
.toHaveAttribute("contenteditable", "false");
31+
await expect
32+
.element(readOnlyDocument)
33+
.not.toHaveAttribute("aria-autocomplete");
34+
await expect.element(readOnlyDocument).not.toHaveAttribute("aria-haspopup");
35+
36+
release();
37+
38+
await expect.element(textbox).toHaveAttribute("contenteditable", "true");
39+
await expect.element(textbox).toHaveAttribute("aria-autocomplete", "list");
40+
await expect.element(textbox).toHaveAttribute("aria-haspopup", "listbox");
41+
});
42+
43+
it("uses document semantics when a plugin restricts editing", async ({
44+
setupEditor,
45+
page,
46+
}) => {
47+
const { editor } = await setupEditor({
48+
mountType: "dom",
49+
extensions: [restriction()],
50+
});
51+
52+
await expect.element(page.getByRole("document")).toBeVisible();
53+
54+
editor.unregisterExtension(restriction);
55+
56+
await expect.element(page.getByRole("textbox")).toBeVisible();
57+
});
58+
59+
it("preserves an explicit editor role across editability changes", async ({
60+
setupEditor,
61+
page,
62+
}) => {
63+
const { editor } = await setupEditor({
64+
mountType: "dom",
65+
domAttributes: { editor: { role: "region", "aria-label": "Notes" } },
66+
});
67+
const region = page.getByRole("region", { name: "Notes" });
68+
await expect.element(region).toHaveAttribute("contenteditable", "true");
69+
70+
editor.isEditable = false;
71+
72+
await expect.element(region).toHaveAttribute("contenteditable", "false");
73+
});
74+
1475
it.for(["application", "plugin"] as const)(
1576
"reconciles inline popup state after remounting with the %s restriction",
1677
async (source, { setupEditor }) => {

‎packages/core/src/editor/managers/StateManager.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ export class StateManager {
6262
const { editable, restrictions } = this.editability.state;
6363
return editable && restrictions.size === 0;
6464
},
65+
// ProseMirror resolves view editability before computing root attributes.
66+
// Use the current view value so other plugins can also make it read-only.
67+
attributes: (): Record<string, string> =>
68+
this.prosemirrorView.editable
69+
? {
70+
role: "textbox",
71+
"aria-autocomplete": "list",
72+
"aria-haspopup": "listbox",
73+
}
74+
: { role: "document" },
6575
},
6676
view: (view) => {
6777
const store = this.editability;

‎packages/react/src/editor/BlockNoteView.browser.test.tsx‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,10 @@ export async function renderTestEditor(
6161
</main>
6262
),
6363
});
64-
await expect.element(page.getByRole("textbox")).toBeVisible();
64+
const content = page.getByRole(editable ? "textbox" : "document");
65+
await expect.element(content).toBeVisible();
6566
await expect
66-
.element(page.getByRole("textbox"))
67+
.element(content)
6768
.toHaveAttribute("contenteditable", String(editable));
6869
page
6970
.getByRole("button", { name: "Before editor", exact: true })
@@ -415,6 +416,29 @@ const schemaDocument: PartialBlock[] = [
415416
// Register these suites only in the regular browser run.
416417
if (!screenReader) {
417418
describe("aria", () => {
419+
it("exposes read-only content and restores textbox semantics when editing resumes", async ({
420+
setupEditor,
421+
}) => {
422+
const { rerender } = await setupEditor({
423+
initialContent: readOnlyLinkDocument,
424+
});
425+
await expect.element(page.getByRole("textbox")).toBeVisible();
426+
427+
rerender({ editable: false });
428+
429+
await expect.element(page.getByRole("document"))
430+
.toMatchAriaInlineSnapshot(`
431+
- document:
432+
- paragraph:
433+
- link "Documentation":
434+
- /url: https://www.blocknotejs.org/
435+
`);
436+
437+
rerender({ editable: true });
438+
439+
await expect.element(page.getByRole("textbox")).toBeVisible();
440+
});
441+
418442
// Numbered list item indices are separate text elements, among other issues.
419443
it.skip("default schema: accessibility tree snapshot", async ({
420444
setupEditor,

‎packages/react/src/editor/BlockNoteView.tsx‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -400,12 +400,6 @@ const ContentEditableElement = (props: {
400400
}) => {
401401
const { autoFocus, mount, contentEditableProps } = props;
402402
return (
403-
<div
404-
aria-autocomplete="list"
405-
aria-haspopup="listbox"
406-
data-bn-autofocus={autoFocus}
407-
ref={mount}
408-
{...contentEditableProps}
409-
/>
403+
<div data-bn-autofocus={autoFocus} ref={mount} {...contentEditableProps} />
410404
);
411405
};

0 commit comments

Comments
 (0)