@zyro-sec/session-ui 0.0.0-stage → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +25 -0
- package/bunfig.toml +2 -0
- package/component-tests/basic-tool.fixture.tsx +46 -0
- package/component-tests/basic-tool.spec.ts +63 -0
- package/component-tests/file-media.fixture.tsx +39 -0
- package/component-tests/file-media.spec.ts +13 -0
- package/component-tests/file-tool.spec.ts +62 -0
- package/component-tests/file-viewer.fixture.tsx +109 -0
- package/component-tests/file-viewer.spec.ts +134 -0
- package/component-tests/file.spec.ts +33 -0
- package/component-tests/line-comment-v2.spec.ts +15 -0
- package/component-tests/markdown-code-wrap.spec.ts +94 -0
- package/component-tests/markdown-readiness.spec.ts +95 -0
- package/component-tests/markdown.fixture.tsx +104 -0
- package/component-tests/markdown.spec.ts +1564 -0
- package/component-tests/mermaid-preview.spec.ts +43 -0
- package/component-tests/patch-group.spec.ts +79 -0
- package/component-tests/read-group.spec.ts +72 -0
- package/component-tests/read-image.fixture.tsx +66 -0
- package/component-tests/read-image.spec.ts +154 -0
- package/component-tests/session-compaction.spec.ts +254 -0
- package/component-tests/session-lifecycle.spec.ts +190 -0
- package/component-tests/session-message-projection.spec.ts +639 -0
- package/component-tests/session-notice-projection.spec.ts +20 -0
- package/component-tests/session-review-comments.spec.ts +179 -0
- package/component-tests/session-shell.spec.ts +93 -0
- package/component-tests/session-timeline-mobile.spec.ts +201 -0
- package/component-tests/session-timeline-notices.spec.ts +38 -0
- package/component-tests/session-timeline-reasoning.spec.ts +107 -0
- package/component-tests/session-timeline.spec.ts +157 -0
- package/component-tests/session-tool-projection.spec.ts +323 -0
- package/component-tests/text-lifecycle.fixture.tsx +26 -0
- package/component-tests/text-lifecycle.spec.ts +42 -0
- package/component-tests/timeline-subagent-rows.spec.ts +33 -0
- package/component-tests/tool-disclosure.spec.ts +48 -0
- package/component-tests/tool-group-sticky.spec.ts +37 -0
- package/component-tests/tool-group.spec.ts +290 -0
- package/component-tests/tool-status-title.fixture.tsx +22 -0
- package/component-tests/tool-status-title.spec.ts +30 -0
- package/component-tests/user-message-quotes.spec.ts +111 -0
- package/package.json +91 -4
- package/playwright.components.config.ts +4 -0
- package/playwright.local.config.ts +2 -0
- package/src/actions.ts +54 -0
- package/src/components/apply-patch-file.ts +122 -0
- package/src/components/basic-tool.css +487 -0
- package/src/components/basic-tool.tsx +258 -0
- package/src/components/copy-machine.ts +78 -0
- package/src/components/copy.ts +85 -0
- package/src/components/diagram-preview.tsx +28 -0
- package/src/components/disclosure-machine.ts +188 -0
- package/src/components/disclosure.ts +129 -0
- package/src/components/dock-prompt.stories.tsx +73 -0
- package/src/components/dock-prompt.tsx +29 -0
- package/src/components/file-comment-selection.tsx +146 -0
- package/src/components/file-media-machine.ts +165 -0
- package/src/components/file-media.stories.tsx +112 -0
- package/src/components/file-media.tsx +323 -0
- package/src/components/file-search.tsx +73 -0
- package/src/components/file.css +59 -0
- package/src/components/file.tsx +1300 -0
- package/src/components/image-preview.tsx +35 -0
- package/src/components/line-comment-annotations.tsx +580 -0
- package/src/components/line-comment-machine.ts +262 -0
- package/src/components/line-comment-styles.ts +293 -0
- package/src/components/line-comment.stories.tsx +152 -0
- package/src/components/line-comment.tsx +448 -0
- package/src/components/link-segments.ts +20 -0
- package/src/components/linked-text.tsx +21 -0
- package/src/components/markdown-cache-machine.ts +257 -0
- package/src/components/markdown-cache.tsx +556 -0
- package/src/components/markdown-clipboard.ts +101 -0
- package/src/components/markdown-code-machine.ts +200 -0
- package/src/components/markdown-code-state.ts +22 -0
- package/src/components/markdown-fragment-links.ts +74 -0
- package/src/components/markdown-github.svg +3 -0
- package/src/components/markdown-globe.svg +5 -0
- package/src/components/markdown-image-machine.ts +170 -0
- package/src/components/markdown-image.ts +156 -0
- package/src/components/markdown-inline-code-kind.ts +39 -0
- package/src/components/markdown-mermaid.ts +18 -0
- package/src/components/markdown-phase-machine.ts +102 -0
- package/src/components/markdown-projection.ts +13 -0
- package/src/components/markdown-session-links.ts +183 -0
- package/src/components/markdown-solid.test.tsx +92 -0
- package/src/components/markdown-solid.tsx +113 -0
- package/src/components/markdown-stream.ts +95 -0
- package/src/components/markdown-worker-machine.ts +516 -0
- package/src/components/markdown-worker-protocol.ts +73 -0
- package/src/components/markdown-worker.ts +250 -0
- package/src/components/markdown.css +672 -0
- package/src/components/markdown.stories.tsx +199 -0
- package/src/components/markdown.tsx +1612 -0
- package/src/components/markdown.worker.ts +188 -0
- package/src/components/message-file.ts +102 -0
- package/src/components/message-nav.css +134 -0
- package/src/components/message-nav.tsx +159 -0
- package/src/components/message-part.css +2109 -0
- package/src/components/message-part.tsx +3 -0
- package/src/components/question-prompt-experimental.css +576 -0
- package/src/components/question-prompt.css +527 -0
- package/src/components/quote-highlight.css +59 -0
- package/src/components/review-focus-driver.ts +142 -0
- package/src/components/review-focus-machine.ts +95 -0
- package/src/components/session-compaction.css +270 -0
- package/src/components/session-diff.ts +358 -0
- package/src/components/session-error.tsx +41 -0
- package/src/components/session-retry.tsx +92 -0
- package/src/components/session-review-machine.ts +241 -0
- package/src/components/session-review.css +264 -0
- package/src/components/session-review.stories.tsx +232 -0
- package/src/components/session-review.tsx +733 -0
- package/src/components/session-turn.css +282 -0
- package/src/components/shell-submessage.css +24 -0
- package/src/components/timeline-separator.tsx +69 -0
- package/src/components/tool-count-label.css +57 -0
- package/src/components/tool-count-label.tsx +76 -0
- package/src/components/tool-count-summary.css +102 -0
- package/src/components/tool-count-summary.stories.tsx +99 -0
- package/src/components/tool-count-summary.tsx +44 -0
- package/src/components/tool-error-card.css +178 -0
- package/src/components/tool-error-card.stories.tsx +113 -0
- package/src/components/tool-error-card.tsx +202 -0
- package/src/components/tool-status-title-machine.ts +128 -0
- package/src/components/tool-status-title.css +89 -0
- package/src/components/tool-status-title.tsx +163 -0
- package/src/context/data.tsx +107 -0
- package/src/context/index.ts +1 -0
- package/src/context/markdown.tsx +129 -0
- package/src/context/sources.tsx +34 -0
- package/src/document.ts +8 -0
- package/src/file-presentation.ts +34 -0
- package/src/message/action-machine.ts +72 -0
- package/src/message/agent-label.ts +3 -0
- package/src/message/attachment-card.css +119 -0
- package/src/message/attachment-card.tsx +40 -0
- package/src/message/attachment-media-machine.ts +213 -0
- package/src/message/comment-card.tsx +155 -0
- package/src/message/compaction-entrance-machine.ts +93 -0
- package/src/message/current-message.tsx +157 -0
- package/src/message/current-tool-state.ts +161 -0
- package/src/message/message-content.tsx +1391 -0
- package/src/message/paced-text-machine.ts +73 -0
- package/src/message/prompt-message.stories.tsx +149 -0
- package/src/message/relative-time.ts +45 -0
- package/src/message/user-message-quote-parts.ts +16 -0
- package/src/message/user-message-quotes.stories.tsx +797 -0
- package/src/message/user-message-quotes.tsx +135 -0
- package/src/pierre/comment-hover-machine.ts +53 -0
- package/src/pierre/comment-hover.ts +131 -0
- package/src/pierre/comment-selection-machine.ts +257 -0
- package/src/pierre/commented-lines.ts +106 -0
- package/src/pierre/diff-selection.ts +91 -0
- package/src/pierre/file-find-machine.ts +323 -0
- package/src/pierre/file-find.ts +537 -0
- package/src/pierre/file-runtime.ts +42 -0
- package/src/pierre/file-selection.ts +140 -0
- package/src/pierre/index.ts +344 -0
- package/src/pierre/media.ts +140 -0
- package/src/pierre/render-machine.ts +381 -0
- package/src/pierre/selection-bridge.ts +82 -0
- package/src/pierre/selection-machine.ts +198 -0
- package/src/pierre/virtualizer.ts +114 -0
- package/src/pierre/worker.ts +46 -0
- package/src/sources/experiment.ts +59 -0
- package/src/sources/site-icon.tsx +53 -0
- package/src/sources/sources-chip.tsx +57 -0
- package/src/sources/sources-expand-machine.ts +101 -0
- package/src/sources/sources-panel.tsx +249 -0
- package/src/sources/sources-title.tsx +21 -0
- package/src/sources/sources.css +266 -0
- package/src/sources/sources.ts +141 -0
- package/src/storybook/current-session-fixtures.ts +1492 -0
- package/src/storybook/current-session-scenarios.ts +76 -0
- package/src/storybook/current-session-story.tsx +154 -0
- package/src/storybook/index.ts +43 -0
- package/src/styles/index.css +19 -0
- package/src/timeline/__snapshots__/group-labels.test.ts.snap +110 -0
- package/src/timeline/__snapshots__/placement.test.ts.snap +1041 -0
- package/src/timeline/code-blocks.stories.tsx +105 -0
- package/src/timeline/commentary.stories.tsx +70 -0
- package/src/timeline/commentary.ts +38 -0
- package/src/timeline/detail.ts +111 -0
- package/src/timeline/error-card.stories.tsx +134 -0
- package/src/timeline/error-spacing.stories.tsx +71 -0
- package/src/timeline/facts.ts +493 -0
- package/src/timeline/file-changes.stories.tsx +436 -0
- package/src/timeline/group-label-fixtures.ts +97 -0
- package/src/timeline/mermaid.stories.tsx +131 -0
- package/src/timeline/place.ts +280 -0
- package/src/timeline/placement-fixtures.ts +365 -0
- package/src/timeline/placement-reactivity.ts +133 -0
- package/src/timeline/projection.ts +1261 -0
- package/src/timeline/research-and-agents.stories.tsx +461 -0
- package/src/timeline/session-timeline-row.tsx +1675 -0
- package/src/timeline/session-timeline.tsx +68 -0
- package/src/timeline/steer-arrival-machine.ts +134 -0
- package/src/timeline/terminal-work.stories.tsx +464 -0
- package/src/timeline/timeline-row.stories.tsx +1069 -0
- package/src/timeline/timeline-row.ts +238 -0
- package/src/timeline/turn-phase-machine.ts +112 -0
- package/src/timeline/user-message-fold.stories.tsx +194 -0
- package/src/timeline/user-runs.ts +87 -0
- package/src/timeline-next/__snapshots__/placement.test.ts.snap +990 -0
- package/src/timeline-next/compaction.tsx +236 -0
- package/src/timeline-next/errors.ts +101 -0
- package/src/timeline-next/event-row.tsx +168 -0
- package/src/timeline-next/events.ts +103 -0
- package/src/timeline-next/index.ts +10 -0
- package/src/timeline-next/nested-rail.ts +125 -0
- package/src/timeline-next/projection.ts +1028 -0
- package/src/timeline-next/row.tsx +1728 -0
- package/src/timeline-next/rules.ts +89 -0
- package/src/timeline-next/separator.tsx +73 -0
- package/src/timeline-next/spacing.ts +127 -0
- package/src/timeline-next/status-line.tsx +41 -0
- package/src/timeline-next/status.ts +33 -0
- package/src/timeline-next/stories/compare-renderer.stories.tsx +190 -0
- package/src/timeline-next/stories/compare-sessions/.gitignore +3 -0
- package/src/timeline-next/stories/compare.stories.tsx +486 -0
- package/src/timeline-next/stories/playground.stories.tsx +297 -0
- package/src/timeline-next/stories/session-replay.ts +1156 -0
- package/src/timeline-next/stories/session-timeline.tsx +50 -0
- package/src/timeline-next/stories/subagent-status.stories.tsx +132 -0
- package/src/timeline-next/timeline-next.css +649 -0
- package/src/timeline-next/tools/__snapshots__/activity-label.test.ts.snap +39 -0
- package/src/timeline-next/tools/activity-label.ts +166 -0
- package/src/timeline-next/tools/assistant-content.tsx +84 -0
- package/src/timeline-next/tools/context-group.tsx +619 -0
- package/src/timeline-next/tools/error-card.tsx +147 -0
- package/src/timeline-next/tools/explore.tsx +119 -0
- package/src/timeline-next/tools/file-group.tsx +128 -0
- package/src/timeline-next/tools/patch.tsx +174 -0
- package/src/timeline-next/tools/shell.tsx +186 -0
- package/src/timeline-next/tools/thought.tsx +87 -0
- package/src/timeline-next/tools/tool-display.tsx +119 -0
- package/src/tools/ansi.ts +8 -0
- package/src/tools/context-group-label.ts +123 -0
- package/src/tools/file-runs.ts +35 -0
- package/src/tools/project-path.ts +18 -0
- package/src/tools/read-group.stories.tsx +159 -0
- package/src/tools/run-machine.ts +185 -0
- package/src/tools/shell-output-machine.ts +233 -0
- package/src/tools/shell-output.ts +91 -0
- package/src/tools/subagent-row-variants.stories.tsx +129 -0
- package/src/tools/tool-group.stories.tsx +345 -0
- package/src/tools/tool-renderer.tsx +2876 -0
- package/src/v2/components/line-comment-annotations-v2.tsx +215 -0
- package/src/v2/components/session-file-panel-v2.tsx +149 -0
- package/src/v2/components/session-progress-indicator-v2.css +5 -0
- package/src/v2/components/session-progress-indicator-v2.stories.tsx +86 -0
- package/src/v2/components/session-progress-indicator-v2.tsx +16 -0
- package/src/v2/components/session-review-empty-changes-v2.tsx +17 -0
- package/src/v2/components/session-review-empty-no-git-v2.tsx +28 -0
- package/src/v2/components/session-review-file-preview-v2-virtualize.ts +5 -0
- package/src/v2/components/session-review-file-preview-v2.tsx +266 -0
- package/src/v2/components/session-review-v2.css +653 -0
- package/src/v2/components/session-review-v2.tsx +422 -0
- package/src/v2/components/sidebar-presence-machine.ts +69 -0
- package/tsconfig.json +20 -0
- package/README.md +0 -3
|
@@ -0,0 +1,1564 @@
|
|
|
1
|
+
import type { Locator, Page } from "@playwright/test"
|
|
2
|
+
import { expect, sourceURL, story } from "../../stories/playwright/story"
|
|
3
|
+
|
|
4
|
+
const png = Buffer.from(
|
|
5
|
+
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+a4ioAAAAASUVORK5CYII=",
|
|
6
|
+
"base64",
|
|
7
|
+
)
|
|
8
|
+
|
|
9
|
+
const fixture = sourceURL(new URL("./markdown.fixture.tsx", import.meta.url))
|
|
10
|
+
|
|
11
|
+
story.beforeEach(async ({ mount }) => {
|
|
12
|
+
const root = await mount("components-markdown--complete-response")
|
|
13
|
+
await expect(root.locator('[data-component="markdown"]')).toHaveAttribute("data-markdown-ready", "")
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
story("renders small completed Markdown immediately without skipping sanitization", async ({ page }) => {
|
|
17
|
+
const result = await page.evaluate(async (fixture) => {
|
|
18
|
+
const { mountMarkdown } = await import(fixture)
|
|
19
|
+
await mountMarkdown({
|
|
20
|
+
text: '**Small response**\n\n<img src="missing" onerror="alert(1)"><script>alert(2)</script>',
|
|
21
|
+
})
|
|
22
|
+
const markdown = document.querySelector('[data-testid="markdown-fixture"] [data-component="markdown"]')!
|
|
23
|
+
|
|
24
|
+
return {
|
|
25
|
+
ready: markdown.hasAttribute("data-markdown-ready"),
|
|
26
|
+
bold: markdown.querySelector("strong")?.textContent,
|
|
27
|
+
unsafe: markdown.querySelectorAll("script, [onerror]").length,
|
|
28
|
+
}
|
|
29
|
+
}, fixture)
|
|
30
|
+
|
|
31
|
+
expect(result).toEqual({ ready: true, bold: "Small response", unsafe: 0 })
|
|
32
|
+
const harness = page.getByTestId("markdown-fixture")
|
|
33
|
+
await harness.getByLabel("Markdown text").fill("```ts\nconst value = 42\n```")
|
|
34
|
+
await expect(harness.locator('[data-component="markdown"]')).toHaveAttribute("data-markdown-ready", "")
|
|
35
|
+
await expect(harness.locator("pre code")).toHaveText("const value = 42")
|
|
36
|
+
await expect(harness.locator("pre.highlights")).toBeVisible()
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
story("keeps Markdown text selectable inside a container that disables selection", async ({ page }) => {
|
|
40
|
+
await page.evaluate(async (fixture) => {
|
|
41
|
+
document.body.style.userSelect = "none"
|
|
42
|
+
const { mountMarkdown } = await import(fixture)
|
|
43
|
+
await mountMarkdown({ text: "Selectable answer text" })
|
|
44
|
+
}, fixture)
|
|
45
|
+
|
|
46
|
+
await page.getByTestId("markdown-fixture").getByText("Selectable answer text").click({ clickCount: 3 })
|
|
47
|
+
expect(await page.evaluate(() => getSelection()?.toString().trim())).toBe("Selectable answer text")
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
story("sanitizes raw HTML while preserving supported Markdown markup", async ({ page }) => {
|
|
51
|
+
const result = await page.evaluate(async (fixture) => {
|
|
52
|
+
const { sanitizeMarkdown } = await import(fixture)
|
|
53
|
+
|
|
54
|
+
return [
|
|
55
|
+
"<p><strong>Safe</strong> <em>formatting</em> <code>const x = 1</code></p>",
|
|
56
|
+
'<script>alert(1)</script><style>body { display: none }</style><img src="safe.png" onerror="alert(2)"><a href="javascript:alert(3)">unsafe</a>',
|
|
57
|
+
'<a href="https://example.com" target="_blank" rel="nofollow">external</a><a href="/local">local</a>',
|
|
58
|
+
'<form id="location" name="document"><input name="cookie"></form>',
|
|
59
|
+
"<math><mrow><mi>x</mi><mo>+</mo><mn>1</mn></mrow></math>",
|
|
60
|
+
'<svg viewBox="0 0 10 10"><path d="M0 0L10 10" onload="alert(4)"></path><script>alert(5)</script></svg>',
|
|
61
|
+
].map(sanitizeMarkdown)
|
|
62
|
+
}, fixture)
|
|
63
|
+
|
|
64
|
+
expect(result).toEqual([
|
|
65
|
+
"<p><strong>Safe</strong> <em>formatting</em> <code>const x = 1</code></p>",
|
|
66
|
+
'<img data-local-image="safe.png"><a data-app-link="javascript:alert(3)">unsafe</a>',
|
|
67
|
+
'<a href="https://example.com" target="_blank" rel="nofollow noopener noreferrer" class="external-link">external</a><a data-local-link="/local" role="link" tabindex="0">local</a>',
|
|
68
|
+
'',
|
|
69
|
+
'<math data-markdown-contain=""><mrow data-markdown-contain=""><mi data-markdown-contain="">x</mi><mo data-markdown-contain="">+</mo><mn data-markdown-contain="">1</mn></mrow></math>',
|
|
70
|
+
'<svg viewBox="0 0 10 10" data-markdown-contain=""><path d="M0 0L10 10" data-markdown-contain=""></path></svg>',
|
|
71
|
+
])
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
story("authored HTML cannot supply application behavior attributes", async ({ page }) => {
|
|
75
|
+
const result = await page.evaluate(async (fixture) => {
|
|
76
|
+
const { sanitizeMarkdown } = await import(fixture)
|
|
77
|
+
const names = ["data-owns-keyboard", "data-owns-escape", "data-prevent-autofocus", "data-floating-overlay", "data-app-link", "data-local-link", "data-local-image", "data-slot", "data-component", "data-future-marker", "aria-owns", "aria-controls", "aria-modal", "aria-hidden", "role", "class", "tabindex", "contenteditable", "draggable", "inert", "hidden", "popover", "autofocus", "slot", "part", "exportparts", "for", "form", "formaction", "command", "commandfor", "accesskey"]
|
|
78
|
+
const box = document.createElement("div")
|
|
79
|
+
box.innerHTML = sanitizeMarkdown(`<a href="#sample" id="sample" name="legacy" ${names.map(n => `${n}="fixture"`).join(" ")}>sample</a><label for="sample">label</label>`)
|
|
80
|
+
const a = box.querySelector("a")!
|
|
81
|
+
|
|
82
|
+
return { retained: names.filter(n => a.hasAttribute(n)), id: a.id, name: a.name, labelFor: box.querySelector("label")?.htmlFor, custom: sanitizeMarkdown('<a is="fixture-link">custom label</a>') }
|
|
83
|
+
}, fixture)
|
|
84
|
+
|
|
85
|
+
expect(result).toEqual({ retained: [], id: "user-content-sample", name: "user-content-legacy", labelFor: undefined, custom: "custom label" })
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
story("limits authored presentation to text and keeps application link hit areas inline", async ({ page }) => {
|
|
89
|
+
const result = await page.evaluate(async (fixture) => {
|
|
90
|
+
const { sanitizeMarkdown } = await import(fixture)
|
|
91
|
+
const fragment = document.createElement("template")
|
|
92
|
+
fragment.innerHTML = sanitizeMarkdown([
|
|
93
|
+
'<p style="position:relative;transform:scale(2);pointer-events:none;color:teal;text-align:center">sample</p>',
|
|
94
|
+
'<span popover="manual">sample</span><button commandfor="unused" command="close" popovertarget="unused">sample</button><dialog>sample</dialog>',
|
|
95
|
+
'<a href="obsidian:fixture" style="font-weight:bold"><strong>Label</strong></a>',
|
|
96
|
+
'<a href="mailto:fixture@example.invalid"><div>Block label</div></a>',
|
|
97
|
+
].join(""))
|
|
98
|
+
const anchors = [...fragment.content.querySelectorAll("a")]
|
|
99
|
+
|
|
100
|
+
return {
|
|
101
|
+
style: fragment.content.querySelector("p")?.style.cssText,
|
|
102
|
+
active: fragment.content.querySelectorAll("[popover], [commandfor], [command], [popovertarget], dialog").length,
|
|
103
|
+
links: anchors.map(a => [a.dataset.appLink, a.hasAttribute("href"), a.hasAttribute("style"), a.textContent]),
|
|
104
|
+
}
|
|
105
|
+
}, fixture)
|
|
106
|
+
|
|
107
|
+
expect(result).toEqual({ style: "color: teal; text-align: center;", active: 0, links: [["obsidian:fixture", false, false, "Label"], [undefined, false, false, "Block label"]] })
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
story("restores layout from the math renderer, never from markup claiming to be KaTeX", async ({ page }) => {
|
|
111
|
+
await page.evaluate(async (fixture) => {
|
|
112
|
+
const { mountMarkdown } = await import(fixture)
|
|
113
|
+
await mountMarkdown({ text: 'Real: $\\frac{x}{y}$.\n\n<span id="0123456789abcdef0123456789abcdef" class="katex" data-markdown-renderer="math" style="position:relative;top:2px">Authored</span>' })
|
|
114
|
+
}, fixture)
|
|
115
|
+
const md = page.getByTestId("markdown-fixture").locator('[data-component="markdown"]')
|
|
116
|
+
await expect(md.locator('.katex-html [style*="top:"]')).not.toHaveCount(0)
|
|
117
|
+
await expect(md.locator('.katex-html').getByText("x", { exact: true })).toBeVisible()
|
|
118
|
+
const authored = md.getByText("Authored", { exact: true })
|
|
119
|
+
await expect(authored).not.toHaveAttribute("style")
|
|
120
|
+
await expect(authored).not.toHaveAttribute("data-markdown-renderer")
|
|
121
|
+
await expect(authored).toHaveAttribute("data-markdown-contain", "")
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
story("renders task-list boxes but never authored form controls or embedded documents", async ({ page }) => {
|
|
125
|
+
await page.evaluate(async url => {
|
|
126
|
+
const { mountMarkdown } = await import(url)
|
|
127
|
+
await mountMarkdown({ text: '- [x] Complete\n- [ ] Pending\n\n<form><button>Sample label</button><input type="checkbox" checked disabled></form>\n\n<figure><figcaption>Caption</figcaption></figure>' })
|
|
128
|
+
}, fixture)
|
|
129
|
+
const md = page.getByTestId("markdown-fixture").locator('[data-component="markdown"]')
|
|
130
|
+
await expect(md.getByText("Sample label")).toBeVisible()
|
|
131
|
+
await expect(md.locator("form,button")).toHaveCount(0)
|
|
132
|
+
await expect(md.locator("input")).toHaveCount(2)
|
|
133
|
+
await expect(md.locator('li input[disabled][checked]')).toHaveCount(1)
|
|
134
|
+
await expect(md.locator('li input[disabled]:not([checked])')).toHaveCount(1)
|
|
135
|
+
await expect(md.locator("figcaption")).toHaveText("Caption")
|
|
136
|
+
// A submitted form would navigate the window to its own address and reload the app.
|
|
137
|
+
await page.evaluate(() => Object.assign(window, { notReloaded: true }))
|
|
138
|
+
await md.getByText("Sample label").click()
|
|
139
|
+
await page.evaluate(() => new Promise((resolve) => setTimeout(resolve, 100)))
|
|
140
|
+
expect(await page.evaluate(() => "notReloaded" in window)).toBe(true)
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
story("keeps the document markup models write", async ({ page }) => {
|
|
144
|
+
await page.evaluate(async (url) => {
|
|
145
|
+
const { mountMarkdown } = await import(url)
|
|
146
|
+
await mountMarkdown({
|
|
147
|
+
text: [
|
|
148
|
+
"<details open><summary>Summary</summary>Press <kbd>Enter</kbd>: H<sub>2</sub>O, x<sup>2</sup>, <mark>marked</mark>, <abbr title=\"HyperText\">HTML</abbr><br>next</details>",
|
|
149
|
+
"<center>Centred</center>\n\n<hr>\n\n<figure><img width=\"64\" alt=\"Figure\" src=\"https://example.invalid/figure.png\"><figcaption>Caption</figcaption></figure>",
|
|
150
|
+
"<div align=\"center\"><span style=\"color:teal\">Teal</span></div>",
|
|
151
|
+
"<table><caption>Rows</caption><thead><tr><th>A</th></tr></thead><tbody><tr><td>1</td></tr></tbody><tfoot><tr><td>Sum</td></tr></tfoot></table>",
|
|
152
|
+
].join("\n\n"),
|
|
153
|
+
})
|
|
154
|
+
}, fixture)
|
|
155
|
+
const md = page.getByTestId("markdown-fixture").locator('[data-component="markdown"]')
|
|
156
|
+
await expect(md.locator("details[open] > summary")).toHaveText("Summary")
|
|
157
|
+
|
|
158
|
+
for (const tag of ["kbd", "sub", "sup", "mark", "abbr[title]", "br", "hr", "center", "figure", "figcaption", "img[width]", "caption", "thead", "tbody", "tfoot", "div[align]"])
|
|
159
|
+
await expect(md.locator(tag), tag).toHaveCount(1)
|
|
160
|
+
await expect(md.getByText("Teal", { exact: true })).toHaveCSS("color", "rgb(0, 128, 128)")
|
|
161
|
+
})
|
|
162
|
+
|
|
163
|
+
// What chat rendered before its allow-list: text-level, legacy and sectioning tags, and their harmless attributes.
|
|
164
|
+
story("keeps the text-level, legacy and sectioning markup chat rendered before", async ({ page }) => {
|
|
165
|
+
await page.evaluate(async (url) => {
|
|
166
|
+
const { mountMarkdown } = await import(url)
|
|
167
|
+
await mountMarkdown({
|
|
168
|
+
text: [
|
|
169
|
+
'<p><u>under</u> <ins datetime="2026-01-01" cite="https://example.invalid/why">inserted</ins> <cite>Work</cite> <q cite="https://example.invalid/q">quoted</q> <var>n</var> <samp>out</samp> <dfn>term</dfn> <time datetime="2026-01-01">New year</time> <data value="7">seven</data> <bdi>isolated</bdi> <bdo dir="rtl">override</bdo> long<wbr>word <ruby>漢<rp>(</rp><rt>kan</rt><rp>)</rp></ruby></p>',
|
|
170
|
+
'<p><acronym title="Hypertext">HTML</acronym> <big>big</big> <tt>mono</tt> <strike>struck</strike> <nobr>no break</nobr> <font color="teal">font</font></p>',
|
|
171
|
+
'<section><header>Header</header><article>Article</article><aside>Aside</aside><nav>Nav</nav><main>Main</main><search>Search</search><address>Address</address><footer>Footer</footer></section>',
|
|
172
|
+
].join("\n\n"),
|
|
173
|
+
})
|
|
174
|
+
}, fixture)
|
|
175
|
+
const md = page.getByTestId("markdown-fixture").locator('[data-component="markdown"]')
|
|
176
|
+
|
|
177
|
+
for (const tag of ["u", "ins[datetime][cite]", "cite", "q[cite]", "var", "samp", "dfn", "time[datetime]", "data[value]", "bdi", 'bdo[dir="rtl"]', "wbr", "ruby", "rt", "rp", "acronym[title]", "big", "tt", "strike", "nobr", "font[color]", "section", "header", "article", "aside", "nav", "main", "search", "address", "footer"])
|
|
178
|
+
await expect(md.locator(tag).first(), tag).toBeAttached()
|
|
179
|
+
await expect(md.getByText("font", { exact: true })).toHaveCSS("color", "rgb(0, 128, 128)")
|
|
180
|
+
})
|
|
181
|
+
|
|
182
|
+
// Media renders as it did before the allow-list, sized and painted inside its message.
|
|
183
|
+
story("keeps video and audio inside their message", async ({ page }) => {
|
|
184
|
+
await page.route("https://example.invalid/**", (route) => route.fulfill({ status: 404, body: "" }))
|
|
185
|
+
await page.evaluate(async (url) => {
|
|
186
|
+
const { mountMarkdown } = await import(url)
|
|
187
|
+
await mountMarkdown({
|
|
188
|
+
text: '<video controls width="5000" height="3000" poster="https://example.invalid/poster.png" onplay="alert(1)"><source src="https://example.invalid/clip.mp4" type="video/mp4"><track kind="captions" src="https://example.invalid/clip.vtt" srclang="en" label="English"></video>\n\n<audio controls loop src="https://example.invalid/clip.mp3" style="position:fixed;inset:0"></audio>',
|
|
189
|
+
})
|
|
190
|
+
}, fixture)
|
|
191
|
+
const md = page.getByTestId("markdown-fixture").locator('[data-component="markdown"]')
|
|
192
|
+
const video = md.locator("video[controls]")
|
|
193
|
+
await expect(video).toHaveAttribute("width", "2048")
|
|
194
|
+
await expect(video).not.toHaveAttribute("onplay")
|
|
195
|
+
await expect(md.locator('video > source[type="video/mp4"]')).toHaveAttribute("src", "https://example.invalid/clip.mp4")
|
|
196
|
+
await expect(md.locator('video > track[kind="captions"]')).toHaveAttribute("srclang", "en")
|
|
197
|
+
await expect(md.locator("audio[controls][loop]")).not.toHaveAttribute("style")
|
|
198
|
+
await expect(md).toHaveCSS("contain", "paint")
|
|
199
|
+
|
|
200
|
+
const fits = await md.evaluate((root) => {
|
|
201
|
+
const bounds = root.getBoundingClientRect()
|
|
202
|
+
|
|
203
|
+
return Array.from(root.querySelectorAll("video, audio")).map((node) => {
|
|
204
|
+
const box = node.getBoundingClientRect()
|
|
205
|
+
|
|
206
|
+
return box.left >= bounds.left - 1 && box.right <= bounds.right + 1
|
|
207
|
+
})
|
|
208
|
+
})
|
|
209
|
+
|
|
210
|
+
expect(fits).toEqual([true, true])
|
|
211
|
+
})
|
|
212
|
+
|
|
213
|
+
story("keeps safe presentation and labels on inline SVG, images and links", async ({ page }) => {
|
|
214
|
+
const result = await page.evaluate(async (fixture) => {
|
|
215
|
+
const { sanitizeMarkdown } = await import(fixture)
|
|
216
|
+
const box = document.createElement("div")
|
|
217
|
+
box.innerHTML = sanitizeMarkdown([
|
|
218
|
+
'<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" onload="alert(1)">',
|
|
219
|
+
'<path d="M5 12h14" fill="url(#paint)" stroke="#ff0000" opacity="0.5" stroke-dasharray="2 2"></path>',
|
|
220
|
+
'<use href="#paint"></use><image href="https://example.invalid/a.png"></image><foreignObject><p>embedded</p></foreignObject>',
|
|
221
|
+
'<a href="https://example.invalid/"><path d="M0 0"></path></a></svg>',
|
|
222
|
+
'<svg width="99999" height="10%"></svg>',
|
|
223
|
+
'<a href="https://example.invalid/" aria-label="Home" aria-describedby="x">home</a><img alt="" aria-label="Chart" src="https://example.invalid/b.png"><div aria-label="Region">text</div>',
|
|
224
|
+
].join(""))
|
|
225
|
+
const [icon, large] = box.querySelectorAll("svg")
|
|
226
|
+
const attributes = (node: Element | null) => Object.fromEntries(Array.from(node?.attributes ?? [], (a) => [a.name, a.value]))
|
|
227
|
+
|
|
228
|
+
return {
|
|
229
|
+
icon: attributes(icon),
|
|
230
|
+
path: attributes(icon.querySelector("path")),
|
|
231
|
+
children: Array.from(icon.children, (child) => child.localName),
|
|
232
|
+
large: attributes(large),
|
|
233
|
+
link: attributes(box.querySelector("a")),
|
|
234
|
+
image: box.querySelector("img")?.getAttribute("aria-label"),
|
|
235
|
+
region: box.querySelector("div")?.hasAttribute("aria-label"),
|
|
236
|
+
}
|
|
237
|
+
}, fixture)
|
|
238
|
+
|
|
239
|
+
expect(result).toEqual({
|
|
240
|
+
icon: { width: "24", height: "24", viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", "stroke-width": "2", "stroke-linecap": "round", "stroke-linejoin": "round", "aria-hidden": "true", "data-markdown-contain": "" },
|
|
241
|
+
path: { d: "M5 12h14", stroke: "#ff0000", opacity: "0.5", "stroke-dasharray": "2 2", "data-markdown-contain": "" },
|
|
242
|
+
children: ["path"],
|
|
243
|
+
large: { width: "2048", height: "10%", "data-markdown-contain": "" },
|
|
244
|
+
link: { href: "https://example.invalid/", "aria-label": "Home" },
|
|
245
|
+
image: "Chart",
|
|
246
|
+
region: false,
|
|
247
|
+
})
|
|
248
|
+
})
|
|
249
|
+
|
|
250
|
+
story("keeps a formula inside an application link label", async ({ page }) => {
|
|
251
|
+
await page.evaluate(async (url) => {
|
|
252
|
+
const { mountMarkdown } = await import(url)
|
|
253
|
+
await mountMarkdown({ text: "[See $x^2$ <b>bold</b>](obsidian://open?file=a)" })
|
|
254
|
+
}, fixture)
|
|
255
|
+
const link = page.getByTestId("markdown-fixture").locator("a[data-app-link]")
|
|
256
|
+
await expect(link).toHaveAttribute("data-app-link", "obsidian://open?file=a")
|
|
257
|
+
await expect(link.locator(".katex")).toHaveCount(1)
|
|
258
|
+
await expect(link.locator("b")).toHaveCount(0)
|
|
259
|
+
await expect(link).toContainText("bold")
|
|
260
|
+
})
|
|
261
|
+
|
|
262
|
+
story("a display formula in an application link label disarms the link", async ({ page }) => {
|
|
263
|
+
await page.evaluate(async (url) => {
|
|
264
|
+
const { mountMarkdown } = await import(url)
|
|
265
|
+
await mountMarkdown({
|
|
266
|
+
text: '<a href="obsidian://open?vault=a">$$\\rule{10em}{10em}$$</a> and <a href="obsidian://open?vault=b">$x^2$</a>',
|
|
267
|
+
})
|
|
268
|
+
}, fixture)
|
|
269
|
+
const md = page.getByTestId("markdown-fixture").locator('[data-component="markdown"]')
|
|
270
|
+
// Armed, the formula's box, up to 10 em high and a line wide, would be the link's hit area.
|
|
271
|
+
await expect(md.locator("a .katex-display")).toHaveCount(1)
|
|
272
|
+
await expect(md.locator("a[data-app-link]")).toHaveCount(1)
|
|
273
|
+
await expect(md.locator("a[data-app-link]")).toHaveAttribute("data-app-link", "obsidian://open?vault=b")
|
|
274
|
+
await expect(md.locator("a[data-app-link] .katex")).toHaveCount(1)
|
|
275
|
+
await expect(md.locator("[data-display]")).toHaveCount(0)
|
|
276
|
+
})
|
|
277
|
+
|
|
278
|
+
story("bounds author-sized math to its own line box", async ({ page }) => {
|
|
279
|
+
await page.evaluate(async (url) => {
|
|
280
|
+
const { mountMarkdown } = await import(url)
|
|
281
|
+
await mountMarkdown({ text: "Before $\\raisebox{-40em}{\\colorbox{red}{\\rule{60em}{30em}}}$ after.\n\nNext paragraph." })
|
|
282
|
+
}, fixture)
|
|
283
|
+
const md = page.getByTestId("markdown-fixture").locator('[data-component="markdown"]')
|
|
284
|
+
await expect(md.locator(".katex")).toHaveCount(1)
|
|
285
|
+
|
|
286
|
+
const box = await md.evaluate((root) => ({
|
|
287
|
+
widest: Math.max(...Array.from(root.querySelectorAll(".katex-html *"), (node) => node.getBoundingClientRect().width)),
|
|
288
|
+
em: parseFloat(getComputedStyle(root.querySelector(".katex")!).fontSize),
|
|
289
|
+
overflow: getComputedStyle(root.querySelector(".katex .katex-base")!).overflow,
|
|
290
|
+
}))
|
|
291
|
+
// KaTeX caps every authored size at maxSize (10em, plus the colour box's padding), and each line box clips the ink
|
|
292
|
+
// that offsets and negative margins push out.
|
|
293
|
+
|
|
294
|
+
expect(box.widest).toBeLessThan(box.em * 11)
|
|
295
|
+
expect(box.overflow).toBe("clip")
|
|
296
|
+
})
|
|
297
|
+
|
|
298
|
+
story("retains local and download link routing and bounds untrusted application metadata", async ({ page }) => {
|
|
299
|
+
const result = await page.evaluate(async (fixture) => {
|
|
300
|
+
const { sanitizeMarkdown } = await import(fixture)
|
|
301
|
+
|
|
302
|
+
return [
|
|
303
|
+
'<a href="file:///C:/workspace/a.ts">file</a>', '<a href="src/a.ts">relative</a>',
|
|
304
|
+
'<a download href="obsidian:fixture">download</a>', '<a href="opencode:fixture">deep link</a>',
|
|
305
|
+
'<a data-app-link="forged">text</a>', '<a href="DaTa:text/plain,test">data</a>',
|
|
306
|
+
`<a href="obsidian:${"a".repeat(2048)}">long</a>`, '<a href="obsidian:a<b">angle</a>',
|
|
307
|
+
'<a download="report" href="mailto:fixture@example.invalid?attach=fixture">mail</a>',
|
|
308
|
+
'<a download href="https://example.invalid/report.csv">web download</a>',
|
|
309
|
+
].map(html => {
|
|
310
|
+
const parent = document.createElement("div")
|
|
311
|
+
parent.innerHTML = sanitizeMarkdown(html)
|
|
312
|
+
const a = parent.querySelector("a")!
|
|
313
|
+
|
|
314
|
+
return [a.dataset.localLink, a.dataset.appLink, a.hasAttribute("href"), a.hasAttribute("download")]
|
|
315
|
+
})
|
|
316
|
+
}, fixture)
|
|
317
|
+
|
|
318
|
+
// Only a web link downloads. Another app's address loses `download` and is reviewed like every app link.
|
|
319
|
+
expect(result).toEqual([["C:/workspace/a.ts", undefined, false, false], ["src/a.ts", undefined, false, false],
|
|
320
|
+
[undefined, "obsidian:fixture", false, false], [undefined, "opencode:fixture", false, false], [undefined, undefined, false, false],
|
|
321
|
+
[undefined, "DaTa:text/plain,test", false, false], [undefined, undefined, false, false], [undefined, undefined, false, false],
|
|
322
|
+
[undefined, "mailto:fixture@example.invalid?attach=fixture", true, false], [undefined, undefined, true, true]])
|
|
323
|
+
})
|
|
324
|
+
|
|
325
|
+
story("keeps Markdown sanitization and link protections after Mermaid renders", async ({ page }) => {
|
|
326
|
+
const result = await page.evaluate(async (fixture) => {
|
|
327
|
+
const { renderMermaidSvg, sanitizeMarkdown } = await import(fixture)
|
|
328
|
+
|
|
329
|
+
const html =
|
|
330
|
+
'<a href="https://example.com" target="_blank" rel="nofollow">external</a><img src="safe.png" onerror="alert(1)">'
|
|
331
|
+
|
|
332
|
+
const before = sanitizeMarkdown(html)
|
|
333
|
+
const renders = []
|
|
334
|
+
|
|
335
|
+
for (const source of [
|
|
336
|
+
"flowchart LR\n A[Start] --> B[End]",
|
|
337
|
+
"sequenceDiagram\n Alice->>Bob: Hello",
|
|
338
|
+
'%%{init: {"flowchart": {"htmlLabels": true}}}%%\nflowchart LR\n A["<a href=\'https://example.com\' target=\'_blank\'>External</a>"] --> B[End]\n click A "https://example.com" _blank',
|
|
339
|
+
]) {
|
|
340
|
+
const svg = await renderMermaidSvg(source)
|
|
341
|
+
const document = new DOMParser().parseFromString(svg, "image/svg+xml")
|
|
342
|
+
renders.push({
|
|
343
|
+
root: document.documentElement.localName,
|
|
344
|
+
text: document.documentElement.textContent,
|
|
345
|
+
unsafe: document.querySelectorAll('script, [onerror], [onload], [href^="javascript:"]').length,
|
|
346
|
+
links: Array.from(document.querySelectorAll("a")).map((link) => ({
|
|
347
|
+
href: link.getAttribute("href"),
|
|
348
|
+
target: link.getAttribute("target"),
|
|
349
|
+
rel: link.getAttribute("rel"),
|
|
350
|
+
})),
|
|
351
|
+
markdown: sanitizeMarkdown(html),
|
|
352
|
+
})
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
return {
|
|
356
|
+
before,
|
|
357
|
+
renders,
|
|
358
|
+
invalid: await renderMermaidSvg("not a diagram"),
|
|
359
|
+
afterInvalid: sanitizeMarkdown(html),
|
|
360
|
+
}
|
|
361
|
+
}, fixture)
|
|
362
|
+
|
|
363
|
+
expect(result.before).toBe(
|
|
364
|
+
'<a href="https://example.com" target="_blank" rel="nofollow noopener noreferrer" class="external-link">external</a><img data-local-image="safe.png">',
|
|
365
|
+
)
|
|
366
|
+
expect(result.renders).toEqual([
|
|
367
|
+
{ root: "svg", text: expect.stringContaining("Start"), unsafe: 0, links: [], markdown: result.before },
|
|
368
|
+
{ root: "svg", text: expect.stringContaining("Hello"), unsafe: 0, links: [], markdown: result.before },
|
|
369
|
+
{
|
|
370
|
+
root: "svg",
|
|
371
|
+
text: expect.stringContaining("External"),
|
|
372
|
+
unsafe: 0,
|
|
373
|
+
links: [{ href: "https://example.com/", target: "_blank", rel: "noopener noreferrer" }],
|
|
374
|
+
markdown: result.before,
|
|
375
|
+
},
|
|
376
|
+
])
|
|
377
|
+
expect(result.invalid).toBeUndefined()
|
|
378
|
+
expect(result.afterInvalid).toBe(result.before)
|
|
379
|
+
})
|
|
380
|
+
|
|
381
|
+
for (const cached of [false, true]) {
|
|
382
|
+
story(`decorates known inline paths before insertion with cached Markdown ${cached}`, async ({ page }) => {
|
|
383
|
+
const initial = await page.evaluate(
|
|
384
|
+
async ({ fixture, cached }) => {
|
|
385
|
+
const { mountMarkdown } = await import(fixture)
|
|
386
|
+
|
|
387
|
+
return mountMarkdown({
|
|
388
|
+
text: "`src/file.ts` and `src/file.ts` and `missing.ts` and [`src/file.ts`](https://example.com)",
|
|
389
|
+
cached,
|
|
390
|
+
synchronousFiles: true,
|
|
391
|
+
})
|
|
392
|
+
},
|
|
393
|
+
{ fixture, cached },
|
|
394
|
+
)
|
|
395
|
+
|
|
396
|
+
expect(initial).toEqual([
|
|
397
|
+
{ text: "src/file.ts", kind: "path" },
|
|
398
|
+
{ text: "src/file.ts", kind: "path" },
|
|
399
|
+
{ text: "missing.ts", kind: null },
|
|
400
|
+
{ text: "src/file.ts", kind: null },
|
|
401
|
+
])
|
|
402
|
+
})
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
story("does not report completed Markdown ready until all inline path checks settle", async ({ page }) => {
|
|
406
|
+
const checks = await page.evaluateHandle(async (fixture) => {
|
|
407
|
+
const { mountMarkdown } = await import(fixture)
|
|
408
|
+
const existing = Promise.withResolvers<boolean>()
|
|
409
|
+
const missing = Promise.withResolvers<boolean>()
|
|
410
|
+
const requested: string[] = []
|
|
411
|
+
await mountMarkdown({
|
|
412
|
+
text: "`src/file.ts` and `src/file.ts` and `missing/file.ts`",
|
|
413
|
+
localFileExists: (path: string) => {
|
|
414
|
+
requested.push(path)
|
|
415
|
+
|
|
416
|
+
return path === "src/file.ts" ? existing.promise : missing.promise
|
|
417
|
+
},
|
|
418
|
+
})
|
|
419
|
+
|
|
420
|
+
return { requested, existing: () => existing.resolve(true), missing: () => missing.reject(new Error("Offline")) }
|
|
421
|
+
}, fixture)
|
|
422
|
+
|
|
423
|
+
const markdown = page.getByTestId("markdown-fixture").locator('[data-component="markdown"]')
|
|
424
|
+
await expect(markdown.locator("code")).toHaveCount(3)
|
|
425
|
+
expect(await checks.evaluate((state) => state.requested)).toEqual(["src/file.ts", "missing/file.ts"])
|
|
426
|
+
await expect(markdown).not.toHaveAttribute("data-markdown-ready")
|
|
427
|
+
await checks.evaluate((state) => state.existing())
|
|
428
|
+
await expect(markdown.locator('code[data-inline-code-kind="path"]')).toHaveCount(2)
|
|
429
|
+
await expect(markdown).not.toHaveAttribute("data-markdown-ready")
|
|
430
|
+
await checks.evaluate((state) => state.missing())
|
|
431
|
+
await expect(markdown).toHaveAttribute("data-markdown-ready", "")
|
|
432
|
+
await expect(markdown.getByText("missing/file.ts", { exact: true })).not.toHaveAttribute("data-inline-code-kind")
|
|
433
|
+
await checks.dispose()
|
|
434
|
+
})
|
|
435
|
+
|
|
436
|
+
story("ignores stale inline path results when completed Markdown is replaced", async ({ page }) => {
|
|
437
|
+
const checks = await page.evaluateHandle(async (fixture) => {
|
|
438
|
+
const { mountMarkdown } = await import(fixture)
|
|
439
|
+
const first = Promise.withResolvers<boolean>()
|
|
440
|
+
const next = Promise.withResolvers<boolean>()
|
|
441
|
+
const requested: string[] = []
|
|
442
|
+
await mountMarkdown({
|
|
443
|
+
text: "Before `src/file.ts`",
|
|
444
|
+
cached: true,
|
|
445
|
+
localFileExists: (path: string) => {
|
|
446
|
+
requested.push(path)
|
|
447
|
+
|
|
448
|
+
return requested.length === 1 ? first.promise : next.promise
|
|
449
|
+
},
|
|
450
|
+
})
|
|
451
|
+
|
|
452
|
+
return { requested, first: () => first.resolve(true), next: () => next.resolve(false) }
|
|
453
|
+
}, fixture)
|
|
454
|
+
|
|
455
|
+
const harness = page.getByTestId("markdown-fixture")
|
|
456
|
+
const markdown = harness.locator('[data-component="markdown"]')
|
|
457
|
+
await expect(markdown.locator("p")).toHaveText("Before src/file.ts")
|
|
458
|
+
await expect(markdown).not.toHaveAttribute("data-markdown-ready")
|
|
459
|
+
await harness.getByLabel("Markdown text").fill("Replacement `src/file.ts`")
|
|
460
|
+
await expect(markdown.locator("p")).toHaveText("Replacement src/file.ts")
|
|
461
|
+
expect(await checks.evaluate((state) => state.requested)).toEqual(["src/file.ts", "src/file.ts"])
|
|
462
|
+
await checks.evaluate((state) => state.first())
|
|
463
|
+
await expect(markdown).not.toHaveAttribute("data-markdown-ready")
|
|
464
|
+
await checks.evaluate((state) => state.next())
|
|
465
|
+
await expect(markdown).toHaveAttribute("data-markdown-ready", "")
|
|
466
|
+
await expect(markdown.locator('code[data-inline-code-kind="path"]')).toHaveCount(0)
|
|
467
|
+
await checks.dispose()
|
|
468
|
+
})
|
|
469
|
+
|
|
470
|
+
story("keeps streaming Markdown ready while a completed prefix waits for a path check", async ({ page }) => {
|
|
471
|
+
const checks = await page.evaluateHandle(async (fixture) => {
|
|
472
|
+
const { mountMarkdown } = await import(fixture)
|
|
473
|
+
const existing = Promise.withResolvers<boolean>()
|
|
474
|
+
const requested: string[] = []
|
|
475
|
+
await mountMarkdown({
|
|
476
|
+
text: "Completed `src/file.ts`.\n\nStreaming **answer**",
|
|
477
|
+
streaming: true,
|
|
478
|
+
localFileExists: (path: string) => {
|
|
479
|
+
requested.push(path)
|
|
480
|
+
|
|
481
|
+
return existing.promise
|
|
482
|
+
},
|
|
483
|
+
})
|
|
484
|
+
|
|
485
|
+
return { requested, resolve: () => existing.resolve(true) }
|
|
486
|
+
}, fixture)
|
|
487
|
+
|
|
488
|
+
const harness = page.getByTestId("markdown-fixture")
|
|
489
|
+
const markdown = harness.locator('[data-component="markdown"]')
|
|
490
|
+
await expect(markdown.locator("strong")).toHaveText("answer")
|
|
491
|
+
await expect(markdown).toHaveAttribute("data-markdown-ready", "")
|
|
492
|
+
expect(await checks.evaluate((state) => state.requested)).toEqual(["src/file.ts"])
|
|
493
|
+
await harness.getByLabel("Streaming").uncheck()
|
|
494
|
+
await expect(markdown).not.toHaveAttribute("data-markdown-ready")
|
|
495
|
+
await checks.evaluate((state) => state.resolve())
|
|
496
|
+
await expect(markdown).toHaveAttribute("data-markdown-ready", "")
|
|
497
|
+
await expect(markdown.locator('code[data-inline-code-kind="path"]')).toHaveText("src/file.ts")
|
|
498
|
+
await checks.dispose()
|
|
499
|
+
})
|
|
500
|
+
|
|
501
|
+
story("mounts cached completed Markdown with sanitized HTML and decorations", async ({ page }) => {
|
|
502
|
+
await page.evaluate(
|
|
503
|
+
async ({ fixture, text }) => {
|
|
504
|
+
const { mountMarkdown } = await import(fixture)
|
|
505
|
+
await mountMarkdown({ text, cached: true })
|
|
506
|
+
},
|
|
507
|
+
{
|
|
508
|
+
fixture,
|
|
509
|
+
text: [
|
|
510
|
+
"# Completed response",
|
|
511
|
+
"`src/file.ts` and `missing.ts` and `https://example.com/docs` and [link](https://example.com)",
|
|
512
|
+
'<img src="missing" onerror="alert(1)"><script>alert(2)</script><a href="javascript:alert(3)">unsafe</a>',
|
|
513
|
+
"```ts\nconst answer = 42\n```",
|
|
514
|
+
].join("\n\n"),
|
|
515
|
+
},
|
|
516
|
+
)
|
|
517
|
+
const harness = page.getByTestId("markdown-fixture")
|
|
518
|
+
const markdown = harness.locator('[data-component="markdown"]')
|
|
519
|
+
await expect(markdown).toHaveAttribute("data-markdown-ready", "")
|
|
520
|
+
await expect(markdown.getByRole("heading")).toHaveText("Completed response")
|
|
521
|
+
await expect(markdown.locator("script, [onerror], [href^='javascript:']")).toHaveCount(0)
|
|
522
|
+
await expect(markdown.locator('code[data-inline-code-kind="path"]')).toHaveText("src/file.ts")
|
|
523
|
+
await expect(markdown.locator('code[data-inline-code-kind="path"]')).toHaveCSS(
|
|
524
|
+
"color",
|
|
525
|
+
await resolvedColor(page, "--v2-text-text-code-path"),
|
|
526
|
+
)
|
|
527
|
+
await expect(markdown.locator('code[data-inline-code-kind="path"]')).toHaveCSS("background-color", "rgba(0, 0, 0, 0)")
|
|
528
|
+
await expect(markdown.getByRole("link", { name: "https://example.com/docs" })).toHaveAttribute("target", "_blank")
|
|
529
|
+
await expect(markdown.getByRole("link", { name: "https://example.com/docs" })).toHaveAttribute(
|
|
530
|
+
"rel",
|
|
531
|
+
"noopener noreferrer",
|
|
532
|
+
)
|
|
533
|
+
await expect(markdown.locator("pre code")).toContainText("const answer = 42")
|
|
534
|
+
await expect(markdown.getByRole("button", { name: "Copy", exact: true })).toBeVisible()
|
|
535
|
+
await expect(markdown.locator("[data-markdown-word]")).toHaveCount(0)
|
|
536
|
+
|
|
537
|
+
await harness.getByLabel("Markdown text").fill("## Replacement\n\n`new/file.ts`")
|
|
538
|
+
await expect(markdown.getByRole("heading")).toHaveText("Replacement")
|
|
539
|
+
await expect(markdown).toHaveAttribute("data-markdown-ready", "")
|
|
540
|
+
await expect(markdown.locator("pre, h1, a")).toHaveCount(0)
|
|
541
|
+
await expect(markdown.locator('code[data-inline-code-kind="path"]')).toHaveText("new/file.ts")
|
|
542
|
+
await harness.getByRole("button", { name: "Toggle Markdown" }).click()
|
|
543
|
+
await expect(markdown).toHaveCount(0)
|
|
544
|
+
await harness.getByRole("button", { name: "Toggle Markdown" }).click()
|
|
545
|
+
await expect(markdown.getByRole("heading")).toHaveText("Replacement")
|
|
546
|
+
await expect(markdown).toHaveAttribute("data-markdown-ready", "")
|
|
547
|
+
await harness.getByLabel("Markdown text").fill("")
|
|
548
|
+
await expect(markdown).toBeEmpty()
|
|
549
|
+
await expect(markdown).toHaveAttribute("data-markdown-ready", "")
|
|
550
|
+
})
|
|
551
|
+
|
|
552
|
+
story("shows a stable GitHub mark without changing link text or other sites", async ({ page }) => {
|
|
553
|
+
await page.evaluate(async (fixture) => {
|
|
554
|
+
const { mountMarkdown } = await import(fixture)
|
|
555
|
+
await mountMarkdown({
|
|
556
|
+
text: [
|
|
557
|
+
"[#540](https://github.com/anomalyco/opencode/pull/540)",
|
|
558
|
+
"[GitHub](https://github.com)",
|
|
559
|
+
"[other site](https://example.com/docs)",
|
|
560
|
+
"[lookalike](https://github.com.evil.example/pull/540)",
|
|
561
|
+
].join(" · "),
|
|
562
|
+
cached: true,
|
|
563
|
+
})
|
|
564
|
+
}, fixture)
|
|
565
|
+
|
|
566
|
+
const markdown = page.getByTestId("markdown-fixture").locator('[data-component="markdown"]')
|
|
567
|
+
await expect(markdown).toHaveAttribute("data-markdown-ready", "")
|
|
568
|
+
const github = markdown.getByRole("link", { name: "#540" })
|
|
569
|
+
await expect(github).toHaveAttribute("href", "https://github.com/anomalyco/opencode/pull/540")
|
|
570
|
+
await expect(github).toHaveText("#540")
|
|
571
|
+
const mark = github.locator(".markdown-link-favicon[data-github]")
|
|
572
|
+
expect(await mark.evaluate((icon) => getComputedStyle(icon, "::before").width)).toBe("14px")
|
|
573
|
+
expect(await mark.evaluate((icon) => getComputedStyle(icon, "::before").maskImage)).toContain("data:image/svg+xml")
|
|
574
|
+
await expect(mark.locator("img")).toHaveCount(0)
|
|
575
|
+
await expect(
|
|
576
|
+
markdown.getByRole("link", { name: "GitHub" }).locator(".markdown-link-favicon[data-github]"),
|
|
577
|
+
).toHaveCount(1)
|
|
578
|
+
|
|
579
|
+
for (const name of ["other site", "lookalike"]) {
|
|
580
|
+
await expect(markdown.getByRole("link", { name }).locator(".markdown-link-favicon[data-github]")).toHaveCount(0)
|
|
581
|
+
}
|
|
582
|
+
})
|
|
583
|
+
|
|
584
|
+
for (const mode of ["cached", "streamed"] as const) {
|
|
585
|
+
story(`keeps the whole label of a link that contains inline code (${mode})`, async ({ page }) => {
|
|
586
|
+
await page.evaluate(
|
|
587
|
+
async ({ fixture, mode }) => {
|
|
588
|
+
const { mountMarkdown } = await import(fixture)
|
|
589
|
+
await mountMarkdown({
|
|
590
|
+
text: [
|
|
591
|
+
"Console [removes `providerCost` from managed standard and Go events](https://github.com/anomalyco/opencode/pull/1) **before** [writing them to the log queue](https://github.com/anomalyco/opencode/pull/2).",
|
|
592
|
+
"[`providerCost`](https://github.com/anomalyco/opencode/pull/3) and [`https://example.com/code`](https://example.com/target)",
|
|
593
|
+
].join("\n\n"),
|
|
594
|
+
cached: mode === "cached",
|
|
595
|
+
streaming: mode === "streamed",
|
|
596
|
+
})
|
|
597
|
+
},
|
|
598
|
+
{ fixture, mode },
|
|
599
|
+
)
|
|
600
|
+
|
|
601
|
+
const harness = page.getByTestId("markdown-fixture")
|
|
602
|
+
const markdown = harness.locator('[data-component="markdown"]')
|
|
603
|
+
|
|
604
|
+
if (mode === "streamed") await harness.getByLabel("Streaming").uncheck()
|
|
605
|
+
await expect(markdown).toHaveAttribute("data-markdown-ready", "")
|
|
606
|
+
|
|
607
|
+
const mixed = markdown.getByRole("link", { name: "removes providerCost from managed standard and Go events" })
|
|
608
|
+
await expect(mixed).toHaveAttribute("href", "https://github.com/anomalyco/opencode/pull/1")
|
|
609
|
+
await expect(mixed.locator("code")).toHaveText("providerCost")
|
|
610
|
+
await expect(mixed.locator(".markdown-link-favicon[data-github]")).toHaveCount(1)
|
|
611
|
+
await expect(markdown.getByRole("link", { name: "writing them to the log queue" })).toHaveAttribute(
|
|
612
|
+
"href",
|
|
613
|
+
"https://github.com/anomalyco/opencode/pull/2",
|
|
614
|
+
)
|
|
615
|
+
await expect(markdown.locator("p").first()).toHaveText(
|
|
616
|
+
"Console removes providerCost from managed standard and Go events before writing them to the log queue.",
|
|
617
|
+
)
|
|
618
|
+
|
|
619
|
+
const code = markdown.getByRole("link", { name: "providerCost", exact: true })
|
|
620
|
+
await expect(code).toHaveAttribute("href", "https://github.com/anomalyco/opencode/pull/3")
|
|
621
|
+
await expect(code.locator("code")).toHaveText("providerCost")
|
|
622
|
+
await expect(markdown.getByRole("link", { name: "https://example.com/code" })).toHaveAttribute(
|
|
623
|
+
"href",
|
|
624
|
+
"https://example.com/target",
|
|
625
|
+
)
|
|
626
|
+
})
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
for (const theme of ["light", "dark"]) {
|
|
630
|
+
story(`styles inline code inside a link like the link label in ${theme}`, async ({ mount, page }) => {
|
|
631
|
+
await mount("components-markdown--complete-response", { globals: { theme } })
|
|
632
|
+
await expect(page.locator("html")).toHaveClass(new RegExp(theme))
|
|
633
|
+
await page.evaluate(async (fixture) => {
|
|
634
|
+
const { mountMarkdown } = await import(fixture)
|
|
635
|
+
await mountMarkdown({
|
|
636
|
+
text: "[removes `providerCost` from events](https://github.com/anomalyco/opencode/pull/1) and `plainCode`",
|
|
637
|
+
cached: true,
|
|
638
|
+
})
|
|
639
|
+
}, fixture)
|
|
640
|
+
|
|
641
|
+
const markdown = page.getByTestId("markdown-fixture").locator('[data-component="markdown"]')
|
|
642
|
+
await expect(markdown).toHaveAttribute("data-markdown-ready", "")
|
|
643
|
+
const link = markdown.getByRole("link", { name: "removes providerCost from events" })
|
|
644
|
+
const code = link.locator("code")
|
|
645
|
+
|
|
646
|
+
const style = () =>
|
|
647
|
+
code.evaluate((code) => {
|
|
648
|
+
const style = getComputedStyle(code)
|
|
649
|
+
|
|
650
|
+
return {
|
|
651
|
+
color: style.color,
|
|
652
|
+
background: style.backgroundColor,
|
|
653
|
+
padding: style.padding,
|
|
654
|
+
font: style.fontFamily,
|
|
655
|
+
}
|
|
656
|
+
})
|
|
657
|
+
|
|
658
|
+
const plain = await markdown.locator("code", { hasText: "plainCode" }).evaluate((code) => {
|
|
659
|
+
const style = getComputedStyle(code)
|
|
660
|
+
|
|
661
|
+
return { color: style.color, backgroundColor: style.backgroundColor, fontFamily: style.fontFamily }
|
|
662
|
+
})
|
|
663
|
+
|
|
664
|
+
expect(await style()).toEqual({
|
|
665
|
+
color: await resolvedColor(page, "--v2-text-text-accent"),
|
|
666
|
+
background: "rgba(0, 0, 0, 0)",
|
|
667
|
+
padding: "0px",
|
|
668
|
+
font: plain.fontFamily,
|
|
669
|
+
})
|
|
670
|
+
expect(plain.backgroundColor).not.toBe("rgba(0, 0, 0, 0)")
|
|
671
|
+
expect(plain.color).not.toBe(await resolvedColor(page, "--v2-text-text-accent"))
|
|
672
|
+
|
|
673
|
+
await link.hover()
|
|
674
|
+
await expect(link).toHaveCSS("text-decoration-line", "underline")
|
|
675
|
+
const hover = await resolvedColor(page, "--v2-text-text-accent-hover")
|
|
676
|
+
await expect.poll(async () => (await style()).color).toBe(hover)
|
|
677
|
+
})
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
story("keeps favicon space stable across loading and failure without fetching private hosts", async ({ page }) => {
|
|
681
|
+
const requested: string[] = []
|
|
682
|
+
let release: () => void = () => undefined
|
|
683
|
+
const loading = new Promise<void>((resolve) => (release = resolve))
|
|
684
|
+
await page.route("https://www.google.com/s2/favicons?**", async (route) => {
|
|
685
|
+
requested.push(route.request().url())
|
|
686
|
+
|
|
687
|
+
if (route.request().url().includes("developer.mozilla.org")) await loading
|
|
688
|
+
|
|
689
|
+
if (route.request().url().includes("broken.example.org")) return route.abort()
|
|
690
|
+
await route.fulfill({ status: 200, contentType: "image/png", body: png })
|
|
691
|
+
})
|
|
692
|
+
|
|
693
|
+
await page.evaluate(async (fixture) => {
|
|
694
|
+
const { mountMarkdown } = await import(fixture)
|
|
695
|
+
await mountMarkdown({
|
|
696
|
+
text: [
|
|
697
|
+
"[docs](https://developer.mozilla.org/docs)",
|
|
698
|
+
"[missing](https://broken.example.org/docs)",
|
|
699
|
+
"[private](http://localhost:8080/docs)",
|
|
700
|
+
"[GitHub](https://github.com/anomalyco/opencode)",
|
|
701
|
+
].join(" · "),
|
|
702
|
+
cached: true,
|
|
703
|
+
})
|
|
704
|
+
}, fixture)
|
|
705
|
+
|
|
706
|
+
const markdown = page.getByTestId("markdown-fixture").locator('[data-component="markdown"]')
|
|
707
|
+
await expect(markdown).toHaveAttribute("data-markdown-ready", "")
|
|
708
|
+
const docs = markdown.getByRole("link", { name: "docs" })
|
|
709
|
+
const mark = docs.locator(".markdown-link-favicon")
|
|
710
|
+
|
|
711
|
+
const artwork = (mark: Locator) =>
|
|
712
|
+
mark.evaluate((icon) => {
|
|
713
|
+
const style = getComputedStyle(icon, "::before")
|
|
714
|
+
|
|
715
|
+
return { mask: style.maskImage, background: style.backgroundImage }
|
|
716
|
+
})
|
|
717
|
+
|
|
718
|
+
await expect.poll(() => requested.some((url) => url.includes("developer.mozilla.org"))).toBe(true)
|
|
719
|
+
await expect(mark).not.toHaveAttribute("data-loaded", "")
|
|
720
|
+
expect((await artwork(mark)).mask).toContain("data:image/svg+xml")
|
|
721
|
+
expect((await mark.boundingBox())?.width).toBe(14)
|
|
722
|
+
// A late web font swap also changes the link width; measure after fonts settle.
|
|
723
|
+
await page.evaluate(() => document.fonts.ready.then(() => undefined))
|
|
724
|
+
const width = await docs.evaluate((link) => link.getBoundingClientRect().width)
|
|
725
|
+
release()
|
|
726
|
+
await expect(mark).toHaveAttribute("data-loaded", "")
|
|
727
|
+
expect(await artwork(mark)).toEqual({
|
|
728
|
+
mask: "none",
|
|
729
|
+
background: expect.stringContaining("https://www.google.com/s2/favicons?domain=developer.mozilla.org"),
|
|
730
|
+
})
|
|
731
|
+
await expect(mark.locator("img")).toBeHidden()
|
|
732
|
+
expect(await docs.evaluate((link) => link.getBoundingClientRect().width)).toBe(width)
|
|
733
|
+
|
|
734
|
+
const missing = markdown.getByRole("link", { name: "missing" }).locator(".markdown-link-favicon")
|
|
735
|
+
await expect(missing).not.toHaveAttribute("data-loaded", "")
|
|
736
|
+
expect((await artwork(missing)).mask).toContain("data:image/svg+xml")
|
|
737
|
+
await expect(markdown.getByRole("link", { name: "private" }).locator(".markdown-link-favicon img")).toHaveCount(0)
|
|
738
|
+
await expect(markdown.getByRole("link", { name: "GitHub" }).locator(".markdown-link-favicon img")).toHaveCount(0)
|
|
739
|
+
|
|
740
|
+
// Routing disables the browser cache, so a loaded favicon's CSS artwork requests its URL again here. That includes
|
|
741
|
+
// the story's own example.com link, whose favicon can load before this route exists and its artwork after.
|
|
742
|
+
const domains = new Set(requested.map((url) => new URL(url).searchParams.get("domain")))
|
|
743
|
+
domains.delete("example.com")
|
|
744
|
+
|
|
745
|
+
expect(domains).toEqual(new Set(["developer.mozilla.org", "broken.example.org"]))
|
|
746
|
+
expect(requested.every((url) => !url.includes("localhost") && !url.includes("github.com"))).toBe(true)
|
|
747
|
+
})
|
|
748
|
+
|
|
749
|
+
story("keeps link icons on the same line as the start of their label", async ({ page }) => {
|
|
750
|
+
await page.route("https://www.google.com/s2/favicons?**", (route) =>
|
|
751
|
+
route.fulfill({ status: 200, contentType: "image/png", body: png }),
|
|
752
|
+
)
|
|
753
|
+
await page.evaluate(async (fixture) => {
|
|
754
|
+
const { mountMarkdown } = await import(fixture)
|
|
755
|
+
await mountMarkdown({
|
|
756
|
+
text: [
|
|
757
|
+
"Read the [MDN reference](https://developer.mozilla.org/docs) and",
|
|
758
|
+
"[#540](https://github.com/anomalyco/opencode/pull/540) then",
|
|
759
|
+
"https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/white-space/long-path",
|
|
760
|
+
].join(" "),
|
|
761
|
+
cached: true,
|
|
762
|
+
})
|
|
763
|
+
}, fixture)
|
|
764
|
+
const markdown = page.getByTestId("markdown-fixture").locator('[data-component="markdown"]')
|
|
765
|
+
await expect(markdown).toHaveAttribute("data-markdown-ready", "")
|
|
766
|
+
await expect(markdown.locator(".markdown-link-favicon img")).toHaveCount(2)
|
|
767
|
+
await page.evaluate(() => document.fonts.ready.then(() => undefined))
|
|
768
|
+
|
|
769
|
+
const result = await markdown.evaluate((root: HTMLElement) => {
|
|
770
|
+
const links = Array.from(root.querySelectorAll<HTMLAnchorElement>("a.external-link"))
|
|
771
|
+
|
|
772
|
+
// An icon is orphaned when the link's first line fragment, which holds the icon, sits above the label's first glyph.
|
|
773
|
+
const orphaned = (link: HTMLAnchorElement) => {
|
|
774
|
+
const text = document.createTreeWalker(link, NodeFilter.SHOW_TEXT).nextNode()!
|
|
775
|
+
const range = document.createRange()
|
|
776
|
+
range.setStart(text, 0)
|
|
777
|
+
range.setEnd(text, 1)
|
|
778
|
+
|
|
779
|
+
return range.getBoundingClientRect().top - link.getClientRects()[0].top > 1
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
const widths = Array.from({ length: 400 }, (_, index) => 120 + index).filter((width) => {
|
|
783
|
+
root.style.width = `${width}px`
|
|
784
|
+
|
|
785
|
+
return links.some(orphaned)
|
|
786
|
+
})
|
|
787
|
+
|
|
788
|
+
root.style.width = "160px"
|
|
789
|
+
|
|
790
|
+
return { widths, long: links[2].getClientRects().length }
|
|
791
|
+
})
|
|
792
|
+
|
|
793
|
+
expect(result.widths).toEqual([])
|
|
794
|
+
expect(result.long).toBeGreaterThan(1)
|
|
795
|
+
})
|
|
796
|
+
|
|
797
|
+
story("selects the whole paragraph on triple click across link icons", async ({ page }) => {
|
|
798
|
+
await page.route("https://www.google.com/s2/favicons?**", (route) =>
|
|
799
|
+
route.fulfill({ status: 200, contentType: "image/png", body: png }),
|
|
800
|
+
)
|
|
801
|
+
await page.evaluate(async (fixture) => {
|
|
802
|
+
const { mountMarkdown } = await import(fixture)
|
|
803
|
+
await mountMarkdown({
|
|
804
|
+
text: "The release is running ([run 41750815701](https://github.com/anomalyco/strobe/actions/runs/41750815701)), see [MDN](https://developer.mozilla.org/docs) for more.",
|
|
805
|
+
cached: true,
|
|
806
|
+
})
|
|
807
|
+
}, fixture)
|
|
808
|
+
const markdown = page.getByTestId("markdown-fixture").locator('[data-component="markdown"]')
|
|
809
|
+
await expect(markdown).toHaveAttribute("data-markdown-ready", "")
|
|
810
|
+
await expect(markdown.locator(".markdown-link-favicon img[data-loaded]")).toHaveCount(1)
|
|
811
|
+
|
|
812
|
+
// Click the plain text after the last link, so the selection has to extend back across both icons.
|
|
813
|
+
const point = await markdown.evaluate((root: HTMLElement) => {
|
|
814
|
+
const text = root.querySelector("p")!.lastChild!
|
|
815
|
+
const range = document.createRange()
|
|
816
|
+
range.setStart(text, text.textContent!.indexOf("more"))
|
|
817
|
+
range.setEnd(text, text.textContent!.indexOf("more") + 4)
|
|
818
|
+
const box = range.getBoundingClientRect()
|
|
819
|
+
|
|
820
|
+
return { x: box.x + box.width / 2, y: box.y + box.height / 2 }
|
|
821
|
+
})
|
|
822
|
+
|
|
823
|
+
await page.mouse.click(point.x, point.y, { clickCount: 3 })
|
|
824
|
+
|
|
825
|
+
expect(await page.evaluate(() => getSelection()?.toString().trim())).toBe(
|
|
826
|
+
"The release is running (run 41750815701), see MDN for more.",
|
|
827
|
+
)
|
|
828
|
+
})
|
|
829
|
+
|
|
830
|
+
story("scrolls fragment links to their heading inside the document's scroller", async ({ page }) => {
|
|
831
|
+
await page.evaluate(async (fixture) => {
|
|
832
|
+
const { mountMarkdown } = await import(fixture)
|
|
833
|
+
const filler = Array.from({ length: 40 }, (_, index) => `Paragraph ${index}`).join("\n\n")
|
|
834
|
+
await mountMarkdown({
|
|
835
|
+
text: `See [Host services](#host-services) and [the second usage](#usage-1).\n\n## Usage\n\n${filler}\n\n## Host services\n\n${filler}\n\n## Usage\n\n${filler}`,
|
|
836
|
+
})
|
|
837
|
+
const host = document.querySelector<HTMLElement>('[data-testid="markdown-fixture"]')!
|
|
838
|
+
host.style.height = "240px"
|
|
839
|
+
host.style.overflowY = "auto"
|
|
840
|
+
}, fixture)
|
|
841
|
+
const host = page.getByTestId("markdown-fixture")
|
|
842
|
+
const markdown = host.locator('[data-component="markdown"]')
|
|
843
|
+
await expect(markdown).toHaveAttribute("data-markdown-ready", "")
|
|
844
|
+
const url = page.url()
|
|
845
|
+
|
|
846
|
+
const offset = (index: number) =>
|
|
847
|
+
host.evaluate(
|
|
848
|
+
(host, index) =>
|
|
849
|
+
host.querySelectorAll("h2")[index]!.getBoundingClientRect().top - host.getBoundingClientRect().top,
|
|
850
|
+
index,
|
|
851
|
+
)
|
|
852
|
+
|
|
853
|
+
await expect(markdown.getByRole("link", { name: "Host services" })).not.toHaveAttribute("target", "_blank")
|
|
854
|
+
await markdown.getByRole("link", { name: "Host services" }).click()
|
|
855
|
+
await expect.poll(() => offset(1)).toBeCloseTo(0, 0)
|
|
856
|
+
|
|
857
|
+
await host.evaluate((host) => host.scrollTo({ top: 0 }))
|
|
858
|
+
await markdown.getByRole("link", { name: "the second usage" }).click()
|
|
859
|
+
await expect.poll(() => offset(2)).toBeCloseTo(0, 0)
|
|
860
|
+
expect(page.url()).toBe(url)
|
|
861
|
+
})
|
|
862
|
+
|
|
863
|
+
async function resolvedColor(page: Page, token: string) {
|
|
864
|
+
return page.evaluate((token) => {
|
|
865
|
+
const probe = document.createElement("span")
|
|
866
|
+
probe.style.color = `var(${token})`
|
|
867
|
+
document.body.append(probe)
|
|
868
|
+
const color = getComputedStyle(probe).color
|
|
869
|
+
probe.remove()
|
|
870
|
+
|
|
871
|
+
return color
|
|
872
|
+
}, token)
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
story("keeps inline code backgrounds 18px tall", async ({ page }) => {
|
|
876
|
+
await page.evaluate(async (fixture) => {
|
|
877
|
+
const { mountMarkdown } = await import(fixture)
|
|
878
|
+
await mountMarkdown({ text: "`value` and `src/file.ts`" })
|
|
879
|
+
}, fixture)
|
|
880
|
+
const code = page.getByTestId("markdown-fixture").locator(":not(pre) > code")
|
|
881
|
+
await expect(code).toHaveCount(2)
|
|
882
|
+
await expect(code.nth(1)).toHaveAttribute("data-inline-code-kind", "path")
|
|
883
|
+
expect(
|
|
884
|
+
await code.evaluateAll((elements) => elements.map((element) => element.getBoundingClientRect().height)),
|
|
885
|
+
).toEqual([18, 18])
|
|
886
|
+
})
|
|
887
|
+
|
|
888
|
+
story("keeps inline code geometry when a completed stream turns it into a file link", async ({ page }) => {
|
|
889
|
+
await page.evaluate(async (fixture) => {
|
|
890
|
+
const { mountMarkdown } = await import(fixture)
|
|
891
|
+
await mountMarkdown({ text: "Edit `src/file.ts` to fix it", streaming: true })
|
|
892
|
+
}, fixture)
|
|
893
|
+
await page.evaluate(() => document.fonts.ready.then(() => undefined))
|
|
894
|
+
const harness = page.getByTestId("markdown-fixture")
|
|
895
|
+
const code = harness.locator(":not(pre) > code")
|
|
896
|
+
await expect(code).toHaveText("src/file.ts")
|
|
897
|
+
await expect(code).not.toHaveAttribute("data-inline-code-kind")
|
|
898
|
+
|
|
899
|
+
const geometry = () =>
|
|
900
|
+
code.evaluate((element) => {
|
|
901
|
+
const next = document.createRange()
|
|
902
|
+
next.selectNodeContents(element.nextSibling!)
|
|
903
|
+
const box = element.getBoundingClientRect()
|
|
904
|
+
|
|
905
|
+
return { left: box.left, right: box.right, next: next.getBoundingClientRect().left }
|
|
906
|
+
})
|
|
907
|
+
|
|
908
|
+
const before = await geometry()
|
|
909
|
+
await harness.getByLabel("Streaming").uncheck()
|
|
910
|
+
await expect(code).toHaveAttribute("data-inline-code-kind", "path")
|
|
911
|
+
expect(await geometry()).toEqual(before)
|
|
912
|
+
})
|
|
913
|
+
|
|
914
|
+
story("shares in-flight Markdown rendering without overwriting a reclaimed cache entry", async ({ page }) => {
|
|
915
|
+
const result = await page.evaluate(async (fixture) => {
|
|
916
|
+
const { getCachedMarkdown, renderCachedMarkdown } = await import(fixture)
|
|
917
|
+
const raw = "**Shared result**"
|
|
918
|
+
const first = renderCachedMarkdown({ raw, src: raw }, "in-flight")
|
|
919
|
+
const second = renderCachedMarkdown({ raw, src: raw }, "in-flight")
|
|
920
|
+
const [left, right] = await Promise.all([first, second])
|
|
921
|
+
const abandoned = renderCachedMarkdown({ raw: "abandoned", src: "abandoned" }, "in-flight")
|
|
922
|
+
await renderCachedMarkdown({ raw, src: raw }, "in-flight")
|
|
923
|
+
await abandoned
|
|
924
|
+
|
|
925
|
+
return {
|
|
926
|
+
shared: left === right,
|
|
927
|
+
html: left.html,
|
|
928
|
+
cached: getCachedMarkdown("in-flight") === left,
|
|
929
|
+
}
|
|
930
|
+
}, fixture)
|
|
931
|
+
|
|
932
|
+
expect(result).toMatchObject({ shared: true, cached: true })
|
|
933
|
+
expect(result.html).toContain("<strong>Shared result</strong>")
|
|
934
|
+
})
|
|
935
|
+
|
|
936
|
+
story("keeps an in-flight parse shared after a readiness check reads the cached entry", async ({ page }) => {
|
|
937
|
+
const result = await page.evaluate(async (fixture) => {
|
|
938
|
+
const { getCachedMarkdown, getReadyMarkdown, renderCachedMarkdown, touchCachedMarkdown } = await import(fixture)
|
|
939
|
+
const previous = "**Previous answer**"
|
|
940
|
+
touchCachedMarkdown("ready-check", { raw: previous, src: previous, hash: "previous", html: "<p>previous</p>" })
|
|
941
|
+
const raw = "```typescript\nconst next = true\n```"
|
|
942
|
+
const first = renderCachedMarkdown({ raw, src: raw }, "ready-check")
|
|
943
|
+
// The timeline asks whether the cached answer is ready while the new one still parses.
|
|
944
|
+
const ready = getReadyMarkdown({ raw: previous, src: previous }, "ready-check")
|
|
945
|
+
const second = renderCachedMarkdown({ raw, src: raw }, "ready-check")
|
|
946
|
+
const [left, right] = await Promise.all([first, second])
|
|
947
|
+
|
|
948
|
+
return { ready: ready?.html, shared: left === right, cached: getCachedMarkdown("ready-check") === left }
|
|
949
|
+
}, fixture)
|
|
950
|
+
|
|
951
|
+
expect(result).toEqual({ ready: "<p>previous</p>", shared: true, cached: true })
|
|
952
|
+
})
|
|
953
|
+
|
|
954
|
+
story("settles an abandoned parse and permits immediate cache-key reuse", async ({ page }) => {
|
|
955
|
+
const result = await page.evaluate(async (fixture) => {
|
|
956
|
+
const { getCachedMarkdown, renderCachedMarkdown, MarkdownWorkerDisposedError } = await import(fixture)
|
|
957
|
+
const controller = new AbortController()
|
|
958
|
+
const raw = "```typescript\nconst abandoned = true\n```"
|
|
959
|
+
|
|
960
|
+
const pending = renderCachedMarkdown({ raw, src: raw }, "released", controller.signal).catch(
|
|
961
|
+
(error: Error) => error instanceof MarkdownWorkerDisposedError,
|
|
962
|
+
)
|
|
963
|
+
|
|
964
|
+
controller.abort()
|
|
965
|
+
const rejected = await pending
|
|
966
|
+
const empty = getCachedMarkdown("released") === undefined
|
|
967
|
+
const replacement = "```typescript\nconst replacement = true\n```"
|
|
968
|
+
const rendered = await renderCachedMarkdown({ raw: replacement, src: replacement }, "released")
|
|
969
|
+
|
|
970
|
+
return { rejected, empty, cached: getCachedMarkdown("released") === rendered, html: rendered.html }
|
|
971
|
+
}, fixture)
|
|
972
|
+
|
|
973
|
+
expect(result).toMatchObject({ rejected: true, empty: true, cached: true })
|
|
974
|
+
expect(result.html).toContain("replacement")
|
|
975
|
+
expect(result.html).not.toContain("abandoned")
|
|
976
|
+
})
|
|
977
|
+
|
|
978
|
+
for (const owned of [true, false]) {
|
|
979
|
+
story(
|
|
980
|
+
`preserves a shared parse for ${owned ? "another mounted consumer" : "an explicit preload"}`,
|
|
981
|
+
async ({ page }) => {
|
|
982
|
+
const result = await page.evaluate(
|
|
983
|
+
async ({ fixture, owned }) => {
|
|
984
|
+
const { getCachedMarkdown, renderCachedMarkdown, MarkdownWorkerDisposedError } = await import(fixture)
|
|
985
|
+
const first = new AbortController()
|
|
986
|
+
const second = new AbortController()
|
|
987
|
+
const raw = "```typescript\nconst shared = true\n```"
|
|
988
|
+
|
|
989
|
+
const pending = renderCachedMarkdown({ raw, src: raw }, "shared-lifetime", first.signal).catch(
|
|
990
|
+
(error: Error) => error instanceof MarkdownWorkerDisposedError,
|
|
991
|
+
)
|
|
992
|
+
|
|
993
|
+
let complete = false
|
|
994
|
+
|
|
995
|
+
const survivor = renderCachedMarkdown(
|
|
996
|
+
{ raw, src: raw },
|
|
997
|
+
"shared-lifetime",
|
|
998
|
+
owned ? second.signal : undefined,
|
|
999
|
+
).then((value: { html: string }) => {
|
|
1000
|
+
complete = true
|
|
1001
|
+
|
|
1002
|
+
return value
|
|
1003
|
+
})
|
|
1004
|
+
|
|
1005
|
+
first.abort()
|
|
1006
|
+
const rejected = await pending
|
|
1007
|
+
const independent = !complete
|
|
1008
|
+
const rendered = await survivor
|
|
1009
|
+
second.abort()
|
|
1010
|
+
|
|
1011
|
+
return {
|
|
1012
|
+
rejected,
|
|
1013
|
+
independent,
|
|
1014
|
+
cached: getCachedMarkdown("shared-lifetime") === rendered,
|
|
1015
|
+
html: rendered.html,
|
|
1016
|
+
}
|
|
1017
|
+
},
|
|
1018
|
+
{ fixture, owned },
|
|
1019
|
+
)
|
|
1020
|
+
|
|
1021
|
+
expect(result).toMatchObject({ rejected: true, independent: true, cached: true })
|
|
1022
|
+
expect(result.html).toContain("shared")
|
|
1023
|
+
},
|
|
1024
|
+
)
|
|
1025
|
+
}
|
|
1026
|
+
|
|
1027
|
+
story("does not admit an already disposed Markdown consumer", async ({ page }) => {
|
|
1028
|
+
const result = await page.evaluate(async (fixture) => {
|
|
1029
|
+
const { getCachedMarkdown, renderCachedMarkdown, MarkdownWorkerDisposedError } = await import(fixture)
|
|
1030
|
+
const controller = new AbortController()
|
|
1031
|
+
controller.abort()
|
|
1032
|
+
const raw = "```typescript\nconst ignored = true\n```"
|
|
1033
|
+
|
|
1034
|
+
const rejected = await renderCachedMarkdown({ raw, src: raw }, "already-disposed", controller.signal).then(
|
|
1035
|
+
() => false,
|
|
1036
|
+
(error: Error) => error instanceof MarkdownWorkerDisposedError,
|
|
1037
|
+
)
|
|
1038
|
+
|
|
1039
|
+
return { rejected, empty: getCachedMarkdown("already-disposed") === undefined }
|
|
1040
|
+
}, fixture)
|
|
1041
|
+
|
|
1042
|
+
expect(result).toEqual({ rejected: true, empty: true })
|
|
1043
|
+
})
|
|
1044
|
+
|
|
1045
|
+
story("releases a timeline preload without cancelling a mounted shared consumer", async ({ page }) => {
|
|
1046
|
+
const result = await page.evaluate(async (fixture) => {
|
|
1047
|
+
const { getCachedMarkdown, preloadMarkdown, renderCachedMarkdown, MarkdownWorkerDisposedError } = await import(
|
|
1048
|
+
fixture
|
|
1049
|
+
)
|
|
1050
|
+
|
|
1051
|
+
const controller = new AbortController()
|
|
1052
|
+
const raw = "```typescript\nconst preloaded = true\n```"
|
|
1053
|
+
|
|
1054
|
+
const preload = preloadMarkdown(raw, "timeline-preload", controller.signal).then(
|
|
1055
|
+
() => false,
|
|
1056
|
+
(error: Error) => error instanceof MarkdownWorkerDisposedError,
|
|
1057
|
+
)
|
|
1058
|
+
|
|
1059
|
+
const mounted = renderCachedMarkdown({ raw, src: raw }, "timeline-preload:0:full")
|
|
1060
|
+
controller.abort()
|
|
1061
|
+
const rejected = await preload
|
|
1062
|
+
const rendered = await mounted
|
|
1063
|
+
|
|
1064
|
+
return { rejected, cached: getCachedMarkdown("timeline-preload:0:full") === rendered, html: rendered.html }
|
|
1065
|
+
}, fixture)
|
|
1066
|
+
|
|
1067
|
+
expect(result).toMatchObject({ rejected: true, cached: true })
|
|
1068
|
+
expect(result.html).toContain("preloaded")
|
|
1069
|
+
})
|
|
1070
|
+
|
|
1071
|
+
story("keeps a reopened cached answer recent under cache pressure", async ({ page }) => {
|
|
1072
|
+
await page.evaluate(async (fixture) => {
|
|
1073
|
+
const { mountMarkdown, touchCachedMarkdown } = await import(fixture)
|
|
1074
|
+
await mountMarkdown({ text: "**Cached answer**", cached: true })
|
|
1075
|
+
Array.from({ length: 199 }, (_, index) =>
|
|
1076
|
+
touchCachedMarkdown(`filler-${index}`, { raw: "filler", hash: "filler", html: "<p>filler</p>" }),
|
|
1077
|
+
)
|
|
1078
|
+
}, fixture)
|
|
1079
|
+
const harness = page.getByTestId("markdown-fixture")
|
|
1080
|
+
const markdown = harness.locator('[data-component="markdown"]')
|
|
1081
|
+
await expect(markdown.locator("strong")).toHaveText("Cached answer")
|
|
1082
|
+
await harness.getByRole("button", { name: "Toggle Markdown" }).click()
|
|
1083
|
+
await expect(markdown).toHaveCount(0)
|
|
1084
|
+
await harness.getByRole("button", { name: "Toggle Markdown" }).click()
|
|
1085
|
+
await expect(markdown.locator("strong")).toHaveText("Cached answer")
|
|
1086
|
+
|
|
1087
|
+
const cached = await page.evaluate(async (fixture) => {
|
|
1088
|
+
const { getCachedMarkdown, touchCachedMarkdown } = await import(fixture)
|
|
1089
|
+
touchCachedMarkdown("newest", { raw: "newest", hash: "newest", html: "<p>newest</p>" })
|
|
1090
|
+
|
|
1091
|
+
return getCachedMarkdown("markdown-test:0:full")?.raw
|
|
1092
|
+
}, fixture)
|
|
1093
|
+
|
|
1094
|
+
expect(cached).toBe("**Cached answer**")
|
|
1095
|
+
})
|
|
1096
|
+
|
|
1097
|
+
story("renders cached Mermaid blocks and falls back to code for invalid diagrams", async ({ page }) => {
|
|
1098
|
+
await page.evaluate(async (fixture) => {
|
|
1099
|
+
const { mountMarkdown } = await import(fixture)
|
|
1100
|
+
await mountMarkdown({ text: "```mermaid\nflowchart LR\n A[Start] --> B[End]\n```", cached: true })
|
|
1101
|
+
}, fixture)
|
|
1102
|
+
const harness = page.getByTestId("markdown-fixture")
|
|
1103
|
+
const markdown = harness.locator('[data-component="markdown"]')
|
|
1104
|
+
await expect(markdown.locator('[data-component="markdown-mermaid"] > svg.pele')).toBeVisible()
|
|
1105
|
+
await harness.getByRole("button", { name: "Toggle Markdown" }).click()
|
|
1106
|
+
await expect(markdown).toHaveCount(0)
|
|
1107
|
+
await harness.getByRole("button", { name: "Toggle Markdown" }).click()
|
|
1108
|
+
await expect(markdown.locator('[data-component="markdown-mermaid"] > svg')).toBeVisible()
|
|
1109
|
+
await harness.getByLabel("Markdown text").fill("```mermaid\nflowchart LR; A[broken\n```")
|
|
1110
|
+
await expect(markdown.locator('[data-component="markdown-mermaid"]')).toHaveCount(0)
|
|
1111
|
+
await expect(markdown.locator("pre code")).toBeVisible()
|
|
1112
|
+
await expect(markdown.locator("pre code")).toHaveText("flowchart LR; A[broken")
|
|
1113
|
+
await expect(markdown.getByRole("button", { name: "Copy", exact: true })).toBeVisible()
|
|
1114
|
+
})
|
|
1115
|
+
|
|
1116
|
+
story("renders the trailing chunk while the stream is paused", async ({ page }) => {
|
|
1117
|
+
await page.evaluate(async (fixture) => {
|
|
1118
|
+
const { mountMarkdown } = await import(fixture)
|
|
1119
|
+
await mountMarkdown({ text: "Checking **the project** before running the comm", streaming: true, cached: true })
|
|
1120
|
+
}, fixture)
|
|
1121
|
+
const harness = page.getByTestId("markdown-fixture")
|
|
1122
|
+
const markdown = harness.locator('[data-component="markdown"]')
|
|
1123
|
+
await expect(markdown).toHaveAttribute("data-markdown-ready", "")
|
|
1124
|
+
await expect(markdown.locator("p")).toHaveText("Checking the project before running the comm")
|
|
1125
|
+
await harness.getByLabel("Markdown text").fill("Checking **the project** before running the command.")
|
|
1126
|
+
await expect(markdown.locator("p")).toHaveText("Checking the project before running the command.")
|
|
1127
|
+
await expect(markdown).toHaveAttribute("data-markdown-ready", "")
|
|
1128
|
+
await expect(markdown.locator("strong")).toHaveText("the project")
|
|
1129
|
+
await expect(harness.getByLabel("Streaming")).toBeChecked()
|
|
1130
|
+
})
|
|
1131
|
+
|
|
1132
|
+
story("keeps live elements and selection when a stream completes and later changes", async ({ page }) => {
|
|
1133
|
+
await page.evaluate(async (fixture) => {
|
|
1134
|
+
const { mountMarkdown } = await import(fixture)
|
|
1135
|
+
await mountMarkdown({ text: "Hello **world**", streaming: true, synchronousFiles: true })
|
|
1136
|
+
}, fixture)
|
|
1137
|
+
const harness = page.getByTestId("markdown-fixture")
|
|
1138
|
+
const markdown = harness.locator('[data-component="markdown"]')
|
|
1139
|
+
const paragraph = markdown.locator("p")
|
|
1140
|
+
await expect(markdown.locator("[data-markdown-word]")).toHaveCount(2)
|
|
1141
|
+
await paragraph.evaluate((element) => element.setAttribute("data-retained", "true"))
|
|
1142
|
+
await harness.getByLabel("Markdown text").fill("Hello **world** again")
|
|
1143
|
+
await expect(markdown.locator("[data-markdown-word]")).toHaveCount(3)
|
|
1144
|
+
await expect(paragraph).toHaveAttribute("data-retained", "true")
|
|
1145
|
+
await expect(markdown.locator("[data-markdown-enter]")).not.toHaveCount(0)
|
|
1146
|
+
await paragraph.evaluate((element) => {
|
|
1147
|
+
const range = document.createRange()
|
|
1148
|
+
range.selectNodeContents(element.querySelector("strong")!)
|
|
1149
|
+
window.getSelection()!.removeAllRanges()
|
|
1150
|
+
window.getSelection()!.addRange(range)
|
|
1151
|
+
// Change the control without moving browser focus or selection.
|
|
1152
|
+
const input = document.querySelector<HTMLInputElement>('[data-testid="markdown-fixture"] input')!
|
|
1153
|
+
input.checked = false
|
|
1154
|
+
input.dispatchEvent(new Event("change", { bubbles: true }))
|
|
1155
|
+
})
|
|
1156
|
+
await expect(harness.getByLabel("Streaming")).not.toBeChecked()
|
|
1157
|
+
await expect(markdown).toHaveAttribute("data-markdown-ready", "")
|
|
1158
|
+
await expect(paragraph).toHaveAttribute("data-retained", "true")
|
|
1159
|
+
expect(await page.evaluate(() => window.getSelection()?.toString())).toBe("world")
|
|
1160
|
+
await harness.getByLabel("Markdown text").fill("Changed **content** in `new/file.ts` and `new/file.ts`")
|
|
1161
|
+
await expect(paragraph).toHaveText("Changed content in new/file.ts and new/file.ts")
|
|
1162
|
+
await expect(markdown.locator('code[data-inline-code-kind="path"]')).toHaveText(["new/file.ts", "new/file.ts"])
|
|
1163
|
+
await expect(paragraph).toHaveAttribute("data-retained", "true")
|
|
1164
|
+
await expect(markdown.locator("[data-markdown-word]")).toHaveCount(0)
|
|
1165
|
+
await harness.getByRole("button", { name: "Toggle Markdown" }).click()
|
|
1166
|
+
await expect(markdown).toHaveCount(0)
|
|
1167
|
+
})
|
|
1168
|
+
|
|
1169
|
+
story("replaces completed DOM before live rendering and retains streamed code copy actions", async ({ page }) => {
|
|
1170
|
+
await page.evaluate(async (fixture) => {
|
|
1171
|
+
const { mountMarkdown } = await import(fixture)
|
|
1172
|
+
await mountMarkdown({ text: "Initial **content**" })
|
|
1173
|
+
}, fixture)
|
|
1174
|
+
const harness = page.getByTestId("markdown-fixture")
|
|
1175
|
+
const markdown = harness.locator('[data-component="markdown"]')
|
|
1176
|
+
await expect(markdown.locator("p")).toHaveText("Initial content")
|
|
1177
|
+
await harness.getByLabel("Streaming").check()
|
|
1178
|
+
await harness.getByLabel("Markdown text").fill("Initial **content** continues")
|
|
1179
|
+
await expect(markdown.locator("p")).toHaveCount(1)
|
|
1180
|
+
await expect(markdown.locator("[data-markdown-word]")).toHaveCount(3)
|
|
1181
|
+
await harness.getByLabel("Markdown text").fill("```sh\necho hello\n")
|
|
1182
|
+
await expect(markdown.locator("pre code")).toHaveText("echo hello\n")
|
|
1183
|
+
await expect(markdown.locator("p")).toHaveCount(0)
|
|
1184
|
+
await expect(markdown.locator('[data-component="markdown-code"]')).toHaveAttribute("data-code-kind", "shell")
|
|
1185
|
+
await page.context().grantPermissions(["clipboard-read", "clipboard-write"])
|
|
1186
|
+
await markdown.getByRole("button", { name: "Copy" }).click()
|
|
1187
|
+
await expect(markdown.getByRole("button", { name: "Copied" })).toBeVisible()
|
|
1188
|
+
expect((await page.evaluate(() => navigator.clipboard.readText())).replaceAll("\r\n", "\n")).toBe("echo hello\n")
|
|
1189
|
+
await harness.getByLabel("Streaming").uncheck()
|
|
1190
|
+
await expect(markdown.locator("[data-markdown-complete]")).toHaveAttribute("data-markdown-complete", "true")
|
|
1191
|
+
await expect(markdown.locator("pre code")).toHaveText("echo hello\n")
|
|
1192
|
+
await harness.getByLabel("Markdown text").fill("Replacement prose")
|
|
1193
|
+
await expect(markdown.locator("p")).toHaveText("Replacement prose")
|
|
1194
|
+
await expect(markdown.locator('pre, [data-slot="markdown-copy-button"]')).toHaveCount(0)
|
|
1195
|
+
await harness.getByRole("button", { name: "Toggle Markdown" }).click()
|
|
1196
|
+
await expect(markdown).toHaveCount(0)
|
|
1197
|
+
})
|
|
1198
|
+
|
|
1199
|
+
story("relabels streamed code copy actions when the language changes", async ({ page }) => {
|
|
1200
|
+
await page.evaluate(async (fixture) => {
|
|
1201
|
+
const { mountMarkdown } = await import(fixture)
|
|
1202
|
+
await mountMarkdown({ text: "```sh\necho hello\n", streaming: true })
|
|
1203
|
+
}, fixture)
|
|
1204
|
+
const harness = page.getByTestId("markdown-fixture")
|
|
1205
|
+
const markdown = harness.locator('[data-component="markdown"]')
|
|
1206
|
+
await expect(markdown.getByRole("button", { name: "Copy" })).toHaveCount(1)
|
|
1207
|
+
await harness.getByLabel("Markdown text").fill("```sh\necho hello\n```\n\nStreaming continues")
|
|
1208
|
+
await expect(markdown.locator("p")).toHaveText("Streaming continues")
|
|
1209
|
+
await harness.getByLabel("German").check()
|
|
1210
|
+
await expect(markdown.getByRole("button", { name: "Kopieren" })).toHaveCount(1)
|
|
1211
|
+
await expect(markdown.getByRole("button", { name: "Copy" })).toHaveCount(0)
|
|
1212
|
+
})
|
|
1213
|
+
|
|
1214
|
+
story("highlights streamed code across comment and string boundaries", async ({ page }) => {
|
|
1215
|
+
await page.evaluate(async (fixture) => {
|
|
1216
|
+
const { mountMarkdown } = await import(fixture)
|
|
1217
|
+
await mountMarkdown({ text: "```ts\n", streaming: true })
|
|
1218
|
+
}, fixture)
|
|
1219
|
+
const harness = page.getByTestId("markdown-fixture")
|
|
1220
|
+
const markdown = harness.locator('[data-component="markdown"]')
|
|
1221
|
+
const code = markdown.locator("pre code")
|
|
1222
|
+
const chunks = ["/* multi", "line\ncomment *", "/\nconst message = `hel", "lo ${1 + 2}`\n", "const pattern = /a+b/g"]
|
|
1223
|
+
|
|
1224
|
+
for (let index = 1; index <= chunks.length; index++) {
|
|
1225
|
+
const text = chunks.slice(0, index).join("")
|
|
1226
|
+
await harness.getByLabel("Markdown text").fill(`\`\`\`ts\n${text}`)
|
|
1227
|
+
await expect(code).toHaveText(text)
|
|
1228
|
+
}
|
|
1229
|
+
|
|
1230
|
+
await expect(code.locator('span[style*="color"]')).not.toHaveCount(0)
|
|
1231
|
+
await harness.getByLabel("Markdown text").fill(`\`\`\`ts\n${chunks.join("")}\n\`\`\``)
|
|
1232
|
+
await harness.getByLabel("Streaming").uncheck()
|
|
1233
|
+
await expect(markdown.locator("[data-markdown-complete]")).toHaveAttribute("data-markdown-complete", "true")
|
|
1234
|
+
await expect(code).toHaveText(chunks.join(""))
|
|
1235
|
+
await expect(code).toContainText("const pattern = /a+b/g")
|
|
1236
|
+
expect(
|
|
1237
|
+
await code
|
|
1238
|
+
.locator("span[style]")
|
|
1239
|
+
.evaluateAll((spans) => new Set(spans.map((span) => getComputedStyle(span).color)).size),
|
|
1240
|
+
).toBeGreaterThan(1)
|
|
1241
|
+
await harness.getByRole("button", { name: "Toggle Markdown" }).click()
|
|
1242
|
+
await harness.getByRole("button", { name: "Toggle Markdown" }).click()
|
|
1243
|
+
await expect(code).toHaveText(chunks.join(""))
|
|
1244
|
+
await expect(code.locator('span[style*="color"]')).not.toHaveCount(0)
|
|
1245
|
+
await harness.getByLabel("Markdown text").fill("```unknown-language\n<plain> & text\n```")
|
|
1246
|
+
await expect(code).toHaveText("<plain> & text")
|
|
1247
|
+
await expect(code.locator("plain")).toHaveCount(0)
|
|
1248
|
+
})
|
|
1249
|
+
|
|
1250
|
+
story("highlights every block of a streamed answer with more than 200 code blocks", async ({ page }) => {
|
|
1251
|
+
// The worker client used to keep 200 highlight keys and dispose the oldest, rejecting block 1's highlight in flight.
|
|
1252
|
+
const blocks = Array.from({ length: 205 }, (_, index) => `\`\`\`ts\nconst value${index} = ${index}\n\`\`\``)
|
|
1253
|
+
const text = blocks.join("\n\n")
|
|
1254
|
+
|
|
1255
|
+
await page.evaluate(
|
|
1256
|
+
async ({ fixture, text }) => {
|
|
1257
|
+
const { mountMarkdown } = await import(fixture)
|
|
1258
|
+
await mountMarkdown({ text, streaming: true })
|
|
1259
|
+
},
|
|
1260
|
+
{ fixture, text },
|
|
1261
|
+
)
|
|
1262
|
+
const markdown = page.getByTestId("markdown-fixture").locator('[data-component="markdown"]')
|
|
1263
|
+
await expect(markdown).toHaveAttribute("data-markdown-ready", "")
|
|
1264
|
+
const code = markdown.locator("pre code")
|
|
1265
|
+
await expect(code).toHaveCount(205)
|
|
1266
|
+
await expect(code.first().locator('span[style*="color"]')).not.toHaveCount(0)
|
|
1267
|
+
await expect(code.last().locator('span[style*="color"]')).not.toHaveCount(0)
|
|
1268
|
+
})
|
|
1269
|
+
|
|
1270
|
+
story("preserves streamed math through completion and a fresh render", async ({ page }) => {
|
|
1271
|
+
await page.evaluate(async (fixture) => {
|
|
1272
|
+
const { mountMarkdown } = await import(fixture)
|
|
1273
|
+
await mountMarkdown({ text: "Formula: \\(P(x_{t+1})\\)", streaming: true })
|
|
1274
|
+
}, fixture)
|
|
1275
|
+
const harness = page.getByTestId("markdown-fixture")
|
|
1276
|
+
const markdown = harness.locator('[data-component="markdown"]')
|
|
1277
|
+
await expect(markdown.locator(".katex")).toHaveCount(1)
|
|
1278
|
+
await expect(markdown.locator(".katex-mathml")).toContainText("P(x_{t+1})")
|
|
1279
|
+
expect(await markdown.locator(".katex").evaluate((element) => element.nextSibling?.textContent ?? "")).toBe("")
|
|
1280
|
+
await harness.getByLabel("Markdown text").fill("Formula: \\(P(x_{t+1})\\) and \\(w^{*}\\).\n\n$$\nx^2 + y^2\n$$\n")
|
|
1281
|
+
await expect(markdown.locator(".katex")).toHaveCount(3)
|
|
1282
|
+
await expect(markdown.locator(".katex-display")).toHaveCount(1)
|
|
1283
|
+
await expect(markdown.locator(".katex-mathml")).toContainText(["P(x_{t+1})", "w^{*}", "x^2 + y^2"])
|
|
1284
|
+
await expect(markdown.locator(".katex-error, em, strong")).toHaveCount(0)
|
|
1285
|
+
await harness.getByLabel("Streaming").uncheck()
|
|
1286
|
+
await expect(markdown.locator(".katex")).toHaveCount(3)
|
|
1287
|
+
await harness.getByRole("button", { name: "Toggle Markdown" }).click()
|
|
1288
|
+
await harness.getByRole("button", { name: "Toggle Markdown" }).click()
|
|
1289
|
+
await expect(markdown.locator(".katex")).toHaveCount(3)
|
|
1290
|
+
await expect(markdown.locator(".katex-display")).toHaveCount(1)
|
|
1291
|
+
await expect(markdown.locator(".katex-error")).toHaveCount(0)
|
|
1292
|
+
})
|
|
1293
|
+
|
|
1294
|
+
story("keeps an unchanged streamed formula, and a selection in it, while text after it streams", async ({ page }) => {
|
|
1295
|
+
await page.evaluate(async (fixture) => {
|
|
1296
|
+
const { mountMarkdown } = await import(fixture)
|
|
1297
|
+
await mountMarkdown({ text: "Formula $x^2$ and", streaming: true })
|
|
1298
|
+
}, fixture)
|
|
1299
|
+
const harness = page.getByTestId("markdown-fixture")
|
|
1300
|
+
const markdown = harness.locator('[data-component="markdown"]')
|
|
1301
|
+
const math = await markdown.locator("math").elementHandle()
|
|
1302
|
+
await markdown.locator("math").evaluate((node) => getSelection()?.selectAllChildren(node))
|
|
1303
|
+
// Typing would move the selection into the field; the stream changes the text without focus.
|
|
1304
|
+
await harness.getByLabel("Markdown text").evaluate((field: HTMLTextAreaElement) => {
|
|
1305
|
+
field.value = "Formula $x^2$ and more words"
|
|
1306
|
+
field.dispatchEvent(new Event("input", { bubbles: true }))
|
|
1307
|
+
})
|
|
1308
|
+
await expect(markdown).toContainText("more words")
|
|
1309
|
+
expect(await markdown.locator("math").evaluate((node, kept) => [node === kept, getSelection()?.anchorNode === kept], math)).toEqual([true, true])
|
|
1310
|
+
})
|
|
1311
|
+
|
|
1312
|
+
for (const theme of ["light", "dark"]) {
|
|
1313
|
+
story(`renders class and connected subgraph diagrams in ${theme}`, async ({ mount, page }) => {
|
|
1314
|
+
await page.setViewportSize({ width: 390, height: 900 })
|
|
1315
|
+
await mount("components-markdown--complete-response", { globals: { theme } })
|
|
1316
|
+
await expect(page.locator("html")).toHaveClass(new RegExp(theme))
|
|
1317
|
+
await page.evaluate(async (fixture) => {
|
|
1318
|
+
const { mountMarkdown } = await import(fixture)
|
|
1319
|
+
await mountMarkdown({
|
|
1320
|
+
text: [
|
|
1321
|
+
"```mermaid\nclassDiagram\nAnimal <|-- Duck\nAnimal : +int age\nDuck : +swim()\n```",
|
|
1322
|
+
"```mermaid\nflowchart LR\nsubgraph Input\ndirection TB\nA[Prompt] --> B[Parse]\nend\nsubgraph Output\ndirection TB\nC[Render] --> D[Display]\nend\nB --> C\n```",
|
|
1323
|
+
].join("\n\n"),
|
|
1324
|
+
streaming: true,
|
|
1325
|
+
})
|
|
1326
|
+
}, fixture)
|
|
1327
|
+
const harness = page.getByTestId("markdown-fixture")
|
|
1328
|
+
const diagrams = harness.locator('[data-component="markdown-mermaid"] > svg')
|
|
1329
|
+
await expect(diagrams).toHaveCount(2)
|
|
1330
|
+
await expect(diagrams.nth(0)).toBeVisible()
|
|
1331
|
+
await expect(diagrams.nth(0)).toContainText("swim()")
|
|
1332
|
+
await expect(diagrams.nth(1)).toBeVisible()
|
|
1333
|
+
await expect(diagrams.nth(1)).toContainText("Display")
|
|
1334
|
+
await expect(diagrams.nth(1).locator(".pele-edge")).toHaveCount(3)
|
|
1335
|
+
await expect(harness.locator('[data-mermaid-ready="true"] > pre:visible')).toHaveCount(0)
|
|
1336
|
+
await harness.getByLabel("Streaming").uncheck()
|
|
1337
|
+
await expect(diagrams).toHaveCount(2)
|
|
1338
|
+
await expect(diagrams.nth(0)).toBeVisible()
|
|
1339
|
+
await expect(diagrams.nth(1)).toBeVisible()
|
|
1340
|
+
})
|
|
1341
|
+
}
|
|
1342
|
+
|
|
1343
|
+
for (const streaming of [false, true]) {
|
|
1344
|
+
story(
|
|
1345
|
+
`loads local images in ${streaming ? "streaming" : "cached"} Markdown and releases their URLs`,
|
|
1346
|
+
async ({ page }) => {
|
|
1347
|
+
const requests: string[] = []
|
|
1348
|
+
await page.route("**/api/fs/read/**", async (route) => {
|
|
1349
|
+
expect(route.request().headers().authorization).toBe(
|
|
1350
|
+
`Basic ${Buffer.from("opencode:fixture").toString("base64")}`,
|
|
1351
|
+
)
|
|
1352
|
+
requests.push(route.request().url())
|
|
1353
|
+
await route.fulfill({ contentType: "image/png", body: png })
|
|
1354
|
+
})
|
|
1355
|
+
await page.evaluate(
|
|
1356
|
+
async ({ fixture, streaming }) => {
|
|
1357
|
+
const { mountMarkdown } = await import(fixture)
|
|
1358
|
+
await mountMarkdown({
|
|
1359
|
+
text: "\n\n",
|
|
1360
|
+
streaming,
|
|
1361
|
+
cached: !streaming,
|
|
1362
|
+
images: true,
|
|
1363
|
+
})
|
|
1364
|
+
},
|
|
1365
|
+
{ fixture, streaming },
|
|
1366
|
+
)
|
|
1367
|
+
const harness = page.getByTestId("markdown-fixture")
|
|
1368
|
+
const image = harness.getByRole("button", { name: "Chart", exact: true })
|
|
1369
|
+
await expect.poll(() => image.evaluate((image: HTMLImageElement) => image.naturalWidth)).toBe(1)
|
|
1370
|
+
await expect(harness.getByRole("button", { name: "Again", exact: true })).toHaveAttribute("src", /^blob:/)
|
|
1371
|
+
expect(requests).toHaveLength(1)
|
|
1372
|
+
expect(new URL(requests[0]).searchParams.get("location[directory]")).toBe("C:/tmp/")
|
|
1373
|
+
const url = await image.getAttribute("src")
|
|
1374
|
+
await harness.getByLabel("Streaming").uncheck()
|
|
1375
|
+
await expect(harness.locator('[data-component="markdown"]')).toHaveAttribute("data-markdown-ready", "")
|
|
1376
|
+
await expect(image).toHaveAttribute("src", url!)
|
|
1377
|
+
await harness.getByLabel("Markdown text").fill("")
|
|
1378
|
+
await expect
|
|
1379
|
+
.poll(() =>
|
|
1380
|
+
harness
|
|
1381
|
+
.getByRole("button", { name: "Replacement" })
|
|
1382
|
+
.evaluate((image: HTMLImageElement) => image.naturalWidth),
|
|
1383
|
+
)
|
|
1384
|
+
.toBe(1)
|
|
1385
|
+
expect(requests).toHaveLength(2)
|
|
1386
|
+
expect(
|
|
1387
|
+
await page.evaluate(
|
|
1388
|
+
(url) =>
|
|
1389
|
+
fetch(url!).then(
|
|
1390
|
+
() => false,
|
|
1391
|
+
() => true,
|
|
1392
|
+
),
|
|
1393
|
+
url,
|
|
1394
|
+
),
|
|
1395
|
+
).toBe(true)
|
|
1396
|
+
const next = await harness.getByRole("button", { name: "Replacement" }).getAttribute("src")
|
|
1397
|
+
await harness.getByRole("button", { name: "Toggle Markdown" }).click()
|
|
1398
|
+
await expect(harness.locator("img")).toHaveCount(0)
|
|
1399
|
+
expect(
|
|
1400
|
+
await page.evaluate(
|
|
1401
|
+
(url) =>
|
|
1402
|
+
fetch(url!).then(
|
|
1403
|
+
() => false,
|
|
1404
|
+
() => true,
|
|
1405
|
+
),
|
|
1406
|
+
next,
|
|
1407
|
+
),
|
|
1408
|
+
).toBe(true)
|
|
1409
|
+
},
|
|
1410
|
+
)
|
|
1411
|
+
}
|
|
1412
|
+
|
|
1413
|
+
story("keeps remote images browser-owned and rejects unsafe image sources", async ({ page }) => {
|
|
1414
|
+
await page.route("https://images.example/chart.png", (route) => {
|
|
1415
|
+
expect(route.request().headers().authorization).toBeUndefined()
|
|
1416
|
+
|
|
1417
|
+
return route.fulfill({ contentType: "image/png", body: png })
|
|
1418
|
+
})
|
|
1419
|
+
await page.evaluate(async (fixture) => {
|
|
1420
|
+
const { mountMarkdown } = await import(fixture)
|
|
1421
|
+
await mountMarkdown({
|
|
1422
|
+
images: true,
|
|
1423
|
+
text: '<img alt="Remote" src="https://images.example/chart.png"><img alt="Unsafe" src="javascript:alert(1)" onerror="alert(2)" data-local-image="/tmp/forged.png">',
|
|
1424
|
+
})
|
|
1425
|
+
}, fixture)
|
|
1426
|
+
const harness = page.getByTestId("markdown-fixture")
|
|
1427
|
+
await expect
|
|
1428
|
+
.poll(() =>
|
|
1429
|
+
harness.getByRole("button", { name: "Remote" }).evaluate((image: HTMLImageElement) => image.naturalWidth),
|
|
1430
|
+
)
|
|
1431
|
+
.toBe(1)
|
|
1432
|
+
await expect(harness.getByRole("button", { name: "Remote" })).toHaveAttribute(
|
|
1433
|
+
"src",
|
|
1434
|
+
"https://images.example/chart.png",
|
|
1435
|
+
)
|
|
1436
|
+
await expect(harness.locator("[onerror], [src^='javascript:'], [data-local-image]")).toHaveCount(0)
|
|
1437
|
+
})
|
|
1438
|
+
|
|
1439
|
+
story("keeps scripts and external resources inactive inside local SVG images", async ({ page }) => {
|
|
1440
|
+
const external: string[] = []
|
|
1441
|
+
await page.context().route("https://images.example/**", async (route) => {
|
|
1442
|
+
external.push(route.request().url())
|
|
1443
|
+
await route.abort()
|
|
1444
|
+
})
|
|
1445
|
+
await page.route("**/api/fs/read/chart.svg?*", (route) =>
|
|
1446
|
+
route.fulfill({
|
|
1447
|
+
contentType: "image/svg+xml",
|
|
1448
|
+
body: `<svg xmlns="http://www.w3.org/2000/svg" width="32" height="32">
|
|
1449
|
+
<script>parent.document.title = "image-script-ran"; fetch("https://images.example/script")</script>
|
|
1450
|
+
<image href="https://images.example/nested.png" width="32" height="32" />
|
|
1451
|
+
<rect width="32" height="32" fill="green" />
|
|
1452
|
+
</svg>`,
|
|
1453
|
+
}),
|
|
1454
|
+
)
|
|
1455
|
+
await page.evaluate(async (fixture) => {
|
|
1456
|
+
const { mountMarkdown } = await import(fixture)
|
|
1457
|
+
await mountMarkdown({ images: true, text: "" })
|
|
1458
|
+
}, fixture)
|
|
1459
|
+
const image = page.getByTestId("markdown-fixture").getByRole("button", { name: "SVG" })
|
|
1460
|
+
await expect.poll(() => image.evaluate((image: HTMLImageElement) => image.naturalWidth)).toBe(32)
|
|
1461
|
+
await expect(image).toHaveAttribute("src", /^data:image\/svg\+xml;/)
|
|
1462
|
+
await expect(page).not.toHaveTitle("image-script-ran")
|
|
1463
|
+
expect(external).toEqual([])
|
|
1464
|
+
// Opening an image as a document must not give its contents the app's origin.
|
|
1465
|
+
const preview = await page.context().newPage()
|
|
1466
|
+
await preview.goto((await image.getAttribute("src"))!)
|
|
1467
|
+
expect(await preview.evaluate(() => location.origin)).toBe("null")
|
|
1468
|
+
await preview.close()
|
|
1469
|
+
})
|
|
1470
|
+
|
|
1471
|
+
story("loads file URLs and Windows paths, and leaves unreadable images as alt text", async ({ page }) => {
|
|
1472
|
+
const requested = new Set<string>()
|
|
1473
|
+
await page.route("**/api/fs/read/**", async (route) => {
|
|
1474
|
+
const url = new URL(route.request().url())
|
|
1475
|
+
requested.add(url.pathname)
|
|
1476
|
+
await route.fulfill(
|
|
1477
|
+
url.pathname.endsWith("missing.png")
|
|
1478
|
+
? { status: 404, body: "Not found" }
|
|
1479
|
+
: { contentType: "image/png", body: png },
|
|
1480
|
+
)
|
|
1481
|
+
})
|
|
1482
|
+
await page.evaluate(async (fixture) => {
|
|
1483
|
+
const { mountMarkdown } = await import(fixture)
|
|
1484
|
+
await mountMarkdown({
|
|
1485
|
+
images: true,
|
|
1486
|
+
// Marked percent-encodes the backslashes of a Windows image path, here inside a table cell.
|
|
1487
|
+
text: "\n\n\n\n| Windows |\n| --- |\n|  |",
|
|
1488
|
+
})
|
|
1489
|
+
}, fixture)
|
|
1490
|
+
const harness = page.getByTestId("markdown-fixture")
|
|
1491
|
+
|
|
1492
|
+
for (const name of ["Available", "Windows"])
|
|
1493
|
+
await expect
|
|
1494
|
+
.poll(() =>
|
|
1495
|
+
harness.getByRole("button", { name, exact: true }).evaluate((image: HTMLImageElement) => image.naturalWidth),
|
|
1496
|
+
)
|
|
1497
|
+
.toBe(1)
|
|
1498
|
+
await expect
|
|
1499
|
+
.poll(() => [...requested].sort())
|
|
1500
|
+
.toEqual(["/api/fs/read/chart%25.png", "/api/fs/read/missing.png", "/api/fs/read/windows.png"])
|
|
1501
|
+
await expect(harness.getByRole("button", { name: "Unavailable" })).not.toHaveAttribute("src")
|
|
1502
|
+
await harness.getByLabel("Markdown text").fill("Still usable")
|
|
1503
|
+
await expect(harness.locator('[data-component="markdown"]')).toHaveText("Still usable")
|
|
1504
|
+
})
|
|
1505
|
+
|
|
1506
|
+
async function copySelection(page: Page, select: "markdown" | "harness") {
|
|
1507
|
+
await page.evaluate((select) => {
|
|
1508
|
+
const harness = document.querySelector('[data-testid="markdown-fixture"]')!
|
|
1509
|
+
const range = document.createRange()
|
|
1510
|
+
range.selectNodeContents(select === "markdown" ? harness.querySelector('[data-component="markdown"]')! : harness)
|
|
1511
|
+
getSelection()!.removeAllRanges()
|
|
1512
|
+
getSelection()!.addRange(range)
|
|
1513
|
+
document.addEventListener(
|
|
1514
|
+
"copy",
|
|
1515
|
+
(event) => {
|
|
1516
|
+
document.body.dataset.copiedText = event.clipboardData?.getData("text/plain")
|
|
1517
|
+
document.body.dataset.copiedHtml = event.clipboardData?.getData("text/html")
|
|
1518
|
+
},
|
|
1519
|
+
{ once: true },
|
|
1520
|
+
)
|
|
1521
|
+
}, select)
|
|
1522
|
+
await page.keyboard.press("ControlOrMeta+C")
|
|
1523
|
+
|
|
1524
|
+
return page.evaluate(() => ({ text: document.body.dataset.copiedText, html: document.body.dataset.copiedHtml }))
|
|
1525
|
+
}
|
|
1526
|
+
|
|
1527
|
+
story("copies selected Markdown as semantic HTML without theme or highlight colours", async ({ page }) => {
|
|
1528
|
+
await page.evaluate(async (fixture) => {
|
|
1529
|
+
document.body.style.color = "rgb(237, 237, 237)"
|
|
1530
|
+
const { mountMarkdown } = await import(fixture)
|
|
1531
|
+
await mountMarkdown({
|
|
1532
|
+
text: "## Answer\n\nSome **bold** and [docs](https://opencode.ai/docs).\n\n- one\n- two\n\n```ts\nconst value = 42\n```",
|
|
1533
|
+
})
|
|
1534
|
+
}, fixture)
|
|
1535
|
+
const harness = page.getByTestId("markdown-fixture")
|
|
1536
|
+
await expect(harness.locator('[data-component="markdown"]')).toHaveAttribute("data-markdown-ready", "")
|
|
1537
|
+
await expect(harness.locator("pre code span[style]").first()).toBeAttached()
|
|
1538
|
+
|
|
1539
|
+
const copied = await copySelection(page, "markdown")
|
|
1540
|
+
|
|
1541
|
+
expect(copied.text).toContain("Some bold and docs.")
|
|
1542
|
+
expect(copied.text).toContain("const value = 42")
|
|
1543
|
+
expect(copied.html).toContain("<h2>Answer</h2>")
|
|
1544
|
+
expect(copied.html).toContain('<p>Some <strong>bold</strong> and <a href="https://opencode.ai/docs">docs</a>.</p>')
|
|
1545
|
+
expect(copied.html).toContain("<li>one</li>")
|
|
1546
|
+
expect(copied.html).toMatch(/<pre><code>(<span>[^<]*<\/span>)+<\/code><\/pre>/)
|
|
1547
|
+
expect(copied.html).not.toMatch(/style=|class=|<button|<svg/)
|
|
1548
|
+
})
|
|
1549
|
+
|
|
1550
|
+
story("keeps the browser copy for selections that leave the Markdown", async ({ page }) => {
|
|
1551
|
+
await page.evaluate(async (fixture) => {
|
|
1552
|
+
const { mountMarkdown } = await import(fixture)
|
|
1553
|
+
await mountMarkdown({ text: "Some **bold** text" })
|
|
1554
|
+
}, fixture)
|
|
1555
|
+
await expect(page.getByTestId("markdown-fixture").locator('[data-component="markdown"]')).toHaveAttribute(
|
|
1556
|
+
"data-markdown-ready",
|
|
1557
|
+
"",
|
|
1558
|
+
)
|
|
1559
|
+
|
|
1560
|
+
const copied = await copySelection(page, "harness")
|
|
1561
|
+
|
|
1562
|
+
expect(copied.html).toBe("")
|
|
1563
|
+
expect(copied.text).toBe("")
|
|
1564
|
+
})
|