From 4e9112656133ff081df862329374333f35ee5118 Mon Sep 17 00:00:00 2001 From: max2787 Date: Sat, 7 Nov 2026 11:48:59 -0500 Subject: [PATCH] fix(clipboard): omit format name for predefined Windows Unicode text (#200) Co-authored-by: max2787 <336753759+max2787@users.noreply.github.com> --- src/clipboard_rich.rs | 41 +++++++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/src/clipboard_rich.rs b/src/clipboard_rich.rs index 51ef94a..b463d28 100644 --- a/src/clipboard_rich.rs +++ b/src/clipboard_rich.rs @@ -17,9 +17,9 @@ impl TextFormat { } fn id(self)->u64{match self{Self::Windows=>UNICODE_TEXT,Self::Mac=>0}} fn name(self)->&'static str{match self{Self::Windows=>"CF_UNICODETEXT",Self::Mac=>MAC_TEXT}} - /// Name in a format-list offer. Official Windows peers name predefined formats `predefined_`; a plain - /// `CF_UNICODETEXT` makes the Windows viewer register a custom format instead of id 13 (issue #151). - fn offer_name(self)->&'static str{match self{Self::Windows=>WINDOWS_OFFER_NAME,Self::Mac=>MAC_TEXT}} + /// Issue #151 reports that Windows registers even `predefined_13` as a custom format. + /// Omit the name for predefined id 13; verified with the official Windows viewer on an X11 host. + fn offer_name(self)->Option<&'static str>{match self{Self::Windows=>None,Self::Mac=>Some(MAC_TEXT)}} fn encode(self,text:&str)->Vec{match self{Self::Windows=>encode_windows(text),Self::Mac=>text.as_bytes().to_vec()}} fn decode(self,data:&[u8])->Result{ match self{ @@ -34,7 +34,6 @@ impl TextFormat { } } } -const WINDOWS_OFFER_NAME:&str="predefined_13"; const BLOCK_BYTES:usize=0x20000; const MAX_BYTES:usize=MAX_TEXT*2+2; pub struct ResultMessages {pub messages:Vec<(u32,Vec)>,pub text:Option} @@ -100,7 +99,7 @@ impl State{ if text.contains('\0')||encode_windows(&text).len()>MAX_BYTES{return Err("clipboard encoded text exceeds limit");} self.local=Some(text); let mut list=Vec::new(); - for kind in formats {let mut format=Vec::new();int(1,kind.id(),&mut format);string(2,kind.offer_name(),&mut format);nested(1,&format,&mut list);} + for kind in formats {let mut format=Vec::new();int(1,kind.id(),&mut format);if let Some(name)=kind.offer_name(){string(2,name,&mut format);}nested(1,&format,&mut list);} Ok(request(id,1,&list)) } pub fn request(&mut self,rpc:&[u8],next:&mut impl FnMut()->u64)->Result{ @@ -192,6 +191,36 @@ impl State{ #[cfg(test)] mod tests{ use super::*; + #[test] + fn windows_format_offer_omits_name_field_entirely(){ + for offer in [State::default().offer(42,"hi".into()).unwrap(),State::default().offer_any_host(42,"hi".into()).unwrap()]{ + assert_eq!(number(header(&offer).unwrap(),1),Ok(42)); + let list=bytes(bytes(&offer,9).unwrap().unwrap(),1).unwrap().unwrap(); + let formats=decode_wire(list).unwrap(); + let WireValue::Bytes(windows)=formats[0].value else{panic!("missing Windows format")}; + assert_eq!(windows,&[8,13]); // Only field 1: not even an empty field 2. + assert!(decode_wire(windows).unwrap().iter().all(|field|field.number!=2)); + if formats.len()==2{ + let WireValue::Bytes(mac)=formats[1].value else{panic!("missing Mac format")}; + assert_eq!(number(mac,1),Ok(0)); + assert_eq!(bytes(mac,2),Ok(Some(MAC_TEXT.as_bytes()))); + } + } + } + #[test] + fn unnamed_windows_offer_still_serves_utf16le_with_crlf_and_nul(){ + let mut provider=State::default(); + provider.offer(1,"A中🙂\nB\r\n".into()).unwrap(); + let mut ask=Vec::new();int(1,13,&mut ask);string(2,"test-key",&mut ask); + let out=provider.request(&request(2,2,&ask),&mut||3).unwrap(); + assert_eq!(out.messages.iter().map(|m|m.0).collect::>(),vec![22,21]); + let confirm=bytes(bytes(&out.messages[0].1,5).unwrap().unwrap(),2).unwrap().unwrap(); + assert_eq!(number(confirm,1),Ok(1));assert_eq!(number(confirm,3),Ok(1)); + let block=bytes(bytes(&out.messages[1].1,9).unwrap().unwrap(),3).unwrap().unwrap(); + assert_eq!(text_field(block,1),Ok("test-key")); + assert_eq!(number(block,2),Ok(0)); + assert_eq!(bytes(block,3).unwrap().unwrap(),&[0x41,0,0x2d,0x4e,0x3d,0xd8,0x42,0xde,13,0,10,0,0x42,0,13,0,10,0,0,0]); + } #[test] fn mac_utf8_text_round_trips_both_directions(){ // Mac host offer: format list {1 id 0, 2 "public.utf8-plain-text"} (observed live). let mut mac=Vec::new();int(1,0,&mut mac);string(2,MAC_TEXT,&mut mac); @@ -218,7 +247,7 @@ mod tests{ assert_eq!(format_ids(&host_offer),vec![13]); let list=bytes(bytes(&host_offer,9).unwrap().unwrap(),1).unwrap().unwrap(); let format=bytes(list,1).unwrap().unwrap(); - assert_eq!(text_field(format,2),Ok(WINDOWS_OFFER_NAME)); + assert_eq!(bytes(format,2),Ok(None)); } #[test] fn desktop_clipboard_negotiates_and_buffers_unicode_until_confirmation(){