Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions packages/core/src/blocks/Heading/Heading.browser.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ describe("Heading", () => {
initialContent: toggleHeadingDocument,
props: { editable: false },
});
const first = page.getByRole("textbox").nth(0);
const first = page.getByRole("document").nth(0);
const toggle = first.getByRole("button", {
name: editor.dictionary.toggle_blocks.toggle_button,
exact: true,
Expand Down Expand Up @@ -470,7 +470,7 @@ describe("Heading", () => {
props: { editable: false },
});
expect(recreated).not.toBe(editor);
const restored = page.getByRole("textbox").nth(1);
const restored = page.getByRole("document").nth(1);
await expect
.element(
restored.getByRole("button", {
Expand Down Expand Up @@ -719,22 +719,22 @@ describe("Heading", () => {
name: blockNoteEditor.dictionary.toggle_blocks.toggle_button,
exact: true,
});
const editor = page.getByRole("textbox");
const editor = page.getByRole("document");
await expect.element(editor).toMatchAriaInlineSnapshot(`
- textbox:
- document:
- button "Expand or collapse"
- heading "Details" [level=2]
`);
await toggleButton.click();
await expect.element(editor).toMatchAriaInlineSnapshot(`
- textbox:
- document:
- button "Expand or collapse" [expanded]
- heading "Details" [level=2]
- paragraph: Nested read-only content
`);
await toggleButton.click();
await expect.element(editor).toMatchAriaInlineSnapshot(`
- textbox:
- document:
- button "Expand or collapse"
- heading "Details" [level=2]
`);
Expand Down Expand Up @@ -825,9 +825,9 @@ describe("Heading", () => {
initialContent: toggleHeadingDocument,
props: { editable: false, theme: "light" },
});
const textbox = page.getByRole("textbox");
await expect.element(textbox).toBeVisible();
expect((await runAxe(textbox)).violations).toEqual([]);
const readOnlyDocument = page.getByRole("document");
await expect.element(readOnlyDocument).toBeVisible();
expect((await runAxe(readOnlyDocument)).violations).toEqual([]);

await page
.getByRole("button", {
Expand All @@ -838,7 +838,7 @@ describe("Heading", () => {
await expect
.element(page.getByText("Nested read-only content", { exact: true }))
.toBeVisible();
expect((await runAxe(textbox)).violations).toEqual([]);
expect((await runAxe(readOnlyDocument)).violations).toEqual([]);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,41 @@ it("renders checkbox as disabled when editor is not editable", async ({
const checkbox = getCheckboxFromView(view);
expect(checkbox.disabled).toBe(true);
});

it("moves the caret from the start of a checklist item to the line above", async ({
setupEditor,
page,
pressKey,
}) => {
const { editor } = await setupEditor({
initialContent: [
{
type: "heading",
props: { level: 2, isToggleable: true },
content: "Toggle heading",
children: [
{ id: "above", type: "paragraph", content: "Heading child" },
],
},
{
id: "task",
type: "checkListItem",
props: { checked: true },
content: "Completed task",
},
],
});
const toggle = page.getByRole("button", {
name: editor.dictionary.toggle_blocks.toggle_button,
});
await toggle.click();
await expect.element(toggle).toHaveAttribute("aria-expanded", "true");
editor.setTextCursorPosition("task", "start");
editor.focus();

await pressKey("ArrowUp");

await expect
.poll(() => editor.getTextCursorPosition().block.id)
.toBe("above");
});
2 changes: 2 additions & 0 deletions packages/core/src/editor/Block.css
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,8 @@ NESTED BLOCKS

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

Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/editor/BlockNoteEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,10 @@ export class BlockNoteEditor<
scrollMargin: { top: 72, bottom: 72, left: 0, right: 0 },
...newOptions._tiptapOptions?.editorProps,
attributes: {
// Let StateManager supply the role dynamically instead of
// inheriting TipTap's unconditional textbox role. Explicit consumer
// roles below still take precedence.
role: "",
// As of TipTap v2.5.0 the tabIndex is removed when the editor is not
// editable, so you can't focus it. We want to revert this as we have
// UI behaviour that relies on it.
Expand Down
61 changes: 61 additions & 0 deletions packages/core/src/editor/managers/StateManager.browser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,67 @@ describe("StateManager editability", () => {
prosemirrorPlugins: [new Plugin({ props: { editable: () => false } })],
}));

it("uses document semantics while an editing lock is held", async ({
setupEditor,
page,
onTestFinished,
}) => {
const { editor } = await setupEditor({ mountType: "dom" });
const textbox = page.getByRole("textbox");
const readOnlyDocument = page.getByRole("document");
await expect.element(textbox).toHaveAttribute("aria-autocomplete", "list");
await expect.element(textbox).toHaveAttribute("aria-haspopup", "listbox");

const release = editor.acquireEditingLock();
onTestFinished(release);

await expect
.element(readOnlyDocument)
.toHaveAttribute("contenteditable", "false");
await expect
.element(readOnlyDocument)
.not.toHaveAttribute("aria-autocomplete");
await expect.element(readOnlyDocument).not.toHaveAttribute("aria-haspopup");

release();

await expect.element(textbox).toHaveAttribute("contenteditable", "true");
await expect.element(textbox).toHaveAttribute("aria-autocomplete", "list");
await expect.element(textbox).toHaveAttribute("aria-haspopup", "listbox");
});

it("uses document semantics when a plugin restricts editing", async ({
setupEditor,
page,
}) => {
const { editor } = await setupEditor({
mountType: "dom",
extensions: [restriction()],
});

await expect.element(page.getByRole("document")).toBeVisible();

editor.unregisterExtension(restriction);

await expect.element(page.getByRole("textbox")).toBeVisible();
});

it("preserves an explicit editor role across editability changes", async ({
setupEditor,
page,
}) => {
const { editor } = await setupEditor({
mountType: "dom",
domAttributes: { editor: { role: "region", "aria-label": "Notes" } },
});
const region = page.getByRole("region", { name: "Notes" });
await expect.element(region).toHaveAttribute("contenteditable", "true");

editor.isEditable = false;

await expect.element(region).toHaveAttribute("contenteditable", "false");
});

it.for(["application", "plugin"] as const)(
"reconciles inline popup state after remounting with the %s restriction",
async (source, { setupEditor }) => {
Expand Down
10 changes: 10 additions & 0 deletions packages/core/src/editor/managers/StateManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ export class StateManager {
const { editable, restrictions } = this.editability.state;
return editable && restrictions.size === 0;
},
// ProseMirror resolves view editability before computing root attributes.
// Use the current view value so other plugins can also make it read-only.
attributes: (): Record<string, string> =>
this.prosemirrorView.editable
? {
role: "textbox",
"aria-autocomplete": "list",
"aria-haspopup": "listbox",
}
: { role: "document" },
},
view: (view) => {
const store = this.editability;
Expand Down
28 changes: 26 additions & 2 deletions packages/react/src/editor/BlockNoteView.browser.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ export async function renderTestEditor(
</main>
),
});
await expect.element(page.getByRole("textbox")).toBeVisible();
const content = page.getByRole(editable ? "textbox" : "document");
await expect.element(content).toBeVisible();
await expect
.element(page.getByRole("textbox"))
.element(content)
.toHaveAttribute("contenteditable", String(editable));
page
.getByRole("button", { name: "Before editor", exact: true })
Expand Down Expand Up @@ -415,6 +416,29 @@ const schemaDocument: PartialBlock[] = [
// Register these suites only in the regular browser run.
if (!screenReader) {
describe("aria", () => {
it("exposes read-only content and restores textbox semantics when editing resumes", async ({
setupEditor,
}) => {
const { rerender } = await setupEditor({
initialContent: readOnlyLinkDocument,
});
await expect.element(page.getByRole("textbox")).toBeVisible();

rerender({ editable: false });

await expect.element(page.getByRole("document"))
.toMatchAriaInlineSnapshot(`
- document:
- paragraph:
- link "Documentation":
- /url: https://www.blocknotejs.org/
`);

rerender({ editable: true });

await expect.element(page.getByRole("textbox")).toBeVisible();
});

// Numbered list item indices are separate text elements, among other issues.
it.skip("default schema: accessibility tree snapshot", async ({
setupEditor,
Expand Down
8 changes: 1 addition & 7 deletions packages/react/src/editor/BlockNoteView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -400,12 +400,6 @@ const ContentEditableElement = (props: {
}) => {
const { autoFocus, mount, contentEditableProps } = props;
return (
<div
aria-autocomplete="list"
aria-haspopup="listbox"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

intentionally removed?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep this is now set by StateManager as it should be listbox when editable and document when read-only

data-bn-autofocus={autoFocus}
ref={mount}
{...contentEditableProps}
/>
<div data-bn-autofocus={autoFocus} ref={mount} {...contentEditableProps} />
);
};
Loading