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