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