11import { afterEach , beforeEach , describe , expect , it , vi } from "vite-plus/test" ;
22
3- import { serializeRenderedMarkdownFragment } from "./markdown-clipboard" ;
3+ import {
4+ chatMarkdownClipboardPayload ,
5+ serializeRenderedMarkdownFragment ,
6+ } from "./markdown-clipboard" ;
47import { EnvironmentId , MessageId , ThreadId } from "@t3tools/contracts" ;
58import {
69 collectAssistantCitations ,
@@ -13,13 +16,15 @@ const ELEMENT_NODE = 1;
1316class FakeText {
1417 readonly nodeType = TEXT_NODE ;
1518 readonly childNodes : ReadonlyArray < never > = [ ] ;
19+ parentElement : FakeElement | null = null ;
1620
1721 constructor ( readonly textContent : string ) { }
1822}
1923
2024class FakeElement {
2125 readonly nodeType = ELEMENT_NODE ;
2226 readonly childNodes : Array < FakeElement | FakeText > = [ ] ;
27+ parentElement : FakeElement | null = null ;
2328 readonly classList = {
2429 contains : ( name : string ) => this . classNames . includes ( name ) ,
2530 } ;
@@ -43,10 +48,16 @@ class FakeElement {
4348 }
4449
4550 append ( ...children : Array < FakeElement | FakeText > ) : this {
51+ for ( const child of children ) child . parentElement = this ;
4652 this . childNodes . push ( ...children ) ;
4753 return this ;
4854 }
4955
56+ appendChild < T extends FakeElement | FakeText > ( child : T ) : T {
57+ this . append ( child ) ;
58+ return child ;
59+ }
60+
5061 getAttribute ( name : string ) : string | null {
5162 return this . attributes [ name ] ?? null ;
5263 }
@@ -55,8 +66,20 @@ class FakeElement {
5566 return Object . hasOwn ( this . attributes , name ) ;
5667 }
5768
58- closest ( ) : FakeElement | null {
59- return null ;
69+ closest ( selector : string ) : FakeElement | null {
70+ if ( selector === "[data-markdown-copy]" && this . hasAttribute ( "data-markdown-copy" ) ) {
71+ return this ;
72+ }
73+ if ( this . tagName === selector . toUpperCase ( ) ) return this ;
74+ return this . parentElement ?. closest ( selector ) ?? null ;
75+ }
76+
77+ querySelectorAll ( ) : ReadonlyArray < FakeElement > {
78+ return [ ] ;
79+ }
80+
81+ get innerHTML ( ) : string {
82+ return this . textContent ;
6083 }
6184
6285 /** Supports only the selectors markdown-clipboard actually asks for. */
@@ -262,4 +285,26 @@ describe("serializeRenderedMarkdownFragment", () => {
262285 "Hello World (Document template)" ,
263286 ) ;
264287 } ) ;
288+
289+ it ( "uses an ancestor's explicit Markdown when a selection only clones its children" , ( ) => {
290+ const label = new FakeElement ( "TEXT" ) . append ( new FakeText ( "A" ) ) ;
291+ new FakeElement ( "DIV" , [ ] , {
292+ "data-markdown-copy" : "```mermaid\nflowchart LR\nA-->B\n```\n\n" ,
293+ } ) . append ( new FakeElement ( "SVG" ) . append ( label ) ) ;
294+ vi . stubGlobal ( "document" , {
295+ createElement : ( ) => new FakeElement ( "DIV" ) ,
296+ } ) ;
297+
298+ const payload = chatMarkdownClipboardPayload ( {
299+ rangeCount : 1 ,
300+ getRangeAt : ( ) => ( {
301+ collapsed : false ,
302+ cloneContents : ( ) => new FakeElement ( "SVG" ) . append ( new FakeText ( "A" ) ) ,
303+ commonAncestorContainer : label ,
304+ toString : ( ) => "A" ,
305+ } ) ,
306+ } as unknown as Selection ) ;
307+
308+ expect ( payload ?. text ) . toBe ( "```mermaid\nflowchart LR\nA-->B\n```\n\n" ) ;
309+ } ) ;
265310} ) ;
0 commit comments