Refactor Mac platform implementation of the Pasteboard class to use Platform Strategies.
[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     // FIXME: this is the last access to NSPasteboard. It will removed when the ClipboardMac
301     // class is refactored.
302     NSPasteboard* pasteboard = static_cast<ClipboardMac*>(clipboard)->pasteboard();
303     platformStrategies()->pasteboardStrategy()->copy([pasteboard name], m_pasteboardName);
304 }
305
306 bool Pasteboard::canSmartReplace()
307 {
308     Vector<String> types;
309     platformStrategies()->pasteboardStrategy()->getTypes(types, m_pasteboardName);
310     return types.contains(WebSmartPastePboardType);
311 }
312
313 String Pasteboard::plainText(Frame* frame)
314 {
315     Vector<String> types;
316     platformStrategies()->pasteboardStrategy()->getTypes(types, m_pasteboardName);
317     
318     if (types.contains(String(NSStringPboardType)))
319         return [(NSString *)platformStrategies()->pasteboardStrategy()->stringForType(NSStringPboardType, m_pasteboardName) precomposedStringWithCanonicalMapping];
320     
321     NSAttributedString *attributedString = nil;
322     NSString *string;
323
324     if (types.contains(String(NSRTFDPboardType))) {
325         RefPtr<SharedBuffer> data = platformStrategies()->pasteboardStrategy()->bufferForType(NSRTFDPboardType, m_pasteboardName);
326         attributedString = [[NSAttributedString alloc] initWithRTFD:[data->createNSData() autorelease] documentAttributes:NULL];
327     }
328     if (attributedString == nil && types.contains(String(NSRTFPboardType))) {
329         RefPtr<SharedBuffer> data = platformStrategies()->pasteboardStrategy()->bufferForType(NSRTFPboardType, m_pasteboardName);
330         attributedString = [[NSAttributedString alloc] initWithRTF:[data->createNSData() autorelease] documentAttributes:NULL];
331     }
332     if (attributedString != nil) {
333         string = [[attributedString string] precomposedStringWithCanonicalMapping];
334         [attributedString release];
335         return string;
336     }
337     
338     if (types.contains(String(NSFilenamesPboardType))) {
339         Vector<String> pathnames;
340         platformStrategies()->pasteboardStrategy()->getPathnamesForType(pathnames, NSFilenamesPboardType, m_pasteboardName);
341         for (size_t i = 0; i < pathnames.size(); i++)
342             string = [string length] ? @"\n" + pathnames[i] : pathnames[i];
343         string = [string precomposedStringWithCanonicalMapping];
344         if (string != nil)
345             return string;
346     }
347     
348     string = platformStrategies()->pasteboardStrategy()->stringForType(NSURLPboardType, m_pasteboardName);
349     if ([string length]) {
350         // FIXME: using the editorClient to call into webkit, for now, since 
351         // calling _web_userVisibleString from WebCore involves migrating a sizable web of 
352         // helper code that should either be done in a separate patch or figured out in another way.
353         string = frame->editor()->client()->userVisibleString([NSURL URLWithString:string]);
354         if ([string length] > 0)
355             return [string precomposedStringWithCanonicalMapping];
356     }
357
358     
359     return String(); 
360 }
361     
362 static PassRefPtr<DocumentFragment> documentFragmentWithImageResource(Frame* frame, PassRefPtr<ArchiveResource> resource)
363 {
364     if (DocumentLoader* loader = frame->loader()->documentLoader())
365         loader->addArchiveResource(resource.get());
366
367     RefPtr<Element> imageElement = frame->document()->createElement(HTMLNames::imgTag, false);
368     if (!imageElement)
369         return 0;
370
371     NSURL *URL = resource->url();
372     imageElement->setAttribute(HTMLNames::srcAttr, [URL isFileURL] ? [URL absoluteString] : resource->url());
373     RefPtr<DocumentFragment> fragment = frame->document()->createDocumentFragment();
374     if (fragment) {
375         ExceptionCode ec;
376         fragment->appendChild(imageElement, ec);
377         return fragment.release();       
378     }
379     return 0;
380 }
381
382 static PassRefPtr<DocumentFragment> documentFragmentWithRTF(Frame* frame, NSString *pasteboardType,const String& pastebordName)
383 {
384     if (!frame || !frame->document() || !frame->document()->isHTMLDocument())
385         return 0;
386
387     NSAttributedString *string = nil;
388     if (pasteboardType == NSRTFDPboardType) {
389         RefPtr<SharedBuffer> data = platformStrategies()->pasteboardStrategy()->bufferForType(NSRTFDPboardType, pastebordName);
390         string = [[NSAttributedString alloc] initWithRTFD:[data->createNSData() autorelease] documentAttributes:NULL];
391     }
392     if (string == nil) {
393         RefPtr<SharedBuffer> data = platformStrategies()->pasteboardStrategy()->bufferForType(NSRTFPboardType, pastebordName);
394         string = [[NSAttributedString alloc] initWithRTF:[data->createNSData() autorelease] documentAttributes:NULL];
395     }
396     if (string == nil)
397         return nil;
398
399     bool wasDeferringCallbacks = frame->page()->defersLoading();
400     if (!wasDeferringCallbacks)
401         frame->page()->setDefersLoading(true);
402
403     Vector<RefPtr<ArchiveResource> > resources;
404     RefPtr<DocumentFragment> fragment = frame->editor()->client()->documentFragmentFromAttributedString(string, resources);
405
406     size_t size = resources.size();
407     if (size) {
408         DocumentLoader* loader = frame->loader()->documentLoader();
409         for (size_t i = 0; i < size; ++i)
410             loader->addArchiveResource(resources[i]);    
411     }
412
413     if (!wasDeferringCallbacks)
414         frame->page()->setDefersLoading(false);
415
416     [string release];
417     return fragment.release();
418 }
419
420 #define WebDataProtocolScheme @"webkit-fake-url"
421
422 static NSURL* uniqueURLWithRelativePart(NSString *relativePart)
423 {
424     CFUUIDRef UUIDRef = CFUUIDCreate(kCFAllocatorDefault);
425     NSString *UUIDString = (NSString *)CFUUIDCreateString(kCFAllocatorDefault, UUIDRef);
426     CFRelease(UUIDRef);
427     NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"%@://%@/%@", WebDataProtocolScheme, UUIDString, relativePart]];
428     CFRelease(UUIDString);
429
430     return URL;
431 }
432
433 PassRefPtr<DocumentFragment> Pasteboard::documentFragment(Frame* frame, PassRefPtr<Range> context, bool allowPlainText, bool& chosePlainText)
434 {
435     Vector<String> types;
436     platformStrategies()->pasteboardStrategy()->getTypes(types, m_pasteboardName);
437     RefPtr<DocumentFragment> fragment;
438     chosePlainText = false;
439
440     if (types.contains(WebArchivePboardType)) {
441         RefPtr<LegacyWebArchive> coreArchive = LegacyWebArchive::create(KURL(), platformStrategies()->pasteboardStrategy()->bufferForType(WebArchivePboardType, m_pasteboardName).get());
442         if (coreArchive) {
443             RefPtr<ArchiveResource> mainResource = coreArchive->mainResource();
444             if (mainResource) {
445                 NSString *MIMEType = mainResource->mimeType();
446                 if (!frame || !frame->document())
447                     return 0;
448                 if (frame->loader()->client()->canShowMIMETypeAsHTML(MIMEType)) {
449                     NSString *markupString = [[NSString alloc] initWithData:[mainResource->data()->createNSData() autorelease] encoding:NSUTF8StringEncoding];
450                     // FIXME: seems poor form to do this as a side effect of getting a document fragment
451                     if (DocumentLoader* loader = frame->loader()->documentLoader())
452                         loader->addAllArchiveResources(coreArchive.get());
453                     
454                     fragment = createFragmentFromMarkup(frame->document(), markupString, mainResource->url(), FragmentScriptingNotAllowed);
455                     [markupString release];
456                 } else if (MIMETypeRegistry::isSupportedImageMIMEType(MIMEType))
457                    fragment = documentFragmentWithImageResource(frame, mainResource);                    
458             }
459         }
460         if (fragment)
461             return fragment.release();
462     } 
463
464     if (types.contains(String(NSFilenamesPboardType))) {
465         Vector<String> paths;
466         platformStrategies()->pasteboardStrategy()->getPathnamesForType(paths, NSFilenamesPboardType, m_pasteboardName);
467         Vector< RefPtr<Node> > refNodesVector;
468         Vector<Node*> nodesVector;
469
470         for (size_t i = 0; i < paths.size(); i++) {
471             // Non-image file types; _web_userVisibleString is appropriate here because this will
472             // be pasted as visible text.
473             NSString *url = frame->editor()->client()->userVisibleString([NSURL fileURLWithPath:paths[i]]);
474             RefPtr<Node> textNode = frame->document()->createTextNode(url);
475             refNodesVector.append(textNode.get());
476             nodesVector.append(textNode.get());
477         }
478         fragment = createFragmentFromNodes(frame->document(), nodesVector);
479         if (fragment && fragment->firstChild())
480             return fragment.release();
481     }
482
483     if (types.contains(String(NSHTMLPboardType))) {
484         NSString *HTMLString = platformStrategies()->pasteboardStrategy()->stringForType(NSHTMLPboardType, m_pasteboardName);
485         // This is a hack to make Microsoft's HTML pasteboard data work. See 3778785.
486         if ([HTMLString hasPrefix:@"Version:"]) {
487             NSRange range = [HTMLString rangeOfString:@"<html" options:NSCaseInsensitiveSearch];
488             if (range.location != NSNotFound) {
489                 HTMLString = [HTMLString substringFromIndex:range.location];
490             }
491         }
492         if ([HTMLString length] != 0 &&
493             (fragment = createFragmentFromMarkup(frame->document(), HTMLString, "", FragmentScriptingNotAllowed)))
494             return fragment.release();
495     }
496
497     if (types.contains(String(NSRTFDPboardType)) &&
498         (fragment = documentFragmentWithRTF(frame, NSRTFDPboardType, m_pasteboardName)))
499        return fragment.release();
500
501     if (types.contains(String(NSRTFPboardType)) &&
502         (fragment = documentFragmentWithRTF(frame, NSRTFPboardType, m_pasteboardName)))
503         return fragment.release();
504
505     if (types.contains(String(NSTIFFPboardType)) &&
506         (fragment = documentFragmentWithImageResource(frame, ArchiveResource::create(platformStrategies()->pasteboardStrategy()->bufferForType(NSTIFFPboardType, m_pasteboardName), uniqueURLWithRelativePart(@"image.tiff"), "image/tiff", "", ""))))
507         return fragment.release();
508
509     if (types.contains(String(NSPDFPboardType)) &&
510         (fragment = documentFragmentWithImageResource(frame, ArchiveResource::create(platformStrategies()->pasteboardStrategy()->bufferForType(NSPDFPboardType, m_pasteboardName).get(), uniqueURLWithRelativePart(@"application.pdf"), "application/pdf", "", ""))))
511         return fragment.release();
512
513     if (types.contains(String(kUTTypePNG)) &&
514         (fragment = documentFragmentWithImageResource(frame, ArchiveResource::create(platformStrategies()->pasteboardStrategy()->bufferForType(String(kUTTypePNG), m_pasteboardName), uniqueURLWithRelativePart(@"image.png"), "image/png", "", ""))))
515         return fragment.release();
516
517     if (types.contains(String(NSURLPboardType))) {
518         NSURL *URL = [NSURL URLWithString:platformStrategies()->pasteboardStrategy()->stringForType(NSURLPboardType, m_pasteboardName)];
519         Document* document = frame->document();
520         ASSERT(document);
521         if (!document)
522             return 0;
523         RefPtr<Element> anchor = document->createElement(HTMLNames::aTag, false);
524         NSString *URLString = [URL absoluteString]; // Original data is ASCII-only, so there is no need to precompose.
525         if ([URLString length] == 0)
526             return nil;
527         NSString *URLTitleString = [platformStrategies()->pasteboardStrategy()->stringForType(WebURLNamePboardType, m_pasteboardName) precomposedStringWithCanonicalMapping];
528         ExceptionCode ec;
529         anchor->setAttribute(HTMLNames::hrefAttr, URLString);
530         anchor->appendChild(document->createTextNode(URLTitleString), ec);
531         fragment = document->createDocumentFragment();
532         if (fragment) {
533             fragment->appendChild(anchor, ec);
534             return fragment.release();
535         }
536     }
537
538     if (allowPlainText && types.contains(String(NSStringPboardType))) {
539         chosePlainText = true;
540         fragment = createFragmentFromText(context.get(), [platformStrategies()->pasteboardStrategy()->stringForType(NSStringPboardType, m_pasteboardName) precomposedStringWithCanonicalMapping]);
541         return fragment.release();
542     }
543
544     return 0;
545 }
546
547 }