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 "ClipboardChromium.h"
35
#include "ClipboardUtilitiesChromium.h"
37
#include "DocumentFragment.h"
40
#include "HTMLNames.h"
41
#include "HTMLParserIdioms.h"
44
#include "NativeImageSkia.h"
46
#include "RenderImage.h"
51
#include "XLinkNames.h"
54
#include <public/Platform.h>
55
#include <public/WebClipboard.h>
56
#include <public/WebDragData.h>
60
Pasteboard* Pasteboard::generalPasteboard()
62
static Pasteboard* pasteboard = new Pasteboard;
66
Pasteboard::Pasteboard()
67
: m_selectionMode(false)
71
void Pasteboard::clear()
73
// The ScopedClipboardWriter class takes care of clearing the clipboard's
77
bool Pasteboard::isSelectionMode() const
79
return m_selectionMode;
82
void Pasteboard::setSelectionMode(bool selectionMode)
84
m_selectionMode = selectionMode;
87
void Pasteboard::writeSelection(Range* selectedRange, bool canSmartCopyOrDelete, Frame* frame)
89
String html = createMarkup(selectedRange, 0, AnnotateForInterchange, false, ResolveNonLocalURLs);
91
KURL url = selectedRange->startContainer(ec)->document()->url();
92
String plainText = frame->editor()->selectedText();
94
replaceNewlinesWithWindowsStyleNewlines(plainText);
96
replaceNBSPWithSpace(plainText);
98
WebKit::Platform::current()->clipboard()->writeHTML(html, url, plainText, canSmartCopyOrDelete);
101
void Pasteboard::writePlainText(const String& text, SmartReplaceOption)
103
// FIXME: add support for smart replace
105
String plainText(text);
106
replaceNewlinesWithWindowsStyleNewlines(plainText);
107
WebKit::Platform::current()->clipboard()->writePlainText(plainText);
109
WebKit::Platform::current()->clipboard()->writePlainText(text);
113
void Pasteboard::writeURL(const KURL& url, const String& titleStr, Frame* frame)
115
ASSERT(!url.isEmpty());
117
String title(titleStr);
118
if (title.isEmpty()) {
119
title = url.lastPathComponent();
124
WebKit::Platform::current()->clipboard()->writeURL(url, title);
127
void Pasteboard::writeImage(Node* node, const KURL&, const String& title)
131
if (!(node->renderer() && node->renderer()->isImage()))
134
RenderImage* renderer = toRenderImage(node->renderer());
135
CachedImage* cachedImage = renderer->cachedImage();
136
if (!cachedImage || cachedImage->errorOccurred())
138
Image* image = cachedImage->imageForRenderer(renderer);
141
NativeImagePtr bitmap = image->nativeImageForCurrentFrame();
145
// If the image is wrapped in a link, |url| points to the target of the
146
// link. This isn't useful to us, so get the actual image URL.
147
AtomicString urlString;
148
if (node->hasTagName(HTMLNames::imgTag) || node->hasTagName(HTMLNames::inputTag))
149
urlString = static_cast<Element*>(node)->getAttribute(HTMLNames::srcAttr);
151
else if (node->hasTagName(SVGNames::imageTag))
152
urlString = static_cast<Element*>(node)->getAttribute(XLinkNames::hrefAttr);
154
else if (node->hasTagName(HTMLNames::embedTag) || node->hasTagName(HTMLNames::objectTag)) {
155
Element* element = static_cast<Element*>(node);
156
urlString = element->getAttribute(element->imageSourceAttributeName());
158
KURL url = urlString.isEmpty() ? KURL() : node->document()->completeURL(stripLeadingAndTrailingHTMLSpaces(urlString));
159
WebKit::WebImage webImage = bitmap->bitmap();
160
WebKit::Platform::current()->clipboard()->writeImage(webImage, WebKit::WebURL(url), WebKit::WebString(title));
163
void Pasteboard::writeClipboard(Clipboard* clipboard)
165
WebKit::WebDragData dragData = static_cast<ClipboardChromium*>(clipboard)->dataObject();
166
WebKit::Platform::current()->clipboard()->writeDataObject(dragData);
169
bool Pasteboard::canSmartReplace()
171
return WebKit::Platform::current()->clipboard()->isFormatAvailable(WebKit::WebClipboard::FormatSmartPaste, m_selectionMode ? WebKit::WebClipboard::BufferSelection : WebKit::WebClipboard::BufferStandard);
174
String Pasteboard::plainText(Frame* frame)
176
return WebKit::Platform::current()->clipboard()->readPlainText(m_selectionMode ? WebKit::WebClipboard::BufferSelection : WebKit::WebClipboard::BufferStandard);
179
PassRefPtr<DocumentFragment> Pasteboard::documentFragment(Frame* frame, PassRefPtr<Range> context, bool allowPlainText, bool& chosePlainText)
181
chosePlainText = false;
182
WebKit::WebClipboard::Buffer buffer = m_selectionMode ? WebKit::WebClipboard::BufferSelection : WebKit::WebClipboard::BufferStandard;
184
if (WebKit::Platform::current()->clipboard()->isFormatAvailable(WebKit::WebClipboard::FormatHTML, buffer)) {
185
WebKit::WebString markup;
186
unsigned fragmentStart = 0;
187
unsigned fragmentEnd = 0;
189
markup = WebKit::Platform::current()->clipboard()->readHTML(buffer, &url, &fragmentStart, &fragmentEnd);
190
if (!markup.isEmpty()) {
191
RefPtr<DocumentFragment> fragment =
192
createFragmentFromMarkupWithContext(frame->document(), markup, fragmentStart, fragmentEnd, KURL(url), DisallowScriptingContent);
194
return fragment.release();
198
if (allowPlainText) {
199
String markup = WebKit::Platform::current()->clipboard()->readPlainText(buffer);
200
if (!markup.isEmpty()) {
201
chosePlainText = true;
203
RefPtr<DocumentFragment> fragment =
204
createFragmentFromText(context.get(), markup);
206
return fragment.release();
213
} // namespace WebCore