Beautiful, AI-native markdown IDE and LLM wiki
TypeScript 2nd-brainagent-skillsclaudecodexcompany-braindocsknowledge-knowledge-baseknowledge-managementllmllm-wikillm-wiki-karpathymarkdownmarkdown-editormdmdxnotespkmsecond-brainskills
fix(ok): clipboard descriptor resolution over-climbs NodeViewContent (#2879)
Nick Gomez934d08f9ba11
diff --git a/.changeset/clipboard-container-descriptor-boundary.md b/.changeset/clipboard-container-descriptor-boundary.mdnew file mode 100644index 000000000..c5dd86ac1--- /dev/null+++ b/.changeset/clipboard-container-descriptor-boundary.md@@ -0,0 +1,7 @@+---+"@inkeep/open-knowledge": patch+---++Fix cross-app clipboard fidelity for container-nested images that carry a non-portable (relative or document-relative) URL. Copying a component such as a Callout or Accordion that holds a relative-URL image no longer pastes the whole component's markdown source at the image's position in rich-text destinations. The image now contributes only its own source-fallback markdown.++Previously the descriptor resolver climbed past the container's content boundary and treated the enclosing component as the leaf's descriptor, so the entire component source was serialized at the leaf's slot. The climb now stops at the container's content boundary, keeping the resolved range within the leaf's own node view. Copy as plain text and Open Knowledge to Open Knowledge paste were unaffected and remain unchanged.diff --git a/packages/app/src/editor/clipboard/serialize.container-descriptor-boundary.test.ts b/packages/app/src/editor/clipboard/serialize.container-descriptor-boundary.test.tsnew file mode 100644index 000000000..d1cf781cf--- /dev/null+++ b/packages/app/src/editor/clipboard/serialize.container-descriptor-boundary.test.ts@@ -0,0 +1,337 @@+/**+ * Container-descriptor boundary contract: a leaf reached THROUGH a container+ * NodeView's content area (`[data-node-view-content]`, emitted by+ * `@tiptap/react`'s `NodeViewContent`) must resolve to its OWN descriptor+ * root and its OWN ProseMirror range, never the enclosing container's.+ *+ * A container descriptor (a `jsxComponent` with children, e.g. `Callout`,+ * `Accordion`) renders its children into a `[data-node-view-content]` hole.+ * When a descriptor-rendered leaf (e.g. a `CommonMarkImage` `jsxComponent`)+ * lives inside that hole, the production DOM is+ * (`JsxComponentView.tsx` Branch 2 + `NodeViewContent`):+ *+ * .ProseMirror+ * .react-renderer.node-jsxComponent (Callout outer wrapper)+ * [data-node-view-wrapper data-jsx-component] (Callout NodeViewWrapper)+ * .jsx-component-chrome+ * <Callout> …+ * [data-node-view-content] (Callout children hole)+ * .react-renderer.node-jsxComponent (image outer wrapper)+ * [data-node-view-wrapper data-jsx-component]+ * img (leaf)+ *+ * The `[data-node-view-content]` element belongs to the container, but+ * everything the container HOSTS inside it is a different, enclosed PM node.+ * A descriptor climb that records the outermost matching wrapper without+ * stopping at that boundary adopts the CONTAINER's wrapper as the leaf's+ * descriptor root, so `posAtDOM → nodeAt → slice` covers the whole container+ * and the cross-app `text/html` source-fallback emits the entire component's+ * markdown at the leaf's slot.+ *+ * These tests pin the invariant at two tiers:+ * 1. `findDescriptorRoot` (the single choke point) resolves the leaf's own+ * descriptor across one and across two nested contentDOM boundaries.+ * 2. `serializeElementMarkdown` (the real consumer) emits the leaf's own+ * markdown bytes, not the whole container component's source.+ *+ * Harness mirrors `serialize.inline-image-range.test.ts`: `buildWalkerEnv`+ * is private, so the walker entry point is stubbed via `vi.doMock` to+ * capture the env, and the env is driven through the same public wiring+ * production uses. The mock DELEGATES to the real implementation outside an+ * explicit capture window so it stays behavior-transparent to the worker.+ */++import type { JSONContent } from '@tiptap/core';+import type { Fragment } from '@tiptap/pm/model';+import { Schema } from '@tiptap/pm/model';+import type { EditorView } from '@tiptap/pm/view';+import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';+import type { SerializeResult, WalkerEnv } from './clipboard-walker.ts';++const actualWalker = await import('./clipboard-walker.ts');+const realWalkLiveDom = actualWalker.walkLiveDomToInlineStyledFragment;++let capturedEnv: WalkerEnv | null = null;+let captureActive = false;++vi.doMock('./clipboard-walker.ts', () => ({+ ...actualWalker,+ walkLiveDomToInlineStyledFragment: (slice: unknown, view: unknown, env: WalkerEnv) => {+ if (captureActive) {+ capturedEnv = env;+ return { childNodes: [] };+ }+ // biome-ignore lint/suspicious/noExplicitAny: pass-through to the real implementation+ return realWalkLiveDom(slice as any, view as any, env);+ },+}));++// Imported AFTER the module mock so `serializeFragment`'s walker tier hits+// the delegating mock above.+const { createClipboardHtmlSerializer, findDescriptorRoot } = await import('./serialize.ts');++// ---------------------------------------------------------------------------+// Fake live-DOM elements — cover exactly the surface the code under test+// touches (`classList.contains`, `hasAttribute`, `parentElement`,+// `childNodes`) with STABLE object identity so `parentElement ===` and+// `childNodes.indexOf(...)` work.+// ---------------------------------------------------------------------------++interface FakeEl {+ classes: Set<string>;+ attrs: Set<string>;+ parentElement: FakeEl | null;+ childNodes: unknown[];+ classList: { contains: (c: string) => boolean };+ hasAttribute: (a: string) => boolean;+}++function el(opts?: { classes?: string[]; attrs?: string[] }): FakeEl {+ const classes = new Set(opts?.classes ?? []);+ const attrs = new Set(opts?.attrs ?? []);+ return {+ classes,+ attrs,+ parentElement: null,+ childNodes: [],+ classList: { contains: (c: string) => classes.has(c) },+ hasAttribute: (a: string) => attrs.has(a),+ };+}++function chain(...els: FakeEl[]): void {+ for (let i = 1; i < els.length; i++) {+ els[i].parentElement = els[i - 1];+ }+}++/**+ * Container (Callout `jsxComponent`) holding an inline image, both real+ * `jsxComponent` NodeViews, with the image nested inside the container's+ * `[data-node-view-content]` hole.+ */+function buildContainerWithImageTopology() {+ const proseMirror = el({ classes: ['ProseMirror'] });+ const calloutRenderer = el({ classes: ['react-renderer', 'node-jsxComponent'] });+ const calloutWrapper = el({ attrs: ['data-node-view-wrapper', 'data-jsx-component'] });+ const calloutBody = el(); // rendered <Callout> element+ const contentDom = el({ attrs: ['data-node-view-content'] }); // NodeViewContent hole+ const imageRenderer = el({ classes: ['react-renderer', 'node-jsxComponent'] });+ const imageWrapper = el({ attrs: ['data-node-view-wrapper', 'data-jsx-component'] });+ const img = el();+ chain(+ proseMirror,+ calloutRenderer,+ calloutWrapper,+ calloutBody,+ contentDom,+ imageRenderer,+ imageWrapper,+ img,+ );+ // Wire the childNodes the descriptor-parent branch indexes through.+ proseMirror.childNodes = [calloutRenderer];+ contentDom.childNodes = [imageRenderer];+ return { proseMirror, calloutRenderer, contentDom, imageRenderer, img };+}++/**+ * Doubly-nested container: an outer container whose content hole hosts an+ * inner container whose content hole hosts the image leaf. Pins that the+ * leaf resolves to its OWN descriptor across TWO contentDOM boundaries, not+ * either enclosing container's.+ */+function buildNestedContainerTopology() {+ const proseMirror = el({ classes: ['ProseMirror'] });+ const outerRenderer = el({ classes: ['react-renderer', 'node-jsxComponent'] });+ const outerWrapper = el({ attrs: ['data-node-view-wrapper', 'data-jsx-component'] });+ const outerContent = el({ attrs: ['data-node-view-content'] });+ const innerRenderer = el({ classes: ['react-renderer', 'node-jsxComponent'] });+ const innerWrapper = el({ attrs: ['data-node-view-wrapper', 'data-jsx-component'] });+ const innerContent = el({ attrs: ['data-node-view-content'] });+ const imageRenderer = el({ classes: ['react-renderer', 'node-jsxComponent'] });+ const imageWrapper = el({ attrs: ['data-node-view-wrapper', 'data-jsx-component'] });+ const img = el();+ chain(+ proseMirror,+ outerRenderer,+ outerWrapper,+ outerContent,+ innerRenderer,+ innerWrapper,+ innerContent,+ imageRenderer,+ imageWrapper,+ img,+ );+ return { proseMirror, outerRenderer, innerRenderer, imageRenderer, img };+}++// ---------------------------------------------------------------------------+// PM document mirroring a container holding a single image descriptor.+// ---------------------------------------------------------------------------++const containerSchema = new Schema({+ nodes: {+ doc: { content: 'block+' },+ container: {+ group: 'block',+ content: 'block+',+ toDOM: () => ['div', { 'data-jsx': 'callout' }, 0],+ parseDOM: [{ tag: 'div[data-jsx=callout]' }],+ },+ image: {+ group: 'block',+ atom: true,+ attrs: { src: { default: '' }, alt: { default: '' } },+ toDOM: (node) => ['img', { src: node.attrs.src, alt: node.attrs.alt }],+ parseDOM: [{ tag: 'img' }],+ },+ // Required by prosemirror-model (every schema needs a text type) even+ // though the container's block-only content never holds text here.+ text: { group: 'inline' },+ },+});++function buildDoc() {+ return containerSchema.node('doc', null, [+ containerSchema.node('container', null, [+ containerSchema.node('image', { src: './shot.png', alt: 'shot' }),+ ]),+ ]);+}++// Doc content starts at 0: the container node opens at 0 (the over-climb+// slices from here), its content starts at 1, and the image atom sits at+// position 1 (the leaf's own range).+const IMAGE_POS = 1;++/**+ * posAtDOM faithful to ProseMirror's contract: the offset counts CHILDNODES+ * of the PM node the passed DOM element maps to.+ * - `.ProseMirror` maps to the doc — offset 0 → position 0 (the container).+ * - the container's `[data-node-view-content]` maps to the container's+ * content region — offset 0 → position 1 (the image atom).+ * The correct climb resolves `descriptorRoot = imageRenderer` (parent =+ * contentDom → image); the over-climb resolves `descriptorRoot =+ * calloutRenderer` (parent = .ProseMirror → whole container).+ */+function fakePosAtDOM(+ topology: ReturnType<typeof buildContainerWithImageTopology>,+ doc: ReturnType<typeof buildDoc>,+) {+ const { proseMirror, contentDom, img } = topology;+ const container = doc.child(0);+ return (node: unknown, offset: number, _bias?: number): number => {+ if (node === img) return IMAGE_POS;+ if (node === proseMirror) {+ let pos = 0;+ for (let i = 0; i < offset; i++) pos += doc.child(i).nodeSize;+ return pos;+ }+ if (node === contentDom) {+ let pos = 1;+ for (let i = 0; i < offset; i++) pos += container.child(i).nodeSize;+ return pos;+ }+ throw new RangeError('fakePosAtDOM: element not in fake mapping');+ };+}++/**+ * Markdown manager double that discriminates WHICH PM node reached+ * serialization: an image emits its markdown source, a container wraps its+ * serialized children in a marker so the over-climb (whole-component source)+ * surfaces as distinct bytes, never a false green.+ */+function discriminatingMdManager() {+ const serializeJson = (json: JSONContent): string => {+ if (json.type === 'image') {+ return ``;+ }+ if (json.type === 'container') {+ return `<callout>${(json.content ?? []).map(serializeJson).join('')}</callout>`;+ }+ if (json.type === 'text') return json.text ?? '';+ return (json.content ?? []).map(serializeJson).join('');+ };+ return {+ serialize: (json: JSONContent) => serializeJson(json),+ parse: () => ({ type: 'doc', content: [] }),+ };+}++function captureEnv(topology: ReturnType<typeof buildContainerWithImageTopology>): WalkerEnv {+ capturedEnv = null;+ captureActive = true;+ const doc = buildDoc();+ const view = {+ posAtDOM: fakePosAtDOM(topology, doc),+ state: {+ schema: containerSchema,+ doc,+ selection: {+ from: 0,+ to: doc.content.size,+ content: () => doc.slice(0, doc.content.size),+ },+ },+ } as unknown as EditorView;+ const handle = createClipboardHtmlSerializer({+ // biome-ignore lint/suspicious/noExplicitAny: markdown-manager double+ mdManager: discriminatingMdManager() as any,+ });+ handle.setView(view);+ try {+ handle.serializer.serializeFragment({ firstChild: null } as unknown as Fragment, undefined, {+ appendChild: () => {},+ } as unknown as DocumentFragment);+ } finally {+ captureActive = false;+ }+ if (!capturedEnv) throw new Error('walker env was not captured');+ return capturedEnv;+}++let origWarn: typeof console.warn;+beforeEach(() => {+ origWarn = console.warn;+ console.warn = () => {};+});+afterEach(() => {+ console.warn = origWarn;+});++describe('container-descriptor boundary — leaf resolves to its own descriptor across [data-node-view-content]', () => {+ test('findDescriptorRoot: a leaf inside a container NodeViewContent resolves to its OWN descriptor, not the enclosing container', () => {+ const { calloutRenderer, imageRenderer, img } = buildContainerWithImageTopology();+ const resolved = findDescriptorRoot(img as unknown as Element);+ // Positive: the image's own outer `.react-renderer` is the descriptor root.+ expect(resolved).toBe(imageRenderer as unknown as Element);+ // Negative: the enclosing container's wrapper must NOT be adopted — the+ // `[data-node-view-content]` boundary belongs to a different PM node.+ expect(resolved).not.toBe(calloutRenderer as unknown as Element);+ });++ test('findDescriptorRoot: through TWO nested containers, the leaf still resolves to the innermost descriptor', () => {+ const { outerRenderer, innerRenderer, imageRenderer, img } = buildNestedContainerTopology();+ const resolved = findDescriptorRoot(img as unknown as Element);+ expect(resolved).toBe(imageRenderer as unknown as Element);+ expect(resolved).not.toBe(innerRenderer as unknown as Element);+ expect(resolved).not.toBe(outerRenderer as unknown as Element);+ });++ test('serializeElementMarkdown emits the leaf image markdown, not the whole container component source', () => {+ const topology = buildContainerWithImageTopology();+ const env = captureEnv(topology);+ const result = env.serializeElementMarkdown?.(+ topology.img as unknown as Element,+ ) as SerializeResult;+ expect(result.kind).toBe('ok');+ if (result.kind === 'ok') {+ // The leaf's own range — NOT `<callout></callout>`,+ // which is what the over-climb slices and serializes.+ expect(result.markdown).toBe('');+ }+ });+});diff --git a/packages/app/src/editor/clipboard/serialize.ts b/packages/app/src/editor/clipboard/serialize.tsindex 39fe65836..912cf068f 100644--- a/packages/app/src/editor/clipboard/serialize.ts+++ b/packages/app/src/editor/clipboard/serialize.ts@@ -504,6 +504,13 @@ export function findDescriptorRoot(live: Element): Element | null {let cur: Element | null = live;let optedOutWrapper: Element | null = null;while (cur && !cur.classList.contains('ProseMirror')) {+ // Stop at a container's NodeViewContent boundary: everything at or above+ // `[data-node-view-content]` belongs to the ENCLOSING container's PM node,+ // not the leaf being resolved. Any descriptor wrapper above this point is+ // the container's own `.react-renderer`; adopting it would make PM position+ // the whole container at the leaf's slot. The descriptor already recorded+ // below the boundary is the leaf's own root, so stop the climb here.+ if (cur.hasAttribute('data-node-view-content')) break;// Opt-out: live-editor render wrappers around bare inline PM atoms// (e.g., `ImageInlineZoom`'s `<Zoom>` wrap). The PM node IS the leaf// `<img>` — there is no descriptor here, even though tiptap stamps@@ -529,10 +536,14 @@ export function findDescriptorRoot(live: Element): Element | null {cur.hasAttribute('data-node-view-wrapper') ||cur.hasAttribute('data-jsx-component')) {- // Keep climbing — for nested descriptors, the OUTERMOST is the one- // PM positions in its parent's content. For example, `CommonMarkImage`- // is a `JsxComponent` rendered as `.react-renderer.node-jsxComponent`- // wrapping `[data-node-view-wrapper data-jsx-component]`.+ // Keep climbing — for nested descriptors OF THE SAME node view, the+ // OUTERMOST is the one PM positions in its parent's content. For example,+ // `CommonMarkImage` is a `JsxComponent` rendered as+ // `.react-renderer.node-jsxComponent` wrapping+ // `[data-node-view-wrapper data-jsx-component]`. This "outermost wins"+ // holds only WITHIN one node view; the climb terminates at the first+ // `[data-node-view-content]` boundary (above), so a container descriptor+ // hosting the leaf in its content hole is never adopted.descriptorRoot = cur;}cur = cur.parentElement;diff --git a/packages/app/tests/stress/clipboard-relative-url-source-fallback.e2e.ts b/packages/app/tests/stress/clipboard-relative-url-source-fallback.e2e.tsindex 6f797631d..8e2591a6d 100644--- a/packages/app/tests/stress/clipboard-relative-url-source-fallback.e2e.ts+++ b/packages/app/tests/stress/clipboard-relative-url-source-fallback.e2e.ts@@ -384,6 +384,84 @@ test.describe('FR-6 / FR-7 partial-failure mid-walk continuation', () => {});});+test.describe('container-nested leaf — descriptor boundary (over-climb regression)', () => {+ test('block relative-URL image inside a Callout emits the image source-fallback, not the whole Callout source', async ({+ page,+ api,+ baseURL,+ }) => {+ // Regression pin for the descriptor over-climb: a non-portable-URL image+ // living inside a container descriptor's content hole must contribute only+ // its OWN source-fallback markdown to the cross-app text/html payload. The+ // resolver used to climb past the Callout's NodeViewContent boundary and+ // serialize the entire component at the image's slot.+ const docName = `test-container-desc-${randomUUID().slice(0, 8)}`;+ await api.createPage(`${docName}.md`);+ await page.goto(`/#/${docName}`);+ await waitForProvider(page);+ await page.waitForSelector('.ProseMirror:not(.composer-prosemirror)');++ // Blank-line separation keeps the image a BLOCK image so image-promoter+ // promotes it to the `CommonMarkImage` jsxComponent NodeView (inline images+ // stay inline `image` nodes and never render as a nested descriptor leaf).+ await fetch(`${baseURL}/api/agent-write-md`, {+ method: 'POST',+ headers: { 'Content-Type': 'application/json' },+ body: JSON.stringify({+ docName,+ markdown: '<Callout type="note">\n\n\n\n</Callout>\n',+ position: 'replace',+ }),+ });+ await expect(async () => {+ expect(await getYText(page)).toContain('');+ }).toPass({ timeout: 5_000 });+ await page.click('.ProseMirror:not(.composer-prosemirror)');++ // MANDATORY precondition: the seed must produce the real nesting topology —+ // the image renders as a `.react-renderer.node-jsxComponent` INSIDE the+ // Callout's `[data-node-view-content]`. Without this, a non-nesting seed+ // would false-pin (the payload assertion would pass regardless of the fix).+ const nesting = await expect(async () => {+ const result = await page.evaluate(() => {+ const containers = Array.from(+ document.querySelectorAll('.react-renderer.node-jsxComponent'),+ );+ for (const container of containers) {+ const content = container.querySelector('[data-node-view-content]');+ if (!content) continue;+ const innerLeaf = content.querySelector('.react-renderer.node-jsxComponent');+ if (innerLeaf?.querySelector('img')) {+ return { nested: true, containers: containers.length };+ }+ }+ return { nested: false, containers: containers.length };+ });+ expect(result.nested).toBe(true);+ return result;+ }).toPass({ timeout: 5_000 });+ void nesting;++ const captured = await simulateCopyAndRead(page, 'wysiwyg');++ // text/plain is canonical markdown, which the descriptor resolver never+ // touches — the image source survives here whether or not the over-climb+ // is fixed, so this is a content-survival sanity check (it also keeps the+ // negative text/html assertion below from passing vacuously on an empty+ // payload), NOT a regression pin. The two text/html assertions ARE the+ // discriminating pins.+ expect(captured.plain).toContain('');++ // The image's slot carries its own source-fallback markdown.+ expect(captured.html).toContain('');+ // The bug: the whole `<Callout …>` source leaked into the image's slot.+ // A correct payload never serializes the container as source (the Callout+ // has no non-portable URL of its own), so its MDX open tag must not appear+ // in either raw or entity-encoded form.+ expect(captured.html).not.toMatch(/<Callout|<Callout/);+ });+});+test.describe('NFR Performance — walker post-pass under typical selections', () => {test('QA-040 50-element non-portable selection emits no clipboard-slow-op', async ({page,