Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(64)

Side by Side Diff: third_party/mozilla/NSPasteboard+Utils.mm

Issue 2014733003: Removing parsing of text from pasteboard. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: splitting calls in two variations, to discass Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* ***** BEGIN LICENSE BLOCK ***** 1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 * 3 *
4 * The contents of this file are subject to the Mozilla Public License Version 4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with 5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at 6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/ 7 * http://www.mozilla.org/MPL/
8 * 8 *
9 * Software distributed under the License is distributed on an "AS IS" basis, 9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
73 - (int)declareURLPasteboardWithAdditionalTypes:(NSArray*)additionalTypes owner:( id)newOwner 73 - (int)declareURLPasteboardWithAdditionalTypes:(NSArray*)additionalTypes owner:( id)newOwner
74 { 74 {
75 NSArray* allTypes = [additionalTypes arrayByAddingObjectsFromArray: 75 NSArray* allTypes = [additionalTypes arrayByAddingObjectsFromArray:
76 [NSArray arrayWithObjects: 76 [NSArray arrayWithObjects:
77 kWebURLsWithTitlesPboardType, 77 kWebURLsWithTitlesPboardType,
78 NSURLPboardType, 78 NSURLPboardType,
79 NSStringPboardType, 79 NSStringPboardType,
80 kCorePasteboardFlavorType_url, 80 kCorePasteboardFlavorType_url,
81 kCorePasteboardFlavorType_urln, 81 kCorePasteboardFlavorType_urln,
82 nil]]; 82 nil]];
83 » return [self declareTypes:allTypes owner:newOwner]; 83 return [self declareTypes:allTypes owner:newOwner];
84 } 84 }
85 85
86 // 86 //
87 // Copy a single URL (with an optional title) to the clipboard in all relevant 87 // Copy a single URL (with an optional title) to the clipboard in all relevant
88 // formats. Convenience method for clients that can only ever deal with one 88 // formats. Convenience method for clients that can only ever deal with one
89 // URL and shouldn't have to build up the arrays for setURLs:withTitles:. 89 // URL and shouldn't have to build up the arrays for setURLs:withTitles:.
90 // 90 //
91 - (void)setDataForURL:(NSString*)url title:(NSString*)title 91 - (void)setDataForURL:(NSString*)url title:(NSString*)title
92 { 92 {
93 NSArray* urlList = [NSArray arrayWithObject:url]; 93 NSArray* urlList = [NSArray arrayWithObject:url];
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
165 [self setData:[NSData dataWithBytes:tempCString length:strlen(tempCString)] forType:kCorePasteboardFlavorType_urln]; 165 [self setData:[NSData dataWithBytes:tempCString length:strlen(tempCString)] forType:kCorePasteboardFlavorType_urln];
166 } 166 }
167 } 167 }
168 168
169 // Get the set of URLs and their corresponding titles from the pasteboard. 169 // Get the set of URLs and their corresponding titles from the pasteboard.
170 // If there are no URLs in a format we understand on the pasteboard empty 170 // If there are no URLs in a format we understand on the pasteboard empty
171 // arrays will be returned. The two arrays will always be the same size. 171 // arrays will be returned. The two arrays will always be the same size.
172 // The arrays returned are on the auto release pool. If |convertFilenames| 172 // The arrays returned are on the auto release pool. If |convertFilenames|
173 // is YES, then the function will attempt to convert filenames in the drag 173 // is YES, then the function will attempt to convert filenames in the drag
174 // to file URLs. 174 // to file URLs.
175 - (void) getURLs:(NSArray**)outUrls 175 - (void)getURLsFromType:(NSArray**)outUrls
176 andTitles:(NSArray**)outTitles 176 andTitles:(NSArray**)outTitles
177 convertingFilenames:(BOOL)convertFilenames 177 convertingFilenames:(BOOL)convertFilenames {
178 {
179 NSArray* types = [self types]; 178 NSArray* types = [self types];
180 NSURL* urlFromNSURL = nil; // Used below in getting an URL from the NSURLPboa rdType. 179 NSURL* urlFromNSURL = nil; // Used below in getting an URL from the NSURLPboa rdType.
181 if ([types containsObject:kWebURLsWithTitlesPboardType]) { 180 if ([types containsObject:kWebURLsWithTitlesPboardType]) {
182 NSArray* urlAndTitleContainer = [self propertyListForType:kWebURLsWithTitles PboardType]; 181 NSArray* urlAndTitleContainer = [self propertyListForType:kWebURLsWithTitles PboardType];
183 *outUrls = [urlAndTitleContainer objectAtIndex:0]; 182 *outUrls = [urlAndTitleContainer objectAtIndex:0];
184 *outTitles = [urlAndTitleContainer objectAtIndex:1]; 183 *outTitles = [urlAndTitleContainer objectAtIndex:1];
185 } else if ([types containsObject:NSFilenamesPboardType]) { 184 } else if ([types containsObject:NSFilenamesPboardType]) {
186 NSArray *files = [self propertyListForType:NSFilenamesPboardType]; 185 NSArray *files = [self propertyListForType:NSFilenamesPboardType];
187 *outUrls = [NSMutableArray arrayWithCapacity:[files count]]; 186 *outUrls = [NSMutableArray arrayWithCapacity:[files count]];
188 *outTitles = [NSMutableArray arrayWithCapacity:[files count]]; 187 *outTitles = [NSMutableArray arrayWithCapacity:[files count]];
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
224 } else if ([types containsObject:NSURLPboardType] && (urlFromNSURL = [NSURL UR LFromPasteboard:self])) { 223 } else if ([types containsObject:NSURLPboardType] && (urlFromNSURL = [NSURL UR LFromPasteboard:self])) {
225 *outUrls = [NSArray arrayWithObject:[urlFromNSURL absoluteString]]; 224 *outUrls = [NSArray arrayWithObject:[urlFromNSURL absoluteString]];
226 NSString* title = nil; 225 NSString* title = nil;
227 if ([types containsObject:kCorePasteboardFlavorType_urld]) 226 if ([types containsObject:kCorePasteboardFlavorType_urld])
228 title = [self stringForType:kCorePasteboardFlavorType_urld]; 227 title = [self stringForType:kCorePasteboardFlavorType_urld];
229 if (!title && [types containsObject:kCorePasteboardFlavorType_urln]) 228 if (!title && [types containsObject:kCorePasteboardFlavorType_urln])
230 title = [self stringForType:kCorePasteboardFlavorType_urln]; 229 title = [self stringForType:kCorePasteboardFlavorType_urln];
231 if (!title && [types containsObject:NSStringPboardType]) 230 if (!title && [types containsObject:NSStringPboardType])
232 title = [self stringForType:NSStringPboardType]; 231 title = [self stringForType:NSStringPboardType];
233 *outTitles = [NSArray arrayWithObject:(title ? title : @"")]; 232 *outTitles = [NSArray arrayWithObject:(title ? title : @"")];
234 } else if ([types containsObject:NSStringPboardType]) { 233 } else {
235 NSString* potentialURLString = [self cleanedStringWithPasteboardString:[self stringForType:NSStringPboardType]]; 234 // We don't recognise any of these formats - return empty arrays
235 *outUrls = [NSArray array];
236 *outTitles = [NSArray array];
237 }
238 }
239
240 - (void)getURLsFromTypeOrText:(NSArray**)outUrls
Mark Mentovai 2016/05/26 20:21:51 GetURLsOrTextFromType
241 andTitles:(NSArray**)outTitles
Mark Mentovai 2016/05/26 20:21:51 align colons
242 convertingFilenames:(BOOL)convertFilenames
243 {
244 if ([self containsURLDataInType]) {
245 [self getURLsFromType:outUrls
246 andTitles:outTitles
Mark Mentovai 2016/05/26 20:21:51 align colons
247 convertingFilenames:convertFilenames];
248 return;
249 }
250
251 NSArray* types = [self types];
252 if ([types containsObject:NSStringPboardType]) {
253 NSString* potentialURLString =
254 [self cleanedStringWithPasteboardString:
255 [self stringForType:NSStringPboardType]];
236 if ([potentialURLString isValidURI]) { 256 if ([potentialURLString isValidURI]) {
237 *outUrls = [NSArray arrayWithObject:potentialURLString]; 257 *outUrls = [NSArray arrayWithObject:potentialURLString];
238 NSString* title = nil; 258 NSString* title = nil;
239 if ([types containsObject:kCorePasteboardFlavorType_urld]) 259 if ([types containsObject:kCorePasteboardFlavorType_urld])
240 title = [self stringForType:kCorePasteboardFlavorType_urld]; 260 title = [self stringForType:kCorePasteboardFlavorType_urld];
241 if (!title && [types containsObject:kCorePasteboardFlavorType_urln]) 261 if (!title && [types containsObject:kCorePasteboardFlavorType_urln])
242 title = [self stringForType:kCorePasteboardFlavorType_urln]; 262 title = [self stringForType:kCorePasteboardFlavorType_urln];
243 *outTitles = [NSArray arrayWithObject:(title ? title : @"")]; 263 *outTitles = [NSArray arrayWithObject:(title ? title : @"")];
244 } else { 264 } else {
245 // The string doesn't look like a URL - return empty arrays 265 // The string doesn't look like a URL - return empty arrays
246 *outUrls = [NSArray array]; 266 *outUrls = [NSArray array];
247 *outTitles = [NSArray array]; 267 *outTitles = [NSArray array];
248 } 268 }
249 } else {
250 // We don't recognise any of these formats - return empty arrays
251 *outUrls = [NSArray array];
252 *outTitles = [NSArray array];
253 } 269 }
254 } 270 }
255
256 // 271 //
257 // Indicates if this pasteboard contains URL data that we understand 272 // Indicates if this pasteboard contains URL data that we understand
258 // Deals with all our URL formats. Only strings that are valid URLs count. 273 // Deals with all our URL formats. Only strings that are valid URLs count.
259 // If this returns YES it is safe to use getURLs:andTitles: to retrieve the data . 274 // If this returns YES it is safe to use getURLs:andTitles: to retrieve the data .
260 // 275 //
261 // NB: Does not consider our internal bookmark list format, because callers 276 // NB: Does not consider our internal bookmark list format, because callers
262 // usually need to deal with this separately because it can include folders etc. 277 // usually need to deal with this separately because it can include folders etc.
263 // 278 //
264 - (BOOL) containsURLData 279 - (BOOL)containsURLDataInType
265 { 280 {
266 NSArray* types = [self types]; 281 NSArray* types = [self types];
267 if ( [types containsObject:kWebURLsWithTitlesPboardType] 282 return [types containsObject:kWebURLsWithTitlesPboardType] ||
268 || [types containsObject:NSURLPboardType] 283 [types containsObject:NSURLPboardType] ||
269 || [types containsObject:NSFilenamesPboardType] ) 284 [types containsObject:NSFilenamesPboardType];
285 }
286
287 - (BOOL) containsURLDataInTypeOrText
288 {
289 if([self containsURLDataInType])
270 return YES; 290 return YES;
271 291 if ([[self types] containsObject:NSStringPboardType]) {
272 if ([types containsObject:NSStringPboardType]) {
273 // Trim whitespace off the ends and newlines out of the middle so we don't r eject otherwise-valid URLs; 292 // Trim whitespace off the ends and newlines out of the middle so we don't r eject otherwise-valid URLs;
274 // we'll do another cleaning when we set the URLs and titles later, so this is safe. 293 // we'll do another cleaning when we set the URLs and titles later, so this is safe.
275 NSString* potentialURLString = [self cleanedStringWithPasteboardString:[self stringForType:NSStringPboardType]]; 294 NSString* potentialURLString = [self cleanedStringWithPasteboardString:[self stringForType:NSStringPboardType]];
276 return [potentialURLString isValidURI]; 295 return [potentialURLString isValidURI];
277 } 296 }
278
279 return NO; 297 return NO;
280 } 298 }
281 @end 299 @end
282 300
283 @implementation NSPasteboard(ChromiumHTMLUtils) 301 @implementation NSPasteboard(ChromiumHTMLUtils)
284 302
285 // Convert the RTF to HTML via an NSAttributedString. 303 // Convert the RTF to HTML via an NSAttributedString.
286 - (NSString*)htmlFromRtf { 304 - (NSString*)htmlFromRtf {
287 if (![[self types] containsObject:NSRTFPboardType]) 305 if (![[self types] containsObject:NSRTFPboardType])
288 return @""; 306 return @"";
289 307
290 NSAttributedString* attributed = 308 NSAttributedString* attributed =
291 [[[NSAttributedString alloc] 309 [[[NSAttributedString alloc]
292 initWithRTF:[self dataForType:NSRTFPboardType] 310 initWithRTF:[self dataForType:NSRTFPboardType]
293 documentAttributes:nil] autorelease]; 311 documentAttributes:nil] autorelease];
294 NSDictionary* attributeDict = 312 NSDictionary* attributeDict =
295 [NSDictionary dictionaryWithObject:NSHTMLTextDocumentType 313 [NSDictionary dictionaryWithObject:NSHTMLTextDocumentType
296 forKey:NSDocumentTypeDocumentAttribute]; 314 forKey:NSDocumentTypeDocumentAttribute];
297 NSData* htmlData = 315 NSData* htmlData =
298 [attributed dataFromRange:NSMakeRange(0, [attributed length]) 316 [attributed dataFromRange:NSMakeRange(0, [attributed length])
299 documentAttributes:attributeDict 317 documentAttributes:attributeDict
300 error:nil]; 318 error:nil];
301 // According to the docs, NSHTMLTextDocumentType is UTF8. 319 // According to the docs, NSHTMLTextDocumentType is UTF8.
302 return [[[NSString alloc] 320 return [[[NSString alloc]
303 initWithData:htmlData encoding:NSUTF8StringEncoding] autorelease]; 321 initWithData:htmlData encoding:NSUTF8StringEncoding] autorelease];
304 } 322 }
305 323
306 @end 324 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698