Refactor ClipboardMac class to use PlatformStrategies.
[WebKit-https.git] / Source / WebCore / platform / mac / PasteboardMac.mm
1 /*
2  * Copyright (C) 2006 Apple Computer, Inc.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
24  */
25
26 #import "config.h"
27 #import "Pasteboard.h"
28
29 #import "CachedResource.h"
30 #import "ClipboardMac.h"
31 #import "DOMRangeInternal.h"
32 #import "Document.h"
33 #import "DocumentFragment.h"
34 #import "DocumentLoader.h"
35 #import "Editor.h"
36 #import "EditorClient.h"
37 #import "Frame.h"
38 #import "FrameView.h"
39 #import "FrameLoaderClient.h"
40 #import "HitTestResult.h"
41 #import "HTMLAnchorElement.h"
42 #import "HTMLConverter.h"
43 #import "htmlediting.h"
44 #import "HTMLNames.h"
45 #import "Image.h"
46 #import "KURL.h"
47 #import "LegacyWebArchive.h"
48 #import "LoaderNSURLExtras.h"
49 #import "MIMETypeRegistry.h"
50 #import "Page.h"
51 #import "RenderImage.h"
52 #import "Text.h"
53 #import "WebCoreNSStringExtras.h"
54 #import "WebNSAttributedStringExtras.h"
55 #import "markup.h"
56 #import <wtf/StdLibExtras.h>
57 #import <wtf/RetainPtr.h>
58 #import <wtf/UnusedParam.h>
59 #import <wtf/unicode/CharacterNames.h>
60
61 #if USE(PLATFORM_STRATEGIES)
62 #include "PasteboardStrategy.h"
63 #include "PlatformStrategies.h"
64 #endif
65
66 namespace WebCore {
67
68 // FIXME: It's not great to have these both here and in WebKit.
69 const char* WebArchivePboardType = "Apple Web Archive pasteboard type";
70 const char* WebSmartPastePboardType = "NeXT smart paste pasteboard type";
71 const char* WebURLNamePboardType = "public.url-name";
72 const char* WebURLPboardType = "public.url";
73 const char* WebURLsWithTitlesPboardType = "WebURLsWithTitlesPboardType";
74
75 static Vector<String> selectionPasteboardTypes(bool canSmartCopyOrDelete, bool selectionContainsAttachments)
76 {
77     Vector<String> types;
78     if (canSmartCopyOrDelete)
79         types.append(WebSmartPastePboardType);
80     types.append(WebArchivePboardType);
81     if (selectionContainsAttachments)
82         types.append(String(NSRTFDPboardType));
83     types.append(String(NSRTFPboardType));
84     types.append(String(NSStringPboardType));
85
86     return types;
87 }
88
89 static const Vector<String> writableTypesForURL()
90 {
91     Vector<String> types;
92     
93     types.append(WebURLsWithTitlesPboardType);
94     types.append(String(NSURLPboardType));
95     types.append(WebURLPboardType);
96     types.append(WebURLNamePboardType);
97     types.append(String(NSStringPboardType));        
98     return types;
99 }
100
101 static inline Vector<String> createWritableTypesForImage()
102 {
103     Vector<String> types;
104     
105     types.append(String(NSTIFFPboardType));
106     types.append(writableTypesForURL());
107     types.append(String(NSRTFDPboardType));
108     return types;
109 }
110
111 static Vector<String> writableTypesForImage()
112 {
113     Vector<String> types;
114     types.append(createWritableTypesForImage());
115     return types;
116 }
117
118 Pasteboard* Pasteboard::generalPasteboard() 
119 {
120     static Pasteboard* pasteboard = new Pasteboard(NSGeneralPboard);
121     return pasteboard;
122 }
123
124 Pasteboard::Pasteboard(const String& pasteboardName)
125     : m_pasteboardName(pasteboardName)
126 {
127     ASSERT(pasteboardName);
128 }
129
130 void Pasteboard::clear()
131 {
132     platformStrategies()->pasteboardStrategy()->setTypes(Vector<String>(), m_pasteboardName);
133 }
134
135 void Pasteboard::writeSelectionForTypes(const Vector<String>& pasteboardTypes, Range* selectedRange, bool canSmartCopyOrDelete, Frame* frame)
136 {
137     if (!WebArchivePboardType)
138         Pasteboard::generalPasteboard(); // Initializes pasteboard types.
139     ASSERT(selectedRange);
140     
141     // If the selection is at the beginning of content inside an anchor tag
142     // we move the selection start to include the anchor.
143     // This way the attributed string will contain the url attribute as well.
144     // See <rdar://problem/9084267>.
145     ExceptionCode ec;
146     Node* commonAncestor = selectedRange->commonAncestorContainer(ec);
147     ASSERT(commonAncestor);
148     Node* enclosingAnchor = enclosingNodeWithTag(firstPositionInNode(commonAncestor), HTMLNames::aTag);
149     if (enclosingAnchor && comparePositions(firstPositionInOrBeforeNode(selectedRange->startPosition().anchorNode()), selectedRange->startPosition()) >= 0)
150         selectedRange->setStart(enclosingAnchor, 0, ec);
151
152     NSAttributedString *attributedString = nil;
153     RetainPtr<WebHTMLConverter> converter(AdoptNS, [[WebHTMLConverter alloc] initWithDOMRange:kit(selectedRange)]);
154     if (converter)
155         attributedString = [converter.get() attributedString];
156
157     const Vector<String> types = !pasteboardTypes.isEmpty() ? pasteboardTypes : selectionPasteboardTypes(canSmartCopyOrDelete, [attributedString containsAttachments]);
158     platformStrategies()->pasteboardStrategy()->setTypes(types, m_pasteboardName);
159     frame->editor()->client()->didSetSelectionTypesForPasteboard();
160     
161     // Put HTML on the pasteboard.
162     if (types.contains(WebArchivePboardType)) {
163         RefPtr<LegacyWebArchive> archive = LegacyWebArchive::createFromSelection(frame);
164         RetainPtr<CFDataRef> data = archive ? archive->rawDataRepresentation() : 0;
165         platformStrategies()->pasteboardStrategy()->setBufferForType(SharedBuffer::wrapNSData((NSData *)data.get()), WebArchivePboardType, m_pasteboardName);
166     }
167     
168     // Put the attributed string on the pasteboard (RTF/RTFD format).
169     if (types.contains(String(NSRTFDPboardType))) {
170         NSData *RTFDData = [attributedString RTFDFromRange:NSMakeRange(0, [attributedString length]) documentAttributes:nil];
171         platformStrategies()->pasteboardStrategy()->setBufferForType(SharedBuffer::wrapNSData((NSData *)RTFDData).get(), NSRTFDPboardType, m_pasteboardName);
172     }
173     if (types.contains(String(NSRTFPboardType))) {
174         if ([attributedString containsAttachments])
175             attributedString = attributedStringByStrippingAttachmentCharacters(attributedString);
176         NSData *RTFData = [attributedString RTFFromRange:NSMakeRange(0, [attributedString length]) documentAttributes:nil];
177         platformStrategies()->pasteboardStrategy()->setBufferForType(SharedBuffer::wrapNSData((NSData *)RTFData).get(), NSRTFPboardType, m_pasteboardName);
178     }
179     
180     // Put plain string on the pasteboard.
181     if (types.contains(String(NSStringPboardType))) {
182         // Map &nbsp; to a plain old space because this is better for source code, other browsers do it,
183         // and because HTML forces you to do this any time you want two spaces in a row.
184         String text = frame->editor()->selectedText();
185         NSMutableString *s = [[[(NSString*)text copy] autorelease] mutableCopy];
186         
187         NSString *NonBreakingSpaceString = [NSString stringWithCharacters:&noBreakSpace length:1];
188         [s replaceOccurrencesOfString:NonBreakingSpaceString withString:@" " options:0 range:NSMakeRange(0, [s length])];
189         platformStrategies()->pasteboardStrategy()->setStringForType(s, NSStringPboardType, m_pasteboardName);
190         [s release];
191     }
192     
193     if (types.contains(WebSmartPastePboardType))
194         platformStrategies()->pasteboardStrategy()->setBufferForType(0, WebSmartPastePboardType, m_pasteboardName);
195 }
196
197 void Pasteboard::writePlainText(const String& text)
198 {
199     Vector<String> types;
200     types.append(NSStringPboardType);
201     platformStrategies()->pasteboardStrategy()->setTypes(types, m_pasteboardName);
202     platformStrategies()->pasteboardStrategy()->setStringForType(text, NSStringPboardType, m_pasteboardName);
203 }
204     
205 void Pasteboard::writeSelection(Range* selectedRange, bool canSmartCopyOrDelete, Frame* frame)
206 {
207     writeSelectionForTypes(Vector<String>(), selectedRange, canSmartCopyOrDelete, frame);
208 }
209
210 static void writeURLForTypes(const Vector<String>& types, const String& pasteboardName, const KURL& url, const String& titleStr, Frame* frame)
211 {
212     platformStrategies()->pasteboardStrategy()->setTypes(types, pasteboardName);
213     
214     ASSERT(!url.isEmpty());
215     
216     NSURL *cocoaURL = url;
217     NSString *userVisibleString = frame->editor()->client()->userVisibleString(cocoaURL);
218     
219     NSString *title = (NSString*)titleStr;
220     if ([title length] == 0) {
221         title = [[cocoaURL path] lastPathComponent];
222         if ([title length] == 0)
223             title = userVisibleString;
224     }
225     if (types.contains(WebURLsWithTitlesPboardType)) {
226         Vector<String> paths;
227         paths.append(userVisibleString);
228         paths.append(titleStr.stripWhiteSpace());
229         platformStrategies()->pasteboardStrategy()->setPathnamesForType(paths, WebURLsWithTitlesPboardType, pasteboardName);
230     }
231     if (types.contains(String(NSURLPboardType)))
232         platformStrategies()->pasteboardStrategy()->setStringForType([cocoaURL absoluteString], NSURLPboardType, pasteboardName);
233     if (types.contains(WebURLPboardType))
234         platformStrategies()->pasteboardStrategy()->setStringForType(userVisibleString, WebURLPboardType, pasteboardName);
235     if (types.contains(WebURLNamePboardType))
236         platformStrategies()->pasteboardStrategy()->setStringForType(title, WebURLNamePboardType, pasteboardName);
237     if (types.contains(String(NSStringPboardType)))
238         platformStrategies()->pasteboardStrategy()->setStringForType(userVisibleString, NSStringPboardType, pasteboardName);
239 }
240     
241 void Pasteboard::writeURL(const KURL& url, const String& titleStr, Frame* frame)
242 {
243     writeURLForTypes(writableTypesForURL(), m_pasteboardName, url, titleStr, frame);
244 }
245
246 static NSFileWrapper* fileWrapperForImage(CachedResource* resource, NSURL *url)
247 {
248     SharedBuffer* coreData = resource->data();
249     NSData *data = [[[NSData alloc] initWithBytes:coreData->data() length:coreData->size()] autorelease];
250     NSFileWrapper *wrapper = [[[NSFileWrapper alloc] initRegularFileWithContents:data] autorelease];
251     String coreMIMEType = resource->response().mimeType();
252     NSString *MIMEType = nil;
253     if (!coreMIMEType.isNull())
254         MIMEType = coreMIMEType;
255     [wrapper setPreferredFilename:suggestedFilenameWithMIMEType(url, MIMEType)];
256     return wrapper;
257 }
258
259 static void writeFileWrapperAsRTFDAttachment(NSFileWrapper* wrapper, const String& pasteboardName)
260 {
261     NSTextAttachment *attachment = [[NSTextAttachment alloc] initWithFileWrapper:wrapper];
262     
263     NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:attachment];
264     [attachment release];
265     
266     NSData *RTFDData = [string RTFDFromRange:NSMakeRange(0, [string length]) documentAttributes:nil];
267     platformStrategies()->pasteboardStrategy()->setBufferForType(SharedBuffer::wrapNSData((NSData *)RTFDData).get(), NSRTFDPboardType, pasteboardName);
268 }
269
270 void Pasteboard::writeImage(Node* node, const KURL& url, const String& title)
271 {
272     ASSERT(node);
273
274     if (!(node->renderer() && node->renderer()->isImage()))
275         return;
276
277     NSURL *cocoaURL = url;
278     ASSERT(cocoaURL);
279
280     RenderImage* renderer = toRenderImage(node->renderer());
281     CachedImage* cachedImage = renderer->cachedImage();
282     if (!cachedImage || cachedImage->errorOccurred())
283         return;
284
285     writeURLForTypes(writableTypesForImage(), m_pasteboardName, cocoaURL, nsStringNilIfEmpty(title), node->document()->frame());
286     
287     Image* image = cachedImage->imageForRenderer(renderer);
288     ASSERT(image);
289     
290     platformStrategies()->pasteboardStrategy()->setBufferForType(SharedBuffer::wrapNSData((NSData *)image->getNSImage()), NSTIFFPboardType, m_pasteboardName);
291
292     String MIMEType = cachedImage->response().mimeType();
293     ASSERT(MIMETypeRegistry::isSupportedImageResourceMIMEType(MIMEType));
294
295     writeFileWrapperAsRTFDAttachment(fileWrapperForImage(cachedImage, cocoaURL), m_pasteboardName);
296 }
297
298 void Pasteboard::writeClipboard(Clipboard* clipboard)
299 {
300     platformStrategies()->pasteboardStrategy()->copy(static_cast<ClipboardMac*>(clipboard)->pasteboardName(), m_pasteboardName);
301 }
302
303 bool Pasteboard::canSmartReplace()
304 {
305     Vector<String> types;
306     platformStrategies()->pasteboardStrategy()->getTypes(types, m_pasteboardName);
307     return types.contains(WebSmartPastePboardType);
308 }
309
310 String Pasteboard::plainText(Frame* frame)
311 {
312     Vector<String> types;
313     platformStrategies()->pasteboardStrategy()->getTypes(types, m_pasteboardName);
314     
315     if (types.contains(String(NSStringPboardType)))
316         return [(NSString *)platformStrategies()->pasteboardStrategy()->stringForType(NSStringPboardType, m_pasteboardName) precomposedStringWithCanonicalMapping];
317     
318     NSAttributedString *attributedString = nil;
319     NSString *string = nil;
320
321     if (types.contains(String(NSRTFDPboardType))) {
322         RefPtr<SharedBuffer> data = platformStrategies()->pasteboardStrategy()->bufferForType(NSRTFDPboardType, m_pasteboardName);
323         attributedString = [[NSAttributedString alloc] initWithRTFD:[data->createNSData() autorelease] documentAttributes:NULL];
324     }
325     if (attributedString == nil && types.contains(String(NSRTFPboardType))) {
326         RefPtr<SharedBuffer> data = platformStrategies()->pasteboardStrategy()->bufferForType(NSRTFPboardType, m_pasteboardName);
327         attributedString = [[NSAttributedString alloc] initWithRTF:[data->createNSData() autorelease] documentAttributes:NULL];
328     }
329     if (attributedString != nil) {
330         string = [[attributedString string] precomposedStringWithCanonicalMapping];
331         [attributedString release];
332         return string;
333     }
334     
335     if (types.contains(String(NSFilenamesPboardType))) {
336         Vector<String> pathnames;
337         platformStrategies()->pasteboardStrategy()->getPathnamesForType(pathnames, NSFilenamesPboardType, m_pasteboardName);
338         for (size_t i = 0; i < pathnames.size(); i++)
339             string = [string length] ? @"\n" + pathnames[i] : pathnames[i];
340         string = [string precomposedStringWithCanonicalMapping];
341         if (string != nil)
342             return string;
343     }
344     
345     string = platformStrategies()->pasteboardStrategy()->stringForType(NSURLPboardType, m_pasteboardName);
346     if ([string length]) {
347         // FIXME: using the editorClient to call into webkit, for now, since 
348         // calling _web_userVisibleString from WebCore involves migrating a sizable web of 
349         // helper code that should either be done in a separate patch or figured out in another way.
350         string = frame->editor()->client()->userVisibleString([NSURL URLWithString:string]);
351         if ([string length] > 0)
352             return [string precomposedStringWithCanonicalMapping];
353     }
354
355     
356     return String(); 
357 }
358     
359 static PassRefPtr<DocumentFragment> documentFragmentWithImageResource(Frame* frame, PassRefPtr<ArchiveResource> resource)
360 {
361     if (DocumentLoader* loader = frame->loader()->documentLoader())
362         loader->addArchiveResource(resource.get());
363
364     RefPtr<Element> imageElement = frame->document()->createElement(HTMLNames::imgTag, false);
365     if (!imageElement)
366         return 0;
367
368     NSURL *URL = resource->url();
369     imageElement->setAttribute(HTMLNames::srcAttr, [URL isFileURL] ? [URL absoluteString] : resource->url());
370     RefPtr<DocumentFragment> fragment = frame->document()->createDocumentFragment();
371     if (fragment) {
372         ExceptionCode ec;
373         fragment->appendChild(imageElement, ec);
374         return fragment.release();       
375     }
376     return 0;
377 }
378
379 static PassRefPtr<DocumentFragment> documentFragmentWithRTF(Frame* frame, NSString *pasteboardType,const String& pastebordName)
380 {
381     if (!frame || !frame->document() || !frame->document()->isHTMLDocument())
382         return 0;
383
384     NSAttributedString *string = nil;
385     if (pasteboardType == NSRTFDPboardType) {
386         RefPtr<SharedBuffer> data = platformStrategies()->pasteboardStrategy()->bufferForType(NSRTFDPboardType, pastebordName);
387         string = [[NSAttributedString alloc] initWithRTFD:[data->createNSData() autorelease] documentAttributes:NULL];
388     }
389     if (string == nil) {
390         RefPtr<SharedBuffer> data = platformStrategies()->pasteboardStrategy()->bufferForType(NSRTFPboardType, pastebordName);
391         string = [[NSAttributedString alloc] initWithRTF:[data->createNSData() autorelease] documentAttributes:NULL];
392     }
393     if (string == nil)
394         return nil;
395
396     bool wasDeferringCallbacks = frame->page()->defersLoading();
397     if (!wasDeferringCallbacks)
398         frame->page()->setDefersLoading(true);
399
400     Vector<RefPtr<ArchiveResource> > resources;
401     RefPtr<DocumentFragment> fragment = frame->editor()->client()->documentFragmentFromAttributedString(string, resources);
402
403     size_t size = resources.size();
404     if (size) {
405         DocumentLoader* loader = frame->loader()->documentLoader();
406         for (size_t i = 0; i < size; ++i)
407             loader->addArchiveResource(resources[i]);    
408     }
409
410     if (!wasDeferringCallbacks)
411         frame->page()->setDefersLoading(false);
412
413     [string release];
414     return fragment.release();
415 }
416
417 #define WebDataProtocolScheme @"webkit-fake-url"
418
419 static NSURL* uniqueURLWithRelativePart(NSString *relativePart)
420 {
421     CFUUIDRef UUIDRef = CFUUIDCreate(kCFAllocatorDefault);
422     NSString *UUIDString = (NSString *)CFUUIDCreateString(kCFAllocatorDefault, UUIDRef);
423     CFRelease(UUIDRef);
424     NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"%@://%@/%@", WebDataProtocolScheme, UUIDString, relativePart]];
425     CFRelease(UUIDString);
426
427     return URL;
428 }
429
430 PassRefPtr<DocumentFragment> Pasteboard::documentFragment(Frame* frame, PassRefPtr<Range> context, bool allowPlainText, bool& chosePlainText)
431 {
432     Vector<String> types;
433     platformStrategies()->pasteboardStrategy()->getTypes(types, m_pasteboardName);
434     RefPtr<DocumentFragment> fragment;
435     chosePlainText = false;
436
437     if (types.contains(WebArchivePboardType)) {
438         RefPtr<LegacyWebArchive> coreArchive = LegacyWebArchive::create(KURL(), platformStrategies()->pasteboardStrategy()->bufferForType(WebArchivePboardType, m_pasteboardName).get());
439         if (coreArchive) {
440             RefPtr<ArchiveResource> mainResource = coreArchive->mainResource();
441             if (mainResource) {
442                 NSString *MIMEType = mainResource->mimeType();
443                 if (!frame || !frame->document())
444                     return 0;
445                 if (frame->loader()->client()->canShowMIMETypeAsHTML(MIMEType)) {
446                     NSString *markupString = [[NSString alloc] initWithData:[mainResource->data()->createNSData() autorelease] encoding:NSUTF8StringEncoding];
447                     // FIXME: seems poor form to do this as a side effect of getting a document fragment
448                     if (DocumentLoader* loader = frame->loader()->documentLoader())
449                         loader->addAllArchiveResources(coreArchive.get());
450                     
451                     fragment = createFragmentFromMarkup(frame->document(), markupString, mainResource->url(), FragmentScriptingNotAllowed);
452                     [markupString release];
453                 } else if (MIMETypeRegistry::isSupportedImageMIMEType(MIMEType))
454                    fragment = documentFragmentWithImageResource(frame, mainResource);                    
455             }
456         }
457         if (fragment)
458             return fragment.release();
459     } 
460
461     if (types.contains(String(NSFilenamesPboardType))) {
462         Vector<String> paths;
463         platformStrategies()->pasteboardStrategy()->getPathnamesForType(paths, NSFilenamesPboardType, m_pasteboardName);
464         Vector< RefPtr<Node> > refNodesVector;
465         Vector<Node*> nodesVector;
466
467         for (size_t i = 0; i < paths.size(); i++) {
468             // Non-image file types; _web_userVisibleString is appropriate here because this will
469             // be pasted as visible text.
470             NSString *url = frame->editor()->client()->userVisibleString([NSURL fileURLWithPath:paths[i]]);
471             RefPtr<Node> textNode = frame->document()->createTextNode(url);
472             refNodesVector.append(textNode.get());
473             nodesVector.append(textNode.get());
474         }
475         fragment = createFragmentFromNodes(frame->document(), nodesVector);
476         if (fragment && fragment->firstChild())
477             return fragment.release();
478     }
479
480     if (types.contains(String(NSHTMLPboardType))) {
481         NSString *HTMLString = platformStrategies()->pasteboardStrategy()->stringForType(NSHTMLPboardType, m_pasteboardName);
482         // This is a hack to make Microsoft's HTML pasteboard data work. See 3778785.
483         if ([HTMLString hasPrefix:@"Version:"]) {
484             NSRange range = [HTMLString rangeOfString:@"<html" options:NSCaseInsensitiveSearch];
485             if (range.location != NSNotFound) {
486                 HTMLString = [HTMLString substringFromIndex:range.location];
487             }
488         }
489         if ([HTMLString length] != 0 &&
490             (fragment = createFragmentFromMarkup(frame->document(), HTMLString, "", FragmentScriptingNotAllowed)))
491             return fragment.release();
492     }
493
494     if (types.contains(String(NSRTFDPboardType)) &&
495         (fragment = documentFragmentWithRTF(frame, NSRTFDPboardType, m_pasteboardName)))
496        return fragment.release();
497
498     if (types.contains(String(NSRTFPboardType)) &&
499         (fragment = documentFragmentWithRTF(frame, NSRTFPboardType, m_pasteboardName)))
500         return fragment.release();
501
502     if (types.contains(String(NSTIFFPboardType)) &&
503         (fragment = documentFragmentWithImageResource(frame, ArchiveResource::create(platformStrategies()->pasteboardStrategy()->bufferForType(NSTIFFPboardType, m_pasteboardName), uniqueURLWithRelativePart(@"image.tiff"), "image/tiff", "", ""))))
504         return fragment.release();
505
506     if (types.contains(String(NSPDFPboardType)) &&
507         (fragment = documentFragmentWithImageResource(frame, ArchiveResource::create(platformStrategies()->pasteboardStrategy()->bufferForType(NSPDFPboardType, m_pasteboardName).get(), uniqueURLWithRelativePart(@"application.pdf"), "application/pdf", "", ""))))
508         return fragment.release();
509
510     if (types.contains(String(kUTTypePNG)) &&
511         (fragment = documentFragmentWithImageResource(frame, ArchiveResource::create(platformStrategies()->pasteboardStrategy()->bufferForType(String(kUTTypePNG), m_pasteboardName), uniqueURLWithRelativePart(@"image.png"), "image/png", "", ""))))
512         return fragment.release();
513
514     if (types.contains(String(NSURLPboardType))) {
515         NSURL *URL = [NSURL URLWithString:platformStrategies()->pasteboardStrategy()->stringForType(NSURLPboardType, m_pasteboardName)];
516         Document* document = frame->document();
517         ASSERT(document);
518         if (!document)
519             return 0;
520         RefPtr<Element> anchor = document->createElement(HTMLNames::aTag, false);
521         NSString *URLString = [URL absoluteString]; // Original data is ASCII-only, so there is no need to precompose.
522         if ([URLString length] == 0)
523             return nil;
524         NSString *URLTitleString = [platformStrategies()->pasteboardStrategy()->stringForType(WebURLNamePboardType, m_pasteboardName) precomposedStringWithCanonicalMapping];
525         ExceptionCode ec;
526         anchor->setAttribute(HTMLNames::hrefAttr, URLString);
527         anchor->appendChild(document->createTextNode(URLTitleString), ec);
528         fragment = document->createDocumentFragment();
529         if (fragment) {
530             fragment->appendChild(anchor, ec);
531             return fragment.release();
532         }
533     }
534
535     if (allowPlainText && types.contains(String(NSStringPboardType))) {
536         chosePlainText = true;
537         fragment = createFragmentFromText(context.get(), [platformStrategies()->pasteboardStrategy()->stringForType(NSStringPboardType, m_pasteboardName) precomposedStringWithCanonicalMapping]);
538         return fragment.release();
539     }
540
541     return 0;
542 }
543
544 }