File tree Expand file tree Collapse file tree 2 files changed +23
-2
lines changed
Expand file tree Collapse file tree 2 files changed +23
-2
lines changed Original file line number Diff line number Diff line change @@ -55,6 +55,25 @@ describe.concurrent("clipboard.read()", () => {
5555 expect ( read ( event , true ) ) . toBe ( "hello" ) ;
5656 } ) ;
5757
58+ it ( "should prefer text/html over text/plain" , ( ) => {
59+ const dataTransfer = new MockDataTransfer ( ) ;
60+ dataTransfer . setData ( "text/html" , "<b>hello</b>" ) ;
61+ dataTransfer . setData ( "text/plain" , "hello" ) ;
62+ const event = new MockClipboardEvent ( "paste" , {
63+ clipboardData : dataTransfer
64+ } ) ;
65+ expect ( read ( event ) ) . toBe ( "<b>hello</b>" ) ;
66+ } ) ;
67+
68+ it ( "should fall back to text/plain when text/html is absent" , ( ) => {
69+ const dataTransfer = new MockDataTransfer ( ) ;
70+ dataTransfer . setData ( "text/plain" , "hello" ) ;
71+ const event = new MockClipboardEvent ( "paste" , {
72+ clipboardData : dataTransfer
73+ } ) ;
74+ expect ( read ( event ) ) . toBe ( "hello" ) ;
75+ } ) ;
76+
5877 it ( "should replace non-breaking spaces" , ( ) => {
5978 const dataTransfer = new MockDataTransfer ( ) ;
6079 dataTransfer . setData ( "text/plain" , "hello\u00a0world" ) ;
Original file line number Diff line number Diff line change @@ -13,13 +13,15 @@ export function read(
1313 if ( e instanceof ClipboardEvent ) {
1414 e . preventDefault ( ) ;
1515
16- const text = e . clipboardData ?. getData ( "text/plain" ) ?? null ;
16+ // getData() returns "" for absent types,
17+ // use || not ?? to treat empty strings as missing
18+ const text = e . clipboardData ?. getData ( "text/plain" ) || null ;
1719
1820 if ( plain === true ) {
1921 return text ;
2022 }
2123
22- const html = text ?? e . clipboardData ?. getData ( "text/plain " ) ?? null ;
24+ const html = e . clipboardData ?. getData ( "text/html " ) || text || null ;
2325
2426 if ( html ) {
2527 return html . replace ( / \u00a0 / g, " " ) ;
You can’t perform that action at this time.
0 commit comments