// A click on a link in a table cell that took focus a moment earlier. The ProseMirror focus check runs before selectionchange, and the link source still opens at the clicked character. import assert from "node:assert/strict"; import { mockTauri, runSmoke, smokeUrl } from "../_harness/index.mjs"; await runSmoke("tables/linkClickAfterFocus", async ({ page }) => { await mockTauri(page, { commands: test => ({ ...test.workspace({ root: "C:\\tab-menu-workspace", files: { "a.md": "| Name | Value |\n| --- | --- |\n| One | Text |\n| Two | Before [Guide](docs/guide.md) after |\n" }, }), spelling_menu_install: () => null, }), }); await page.goto(smokeUrl); await page.locator('[data-tree-rel="a.md"]').click(); await page.locator('.writer-table').waitFor(); await page.getByRole('cell', { name:'B2 · Row 2, column 2', exact:true }).click(); await page.locator('.writer-cell-editor').waitFor(); // Force the order that load causes: the click moves the native selection, the focus check runs, and only then selectionchange reaches ProseMirror. // The focus check is the 20 ms timer in the focus handler of prosemirror-view. Its source is the only timer source that reads currentSelection. await page.evaluate(() => { const setTimeout = window.setTimeout, checks = []; const hold = event => event.stopImmediatePropagation(); window.setTimeout = (run, delay, ...rest) => { if (delay !== 20 || !String(run).includes('currentSelection')) return setTimeout(run, delay, ...rest); checks.push(run); return 0; }; document.addEventListener('selectionchange', hold, true); window.__runFocusCheck = () => { window.setTimeout = setTimeout; for (const check of checks) check(); document.removeEventListener('selectionchange', hold, true); return checks.length; }; }); await page.keyboard.press('Enter'); const linkCell = page.getByRole('cell', { name:'B3 · Row 3, column 2', exact:true }); await linkCell.locator('.writer-cell-editor').waitFor(); // Let the other timers of the new editor run. A flush after the click reads the native selection and hides the race. await page.waitForTimeout(100); const point = await linkCell.locator('a').evaluate(link => { const range = document.createRange(); range.setStart(link.firstChild, 1); range.collapse(true); const rect = range.getBoundingClientRect(); return { x: rect.x, y: rect.y + rect.height / 2 }; }); await page.mouse.click(point.x, point.y); assert.ok(await page.evaluate(() => window.__runFocusCheck()) > 0, 'The focus check ran after the click'); await page.locator('.is-source-editing .writer-cell-editor').waitFor(); assert.equal(await linkCell.locator('.writer-link-target').innerText(), 'docs/guide.md', 'The click opens the link source'); const caretPrefix = await page.evaluate(() => { const selection = getSelection(), range = document.createRange(); range.selectNodeContents(document.querySelector('.is-source-editing .writer-cell-editor')); range.setEnd(selection.anchorNode, selection.anchorOffset); return range.toString(); }); assert.equal(caretPrefix, 'Before [G', 'The caret stays at the clicked label character'); });