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
205 changes: 205 additions & 0 deletions packages/react/src/components/Popovers/GenericPopover.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
// Closing a popover used to re-parse a stored HTML string
// (`dangerouslySetInnerHTML`). That re-applies inline style attributes, which
// a CSP `style-src` without 'unsafe-inline' blocks. jsdom does not enforce
// CSP, but it does perform the assignment, so this test fails on that write
// and on a close transition that drops or replaces the open content.
import { act, type CSSProperties, type ReactElement } from "react";
import { createRoot, type Root } from "react-dom/client";
import { afterEach, beforeEach, describe, expect, it } from "vite-plus/test";

import {
BlockNoteContext,
type BlockNoteContextValue,
} from "../../editor/BlockNoteContext.js";
import { GenericPopover } from "./GenericPopover.js";

declare global {
// eslint-disable-next-line no-var
var IS_REACT_ACT_ENVIRONMENT: boolean;
}
globalThis.IS_REACT_ACT_ENVIRONMENT = true;

const POPOVER_CLASS = "bn-test-popover";
const LABEL_ID = "popover-label";

let reactHost: HTMLDivElement;
let editorDom: HTMLDivElement;
let root: Root;
let editor: NonNullable<BlockNoteContextValue["editor"]>;

function createEditor(domElement: HTMLElement) {
return {
domElement,
isWithinEditor: () => false,
_tiptapEditor: {
on() {
return;
},
off() {
return;
},
},
} as unknown as NonNullable<BlockNoteContextValue["editor"]>;
}

function popover() {
return document.querySelector<HTMLElement>(`.${POPOVER_CLASS}`);
}

function labels() {
return document.querySelectorAll<HTMLElement>(`[data-testid="${LABEL_ID}"]`);
}

function Harness(props: {
open: boolean;
marker: string;
zIndex?: number;
childrenWhileClosed?: boolean;
focusManagerDisabled?: boolean;
}) {
const labelStyle: CSSProperties & { "--bn-test": string } = {
color: "rgb(255, 0, 0)",
"--bn-test": "1",
};
const content = (
<span data-testid={LABEL_ID} style={labelStyle}>
{props.marker}
</span>
);

return (
<BlockNoteContext.Provider value={{ editor }}>
<GenericPopover
elementProps={{
className: POPOVER_CLASS,
style: { zIndex: props.zIndex ?? 1 },
}}
focusManagerProps={
props.focusManagerDisabled === false ? undefined : { disabled: true }
}
useFloatingOptions={{ open: props.open }}
>
{props.open || props.childrenWhileClosed ? content : null}
</GenericPopover>
</BlockNoteContext.Provider>
);
}

async function render(element: ReactElement) {
await act(async () => {
root.render(element);
});
}

beforeEach(() => {
reactHost = document.createElement("div");
const portalParent = document.createElement("div");
editorDom = document.createElement("div");
portalParent.append(editorDom);
document.body.append(reactHost, portalParent);
root = createRoot(reactHost);
editor = createEditor(editorDom);
});

afterEach(async () => {
await act(async () => root.unmount());
reactHost.remove();
editorDom.parentElement?.remove();
});

