Home | History | Annotate | Download | only in WebCoreSupport
      1 /*
      2  * Copyright (C) 2007 Apple 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 "WebPasteboardHelper.h"
     27 
     28 #import "WebArchive.h"
     29 #import "WebHTMLViewInternal.h"
     30 #import "WebNSPasteboardExtras.h"
     31 #import "WebNSURLExtras.h"
     32 #import <WebCore/PlatformString.h>
     33 #import <WebKit/DOMDocument.h>
     34 #import <WebKit/DOMDocumentFragment.h>
     35 #import <wtf/RetainPtr.h>
     36 #import <wtf/StdLibExtras.h>
     37 
     38 using namespace WebCore;
     39 
     40 String WebPasteboardHelper::urlFromPasteboard(const NSPasteboard* pasteboard, String* title) const
     41 {
     42     NSURL *URL = [pasteboard _web_bestURL];
     43     if (title) {
     44         if (NSString *URLTitleString = [pasteboard stringForType:WebURLNamePboardType])
     45             *title = URLTitleString;
     46     }
     47 
     48     return [URL _web_originalDataAsString];
     49 }
     50 
     51 String WebPasteboardHelper::plainTextFromPasteboard(const NSPasteboard *pasteboard) const
     52 {
     53     NSArray *types = [pasteboard types];
     54 
     55     if ([types containsObject:NSStringPboardType])
     56         return [[pasteboard stringForType:NSStringPboardType] precomposedStringWithCanonicalMapping];
     57 
     58     NSAttributedString *attributedString = nil;
     59     NSString *string;
     60 
     61     if ([types containsObject:NSRTFDPboardType])
     62         attributedString = [[NSAttributedString alloc] initWithRTFD:[pasteboard dataForType:NSRTFDPboardType] documentAttributes:nil];
     63     if (!attributedString && [types containsObject:NSRTFPboardType])
     64         attributedString = [[NSAttributedString alloc] initWithRTF:[pasteboard dataForType:NSRTFPboardType] documentAttributes:nil];
     65     if (attributedString) {
     66         string = [[attributedString string] precomposedStringWithCanonicalMapping];
     67         [attributedString release];
     68         return string;
     69     }
     70 
     71     if ([types containsObject:NSFilenamesPboardType]) {
     72         string = [[pasteboard propertyListForType:NSFilenamesPboardType] componentsJoinedByString:@"\n"];
     73         if (string)
     74             return [string precomposedStringWithCanonicalMapping];
     75     }
     76 
     77     NSURL *URL;
     78 
     79     if ((URL = [NSURL URLFromPasteboard:pasteboard])) {
     80         string = [URL _web_userVisibleString];
     81         if ([string length] > 0)
     82             return string;
     83     }
     84 
     85     return String();
     86 }
     87 
     88 DOMDocumentFragment *WebPasteboardHelper::fragmentFromPasteboard(const NSPasteboard *pasteboard) const
     89 {
     90     return [m_view _documentFragmentFromPasteboard:pasteboard];
     91 }
     92 
     93 NSArray *WebPasteboardHelper::insertablePasteboardTypes() const
     94 {
     95     DEFINE_STATIC_LOCAL(RetainPtr<NSArray>, types, ([[NSArray alloc] initWithObjects:WebArchivePboardType, NSHTMLPboardType, NSFilenamesPboardType, NSTIFFPboardType, NSPDFPboardType,
     96 #if defined(BUILDING_ON_TIGER) || defined(BUILDING_ON_LEOPARD)
     97            NSPICTPboardType,
     98 #endif
     99            NSURLPboardType, NSRTFDPboardType, NSRTFPboardType, NSStringPboardType, NSColorPboardType, nil]));
    100 
    101     return types.get();
    102 }
    103