2 * Copyright (c) 2008, 2009, Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "Pasteboard.h"
34 #include "ClipboardUtilitiesChromium.h"
36 #include "DocumentFragment.h"
39 #include "HTMLNames.h"
40 #include "HTMLParserIdioms.h"
43 #include "NativeImageSkia.h"
44 #include "PlatformSupport.h"
46 #include "RenderImage.h"
51 #include "XLinkNames.h"
56 Pasteboard* Pasteboard::generalPasteboard()
58 static Pasteboard* pasteboard = new Pasteboard;
62 Pasteboard::Pasteboard()
63 : m_selectionMode(false)
67 void Pasteboard::clear()
69 // The ScopedClipboardWriter class takes care of clearing the clipboard's
73 bool Pasteboard::isSelectionMode() const
75 return m_selectionMode;
78 void Pasteboard::setSelectionMode(bool selectionMode)
80 m_selectionMode = selectionMode;
83 void Pasteboard::writeSelection(Range* selectedRange, bool canSmartCopyOrDelete, Frame* frame)
85 String html = createMarkup(selectedRange, 0, AnnotateForInterchange, false, ResolveNonLocalURLs);
87 KURL url = selectedRange->startContainer(ec)->document()->url();
88 String plainText = frame->editor()->selectedText();
90 replaceNewlinesWithWindowsStyleNewlines(plainText);
92 replaceNBSPWithSpace(plainText);
94 PlatformSupport::clipboardWriteSelection(html, url, plainText, canSmartCopyOrDelete);
97 void Pasteboard::writePlainText(const String& text)
100 String plainText(text);
101 replaceNewlinesWithWindowsStyleNewlines(plainText);
102 PlatformSupport::clipboardWritePlainText(plainText);
104 PlatformSupport::clipboardWritePlainText(text);
108 void Pasteboard::writeURL(const KURL& url, const String& titleStr, Frame* frame)
110 ASSERT(!url.isEmpty());
112 String title(titleStr);
113 if (title.isEmpty()) {
114 title = url.lastPathComponent();
119 PlatformSupport::clipboardWriteURL(url, title);
122 void Pasteboard::writeImage(Node* node, const KURL&, const String& title)
126 if (!(node->renderer() && node->renderer()->isImage()))
129 RenderImage* renderer = toRenderImage(node->renderer());
130 CachedImage* cachedImage = renderer->cachedImage();
131 if (!cachedImage || cachedImage->errorOccurred())
133 Image* image = cachedImage->image();
136 NativeImagePtr bitmap = image->nativeImageForCurrentFrame();
140 // If the image is wrapped in a link, |url| points to the target of the
141 // link. This isn't useful to us, so get the actual image URL.
142 AtomicString urlString;
143 if (node->hasTagName(HTMLNames::imgTag) || node->hasTagName(HTMLNames::inputTag))
144 urlString = static_cast<Element*>(node)->getAttribute(HTMLNames::srcAttr);
146 else if (node->hasTagName(SVGNames::imageTag))
147 urlString = static_cast<Element*>(node)->getAttribute(XLinkNames::hrefAttr);
149 else if (node->hasTagName(HTMLNames::embedTag) || node->hasTagName(HTMLNames::objectTag)) {
150 Element* element = static_cast<Element*>(node);
151 urlString = element->getAttribute(element->imageSourceAttributeName());
153 KURL url = urlString.isEmpty() ? KURL() : node->document()->completeURL(stripLeadingAndTrailingHTMLSpaces(urlString));
155 PlatformSupport::clipboardWriteImage(bitmap, url, title);
158 bool Pasteboard::canSmartReplace()
160 return PlatformSupport::clipboardIsFormatAvailable(PasteboardPrivate::WebSmartPasteFormat, m_selectionMode ? PasteboardPrivate::SelectionBuffer : PasteboardPrivate::StandardBuffer);
163 String Pasteboard::plainText(Frame* frame)
165 return PlatformSupport::clipboardReadPlainText(m_selectionMode ? PasteboardPrivate::SelectionBuffer : PasteboardPrivate::StandardBuffer);
168 PassRefPtr<DocumentFragment> Pasteboard::documentFragment(Frame* frame, PassRefPtr<Range> context, bool allowPlainText, bool& chosePlainText)
170 chosePlainText = false;
171 PasteboardPrivate::ClipboardBuffer buffer = m_selectionMode ? PasteboardPrivate::SelectionBuffer : PasteboardPrivate::StandardBuffer;
173 if (PlatformSupport::clipboardIsFormatAvailable(PasteboardPrivate::HTMLFormat, buffer)) {
176 PlatformSupport::clipboardReadHTML(buffer, &markup, &srcURL);
178 RefPtr<DocumentFragment> fragment =
179 createFragmentFromMarkup(frame->document(), markup, srcURL, FragmentScriptingNotAllowed);
181 return fragment.release();
184 if (allowPlainText) {
185 String markup = PlatformSupport::clipboardReadPlainText(buffer);
186 if (!markup.isEmpty()) {
187 chosePlainText = true;
189 RefPtr<DocumentFragment> fragment =
190 createFragmentFromText(context.get(), markup);
192 return fragment.release();
199 } // namespace WebCore