2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2001-02-15 16:22:26 +03:00
|
|
|
|
|
|
|
#include "nsCopySupport.h"
|
|
|
|
#include "nsIDocumentEncoder.h"
|
|
|
|
#include "nsISupports.h"
|
|
|
|
#include "nsIContent.h"
|
2017-07-06 15:00:35 +03:00
|
|
|
#include "nsIComponentManager.h"
|
2001-02-15 16:22:26 +03:00
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIClipboard.h"
|
2015-06-17 13:44:27 +03:00
|
|
|
#include "nsIFormControl.h"
|
2001-02-15 16:22:26 +03:00
|
|
|
#include "nsWidgetsCID.h"
|
2002-09-04 03:36:13 +04:00
|
|
|
#include "nsXPCOM.h"
|
2001-02-15 16:22:26 +03:00
|
|
|
#include "nsISupportsPrimitives.h"
|
2010-07-13 00:24:25 +04:00
|
|
|
#include "nsRange.h"
|
Bug 753 - Remove nsIImage, gfxIImageFrame, and their implementations, and expose an equivalent api on imgIContainer. r=roc,josh,bz,longsonr,vlad,karlt,jimm,bsmedberg,mfinkle,peterw,peterv sr=vlad,roc
--HG--
rename : gfx/src/shared/gfxImageFrame.cpp => modules/libpr0n/src/imgFrame.cpp
rename : gfx/src/shared/gfxImageFrame.h => modules/libpr0n/src/imgFrame.h
2009-07-21 05:50:15 +04:00
|
|
|
#include "imgIContainer.h"
|
2016-06-30 11:23:30 +03:00
|
|
|
#include "imgIRequest.h"
|
2010-03-19 21:32:13 +03:00
|
|
|
#include "nsIPresShell.h"
|
|
|
|
#include "nsFocusManager.h"
|
2013-08-12 18:45:33 +04:00
|
|
|
#include "mozilla/dom/DataTransfer.h"
|
2001-07-25 09:16:32 +04:00
|
|
|
|
2003-04-17 17:16:36 +04:00
|
|
|
#include "nsIDocShell.h"
|
2005-03-11 14:12:15 +03:00
|
|
|
#include "nsIContentViewerEdit.h"
|
2010-03-19 21:32:13 +03:00
|
|
|
#include "nsIClipboardHelper.h"
|
|
|
|
#include "nsISelectionController.h"
|
2003-04-17 17:16:36 +04:00
|
|
|
|
2010-03-19 21:32:13 +03:00
|
|
|
#include "nsPIDOMWindow.h"
|
2001-03-21 04:16:22 +03:00
|
|
|
#include "nsIDocument.h"
|
2001-07-13 17:38:10 +04:00
|
|
|
#include "nsIHTMLDocument.h"
|
2006-12-26 20:47:52 +03:00
|
|
|
#include "nsGkAtoms.h"
|
2010-03-19 21:32:13 +03:00
|
|
|
#include "nsIFrame.h"
|
2013-09-07 17:01:08 +04:00
|
|
|
#include "nsIURI.h"
|
2018-01-31 04:33:06 +03:00
|
|
|
#include "nsIURIMutator.h"
|
2013-09-23 21:29:27 +04:00
|
|
|
#include "nsISimpleEnumerator.h"
|
2001-02-15 16:22:26 +03:00
|
|
|
|
2003-04-19 00:20:04 +04:00
|
|
|
// image copy stuff
|
|
|
|
#include "nsIImageLoadingContent.h"
|
|
|
|
#include "nsIInterfaceRequestorUtils.h"
|
2004-07-30 10:04:57 +04:00
|
|
|
#include "nsContentUtils.h"
|
2006-03-14 20:12:41 +03:00
|
|
|
#include "nsContentCID.h"
|
2003-04-19 00:20:04 +04:00
|
|
|
|
2016-06-30 11:23:30 +03:00
|
|
|
#ifdef XP_WIN
|
|
|
|
#include "nsCExternalHandlerService.h"
|
|
|
|
#include "nsEscape.h"
|
2016-11-04 17:40:11 +03:00
|
|
|
#include "nsIMIMEInfo.h"
|
2016-06-30 11:23:30 +03:00
|
|
|
#include "nsIMIMEService.h"
|
|
|
|
#include "nsIURL.h"
|
|
|
|
#include "nsReadableUtils.h"
|
|
|
|
#include "nsXULAppAPI.h"
|
|
|
|
#endif
|
|
|
|
|
2013-09-25 15:21:20 +04:00
|
|
|
#include "mozilla/ContentEvents.h"
|
2011-07-20 23:18:54 +04:00
|
|
|
#include "mozilla/dom/Element.h"
|
2014-03-18 08:48:21 +04:00
|
|
|
#include "mozilla/EventDispatcher.h"
|
2012-02-13 22:06:26 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
2014-04-10 20:09:40 +04:00
|
|
|
#include "mozilla/dom/Selection.h"
|
2015-03-10 09:11:55 +03:00
|
|
|
#include "mozilla/IntegerRange.h"
|
2012-02-13 22:06:26 +04:00
|
|
|
|
|
|
|
using namespace mozilla;
|
2013-08-12 18:45:33 +04:00
|
|
|
using namespace mozilla::dom;
|
2012-02-13 22:06:26 +04:00
|
|
|
|
2001-02-15 16:22:26 +03:00
|
|
|
static NS_DEFINE_CID(kCClipboardCID, NS_CLIPBOARD_CID);
|
|
|
|
static NS_DEFINE_CID(kCTransferableCID, NS_TRANSFERABLE_CID);
|
|
|
|
static NS_DEFINE_CID(kHTMLConverterCID, NS_HTMLFORMATCONVERTER_CID);
|
|
|
|
|
2005-03-11 14:12:15 +03:00
|
|
|
// copy string data onto the transferable
|
|
|
|
static nsresult AppendString(nsITransferable *aTransferable,
|
|
|
|
const nsAString& aString,
|
|
|
|
const char* aFlavor);
|
|
|
|
|
|
|
|
// copy HTML node data
|
|
|
|
static nsresult AppendDOMNode(nsITransferable *aTransferable,
|
2014-01-15 18:26:51 +04:00
|
|
|
nsINode* aDOMNode);
|
2001-02-15 16:22:26 +03:00
|
|
|
|
2016-06-30 11:23:30 +03:00
|
|
|
#ifdef XP_WIN
|
|
|
|
// copy image as file promise onto the transferable
|
|
|
|
static nsresult AppendImagePromise(nsITransferable* aTransferable,
|
|
|
|
imgIRequest* aImgRequest,
|
|
|
|
nsIImageLoadingContent* aImageElement);
|
|
|
|
#endif
|
|
|
|
|
2009-03-18 05:04:01 +03:00
|
|
|
// Helper used for HTMLCopy and GetTransferableForSelection since both routines
|
|
|
|
// share common code.
|
|
|
|
static nsresult
|
2018-05-08 20:52:36 +03:00
|
|
|
SelectionCopyHelper(Selection *aSel, nsIDocument *aDoc,
|
2012-08-22 19:56:38 +04:00
|
|
|
bool doPutOnClipboard, int16_t aClipboardID,
|
|
|
|
uint32_t aFlags, nsITransferable ** aTransferable)
|
2001-02-15 16:22:26 +03:00
|
|
|
{
|
2009-03-18 05:04:01 +03:00
|
|
|
// Clear the output parameter for the transferable, if provided.
|
|
|
|
if (aTransferable) {
|
2012-07-30 18:20:58 +04:00
|
|
|
*aTransferable = nullptr;
|
2009-03-18 05:04:01 +03:00
|
|
|
}
|
|
|
|
|
2013-04-03 22:39:06 +04:00
|
|
|
nsresult rv;
|
2001-02-15 16:22:26 +03:00
|
|
|
|
2004-12-07 17:13:54 +03:00
|
|
|
nsCOMPtr<nsIDocumentEncoder> docEncoder;
|
|
|
|
docEncoder = do_CreateInstance(NS_HTMLCOPY_ENCODER_CONTRACTID);
|
|
|
|
NS_ENSURE_TRUE(docEncoder, NS_ERROR_FAILURE);
|
|
|
|
|
2009-11-09 17:41:12 +03:00
|
|
|
// note that we assign text/unicode as mime type, but in fact nsHTMLCopyEncoder
|
|
|
|
// ignore it and use text/html or text/plain depending where the selection
|
|
|
|
// is. if it is a selection into input/textarea element or in a html content
|
|
|
|
// with pre-wrap style : text/plain. Otherwise text/html.
|
|
|
|
// see nsHTMLCopyEncoder::SetSelection
|
2013-04-03 22:39:06 +04:00
|
|
|
nsAutoString mimeType;
|
2004-12-07 17:13:54 +03:00
|
|
|
mimeType.AssignLiteral(kUnicodeMime);
|
2013-04-03 22:39:06 +04:00
|
|
|
|
|
|
|
// Do the first and potentially trial encoding as preformatted and raw.
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t flags = aFlags | nsIDocumentEncoder::OutputPreformatted
|
2015-01-18 01:31:59 +03:00
|
|
|
| nsIDocumentEncoder::OutputRaw
|
|
|
|
| nsIDocumentEncoder::OutputForPlainTextClipboardCopy;
|
2001-07-13 17:38:10 +04:00
|
|
|
|
2018-05-11 20:46:15 +03:00
|
|
|
rv = docEncoder->Init(aDoc, mimeType, flags);
|
2013-04-03 22:39:06 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2009-03-18 05:04:01 +03:00
|
|
|
|
2001-07-13 17:38:10 +04:00
|
|
|
rv = docEncoder->SetSelection(aSel);
|
2013-04-03 22:39:06 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2004-12-07 17:13:54 +03:00
|
|
|
|
2013-04-03 22:39:06 +04:00
|
|
|
// SetSelection set the mime type to text/plain if the selection is inside a
|
|
|
|
// text widget.
|
|
|
|
rv = docEncoder->GetMimeType(mimeType);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
bool selForcedTextPlain = mimeType.EqualsLiteral(kTextMime);
|
2004-12-07 17:13:54 +03:00
|
|
|
|
2013-04-03 22:39:06 +04:00
|
|
|
nsAutoString buf;
|
|
|
|
rv = docEncoder->EncodeToString(buf);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2004-12-07 17:13:54 +03:00
|
|
|
|
2013-04-03 22:39:06 +04:00
|
|
|
rv = docEncoder->GetMimeType(mimeType);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2013-07-20 06:56:18 +04:00
|
|
|
|
|
|
|
if (!selForcedTextPlain && mimeType.EqualsLiteral(kTextMime)) {
|
|
|
|
// SetSelection and EncodeToString use this case to signal that text/plain
|
|
|
|
// was forced because the document is either not an nsIHTMLDocument or it's
|
|
|
|
// XHTML. We want to pretty print XHTML but not non-nsIHTMLDocuments.
|
|
|
|
nsCOMPtr<nsIHTMLDocument> htmlDoc = do_QueryInterface(aDoc);
|
|
|
|
if (!htmlDoc) {
|
|
|
|
selForcedTextPlain = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The mime type is ultimately text/html if the encoder successfully encoded
|
|
|
|
// the selection as text/html.
|
2013-04-03 22:39:06 +04:00
|
|
|
bool encodedTextHTML = mimeType.EqualsLiteral(kHTMLMime);
|
|
|
|
|
|
|
|
// First, prepare the text/plain clipboard flavor.
|
|
|
|
nsAutoString textPlainBuf;
|
|
|
|
if (selForcedTextPlain) {
|
|
|
|
// Nothing to do. buf contains the final, preformatted, raw text/plain.
|
|
|
|
textPlainBuf.Assign(buf);
|
|
|
|
} else {
|
|
|
|
// Redo the encoding, but this time use pretty printing.
|
2012-03-31 18:10:34 +04:00
|
|
|
flags =
|
|
|
|
nsIDocumentEncoder::OutputSelectionOnly |
|
|
|
|
nsIDocumentEncoder::OutputAbsoluteLinks |
|
|
|
|
nsIDocumentEncoder::SkipInvisibleContent |
|
|
|
|
nsIDocumentEncoder::OutputDropInvisibleBreak |
|
2015-03-10 09:11:55 +03:00
|
|
|
(aFlags & (nsIDocumentEncoder::OutputNoScriptContent |
|
|
|
|
nsIDocumentEncoder::OutputRubyAnnotation));
|
2004-12-09 15:59:29 +03:00
|
|
|
|
2013-04-03 22:39:06 +04:00
|
|
|
mimeType.AssignLiteral(kTextMime);
|
2018-05-11 20:46:15 +03:00
|
|
|
rv = docEncoder->Init(aDoc, mimeType, flags);
|
2013-04-03 22:39:06 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-03-31 18:10:34 +04:00
|
|
|
|
|
|
|
rv = docEncoder->SetSelection(aSel);
|
2013-04-03 22:39:06 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-03-31 18:10:34 +04:00
|
|
|
|
2013-04-03 22:39:06 +04:00
|
|
|
rv = docEncoder->EncodeToString(textPlainBuf);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2004-12-07 17:13:54 +03:00
|
|
|
|
2013-04-03 22:39:06 +04:00
|
|
|
// Second, prepare the text/html flavor.
|
|
|
|
nsAutoString textHTMLBuf;
|
|
|
|
nsAutoString htmlParentsBuf;
|
|
|
|
nsAutoString htmlInfoBuf;
|
|
|
|
if (encodedTextHTML) {
|
|
|
|
// Redo the encoding, but this time use the passed-in flags.
|
2016-05-10 12:18:00 +03:00
|
|
|
// Don't allow wrapping of CJK strings.
|
2004-12-07 17:13:54 +03:00
|
|
|
mimeType.AssignLiteral(kHTMLMime);
|
2018-05-11 20:46:15 +03:00
|
|
|
rv = docEncoder->Init(aDoc, mimeType,
|
2016-05-10 12:18:00 +03:00
|
|
|
aFlags |
|
|
|
|
nsIDocumentEncoder::OutputDisallowLineBreaking);
|
2005-09-10 15:39:32 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2004-12-07 17:13:54 +03:00
|
|
|
rv = docEncoder->SetSelection(aSel);
|
2005-09-10 15:39:32 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2004-12-07 17:13:54 +03:00
|
|
|
|
2013-04-03 22:39:06 +04:00
|
|
|
rv = docEncoder->EncodeToStringWithContext(htmlParentsBuf, htmlInfoBuf,
|
|
|
|
textHTMLBuf);
|
2005-09-10 15:39:32 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2001-02-15 16:22:26 +03:00
|
|
|
}
|
2013-04-03 22:39:06 +04:00
|
|
|
|
2001-02-15 16:22:26 +03:00
|
|
|
// Get the Clipboard
|
2009-03-18 05:04:01 +03:00
|
|
|
nsCOMPtr<nsIClipboard> clipboard;
|
|
|
|
if (doPutOnClipboard) {
|
|
|
|
clipboard = do_GetService(kCClipboardCID, &rv);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
}
|
2001-02-15 16:22:26 +03:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
if ((doPutOnClipboard && clipboard) || aTransferable != nullptr) {
|
2001-02-15 16:22:26 +03:00
|
|
|
// Create a transferable for putting data on the Clipboard
|
|
|
|
nsCOMPtr<nsITransferable> trans = do_CreateInstance(kCTransferableCID);
|
2009-03-18 05:04:01 +03:00
|
|
|
if (trans) {
|
2012-04-17 06:14:01 +04:00
|
|
|
trans->Init(aDoc->GetLoadContext());
|
2013-04-03 22:39:06 +04:00
|
|
|
if (encodedTextHTML) {
|
2012-03-31 18:10:34 +04:00
|
|
|
// Set up a format converter so that clipboard flavor queries work.
|
|
|
|
// This converter isn't really used for conversions.
|
|
|
|
nsCOMPtr<nsIFormatConverter> htmlConverter =
|
|
|
|
do_CreateInstance(kHTMLConverterCID);
|
2001-02-15 16:22:26 +03:00
|
|
|
trans->SetConverter(htmlConverter);
|
2004-12-07 17:13:54 +03:00
|
|
|
|
2013-04-03 22:39:06 +04:00
|
|
|
if (!textHTMLBuf.IsEmpty()) {
|
2001-04-02 18:11:01 +04:00
|
|
|
// Add the html DataFlavor to the transferable
|
2013-04-03 22:39:06 +04:00
|
|
|
rv = AppendString(trans, textHTMLBuf, kHTMLMime);
|
2005-03-11 14:12:15 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2001-04-02 18:11:01 +04:00
|
|
|
}
|
2009-03-18 05:04:01 +03:00
|
|
|
|
|
|
|
// Add the htmlcontext DataFlavor to the transferable
|
|
|
|
// Even if parents is empty string, this flavor should
|
|
|
|
// be attached to the transferable
|
2013-04-03 22:39:06 +04:00
|
|
|
rv = AppendString(trans, htmlParentsBuf, kHTMLContext);
|
2009-03-18 05:04:01 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2013-04-03 22:39:06 +04:00
|
|
|
if (!htmlInfoBuf.IsEmpty()) {
|
2001-04-02 18:11:01 +04:00
|
|
|
// Add the htmlinfo DataFlavor to the transferable
|
2013-04-03 22:39:06 +04:00
|
|
|
rv = AppendString(trans, htmlInfoBuf, kHTMLInfo);
|
2005-03-11 14:12:15 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2001-04-02 18:11:01 +04:00
|
|
|
}
|
2009-03-18 05:04:01 +03:00
|
|
|
|
2013-04-03 22:39:06 +04:00
|
|
|
if (!textPlainBuf.IsEmpty()) {
|
2004-12-07 17:13:54 +03:00
|
|
|
// unicode text
|
|
|
|
// Add the unicode DataFlavor to the transferable
|
|
|
|
// If we didn't have this, then nsDataObj::GetData matches text/unicode against
|
|
|
|
// the kURLMime flavour which is not desirable (eg. when pasting into Notepad)
|
2013-04-03 22:39:06 +04:00
|
|
|
rv = AppendString(trans, textPlainBuf, kUnicodeMime);
|
2005-03-11 14:12:15 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2004-12-07 17:13:54 +03:00
|
|
|
}
|
|
|
|
|
2005-09-10 15:39:32 +04:00
|
|
|
// Try and get source URI of the items that are being dragged
|
|
|
|
nsIURI *uri = aDoc->GetDocumentURI();
|
|
|
|
if (uri) {
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString spec;
|
2016-09-08 07:17:58 +03:00
|
|
|
nsresult rv = uri->GetSpec(spec);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2005-09-10 15:39:32 +04:00
|
|
|
if (!spec.IsEmpty()) {
|
|
|
|
nsAutoString shortcut;
|
|
|
|
AppendUTF8toUTF16(spec, shortcut);
|
|
|
|
|
2006-04-21 19:16:22 +04:00
|
|
|
// Add the URL DataFlavor to the transferable. Don't use kURLMime, as it will
|
|
|
|
// cause an unnecessary UniformResourceLocator to be added which confuses
|
2006-06-21 00:26:30 +04:00
|
|
|
// some apps eg. Outlook 2000 - (See Bug 315370). Don't use
|
|
|
|
// kURLDataMime, as it will cause a bogus 'url ' flavor to
|
|
|
|
// show up on the Mac clipboard, confusing other apps, like
|
|
|
|
// Terminal (see bug 336012).
|
|
|
|
rv = AppendString(trans, shortcut, kURLPrivateMime);
|
2005-09-10 15:39:32 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
}
|
2009-03-18 05:04:01 +03:00
|
|
|
} else {
|
2013-04-03 22:39:06 +04:00
|
|
|
if (!textPlainBuf.IsEmpty()) {
|
2009-03-18 05:04:01 +03:00
|
|
|
// Add the unicode DataFlavor to the transferable
|
2013-04-03 22:39:06 +04:00
|
|
|
rv = AppendString(trans, textPlainBuf, kUnicodeMime);
|
2005-03-11 14:12:15 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2001-04-02 18:11:01 +04:00
|
|
|
}
|
2001-02-15 16:22:26 +03:00
|
|
|
}
|
2003-04-17 17:16:36 +04:00
|
|
|
|
2009-03-18 05:04:01 +03:00
|
|
|
if (doPutOnClipboard && clipboard) {
|
|
|
|
// put the transferable on the clipboard
|
2018-03-26 17:56:53 +03:00
|
|
|
clipboard->SetData(trans, nullptr, aClipboardID);
|
2009-03-18 05:04:01 +03:00
|
|
|
}
|
2003-04-17 17:16:36 +04:00
|
|
|
|
2009-03-18 05:04:01 +03:00
|
|
|
// Return the transferable to the caller if requested.
|
2012-07-30 18:20:58 +04:00
|
|
|
if (aTransferable != nullptr) {
|
2009-03-18 05:04:01 +03:00
|
|
|
trans.swap(*aTransferable);
|
|
|
|
}
|
2001-02-15 16:22:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
2001-07-13 17:38:10 +04:00
|
|
|
|
2010-07-13 00:24:25 +04:00
|
|
|
nsresult
|
2018-05-08 20:52:36 +03:00
|
|
|
nsCopySupport::HTMLCopy(Selection* aSel, nsIDocument* aDoc,
|
2015-03-10 09:11:55 +03:00
|
|
|
int16_t aClipboardID, bool aWithRubyAnnotation)
|
2010-07-13 00:24:25 +04:00
|
|
|
{
|
2015-03-10 09:11:55 +03:00
|
|
|
uint32_t flags = nsIDocumentEncoder::SkipInvisibleContent;
|
|
|
|
if (aWithRubyAnnotation) {
|
|
|
|
flags |= nsIDocumentEncoder::OutputRubyAnnotation;
|
|
|
|
}
|
|
|
|
return SelectionCopyHelper(aSel, aDoc, true, aClipboardID, flags, nullptr);
|
2010-07-13 00:24:25 +04:00
|
|
|
}
|
|
|
|
|
2016-06-08 21:11:39 +03:00
|
|
|
nsresult
|
|
|
|
nsCopySupport::ClearSelectionCache()
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIClipboard> clipboard = do_GetService(kCClipboardCID, &rv);
|
|
|
|
clipboard->EmptyClipboard(nsIClipboard::kSelectionCache);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2010-07-13 00:24:25 +04:00
|
|
|
nsresult
|
2018-05-08 20:52:36 +03:00
|
|
|
nsCopySupport::GetTransferableForSelection(Selection* aSel,
|
2010-07-13 00:24:25 +04:00
|
|
|
nsIDocument* aDoc,
|
|
|
|
nsITransferable** aTransferable)
|
2009-03-18 05:04:01 +03:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
return SelectionCopyHelper(aSel, aDoc, false, 0,
|
2010-07-13 00:24:25 +04:00
|
|
|
nsIDocumentEncoder::SkipInvisibleContent,
|
|
|
|
aTransferable);
|
2009-03-18 05:04:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2010-07-13 00:24:25 +04:00
|
|
|
nsCopySupport::GetTransferableForNode(nsINode* aNode,
|
|
|
|
nsIDocument* aDoc,
|
|
|
|
nsITransferable** aTransferable)
|
2009-03-18 05:04:01 +03:00
|
|
|
{
|
2010-07-13 00:24:25 +04:00
|
|
|
// Make a temporary selection with aNode in a single range.
|
2017-02-25 13:33:34 +03:00
|
|
|
// XXX We should try to get rid of the Selection object here.
|
|
|
|
// XXX bug 1245883
|
2018-05-08 20:52:36 +03:00
|
|
|
RefPtr<Selection> selection = new Selection();
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsRange> range = new nsRange(aNode);
|
2017-02-25 13:33:34 +03:00
|
|
|
ErrorResult result;
|
2018-03-27 07:35:21 +03:00
|
|
|
range->SelectNode(*aNode, result);
|
|
|
|
if (NS_WARN_IF(result.Failed())) {
|
|
|
|
return result.StealNSResult();
|
|
|
|
}
|
2018-05-08 20:52:42 +03:00
|
|
|
selection->AddRangeInternal(*range, aDoc, result);
|
2018-03-27 07:35:21 +03:00
|
|
|
if (NS_WARN_IF(result.Failed())) {
|
|
|
|
return result.StealNSResult();
|
|
|
|
}
|
2010-07-13 00:24:25 +04:00
|
|
|
// It's not the primary selection - so don't skip invisible content.
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t flags = 0;
|
2011-10-17 18:59:28 +04:00
|
|
|
return SelectionCopyHelper(selection, aDoc, false, 0, flags,
|
2010-07-13 00:24:25 +04:00
|
|
|
aTransferable);
|
2009-03-18 05:04:01 +03:00
|
|
|
}
|
|
|
|
|
2003-04-19 00:20:04 +04:00
|
|
|
nsresult
|
2018-05-08 20:52:36 +03:00
|
|
|
nsCopySupport::GetContents(const nsACString& aMimeType, uint32_t aFlags, Selection *aSel, nsIDocument *aDoc, nsAString& outdata)
|
2003-04-19 00:20:04 +04:00
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2003-04-19 00:20:04 +04:00
|
|
|
nsCOMPtr<nsIDocumentEncoder> docEncoder;
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString encoderContractID(NS_DOC_ENCODER_CONTRACTID_BASE);
|
2003-04-19 00:20:04 +04:00
|
|
|
encoderContractID.Append(aMimeType);
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2003-04-19 00:20:04 +04:00
|
|
|
docEncoder = do_CreateInstance(encoderContractID.get());
|
|
|
|
NS_ENSURE_TRUE(docEncoder, NS_ERROR_FAILURE);
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t flags = aFlags | nsIDocumentEncoder::SkipInvisibleContent;
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2014-05-22 07:48:51 +04:00
|
|
|
if (aMimeType.EqualsLiteral("text/plain"))
|
2003-04-19 00:20:04 +04:00
|
|
|
flags |= nsIDocumentEncoder::OutputPreformatted;
|
|
|
|
|
2006-02-03 17:18:39 +03:00
|
|
|
NS_ConvertASCIItoUTF16 unicodeMimeType(aMimeType);
|
2006-05-01 09:25:52 +04:00
|
|
|
|
2018-05-11 20:46:15 +03:00
|
|
|
rv = docEncoder->Init(aDoc, unicodeMimeType, flags);
|
2003-04-19 00:20:04 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2003-04-19 00:20:04 +04:00
|
|
|
if (aSel)
|
|
|
|
{
|
|
|
|
rv = docEncoder->SetSelection(aSel);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2017-07-06 15:00:35 +03:00
|
|
|
}
|
|
|
|
|
2003-04-19 00:20:04 +04:00
|
|
|
// encode the selection
|
|
|
|
return docEncoder->EncodeToString(outdata);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
2005-01-12 07:11:26 +03:00
|
|
|
nsCopySupport::ImageCopy(nsIImageLoadingContent* aImageElement,
|
2012-04-17 06:14:01 +04:00
|
|
|
nsILoadContext* aLoadContext,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aCopyFlags)
|
2003-04-19 00:20:04 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2004-06-05 21:57:00 +04:00
|
|
|
|
2005-01-12 07:11:26 +03:00
|
|
|
// create a transferable for putting data on the Clipboard
|
|
|
|
nsCOMPtr<nsITransferable> trans(do_CreateInstance(kCTransferableCID, &rv));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-04-17 06:14:01 +04:00
|
|
|
trans->Init(aLoadContext);
|
2003-04-19 00:20:04 +04:00
|
|
|
|
2005-03-11 14:12:15 +03:00
|
|
|
if (aCopyFlags & nsIContentViewerEdit::COPY_IMAGE_TEXT) {
|
|
|
|
// get the location from the element
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
rv = aImageElement->GetCurrentURI(getter_AddRefs(uri));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
NS_ENSURE_TRUE(uri, NS_ERROR_FAILURE);
|
2003-04-19 00:20:04 +04:00
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString location;
|
2005-03-11 14:12:15 +03:00
|
|
|
rv = uri->GetSpec(location);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2003-04-19 00:20:04 +04:00
|
|
|
|
2005-03-11 14:12:15 +03:00
|
|
|
// append the string to the transferable
|
|
|
|
rv = AppendString(trans, NS_ConvertUTF8toUTF16(location), kUnicodeMime);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2003-04-19 00:20:04 +04:00
|
|
|
|
2005-03-11 14:12:15 +03:00
|
|
|
if (aCopyFlags & nsIContentViewerEdit::COPY_IMAGE_HTML) {
|
|
|
|
// append HTML data to the transferable
|
2014-01-15 18:26:51 +04:00
|
|
|
nsCOMPtr<nsINode> node(do_QueryInterface(aImageElement, &rv));
|
2005-03-11 14:12:15 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2005-01-12 07:11:26 +03:00
|
|
|
|
2005-03-11 14:12:15 +03:00
|
|
|
rv = AppendDOMNode(trans, node);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2005-01-12 07:11:26 +03:00
|
|
|
|
2005-03-11 14:12:15 +03:00
|
|
|
if (aCopyFlags & nsIContentViewerEdit::COPY_IMAGE_DATA) {
|
2016-06-30 11:23:30 +03:00
|
|
|
// get the image data and its request from the element
|
|
|
|
nsCOMPtr<imgIRequest> imgRequest;
|
Bug 753 - Remove nsIImage, gfxIImageFrame, and their implementations, and expose an equivalent api on imgIContainer. r=roc,josh,bz,longsonr,vlad,karlt,jimm,bsmedberg,mfinkle,peterw,peterv sr=vlad,roc
--HG--
rename : gfx/src/shared/gfxImageFrame.cpp => modules/libpr0n/src/imgFrame.cpp
rename : gfx/src/shared/gfxImageFrame.h => modules/libpr0n/src/imgFrame.h
2009-07-21 05:50:15 +04:00
|
|
|
nsCOMPtr<imgIContainer> image =
|
2016-06-30 11:23:30 +03:00
|
|
|
nsContentUtils::GetImageFromContent(aImageElement,
|
|
|
|
getter_AddRefs(imgRequest));
|
2005-01-12 07:11:26 +03:00
|
|
|
NS_ENSURE_TRUE(image, NS_ERROR_FAILURE);
|
|
|
|
|
2016-06-30 11:23:30 +03:00
|
|
|
#ifdef XP_WIN
|
|
|
|
rv = AppendImagePromise(trans, imgRequest, aImageElement);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
#endif
|
|
|
|
|
2005-01-12 07:11:26 +03:00
|
|
|
nsCOMPtr<nsISupportsInterfacePointer>
|
|
|
|
imgPtr(do_CreateInstance(NS_SUPPORTS_INTERFACE_POINTER_CONTRACTID, &rv));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = imgPtr->SetData(image);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// copy the image data onto the transferable
|
|
|
|
rv = trans->SetTransferData(kNativeImageMime, imgPtr,
|
|
|
|
sizeof(nsISupports*));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
// get clipboard
|
|
|
|
nsCOMPtr<nsIClipboard> clipboard(do_GetService(kCClipboardCID, &rv));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// check whether the system supports the selection clipboard or not.
|
2011-09-29 10:19:26 +04:00
|
|
|
bool selectionSupported;
|
2005-01-12 07:11:26 +03:00
|
|
|
rv = clipboard->SupportsSelectionClipboard(&selectionSupported);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2003-04-19 00:20:04 +04:00
|
|
|
|
|
|
|
// put the transferable on the clipboard
|
2005-01-12 07:11:26 +03:00
|
|
|
if (selectionSupported) {
|
2012-07-30 18:20:58 +04:00
|
|
|
rv = clipboard->SetData(trans, nullptr, nsIClipboard::kSelectionClipboard);
|
2005-01-12 07:11:26 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
return clipboard->SetData(trans, nullptr, nsIClipboard::kGlobalClipboard);
|
2003-04-19 00:20:04 +04:00
|
|
|
}
|
2005-03-11 14:12:15 +03:00
|
|
|
|
|
|
|
static nsresult AppendString(nsITransferable *aTransferable,
|
|
|
|
const nsAString& aString,
|
|
|
|
const char* aFlavor)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsISupportsString>
|
|
|
|
data(do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID, &rv));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = data->SetData(aString);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aTransferable->AddDataFlavor(aFlavor);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return aTransferable->SetTransferData(aFlavor, data,
|
2014-01-04 19:02:17 +04:00
|
|
|
aString.Length() * sizeof(char16_t));
|
2005-03-11 14:12:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static nsresult AppendDOMNode(nsITransferable *aTransferable,
|
2014-01-15 18:26:51 +04:00
|
|
|
nsINode *aDOMNode)
|
2005-03-11 14:12:15 +03:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2014-01-15 18:26:51 +04:00
|
|
|
|
2005-03-11 14:12:15 +03:00
|
|
|
// selializer
|
|
|
|
nsCOMPtr<nsIDocumentEncoder>
|
|
|
|
docEncoder(do_CreateInstance(NS_HTMLCOPY_ENCODER_CONTRACTID, &rv));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// get document for the encoder
|
2014-01-15 18:26:51 +04:00
|
|
|
nsCOMPtr<nsIDocument> document = aDOMNode->OwnerDoc();
|
2005-03-11 14:12:15 +03:00
|
|
|
|
|
|
|
// Note that XHTML is not counted as HTML here, because we can't copy it
|
|
|
|
// properly (all the copy code for non-plaintext assumes using HTML
|
|
|
|
// serializers and parsers is OK, and those mess up XHTML).
|
2016-08-30 17:44:52 +03:00
|
|
|
DebugOnly<nsCOMPtr<nsIHTMLDocument>> htmlDoc =
|
|
|
|
nsCOMPtr<nsIHTMLDocument>(do_QueryInterface(document, &rv));
|
2005-03-11 14:12:15 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_OK);
|
2006-05-01 09:25:52 +04:00
|
|
|
|
2015-03-03 14:08:59 +03:00
|
|
|
NS_ENSURE_TRUE(document->IsHTMLDocument(), NS_OK);
|
2005-03-11 14:12:15 +03:00
|
|
|
|
|
|
|
// init encoder with document and node
|
2014-01-15 18:26:51 +04:00
|
|
|
rv = docEncoder->NativeInit(document, NS_LITERAL_STRING(kHTMLMime),
|
|
|
|
nsIDocumentEncoder::OutputAbsoluteLinks |
|
2017-08-04 12:37:53 +03:00
|
|
|
nsIDocumentEncoder::OutputEncodeBasicEntities);
|
2005-03-11 14:12:15 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2018-05-30 05:58:49 +03:00
|
|
|
rv = docEncoder->SetNode(aDOMNode);
|
2005-03-11 14:12:15 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// serialize to string
|
|
|
|
nsAutoString html, context, info;
|
2006-03-14 20:12:41 +03:00
|
|
|
rv = docEncoder->EncodeToStringWithContext(context, info, html);
|
2005-03-11 14:12:15 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// copy them to the transferable
|
|
|
|
if (!html.IsEmpty()) {
|
|
|
|
rv = AppendString(aTransferable, html, kHTMLMime);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!info.IsEmpty()) {
|
|
|
|
rv = AppendString(aTransferable, info, kHTMLInfo);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
// add a special flavor, even if we don't have html context data
|
|
|
|
return AppendString(aTransferable, context, kHTMLContext);
|
|
|
|
}
|
2007-07-26 08:14:33 +04:00
|
|
|
|
2016-06-30 11:23:30 +03:00
|
|
|
#ifdef XP_WIN
|
|
|
|
static nsresult AppendImagePromise(nsITransferable* aTransferable,
|
|
|
|
imgIRequest* aImgRequest,
|
|
|
|
nsIImageLoadingContent* aImageElement)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
NS_ENSURE_TRUE(aImgRequest, NS_OK);
|
2016-12-02 06:25:25 +03:00
|
|
|
|
|
|
|
uint32_t imageStatus;
|
|
|
|
rv = aImgRequest->GetImageStatus(&imageStatus);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (!(imageStatus & imgIRequest::STATUS_FRAME_COMPLETE) ||
|
|
|
|
(imageStatus & imgIRequest::STATUS_ERROR)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-06-05 11:06:15 +03:00
|
|
|
bool isMultipart;
|
|
|
|
rv = aImgRequest->GetMultipart(&isMultipart);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (isMultipart) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-06-30 11:23:30 +03:00
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(aImageElement, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Fix the file extension in the URL if necessary
|
|
|
|
nsCOMPtr<nsIMIMEService> mimeService =
|
|
|
|
do_GetService(NS_MIMESERVICE_CONTRACTID);
|
|
|
|
NS_ENSURE_TRUE(mimeService, NS_OK);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> imgUri;
|
2017-11-13 11:31:24 +03:00
|
|
|
rv = aImgRequest->GetFinalURI(getter_AddRefs(imgUri));
|
2016-06-30 11:23:30 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURL> imgUrl = do_QueryInterface(imgUri);
|
|
|
|
NS_ENSURE_TRUE(imgUrl, NS_OK);
|
|
|
|
|
|
|
|
nsAutoCString extension;
|
|
|
|
rv = imgUrl->GetFileExtension(extension);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2017-08-16 06:58:35 +03:00
|
|
|
nsCString mimeType;
|
2016-06-30 11:23:30 +03:00
|
|
|
rv = aImgRequest->GetMimeType(getter_Copies(mimeType));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIMIMEInfo> mimeInfo;
|
|
|
|
mimeService->GetFromTypeAndExtension(mimeType, EmptyCString(),
|
|
|
|
getter_AddRefs(mimeInfo));
|
|
|
|
NS_ENSURE_TRUE(mimeInfo, NS_OK);
|
|
|
|
|
|
|
|
nsAutoCString spec;
|
|
|
|
rv = imgUrl->GetSpec(spec);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// pass out the image source string
|
|
|
|
nsString imageSourceString;
|
|
|
|
CopyUTF8toUTF16(spec, imageSourceString);
|
|
|
|
|
|
|
|
bool validExtension;
|
|
|
|
if (extension.IsEmpty() ||
|
|
|
|
NS_FAILED(mimeInfo->ExtensionExists(extension,
|
|
|
|
&validExtension)) ||
|
|
|
|
!validExtension) {
|
|
|
|
// Fix the file extension in the URL
|
|
|
|
nsAutoCString primaryExtension;
|
|
|
|
mimeInfo->GetPrimaryExtension(primaryExtension);
|
|
|
|
|
2018-01-31 04:33:06 +03:00
|
|
|
rv = NS_MutateURI(imgUri)
|
2018-02-21 03:00:54 +03:00
|
|
|
.Apply(NS_MutatorMethod(&nsIURLMutator::SetFileExtension,
|
|
|
|
primaryExtension, nullptr))
|
2018-01-31 04:33:06 +03:00
|
|
|
.Finalize(imgUrl);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2016-06-30 11:23:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString fileName;
|
|
|
|
imgUrl->GetFileName(fileName);
|
|
|
|
|
|
|
|
NS_UnescapeURL(fileName);
|
|
|
|
|
|
|
|
// make the filename safe for the filesystem
|
|
|
|
fileName.ReplaceChar(FILE_PATH_SEPARATOR FILE_ILLEGAL_CHARACTERS, '-');
|
|
|
|
|
|
|
|
nsString imageDestFileName;
|
|
|
|
CopyUTF8toUTF16(fileName, imageDestFileName);
|
|
|
|
|
|
|
|
rv = AppendString(aTransferable, imageSourceString, kFilePromiseURLMime);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = AppendString(aTransferable, imageDestFileName, kFilePromiseDestFilename);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
aTransferable->SetRequestingPrincipal(node->NodePrincipal());
|
2018-03-14 11:44:36 +03:00
|
|
|
aTransferable->SetContentPolicyType(nsIContentPolicy::TYPE_INTERNAL_IMAGE);
|
2016-06-30 11:23:30 +03:00
|
|
|
|
|
|
|
// add the dataless file promise flavor
|
|
|
|
return aTransferable->AddDataFlavor(kFilePromiseMime);
|
|
|
|
}
|
|
|
|
#endif // XP_WIN
|
|
|
|
|
2010-03-19 21:32:13 +03:00
|
|
|
nsIContent*
|
2018-03-27 07:35:20 +03:00
|
|
|
nsCopySupport::GetSelectionForCopy(nsIDocument* aDocument, Selection** aSelection)
|
2007-07-26 08:14:33 +04:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
*aSelection = nullptr;
|
2010-03-19 21:32:13 +03:00
|
|
|
|
2010-06-25 17:59:57 +04:00
|
|
|
nsIPresShell* presShell = aDocument->GetShell();
|
2010-03-19 21:32:13 +03:00
|
|
|
if (!presShell)
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2010-03-19 21:32:13 +03:00
|
|
|
|
2016-06-10 15:01:32 +03:00
|
|
|
nsCOMPtr<nsIContent> focusedContent;
|
|
|
|
nsCOMPtr<nsISelectionController> selectionController =
|
|
|
|
presShell->GetSelectionControllerForFocusedContent(
|
|
|
|
getter_AddRefs(focusedContent));
|
|
|
|
if (!selectionController) {
|
|
|
|
return nullptr;
|
2010-03-19 21:32:13 +03:00
|
|
|
}
|
2007-07-26 08:14:33 +04:00
|
|
|
|
2018-03-27 07:35:20 +03:00
|
|
|
RefPtr<Selection> sel =
|
2018-05-08 20:52:36 +03:00
|
|
|
selectionController->GetSelection(nsISelectionController::SELECTION_NORMAL);
|
2018-03-27 07:35:20 +03:00
|
|
|
sel.forget(aSelection);
|
2016-06-10 15:01:32 +03:00
|
|
|
return focusedContent;
|
2010-03-19 21:32:13 +03:00
|
|
|
}
|
2007-07-26 08:14:33 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2010-03-19 21:32:13 +03:00
|
|
|
nsCopySupport::CanCopy(nsIDocument* aDocument)
|
|
|
|
{
|
|
|
|
if (!aDocument)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2007-07-26 08:14:33 +04:00
|
|
|
|
2018-03-27 07:35:20 +03:00
|
|
|
RefPtr<Selection> sel;
|
2010-03-19 21:32:13 +03:00
|
|
|
GetSelectionForCopy(aDocument, getter_AddRefs(sel));
|
2011-10-17 18:59:28 +04:00
|
|
|
NS_ENSURE_TRUE(sel, false);
|
2010-03-19 21:32:13 +03:00
|
|
|
|
2018-03-27 07:35:20 +03:00
|
|
|
return !sel->IsCollapsed();
|
2010-03-19 21:32:13 +03:00
|
|
|
}
|
|
|
|
|
2015-03-10 09:11:55 +03:00
|
|
|
static bool
|
|
|
|
IsInsideRuby(nsINode* aNode)
|
|
|
|
{
|
|
|
|
for (; aNode; aNode = aNode->GetParent()) {
|
|
|
|
if (aNode->IsHTMLElement(nsGkAtoms::ruby)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2018-03-27 07:35:21 +03:00
|
|
|
IsSelectionInsideRuby(Selection* aSelection)
|
2015-03-10 09:11:55 +03:00
|
|
|
{
|
2018-03-27 07:35:21 +03:00
|
|
|
uint32_t rangeCount = aSelection->RangeCount();;
|
2017-01-06 17:22:53 +03:00
|
|
|
for (auto i : IntegerRange(rangeCount)) {
|
2018-03-27 07:35:21 +03:00
|
|
|
nsRange* range = aSelection->GetRangeAt(i);
|
|
|
|
if (!IsInsideRuby(range->GetCommonAncestor())) {
|
2015-03-10 09:11:55 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2015-08-26 15:56:59 +03:00
|
|
|
nsCopySupport::FireClipboardEvent(EventMessage aEventMessage,
|
|
|
|
int32_t aClipboardType,
|
|
|
|
nsIPresShell* aPresShell,
|
2018-03-27 07:35:20 +03:00
|
|
|
Selection* aSelection,
|
2015-08-26 15:56:59 +03:00
|
|
|
bool* aActionTaken)
|
2010-03-19 21:32:13 +03:00
|
|
|
{
|
2015-05-13 09:51:00 +03:00
|
|
|
if (aActionTaken) {
|
|
|
|
*aActionTaken = false;
|
|
|
|
}
|
|
|
|
|
2017-02-22 12:34:45 +03:00
|
|
|
EventMessage originalEventMessage = aEventMessage;
|
|
|
|
if (originalEventMessage == ePasteNoFormatting) {
|
|
|
|
originalEventMessage = ePaste;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(originalEventMessage == eCut || originalEventMessage == eCopy ||
|
|
|
|
originalEventMessage == ePaste,
|
2010-03-19 21:32:13 +03:00
|
|
|
"Invalid clipboard event type");
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPresShell> presShell = aPresShell;
|
|
|
|
if (!presShell)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-03-19 21:32:13 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDocument> doc = presShell->GetDocument();
|
|
|
|
if (!doc)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-03-19 21:32:13 +03:00
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowOuter> piWindow = doc->GetWindow();
|
2010-03-19 21:32:13 +03:00
|
|
|
if (!piWindow)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-03-19 21:32:13 +03:00
|
|
|
|
|
|
|
// if a selection was not supplied, try to find it
|
|
|
|
nsCOMPtr<nsIContent> content;
|
2018-03-27 07:35:20 +03:00
|
|
|
RefPtr<Selection> sel = aSelection;
|
|
|
|
if (!sel) {
|
2010-03-19 21:32:13 +03:00
|
|
|
content = GetSelectionForCopy(doc, getter_AddRefs(sel));
|
2018-03-27 07:35:20 +03:00
|
|
|
}
|
2010-03-19 21:32:13 +03:00
|
|
|
|
|
|
|
// retrieve the event target node from the start of the selection
|
|
|
|
if (sel) {
|
2018-03-27 07:35:21 +03:00
|
|
|
RefPtr<nsRange> range = sel->GetRangeAt(0);
|
|
|
|
if (range) {
|
|
|
|
nsINode* startContainer = range->GetStartContainer();
|
|
|
|
if (startContainer) {
|
2010-03-19 21:32:13 +03:00
|
|
|
content = do_QueryInterface(startContainer);
|
2018-03-27 07:35:21 +03:00
|
|
|
}
|
2010-03-19 21:32:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if no content node was set, just get the root
|
|
|
|
if (!content) {
|
2010-04-30 17:12:05 +04:00
|
|
|
content = doc->GetRootElement();
|
2010-03-19 21:32:13 +03:00
|
|
|
if (!content)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-03-19 21:32:13 +03:00
|
|
|
}
|
2007-07-26 08:14:33 +04:00
|
|
|
|
2010-03-19 21:32:13 +03:00
|
|
|
// It seems to be unsafe to fire an event handler during reflow (bug 393696)
|
2014-08-06 08:44:13 +04:00
|
|
|
if (!nsContentUtils::IsSafeToRunScript()) {
|
|
|
|
nsContentUtils::WarnScriptWasIgnored(doc);
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2014-08-06 08:44:13 +04:00
|
|
|
}
|
2010-03-19 21:32:13 +03:00
|
|
|
|
2014-01-10 06:03:47 +04:00
|
|
|
nsCOMPtr<nsIDocShell> docShell = piWindow->GetDocShell();
|
2014-01-20 11:58:26 +04:00
|
|
|
const bool chromeShell =
|
|
|
|
docShell && docShell->ItemType() == nsIDocShellTreeItem::typeChrome;
|
2013-10-08 18:49:58 +04:00
|
|
|
|
2013-03-11 19:49:48 +04:00
|
|
|
// next, fire the cut, copy or paste event
|
|
|
|
bool doDefault = true;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DataTransfer> clipboardData;
|
2013-10-08 18:49:58 +04:00
|
|
|
if (chromeShell || Preferences::GetBool("dom.event.clipboardevents.enabled", true)) {
|
2014-02-27 07:23:31 +04:00
|
|
|
clipboardData =
|
2015-08-28 22:21:08 +03:00
|
|
|
new DataTransfer(doc->GetScopeObject(), aEventMessage,
|
2017-02-22 12:34:45 +03:00
|
|
|
originalEventMessage == ePaste, aClipboardType);
|
2013-03-11 19:49:48 +04:00
|
|
|
|
2012-02-13 22:06:26 +04:00
|
|
|
nsEventStatus status = nsEventStatus_eIgnore;
|
2017-02-22 12:34:45 +03:00
|
|
|
InternalClipboardEvent evt(true, originalEventMessage);
|
2016-03-26 11:19:57 +03:00
|
|
|
evt.mClipboardData = clipboardData;
|
2014-03-18 08:48:21 +04:00
|
|
|
EventDispatcher::Dispatch(content, presShell->GetPresContext(), &evt,
|
|
|
|
nullptr, &status);
|
2013-03-11 19:49:48 +04:00
|
|
|
// If the event was cancelled, don't do the clipboard operation
|
|
|
|
doDefault = (status != nsEventStatus_eConsumeNoDefault);
|
2012-02-13 22:06:26 +04:00
|
|
|
}
|
2015-05-13 09:51:00 +03:00
|
|
|
|
2017-09-06 18:26:50 +03:00
|
|
|
// When this function exits, the event dispatch is over. We want to disconnect
|
|
|
|
// our DataTransfer, which means setting its mode to `Protected` and clearing
|
|
|
|
// all stored data, before we return.
|
|
|
|
auto clearAfter = MakeScopeExit([&] {
|
2013-03-12 22:50:05 +04:00
|
|
|
if (clipboardData) {
|
2017-09-11 22:35:16 +03:00
|
|
|
clipboardData->Disconnect();
|
|
|
|
|
|
|
|
// NOTE: Disconnect may not actually clear the DataTransfer if the
|
|
|
|
// dom.events.dataTransfer.protected.enabled pref is not on, so we make
|
|
|
|
// sure we clear here, as not clearing could provide the DataTransfer
|
|
|
|
// access to information from the system clipboard at an arbitrary point
|
|
|
|
// in the future.
|
|
|
|
if (originalEventMessage == ePaste) {
|
|
|
|
clipboardData->ClearAll();
|
|
|
|
}
|
2013-03-12 22:50:05 +04:00
|
|
|
}
|
2017-09-06 18:26:50 +03:00
|
|
|
});
|
2013-03-12 22:50:05 +04:00
|
|
|
|
2017-09-06 18:26:50 +03:00
|
|
|
// No need to do anything special during a paste. Either an event listener
|
|
|
|
// took care of it and cancelled the event, or the caller will handle it.
|
|
|
|
// Return true to indicate that the event wasn't cancelled.
|
|
|
|
if (originalEventMessage == ePaste) {
|
2015-05-13 09:51:00 +03:00
|
|
|
if (aActionTaken) {
|
|
|
|
*aActionTaken = true;
|
|
|
|
}
|
2013-03-11 19:49:48 +04:00
|
|
|
return doDefault;
|
|
|
|
}
|
2010-03-19 21:32:13 +03:00
|
|
|
|
2010-10-11 02:07:00 +04:00
|
|
|
// Update the presentation in case the event handler modified the selection,
|
|
|
|
// see bug 602231.
|
2017-01-05 10:31:56 +03:00
|
|
|
presShell->FlushPendingNotifications(FlushType::Frames);
|
2010-10-11 02:07:00 +04:00
|
|
|
if (presShell->IsDestroying())
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-10-11 02:07:00 +04:00
|
|
|
|
2013-03-11 19:49:48 +04:00
|
|
|
// if the event was not cancelled, do the default copy. If the event was cancelled,
|
|
|
|
// use the data added to the data transfer and copy that instead.
|
|
|
|
uint32_t count = 0;
|
|
|
|
if (doDefault) {
|
2015-05-13 09:51:00 +03:00
|
|
|
// find the focused node
|
|
|
|
nsCOMPtr<nsIContent> srcNode = content;
|
|
|
|
if (content->IsInNativeAnonymousSubtree()) {
|
|
|
|
srcNode = content->FindFirstNonChromeOnlyAccessContent();
|
2015-05-13 09:51:00 +03:00
|
|
|
}
|
2015-05-13 09:51:00 +03:00
|
|
|
|
|
|
|
// check if we are looking at a password input
|
|
|
|
nsCOMPtr<nsIFormControl> formControl = do_QueryInterface(srcNode);
|
|
|
|
if (formControl) {
|
2017-04-01 05:49:00 +03:00
|
|
|
if (formControl->ControlType() == NS_FORM_INPUT_PASSWORD) {
|
2015-05-13 09:51:00 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// when cutting non-editable content, do nothing
|
|
|
|
// XXX this is probably the wrong editable flag to check
|
2017-02-22 12:34:45 +03:00
|
|
|
if (originalEventMessage != eCut || content->IsEditable()) {
|
2015-05-13 09:51:00 +03:00
|
|
|
// get the data from the selection if any
|
2018-05-08 20:52:39 +03:00
|
|
|
if (sel->IsCollapsed()) {
|
2015-05-13 09:51:00 +03:00
|
|
|
if (aActionTaken) {
|
|
|
|
*aActionTaken = true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// XXX Code which decides whether we should copy text with ruby
|
|
|
|
// annotation is currenct depending on whether each range of the
|
|
|
|
// selection is inside a same ruby container. But we really should
|
|
|
|
// expose the full functionality in browser. See bug 1130891.
|
|
|
|
bool withRubyAnnotation = IsSelectionInsideRuby(sel);
|
|
|
|
// call the copy code
|
2018-03-27 07:35:21 +03:00
|
|
|
nsresult rv = HTMLCopy(sel, doc, aClipboardType, withRubyAnnotation);
|
2015-05-13 09:51:00 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
2013-03-11 19:49:48 +04:00
|
|
|
return false;
|
|
|
|
}
|
2013-03-12 22:50:05 +04:00
|
|
|
} else if (clipboardData) {
|
2013-03-11 19:49:48 +04:00
|
|
|
// check to see if any data was put on the data transfer.
|
2018-03-13 23:23:59 +03:00
|
|
|
count = clipboardData->MozItemCount();
|
2013-03-11 19:49:48 +04:00
|
|
|
if (count) {
|
|
|
|
nsCOMPtr<nsIClipboard> clipboard(do_GetService("@mozilla.org/widget/clipboard;1"));
|
|
|
|
NS_ENSURE_TRUE(clipboard, false);
|
|
|
|
|
|
|
|
nsCOMPtr<nsITransferable> transferable =
|
|
|
|
clipboardData->GetTransferable(0, doc->GetLoadContext());
|
|
|
|
|
|
|
|
NS_ENSURE_TRUE(transferable, false);
|
|
|
|
|
|
|
|
// put the transferable on the clipboard
|
2018-03-27 07:35:21 +03:00
|
|
|
nsresult rv = clipboard->SetData(transferable, nullptr, aClipboardType);
|
2013-03-11 19:49:48 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-03-19 21:32:13 +03:00
|
|
|
|
|
|
|
// Now that we have copied, update the clipboard commands. This should have
|
2013-03-11 19:49:48 +04:00
|
|
|
// the effect of updating the enabled state of the paste menu item.
|
|
|
|
if (doDefault || count) {
|
2014-07-28 03:21:00 +04:00
|
|
|
piWindow->UpdateCommands(NS_LITERAL_STRING("clipboard"), nullptr, 0);
|
2013-03-11 19:49:48 +04:00
|
|
|
}
|
2010-03-19 21:32:13 +03:00
|
|
|
|
2015-05-13 09:51:00 +03:00
|
|
|
if (aActionTaken) {
|
|
|
|
*aActionTaken = true;
|
|
|
|
}
|
2013-03-11 19:49:48 +04:00
|
|
|
return doDefault;
|
2007-07-26 08:14:33 +04:00
|
|
|
}
|