diff --git a/packages/session-ui/component-tests/markdown.spec.ts b/packages/session-ui/component-tests/markdown.spec.ts index ab42613d7d6..7ac43062786 100644 --- a/packages/session-ui/component-tests/markdown.spec.ts +++ b/packages/session-ui/component-tests/markdown.spec.ts @@ -318,9 +318,8 @@ story("shows a stable GitHub mark without changing link text or other sites", as await expect(github).toHaveAttribute("href", "https://github.com/anomalyco/opencode/pull/540") await expect(github).toHaveText("#540") const mark = github.locator(".markdown-link-favicon[data-github]") - const glyph = mark.locator(".markdown-link-favicon-glyph") - expect(await glyph.evaluate((icon) => getComputedStyle(icon).width)).toBe("14px") - expect(await glyph.evaluate((icon) => getComputedStyle(icon).maskImage)).toContain("data:image/svg+xml") + expect(await mark.evaluate((icon) => getComputedStyle(icon, "::before").width)).toBe("14px") + expect(await mark.evaluate((icon) => getComputedStyle(icon, "::before").maskImage)).toContain("data:image/svg+xml") await expect(mark.locator("img")).toHaveCount(0) await expect(markdown.getByRole("link", { name: "GitHub" }).locator(".markdown-link-favicon[data-github]")).toHaveCount( 1, @@ -360,38 +359,38 @@ story("keeps favicon space stable across loading and failure without fetching pr const markdown = page.getByTestId("markdown-fixture").locator('[data-component="markdown"]') await expect(markdown).toHaveAttribute("data-markdown-ready", "") const docs = markdown.getByRole("link", { name: "docs" }) - const image = docs.locator(".markdown-link-favicon img") - const globe = (link: Locator) => link.locator(".markdown-link-favicon-glyph") + const mark = docs.locator(".markdown-link-favicon") + + const artwork = (mark: Locator) => + mark.evaluate((icon) => { + const style = getComputedStyle(icon, "::before") + + return { mask: style.maskImage, background: style.backgroundImage } + }) + await expect.poll(() => requested.some((url) => url.includes("developer.mozilla.org"))).toBe(true) - await expect(image).toHaveCSS("opacity", "0") - await expect(globe(docs)).toBeVisible() - expect((await docs.locator(".markdown-link-favicon").boundingBox())?.width).toBe(14) + await expect(mark).not.toHaveAttribute("data-loaded", "") + expect((await artwork(mark)).mask).toContain("data:image/svg+xml") + expect((await mark.boundingBox())?.width).toBe(14) // A late web font swap also changes the link width; measure after fonts settle. await page.evaluate(() => document.fonts.ready.then(() => undefined)) const width = await docs.evaluate((link) => link.getBoundingClientRect().width) release() - await expect(image).toHaveAttribute("data-loaded", "") - await expect(image).toHaveCSS("opacity", "1") - await expect(globe(docs)).toHaveCount(0) - await expect(image).not.toHaveAttribute("role", "button") - expect(await image.evaluate((favicon) => favicon.onclick)).toBeNull() + await expect(mark).toHaveAttribute("data-loaded", "") + expect(await artwork(mark)).toEqual({ + mask: "none", + background: expect.stringContaining("https://www.google.com/s2/favicons?domain=developer.mozilla.org"), + }) + await expect(mark.locator("img")).toBeHidden() expect(await docs.evaluate((link) => link.getBoundingClientRect().width)).toBe(width) - expect( - await image.evaluate((favicon) => { - const image = favicon.getBoundingClientRect() - const box = favicon.parentElement!.getBoundingClientRect() - return image.top + image.height / 2 - (box.top + box.height / 2) - }), - ).toBe(0) - - await expect( - markdown.getByRole("link", { name: "missing" }).locator(".markdown-link-favicon img"), - ).not.toHaveAttribute("data-loaded", "") - await expect(globe(markdown.getByRole("link", { name: "missing" }))).toBeVisible() + const missing = markdown.getByRole("link", { name: "missing" }).locator(".markdown-link-favicon") + await expect(missing).not.toHaveAttribute("data-loaded", "") + expect((await artwork(missing)).mask).toContain("data:image/svg+xml") await expect(markdown.getByRole("link", { name: "private" }).locator(".markdown-link-favicon img")).toHaveCount(0) await expect(markdown.getByRole("link", { name: "GitHub" }).locator(".markdown-link-favicon img")).toHaveCount(0) - expect(requested).toHaveLength(2) + // Routing disables the browser cache, so the loaded favicon's CSS artwork requests its URL again here. + expect(new Set(requested).size).toBe(2) expect(requested.every((url) => !url.includes("localhost") && !url.includes("github.com"))).toBe(true) }) @@ -443,6 +442,39 @@ story("keeps link icons on the same line as the start of their label", async ({ expect(result.long).toBeGreaterThan(1) }) +story("selects the whole paragraph on triple click across link icons", async ({ page }) => { + await page.route("https://www.google.com/s2/favicons?**", (route) => + route.fulfill({ status: 200, contentType: "image/png", body: png }), + ) + await page.evaluate(async (fixture) => { + const { mountMarkdown } = await import(fixture) + await mountMarkdown({ + text: "The release is running ([run 41750815701](https://github.com/Hona/strobe/actions/runs/41750815701)), see [MDN](https://developer.mozilla.org/docs) for more.", + cached: true, + }) + }, fixture) + const markdown = page.getByTestId("markdown-fixture").locator('[data-component="markdown"]') + await expect(markdown).toHaveAttribute("data-markdown-ready", "") + await expect(markdown.locator(".markdown-link-favicon img[data-loaded]")).toHaveCount(1) + + // Click the plain text after the last link, so the selection has to extend back across both icons. + const point = await markdown.evaluate((root: HTMLElement) => { + const text = root.querySelector("p")!.lastChild! + const range = document.createRange() + range.setStart(text, text.textContent!.indexOf("more")) + range.setEnd(text, text.textContent!.indexOf("more") + 4) + const box = range.getBoundingClientRect() + + return { x: box.x + box.width / 2, y: box.y + box.height / 2 } + }) + + await page.mouse.click(point.x, point.y, { clickCount: 3 }) + + expect(await page.evaluate(() => getSelection()?.toString().trim())).toBe( + "The release is running (run 41750815701), see MDN for more.", + ) +}) + story("scrolls fragment links to their heading inside the document's scroller", async ({ page }) => { await page.evaluate(async (fixture) => { const { mountMarkdown } = await import(fixture) diff --git a/packages/session-ui/src/components/markdown.css b/packages/session-ui/src/components/markdown.css index 6a487c00b38..51869fd5d52 100644 --- a/packages/session-ui/src/components/markdown.css +++ b/packages/session-ui/src/components/markdown.css @@ -146,14 +146,17 @@ } /* An atomic inline such as inline-block leaves a line break opportunity before the label, so the icon stays an - empty inline box with out-of-flow artwork, which keeps it on the line of the label's first word. */ + empty inline box with out-of-flow artwork, which keeps it on the line of the label's first word. + The artwork is a pseudo-element because an out-of-flow element is a block, and Chromium ends a triple-click + paragraph selection at a block. The hidden img only reports when the favicon has loaded. */ .markdown-link-favicon { position: relative; padding-inline-start: 14px; margin-inline-end: 4px; } - .markdown-link-favicon-glyph { + .markdown-link-favicon::before { + content: ""; position: absolute; inset: 0; height: 14px; @@ -162,24 +165,18 @@ mask: url("./markdown-globe.svg") center / contain no-repeat; } - .markdown-link-favicon[data-github] .markdown-link-favicon-glyph { + .markdown-link-favicon[data-github]::before { mask-image: url("./markdown-github.svg"); } - .markdown-link-favicon img { - position: absolute; - inset: 0; - display: block; - width: 14px; - height: 14px; - margin: auto 0; + .markdown-link-favicon[data-loaded]::before { border-radius: 2px; - cursor: inherit; - opacity: 0; + background: var(--markdown-link-favicon) center / contain no-repeat; + mask: none; } - .markdown-link-favicon img[data-loaded] { - opacity: 1; + .markdown-link-favicon img { + display: none; } a[data-local-link] { diff --git a/packages/session-ui/src/components/markdown.tsx b/packages/session-ui/src/components/markdown.tsx index de4eda35964..995f77e1f12 100644 --- a/packages/session-ui/src/components/markdown.tsx +++ b/packages/session-ui/src/components/markdown.tsx @@ -495,9 +495,6 @@ function markExternalLinkFavicons(root: HTMLDivElement) { favicon.className = "markdown-link-favicon" favicon.setAttribute("aria-hidden", "true") const host = url.hostname.toLowerCase() - const glyph = document.createElement("span") - glyph.className = "markdown-link-favicon-glyph" - favicon.appendChild(glyph) if (host === "github.com") favicon.dataset.github = "" else if ( @@ -508,6 +505,7 @@ function markExternalLinkFavicons(root: HTMLDivElement) { ) { const image = document.createElement("img") image.src = `https://www.google.com/s2/favicons?domain=${encodeURIComponent(url.host)}&sz=32` + favicon.style.setProperty("--markdown-link-favicon", `url("${image.src}")`) image.alt = "" image.setAttribute("data-markdown-favicon", "") image.width = 14 @@ -520,10 +518,10 @@ function markExternalLinkFavicons(root: HTMLDivElement) { }) } -// The globe stands in only until the favicon loads; removing it keeps transparent favicons from showing it through. +// The globe stands in only until the favicon loads, then the favicon replaces it so a transparent one hides it. function showFavicon(image: HTMLImageElement) { image.dataset.loaded = "" - image.parentElement?.querySelector(".markdown-link-favicon-glyph")?.remove() + image.parentElement?.setAttribute("data-loaded", "") } function setupExternalLinkFavicons(root: HTMLDivElement) {