describe("GenericPopover close transition", () => {
it("keeps the open content visible while closing without assigning innerHTML", async () => {
await render(<Harness open marker="Slash menu" />);

expect(popover()?.textContent).toContain("Slash menu");

const writes: Array<{ element: Element; value: string }> = [];
const descriptor = Object.getOwnPropertyDescriptor(
Element.prototype,
"innerHTML",
);
if (!descriptor?.set || !descriptor.get) {
throw new Error("Expected Element.innerHTML to be configurable");
}
// oxlint-disable-next-line typescript/unbound-method -- Called with the target element as `this` below.
const setInnerHTML = descriptor.set;
Object.defineProperty(Element.prototype, "innerHTML", {
...descriptor,
set(this: Element, value: string) {
writes.push({ element: this, value: String(value) });
setInnerHTML.call(this, value);
},
});

try {
await render(<Harness open={false} marker="replaced while closing" />);
} finally {
Object.defineProperty(Element.prototype, "innerHTML", descriptor);
}

const floating = popover();
expect(floating?.textContent).toContain("Slash menu");
expect(floating?.textContent).not.toContain("replaced while closing");
expect(labels()).toHaveLength(1);
const label = labels()[0];
expect(label?.style.color).toBe("rgb(255, 0, 0)");
expect(label?.style.getPropertyValue("--bn-test")).toBe("1");
expect(floating?.style.opacity).toBe("0");
// Re-parsing HTML that contains `style="..."` is what CSP rejects.
expect(
writes.filter(
(write) =>
(write.element === floating || floating?.contains(write.element)) &&
(write.value.includes("style") || write.value.includes("Slash menu")),
),
).toEqual([]);

await render(
<Harness open={false} marker="replaced while closing" zIndex={4} />,
);
expect(popover()?.textContent).toContain("Slash menu");
expect(labels()).toHaveLength(1);
});

it("ignores child updates during the close transition", async () => {
await render(<Harness open marker="Slash menu" />);
await render(<Harness open={false} marker="replaced while closing" />);
await render(
<Harness
open={false}
marker="replaced while closing"
childrenWhileClosed
/>,
);

expect(popover()?.textContent).toContain("Slash menu");
expect(popover()?.textContent).not.toContain("replaced while closing");
expect(labels()).toHaveLength(1);
});

it("drops the frozen content when the popover opens again", async () => {
await render(<Harness open marker="Slash menu" />);
await render(<Harness open={false} marker="replaced while closing" />);
await render(<Harness open marker="Open again" />);

expect(popover()?.textContent).toContain("Open again");
expect(popover()?.textContent).not.toContain("Slash menu");
expect(labels()).toHaveLength(1);
});

it("keeps the close snapshot when the focus manager is enabled", async () => {
await render(
<Harness open marker="Slash menu" focusManagerDisabled={false} />,
);
await render(
<Harness
open={false}
marker="replaced while closing"
focusManagerDisabled={false}
/>,
);

expect(popover()?.textContent).toContain("Slash menu");
expect(labels()).toHaveLength(1);
});
});
36 changes: 24 additions & 12 deletions packages/react/src/components/Popovers/GenericPopover.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable react/refs -- Closing transitions render the previously captured HTML snapshot from this ref. */
import {
autoUpdate,
FloatingFocusManager,
Expand All @@ -21,6 +20,7 @@ import {
usePortalElement,
} from "../../editor/PortalElementOverride.js";
import { useBlockNoteEditor } from "../../hooks/useBlockNoteEditor.js";
import { useIsomorphicLayoutEffect } from "../../util/useIsomorphicLayoutEffect.js";
import { FloatingUIOptions } from "./FloatingUIOptions.js";

export type GenericPopoverReference =
Expand Down Expand Up @@ -164,7 +164,7 @@ export const GenericPopover = (
// possible both are needed.
const { getFloatingProps } = useInteractions([dismiss, hover]);

const innerHTML = useRef<string>("");
const snapshot = useRef<NodeListOf<ChildNode> | undefined>(undefined);
const ref = useRef<HTMLDivElement>(null);
const mergedRefs = useMergeRefs([ref, refs.setFloating]);

Expand Down Expand Up @@ -207,8 +207,8 @@ export const GenericPopover = (
}
}, [props.reference, refs, props.focusManagerProps?.disabled, editor]);

// Stores the last rendered `innerHTML` of the popover while it was open. The
// `innerHTML` is used while the popover is closing, as the React children
// Stores the last rendered DOM of the popover while it was open. The
// snapshot is used while the popover is closing, as the React children
// may rerender during this time, causing unwanted behaviour.
useEffect(
() => {
Expand All @@ -220,15 +220,31 @@ export const GenericPopover = (
// about to show. The wrapper is never truly empty though: it always
// contains the `PortalElementAnchor` holder.
if (ref.current && hasChildrenBesidesPortalElementAnchor(ref.current)) {
innerHTML.current = ref.current.innerHTML;
snapshot.current = ref.current.cloneNode(true).childNodes;
}
}
},
// `props.children` is added to the deps, since it's ultimately the HTML of
// `props.children` is added to the deps, since it's ultimately the DOM of
// the children that we're storing.
[status, props.reference, props.children],
);

// Attach clones before paint, avoiding HTML parsing and its inline-style CSP checks.
useIsomorphicLayoutEffect(() => {
if (status !== "close" || !ref.current || !snapshot.current) {
return;
}

const nodes = [...snapshot.current];
ref.current.append(...nodes);

return () => {
for (const node of nodes) {
node.parentNode?.removeChild(node);
}
};
}, [status]);

if (!isMounted || !portalElement) {
return false;
}
Expand All @@ -249,7 +265,7 @@ export const GenericPopover = (
};

if (status === "close") {
// While the popover is closing, shows its last rendered `innerHTML` while
// While the popover is closing, shows its last rendered DOM snapshot while
// it was open, instead of the React children. This is because they may
// rerender during this time, causing unwanted behaviour.
//
Expand All @@ -259,11 +275,7 @@ export const GenericPopover = (
// out and will instead appear to hide instantly.
return (
<FloatingPortal root={portalElement}>
<div
ref={mergedRefs}
{...mergedProps}
dangerouslySetInnerHTML={{ __html: innerHTML.current }}
/>
<div ref={mergedRefs} {...mergedProps} />
</FloatingPortal>
);
}
Expand Down
Loading