@@ -121,6 +121,27 @@ impl Clipboard {
121
121
unsafe { self . pasteboard . clearContents ( ) } ;
122
122
}
123
123
124
+ fn string_from_type ( & self , type_ : & ' static NSString ) -> Result < String , Error > {
125
+ // XXX: There does not appear to be an alternative for obtaining text without the need for
126
+ // autorelease behavior.
127
+ autoreleasepool ( |_| {
128
+ // XXX: We explicitly use `pasteboardItems` and not `stringForType` since the latter will concat
129
+ // multiple strings, if present, into one and return it instead of reading just the first which is `arboard`'s
130
+ // historical behavior.
131
+ let contents = unsafe { self . pasteboard . pasteboardItems ( ) } . ok_or_else ( || {
132
+ Error :: Unknown { description : String :: from ( "NSPasteboard#pasteboardItems errored" ) }
133
+ } ) ?;
134
+
135
+ for item in contents {
136
+ if let Some ( string) = unsafe { item. stringForType ( type_) } {
137
+ return Ok ( string. to_string ( ) ) ;
138
+ }
139
+ }
140
+
141
+ Err ( Error :: ContentNotAvailable )
142
+ } )
143
+ }
144
+
124
145
// fn get_binary_contents(&mut self) -> Result<Option<ClipboardContent>, Box<dyn std::error::Error>> {
125
146
// let string_class: Id<NSObject> = {
126
147
// let cls: Id<Class> = unsafe { Id::from_ptr(class("NSString")) };
@@ -182,27 +203,11 @@ impl<'clipboard> Get<'clipboard> {
182
203
}
183
204
184
205
pub ( crate ) fn text ( self ) -> Result < String , Error > {
185
- // XXX: There does not appear to be an alternative for obtaining text without the need for
186
- // autorelease behavior.
187
- autoreleasepool ( |_| {
188
- // XXX: We explicitly use `pasteboardItems` and not `stringForType` since the latter will concat
189
- // multiple strings, if present, into one and return it instead of reading just the first which is `arboard`'s
190
- // historical behavior.
191
- let contents =
192
- unsafe { self . clipboard . pasteboard . pasteboardItems ( ) } . ok_or_else ( || {
193
- Error :: Unknown {
194
- description : String :: from ( "NSPasteboard#pasteboardItems errored" ) ,
195
- }
196
- } ) ?;
197
-
198
- for item in contents {
199
- if let Some ( string) = unsafe { item. stringForType ( NSPasteboardTypeString ) } {
200
- return Ok ( string. to_string ( ) ) ;
201
- }
202
- }
206
+ unsafe { self . clipboard . string_from_type ( NSPasteboardTypeString ) }
207
+ }
203
208
204
- Err ( Error :: ContentNotAvailable )
205
- } )
209
+ pub ( crate ) fn html ( self ) -> Result < String , Error > {
210
+ unsafe { self . clipboard . string_from_type ( NSPasteboardTypeHTML ) }
206
211
}
207
212
208
213
#[ cfg( feature = "image-data" ) ]
0 commit comments