2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla Communicator.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2002
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Mike Pinkerton <pinkerton@netscape.com>
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#include "nsReadableUtils.h"
|
|
|
|
|
|
|
|
// Local Includes
|
|
|
|
#include "nsContentAreaDragDrop.h"
|
|
|
|
|
|
|
|
// Helper Classes
|
|
|
|
#include "nsString.h"
|
|
|
|
|
|
|
|
// Interfaces needed to be included
|
2010-05-24 16:24:15 -07:00
|
|
|
#include "nsCopySupport.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIDOMUIEvent.h"
|
|
|
|
#include "nsISelection.h"
|
2012-02-17 06:16:59 -08:00
|
|
|
#include "nsISelectionController.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIDOMNode.h"
|
|
|
|
#include "nsIDOMNodeList.h"
|
|
|
|
#include "nsIDOMEvent.h"
|
|
|
|
#include "nsIDOMNSEvent.h"
|
2008-08-27 05:07:27 -07:00
|
|
|
#include "nsIDOMDragEvent.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsPIDOMWindow.h"
|
|
|
|
#include "nsIDOMDocument.h"
|
|
|
|
#include "nsIDOMRange.h"
|
|
|
|
#include "nsIFormControl.h"
|
|
|
|
#include "nsIDOMHTMLAreaElement.h"
|
|
|
|
#include "nsIDOMHTMLAnchorElement.h"
|
|
|
|
#include "nsITransferable.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "nsXPCOM.h"
|
|
|
|
#include "nsISupportsPrimitives.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsIFile.h"
|
|
|
|
#include "nsIWebNavigation.h"
|
|
|
|
#include "nsIDocShell.h"
|
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsIImageLoadingContent.h"
|
2012-02-17 06:16:59 -08:00
|
|
|
#include "nsITextControlElement.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsUnicharUtils.h"
|
|
|
|
#include "nsIURL.h"
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsIScriptSecurityManager.h"
|
2008-08-27 05:07:27 -07:00
|
|
|
#include "nsIPrincipal.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIDocShellTreeItem.h"
|
|
|
|
#include "nsIWebBrowserPersist.h"
|
|
|
|
#include "nsEscape.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsIMIMEService.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-20 18:50:15 -07:00
|
|
|
#include "imgIContainer.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "imgIRequest.h"
|
2008-08-27 05:07:27 -07:00
|
|
|
#include "nsDOMDataTransfer.h"
|
2012-05-06 00:53:11 -07:00
|
|
|
#include "mozilla/dom/Element.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-04-18 11:27:18 -07:00
|
|
|
class NS_STACK_CLASS DragDataProducer
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
public:
|
2010-04-18 11:27:18 -07:00
|
|
|
DragDataProducer(nsIDOMWindow* aWindow,
|
|
|
|
nsIContent* aTarget,
|
|
|
|
nsIContent* aSelectionTargetNode,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aIsAltKeyPressed);
|
2008-08-27 05:07:27 -07:00
|
|
|
nsresult Produce(nsDOMDataTransfer* aDataTransfer,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool* aCanDrag,
|
2012-02-17 06:16:59 -08:00
|
|
|
nsISelection** aSelection,
|
2008-08-27 05:07:27 -07:00
|
|
|
nsIContent** aDragNode);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
private:
|
2008-08-27 05:07:27 -07:00
|
|
|
void AddString(nsDOMDataTransfer* aDataTransfer,
|
|
|
|
const nsAString& aFlavor,
|
|
|
|
const nsAString& aData,
|
|
|
|
nsIPrincipal* aPrincipal);
|
|
|
|
nsresult AddStringsToDataTransfer(nsIContent* aDragNode,
|
|
|
|
nsDOMDataTransfer* aDataTransfer);
|
2007-03-22 10:30:00 -07:00
|
|
|
static nsresult GetDraggableSelectionData(nsISelection* inSelection,
|
2008-08-27 05:07:27 -07:00
|
|
|
nsIContent* inRealTargetNode,
|
|
|
|
nsIContent **outImageOrLinkNode,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool* outDragSelectedText);
|
2008-08-27 05:07:27 -07:00
|
|
|
static already_AddRefed<nsIContent> FindParentLinkNode(nsIContent* inNode);
|
|
|
|
static void GetAnchorURL(nsIContent* inNode, nsAString& outURL);
|
|
|
|
static void GetNodeString(nsIContent* inNode, nsAString & outNodeString);
|
2007-03-22 10:30:00 -07:00
|
|
|
static void CreateLinkText(const nsAString& inURL, const nsAString & inText,
|
|
|
|
nsAString& outLinkText);
|
|
|
|
static void GetSelectedLink(nsISelection* inSelection,
|
2008-08-27 05:07:27 -07:00
|
|
|
nsIContent **outLinkNode);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-08-27 05:07:27 -07:00
|
|
|
nsCOMPtr<nsIDOMWindow> mWindow;
|
|
|
|
nsCOMPtr<nsIContent> mTarget;
|
|
|
|
nsCOMPtr<nsIContent> mSelectionTargetNode;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mIsAltKeyPressed;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsString mUrlString;
|
|
|
|
nsString mImageSourceString;
|
|
|
|
nsString mImageDestFileName;
|
|
|
|
nsString mTitleString;
|
|
|
|
// will be filled automatically if you fill urlstring
|
|
|
|
nsString mHtmlString;
|
|
|
|
nsString mContextString;
|
|
|
|
nsString mInfoString;
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mIsAnchor;
|
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-20 18:50:15 -07:00
|
|
|
nsCOMPtr<imgIContainer> mImage;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-08-27 05:07:27 -07:00
|
|
|
nsresult
|
|
|
|
nsContentAreaDragDrop::GetDragData(nsIDOMWindow* aWindow,
|
|
|
|
nsIContent* aTarget,
|
|
|
|
nsIContent* aSelectionTargetNode,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aIsAltKeyPressed,
|
2008-08-27 05:07:27 -07:00
|
|
|
nsDOMDataTransfer* aDataTransfer,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool* aCanDrag,
|
2012-02-17 06:16:59 -08:00
|
|
|
nsISelection** aSelection,
|
2008-08-27 05:07:27 -07:00
|
|
|
nsIContent** aDragNode)
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(aSelectionTargetNode, NS_ERROR_INVALID_ARG);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
*aCanDrag = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-04-18 11:27:18 -07:00
|
|
|
DragDataProducer
|
|
|
|
provider(aWindow, aTarget, aSelectionTargetNode, aIsAltKeyPressed);
|
2012-02-17 06:16:59 -08:00
|
|
|
return provider.Produce(aDataTransfer, aCanDrag, aSelection, aDragNode);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-27 05:07:27 -07:00
|
|
|
NS_IMPL_ISUPPORTS1(nsContentAreaDragDropDataProvider, nsIFlavorDataProvider)
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// SaveURIToFile
|
|
|
|
// used on platforms where it's possible to drag items (e.g. images)
|
|
|
|
// into the file system
|
|
|
|
nsresult
|
2008-08-27 05:07:27 -07:00
|
|
|
nsContentAreaDragDropDataProvider::SaveURIToFile(nsAString& inSourceURIString,
|
|
|
|
nsIFile* inDestFile)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIURI> sourceURI;
|
|
|
|
nsresult rv = NS_NewURI(getter_AddRefs(sourceURI), inSourceURIString);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURL> sourceURL = do_QueryInterface(sourceURI);
|
|
|
|
if (!sourceURL) {
|
|
|
|
return NS_ERROR_NO_INTERFACE;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = inDestFile->CreateUnique(nsIFile::NORMAL_FILE_TYPE, 0600);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// we rely on the fact that the WPB is refcounted by the channel etc,
|
|
|
|
// so we don't keep a ref to it. It will die when finished.
|
|
|
|
nsCOMPtr<nsIWebBrowserPersist> persist =
|
|
|
|
do_CreateInstance("@mozilla.org/embedding/browser/nsWebBrowserPersist;1",
|
|
|
|
&rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-12-15 11:14:38 -08:00
|
|
|
persist->SetPersistFlags(nsIWebBrowserPersist::PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION);
|
|
|
|
|
2008-08-27 05:07:27 -07:00
|
|
|
return persist->SaveURI(sourceURI, nsnull, nsnull, nsnull, nsnull, inDestFile);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// This is our nsIFlavorDataProvider callback. There are several
|
|
|
|
// assumptions here that make this work:
|
|
|
|
//
|
|
|
|
// 1. Someone put a kFilePromiseURLMime flavor into the transferable
|
|
|
|
// with the source URI of the file to save (as a string). We did
|
2008-08-27 05:07:27 -07:00
|
|
|
// that in AddStringsToDataTransfer.
|
2007-03-22 10:30:00 -07:00
|
|
|
//
|
|
|
|
// 2. Someone put a kFilePromiseDirectoryMime flavor into the
|
|
|
|
// transferable with an nsILocalFile for the directory we are to
|
|
|
|
// save in. That has to be done by platform-specific code (in
|
2008-08-27 05:07:27 -07:00
|
|
|
// widget), which gets the destination directory from
|
|
|
|
// OS-specific drag information.
|
2007-03-22 10:30:00 -07:00
|
|
|
//
|
|
|
|
NS_IMETHODIMP
|
2008-08-27 05:07:27 -07:00
|
|
|
nsContentAreaDragDropDataProvider::GetFlavorData(nsITransferable *aTransferable,
|
|
|
|
const char *aFlavor,
|
|
|
|
nsISupports **aData,
|
|
|
|
PRUint32 *aDataLen)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aData && aDataLen);
|
|
|
|
*aData = nsnull;
|
|
|
|
*aDataLen = 0;
|
|
|
|
|
|
|
|
nsresult rv = NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
|
|
|
|
if (strcmp(aFlavor, kFilePromiseMime) == 0) {
|
|
|
|
// get the URI from the kFilePromiseURLMime flavor
|
|
|
|
NS_ENSURE_ARG(aTransferable);
|
|
|
|
nsCOMPtr<nsISupports> tmp;
|
|
|
|
PRUint32 dataSize = 0;
|
|
|
|
aTransferable->GetTransferData(kFilePromiseURLMime,
|
|
|
|
getter_AddRefs(tmp), &dataSize);
|
|
|
|
nsCOMPtr<nsISupportsString> supportsString =
|
|
|
|
do_QueryInterface(tmp);
|
|
|
|
if (!supportsString)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
nsAutoString sourceURLString;
|
|
|
|
supportsString->GetData(sourceURLString);
|
|
|
|
if (sourceURLString.IsEmpty())
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
aTransferable->GetTransferData(kFilePromiseDestFilename,
|
|
|
|
getter_AddRefs(tmp), &dataSize);
|
|
|
|
supportsString = do_QueryInterface(tmp);
|
|
|
|
if (!supportsString)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
nsAutoString targetFilename;
|
|
|
|
supportsString->GetData(targetFilename);
|
|
|
|
if (targetFilename.IsEmpty())
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// get the target directory from the kFilePromiseDirectoryMime
|
|
|
|
// flavor
|
|
|
|
nsCOMPtr<nsISupports> dirPrimitive;
|
|
|
|
dataSize = 0;
|
|
|
|
aTransferable->GetTransferData(kFilePromiseDirectoryMime,
|
|
|
|
getter_AddRefs(dirPrimitive), &dataSize);
|
|
|
|
nsCOMPtr<nsILocalFile> destDirectory = do_QueryInterface(dirPrimitive);
|
|
|
|
if (!destDirectory)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> file;
|
|
|
|
rv = destDirectory->Clone(getter_AddRefs(file));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
file->Append(targetFilename);
|
|
|
|
|
|
|
|
rv = SaveURIToFile(sourceURLString, file);
|
|
|
|
// send back an nsILocalFile
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
CallQueryInterface(file, aData);
|
|
|
|
*aDataLen = sizeof(nsIFile*);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2010-04-18 11:27:18 -07:00
|
|
|
DragDataProducer::DragDataProducer(nsIDOMWindow* aWindow,
|
|
|
|
nsIContent* aTarget,
|
|
|
|
nsIContent* aSelectionTargetNode,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aIsAltKeyPressed)
|
2008-08-27 05:07:27 -07:00
|
|
|
: mWindow(aWindow),
|
|
|
|
mTarget(aTarget),
|
|
|
|
mSelectionTargetNode(aSelectionTargetNode),
|
2010-04-18 11:27:18 -07:00
|
|
|
mIsAltKeyPressed(aIsAltKeyPressed),
|
2011-10-17 07:59:28 -07:00
|
|
|
mIsAnchor(false)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// FindParentLinkNode
|
|
|
|
//
|
|
|
|
// Finds the parent with the given link tag starting at |inNode|. If
|
|
|
|
// it gets up to the root without finding it, we stop looking and
|
|
|
|
// return null.
|
|
|
|
//
|
2008-08-27 05:07:27 -07:00
|
|
|
already_AddRefed<nsIContent>
|
2010-04-18 11:27:18 -07:00
|
|
|
DragDataProducer::FindParentLinkNode(nsIContent* inNode)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-08-27 05:07:27 -07:00
|
|
|
nsIContent* content = inNode;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!content) {
|
|
|
|
// That must have been the document node; nothing else to do here;
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; content; content = content->GetParent()) {
|
|
|
|
if (nsContentUtils::IsDraggableLink(content)) {
|
2008-08-27 05:07:27 -07:00
|
|
|
NS_ADDREF(content);
|
|
|
|
return content;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// GetAnchorURL
|
|
|
|
//
|
|
|
|
void
|
2010-04-18 11:27:18 -07:00
|
|
|
DragDataProducer::GetAnchorURL(nsIContent* inNode, nsAString& outURL)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIURI> linkURI;
|
2008-08-27 05:07:27 -07:00
|
|
|
if (!inNode || !inNode->IsLink(getter_AddRefs(linkURI))) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// Not a link
|
|
|
|
outURL.Truncate();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCAutoString spec;
|
|
|
|
linkURI->GetSpec(spec);
|
|
|
|
CopyUTF8toUTF16(spec, outURL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// CreateLinkText
|
|
|
|
//
|
|
|
|
// Creates the html for an anchor in the form
|
|
|
|
// <a href="inURL">inText</a>
|
|
|
|
//
|
|
|
|
void
|
2010-04-18 11:27:18 -07:00
|
|
|
DragDataProducer::CreateLinkText(const nsAString& inURL,
|
|
|
|
const nsAString & inText,
|
|
|
|
nsAString& outLinkText)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// use a temp var in case |inText| is the same string as
|
|
|
|
// |outLinkText| to avoid overwriting it while building up the
|
|
|
|
// string in pieces.
|
|
|
|
nsAutoString linkText(NS_LITERAL_STRING("<a href=\"") +
|
|
|
|
inURL +
|
|
|
|
NS_LITERAL_STRING("\">") +
|
|
|
|
inText +
|
|
|
|
NS_LITERAL_STRING("</a>") );
|
|
|
|
|
|
|
|
outLinkText = linkText;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// GetNodeString
|
|
|
|
//
|
|
|
|
// Gets the text associated with a node
|
|
|
|
//
|
|
|
|
void
|
2010-04-18 11:27:18 -07:00
|
|
|
DragDataProducer::GetNodeString(nsIContent* inNode,
|
|
|
|
nsAString & outNodeString)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-08-27 05:07:27 -07:00
|
|
|
nsCOMPtr<nsIDOMNode> node = do_QueryInterface(inNode);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
outNodeString.Truncate();
|
|
|
|
|
|
|
|
// use a range to get the text-equivalent of the node
|
|
|
|
nsCOMPtr<nsIDOMDocument> doc;
|
2008-08-27 05:07:27 -07:00
|
|
|
node->GetOwnerDocument(getter_AddRefs(doc));
|
2011-05-15 03:07:28 -07:00
|
|
|
if (doc) {
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIDOMRange> range;
|
2011-05-15 03:07:28 -07:00
|
|
|
doc->CreateRange(getter_AddRefs(range));
|
2007-03-22 10:30:00 -07:00
|
|
|
if (range) {
|
2008-08-27 05:07:27 -07:00
|
|
|
range->SelectNode(node);
|
2007-03-22 10:30:00 -07:00
|
|
|
range->ToString(outNodeString);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2010-04-18 11:27:18 -07:00
|
|
|
DragDataProducer::Produce(nsDOMDataTransfer* aDataTransfer,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool* aCanDrag,
|
2012-02-17 06:16:59 -08:00
|
|
|
nsISelection** aSelection,
|
2010-04-18 11:27:18 -07:00
|
|
|
nsIContent** aDragNode)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-02-17 06:16:59 -08:00
|
|
|
NS_PRECONDITION(aCanDrag && aSelection && aDataTransfer && aDragNode,
|
2008-08-27 05:07:27 -07:00
|
|
|
"null pointer passed to Produce");
|
|
|
|
NS_ASSERTION(mWindow, "window not set");
|
|
|
|
NS_ASSERTION(mSelectionTargetNode, "selection target node should have been set");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-08-27 05:07:27 -07:00
|
|
|
*aDragNode = nsnull;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-05-24 16:24:15 -07:00
|
|
|
nsresult rv;
|
2008-08-27 05:07:27 -07:00
|
|
|
nsIContent* dragNode = nsnull;
|
2012-02-17 06:16:59 -08:00
|
|
|
*aSelection = nsnull;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-02-17 06:16:59 -08:00
|
|
|
// Find the selection to see what we could be dragging and if what we're
|
|
|
|
// dragging is in what is selected. If this is an editable textbox, use
|
|
|
|
// the textbox's selection, otherwise use the window's selection.
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsISelection> selection;
|
2012-02-17 06:16:59 -08:00
|
|
|
nsIContent* editingElement = mSelectionTargetNode->IsEditable() ?
|
|
|
|
mSelectionTargetNode->GetEditingHost() : nsnull;
|
2010-11-16 12:45:49 -08:00
|
|
|
nsCOMPtr<nsITextControlElement> textControl =
|
|
|
|
nsITextControlElement::GetTextControlElementFromEditingHost(editingElement);
|
2012-02-17 06:16:59 -08:00
|
|
|
if (textControl) {
|
|
|
|
nsISelectionController* selcon = textControl->GetSelectionController();
|
|
|
|
if (selcon) {
|
|
|
|
selcon->GetSelection(nsISelectionController::SELECTION_NORMAL, getter_AddRefs(selection));
|
|
|
|
}
|
2012-02-28 12:33:30 -08:00
|
|
|
|
|
|
|
if (!selection)
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-02-17 06:16:59 -08:00
|
|
|
else {
|
|
|
|
mWindow->GetSelection(getter_AddRefs(selection));
|
|
|
|
if (!selection)
|
2008-08-27 05:07:27 -07:00
|
|
|
return NS_OK;
|
2012-02-17 06:16:59 -08:00
|
|
|
|
|
|
|
// Check if the node is inside a form control. Don't set aCanDrag to false
|
|
|
|
//however, as we still want to allow the drag.
|
|
|
|
nsCOMPtr<nsIContent> findFormNode = mSelectionTargetNode;
|
|
|
|
nsIContent* findFormParent = findFormNode->GetParent();
|
|
|
|
while (findFormParent) {
|
|
|
|
nsCOMPtr<nsIFormControl> form(do_QueryInterface(findFormParent));
|
|
|
|
if (form && !form->AllowDraggableChildren()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
findFormParent = findFormParent->GetParent();
|
2011-03-24 07:55:32 -07:00
|
|
|
}
|
2008-08-27 05:07:27 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// if set, serialize the content under this node
|
2008-08-27 05:07:27 -07:00
|
|
|
nsCOMPtr<nsIContent> nodeToSerialize;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-02-17 06:16:59 -08:00
|
|
|
bool isChromeShell = false;
|
|
|
|
nsCOMPtr<nsIWebNavigation> webnav = do_GetInterface(mWindow);
|
|
|
|
nsCOMPtr<nsIDocShellTreeItem> dsti = do_QueryInterface(webnav);
|
|
|
|
if (dsti) {
|
|
|
|
PRInt32 type = -1;
|
|
|
|
if (NS_SUCCEEDED(dsti->GetItemType(&type)) &&
|
|
|
|
type == nsIDocShellTreeItem::typeChrome) {
|
|
|
|
isChromeShell = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// In chrome shells, only allow dragging inside editable areas.
|
|
|
|
if (isChromeShell && !editingElement)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
if (isChromeShell && textControl) {
|
2012-02-28 12:33:30 -08:00
|
|
|
// Only use the selection if the target node is in the selection.
|
|
|
|
bool selectionContainsTarget = false;
|
|
|
|
nsCOMPtr<nsIDOMNode> targetNode = do_QueryInterface(mSelectionTargetNode);
|
|
|
|
selection->ContainsNode(targetNode, false, &selectionContainsTarget);
|
|
|
|
if (!selectionContainsTarget)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
selection.swap(*aSelection);
|
2012-02-17 06:16:59 -08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// In content shells, a number of checks are made below to determine
|
|
|
|
// whether an image or a link is being dragged. If so, add additional
|
|
|
|
// data to the data transfer. This is also done for chrome shells, but
|
|
|
|
// only when in a non-textbox editor.
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool haveSelectedContent = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// possible parent link node
|
2008-08-27 05:07:27 -07:00
|
|
|
nsCOMPtr<nsIContent> parentLink;
|
|
|
|
nsCOMPtr<nsIContent> draggedNode;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
{
|
|
|
|
// only drag form elements by using the alt key,
|
|
|
|
// otherwise buttons and select widgets are hard to use
|
|
|
|
|
|
|
|
// Note that while <object> elements implement nsIFormControl, we should
|
|
|
|
// really allow dragging them if they happen to be images.
|
2008-08-27 05:07:27 -07:00
|
|
|
nsCOMPtr<nsIFormControl> form(do_QueryInterface(mTarget));
|
|
|
|
if (form && !mIsAltKeyPressed && form->GetType() != NS_FORM_OBJECT) {
|
2011-10-17 07:59:28 -07:00
|
|
|
*aCanDrag = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-08-27 05:07:27 -07:00
|
|
|
draggedNode = mTarget;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMHTMLAreaElement> area; // client-side image map
|
|
|
|
nsCOMPtr<nsIImageLoadingContent> image;
|
|
|
|
nsCOMPtr<nsIDOMHTMLAnchorElement> link;
|
|
|
|
|
2008-08-27 05:07:27 -07:00
|
|
|
nsCOMPtr<nsIContent> selectedImageOrLinkNode;
|
|
|
|
GetDraggableSelectionData(selection, mSelectionTargetNode,
|
|
|
|
getter_AddRefs(selectedImageOrLinkNode),
|
|
|
|
&haveSelectedContent);
|
|
|
|
|
|
|
|
// either plain text or anchor text is selected
|
|
|
|
if (haveSelectedContent) {
|
|
|
|
link = do_QueryInterface(selectedImageOrLinkNode);
|
|
|
|
if (link && mIsAltKeyPressed) {
|
|
|
|
// if alt is pressed, select the link text instead of drag the link
|
2011-10-17 07:59:28 -07:00
|
|
|
*aCanDrag = false;
|
2008-08-27 05:07:27 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-02-17 06:16:59 -08:00
|
|
|
selection.swap(*aSelection);
|
2008-08-27 05:07:27 -07:00
|
|
|
} else if (selectedImageOrLinkNode) {
|
|
|
|
// an image is selected
|
|
|
|
image = do_QueryInterface(selectedImageOrLinkNode);
|
|
|
|
} else {
|
|
|
|
// nothing is selected -
|
|
|
|
//
|
|
|
|
// look for draggable elements under the mouse
|
|
|
|
//
|
|
|
|
// if the alt key is down, don't start a drag if we're in an
|
|
|
|
// anchor because we want to do selection.
|
|
|
|
parentLink = FindParentLinkNode(draggedNode);
|
|
|
|
if (parentLink && mIsAltKeyPressed) {
|
2011-10-17 07:59:28 -07:00
|
|
|
*aCanDrag = false;
|
2008-08-27 05:07:27 -07:00
|
|
|
return NS_OK;
|
2008-08-25 08:08:28 -07:00
|
|
|
}
|
2008-08-27 05:07:27 -07:00
|
|
|
|
|
|
|
area = do_QueryInterface(draggedNode);
|
|
|
|
image = do_QueryInterface(draggedNode);
|
|
|
|
link = do_QueryInterface(draggedNode);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// set for linked images, and links
|
2008-08-27 05:07:27 -07:00
|
|
|
nsCOMPtr<nsIContent> linkNode;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (area) {
|
|
|
|
// use the alt text (or, if missing, the href) as the title
|
|
|
|
area->GetAttribute(NS_LITERAL_STRING("alt"), mTitleString);
|
|
|
|
if (mTitleString.IsEmpty()) {
|
|
|
|
// this can be a relative link
|
|
|
|
area->GetAttribute(NS_LITERAL_STRING("href"), mTitleString);
|
|
|
|
}
|
|
|
|
|
|
|
|
// we'll generate HTML like <a href="absurl">alt text</a>
|
2011-10-17 07:59:28 -07:00
|
|
|
mIsAnchor = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// gives an absolute link
|
2008-08-27 05:07:27 -07:00
|
|
|
GetAnchorURL(draggedNode, mUrlString);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
mHtmlString.AssignLiteral("<a href=\"");
|
|
|
|
mHtmlString.Append(mUrlString);
|
|
|
|
mHtmlString.AppendLiteral("\">");
|
|
|
|
mHtmlString.Append(mTitleString);
|
|
|
|
mHtmlString.AppendLiteral("</a>");
|
2008-08-27 05:07:27 -07:00
|
|
|
|
|
|
|
dragNode = draggedNode;
|
2007-03-22 10:30:00 -07:00
|
|
|
} else if (image) {
|
2011-10-17 07:59:28 -07:00
|
|
|
mIsAnchor = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
// grab the href as the url, use alt text as the title of the
|
|
|
|
// area if it's there. the drag data is the image tag and src
|
|
|
|
// attribute.
|
|
|
|
nsCOMPtr<nsIURI> imageURI;
|
|
|
|
image->GetCurrentURI(getter_AddRefs(imageURI));
|
|
|
|
if (imageURI) {
|
|
|
|
nsCAutoString spec;
|
|
|
|
imageURI->GetSpec(spec);
|
|
|
|
CopyUTF8toUTF16(spec, mUrlString);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMElement> imageElement(do_QueryInterface(image));
|
|
|
|
// XXXbz Shouldn't we use the "title" attr for title? Using
|
|
|
|
// "alt" seems very wrong....
|
|
|
|
if (imageElement) {
|
|
|
|
imageElement->GetAttribute(NS_LITERAL_STRING("alt"), mTitleString);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mTitleString.IsEmpty()) {
|
|
|
|
mTitleString = mUrlString;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<imgIRequest> imgRequest;
|
|
|
|
|
|
|
|
// grab the image data, and its request.
|
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-20 18:50:15 -07:00
|
|
|
nsCOMPtr<imgIContainer> img =
|
2007-03-22 10:30:00 -07:00
|
|
|
nsContentUtils::GetImageFromContent(image,
|
|
|
|
getter_AddRefs(imgRequest));
|
|
|
|
|
|
|
|
nsCOMPtr<nsIMIMEService> mimeService =
|
|
|
|
do_GetService("@mozilla.org/mime;1");
|
|
|
|
|
|
|
|
// Fix the file extension in the URL if necessary
|
|
|
|
if (imgRequest && mimeService) {
|
|
|
|
nsCOMPtr<nsIURI> imgUri;
|
|
|
|
imgRequest->GetURI(getter_AddRefs(imgUri));
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURL> imgUrl(do_QueryInterface(imgUri));
|
|
|
|
|
|
|
|
if (imgUrl) {
|
|
|
|
nsCAutoString extension;
|
|
|
|
imgUrl->GetFileExtension(extension);
|
|
|
|
|
|
|
|
nsXPIDLCString mimeType;
|
|
|
|
imgRequest->GetMimeType(getter_Copies(mimeType));
|
|
|
|
|
|
|
|
nsCOMPtr<nsIMIMEInfo> mimeInfo;
|
|
|
|
mimeService->GetFromTypeAndExtension(mimeType, EmptyCString(),
|
|
|
|
getter_AddRefs(mimeInfo));
|
|
|
|
|
|
|
|
if (mimeInfo) {
|
|
|
|
nsCAutoString spec;
|
|
|
|
imgUrl->GetSpec(spec);
|
|
|
|
|
|
|
|
// pass out the image source string
|
|
|
|
CopyUTF8toUTF16(spec, mImageSourceString);
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool validExtension;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (extension.IsEmpty() ||
|
|
|
|
NS_FAILED(mimeInfo->ExtensionExists(extension,
|
|
|
|
&validExtension)) ||
|
|
|
|
!validExtension) {
|
|
|
|
// Fix the file extension in the URL
|
|
|
|
nsresult rv = imgUrl->Clone(getter_AddRefs(imgUri));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
imgUrl = do_QueryInterface(imgUri);
|
|
|
|
|
|
|
|
nsCAutoString primaryExtension;
|
|
|
|
mimeInfo->GetPrimaryExtension(primaryExtension);
|
|
|
|
|
|
|
|
imgUrl->SetFileExtension(primaryExtension);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCAutoString fileName;
|
|
|
|
imgUrl->GetFileName(fileName);
|
|
|
|
|
|
|
|
NS_UnescapeURL(fileName);
|
|
|
|
|
|
|
|
// make the filename safe for the filesystem
|
|
|
|
fileName.ReplaceChar(FILE_PATH_SEPARATOR FILE_ILLEGAL_CHARACTERS,
|
|
|
|
'-');
|
|
|
|
|
|
|
|
CopyUTF8toUTF16(fileName, mImageDestFileName);
|
|
|
|
|
|
|
|
// and the image object
|
|
|
|
mImage = img;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parentLink) {
|
|
|
|
// If we are dragging around an image in an anchor, then we
|
|
|
|
// are dragging the entire anchor
|
|
|
|
linkNode = parentLink;
|
|
|
|
nodeToSerialize = linkNode;
|
|
|
|
} else {
|
2008-08-27 05:07:27 -07:00
|
|
|
nodeToSerialize = do_QueryInterface(draggedNode);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2008-08-27 05:07:27 -07:00
|
|
|
dragNode = nodeToSerialize;
|
2007-03-22 10:30:00 -07:00
|
|
|
} else if (link) {
|
|
|
|
// set linkNode. The code below will handle this
|
2008-08-27 05:07:27 -07:00
|
|
|
linkNode = do_QueryInterface(link); // XXX test this
|
2007-03-22 10:30:00 -07:00
|
|
|
GetNodeString(draggedNode, mTitleString);
|
|
|
|
} else if (parentLink) {
|
|
|
|
// parentLink will always be null if there's selected content
|
|
|
|
linkNode = parentLink;
|
|
|
|
nodeToSerialize = linkNode;
|
|
|
|
} else if (!haveSelectedContent) {
|
|
|
|
// nothing draggable
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (linkNode) {
|
2011-10-17 07:59:28 -07:00
|
|
|
mIsAnchor = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
GetAnchorURL(linkNode, mUrlString);
|
2008-08-27 05:07:27 -07:00
|
|
|
dragNode = linkNode;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-17 06:16:59 -08:00
|
|
|
if (nodeToSerialize || *aSelection) {
|
2010-05-24 16:24:15 -07:00
|
|
|
mHtmlString.Truncate();
|
|
|
|
mContextString.Truncate();
|
|
|
|
mInfoString.Truncate();
|
|
|
|
mTitleString.Truncate();
|
2012-02-17 06:16:59 -08:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMDocument> domDoc;
|
|
|
|
mWindow->GetDocument(getter_AddRefs(domDoc));
|
|
|
|
nsCOMPtr<nsIDocument> doc = do_QueryInterface(domDoc);
|
|
|
|
NS_ENSURE_TRUE(doc, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
// if we have selected text, use it in preference to the node
|
2010-05-24 16:24:15 -07:00
|
|
|
nsCOMPtr<nsITransferable> transferable;
|
2012-02-17 06:16:59 -08:00
|
|
|
if (*aSelection) {
|
|
|
|
rv = nsCopySupport::GetTransferableForSelection(*aSelection, doc,
|
|
|
|
getter_AddRefs(transferable));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rv = nsCopySupport::GetTransferableForNode(nodeToSerialize, doc,
|
|
|
|
getter_AddRefs(transferable));
|
|
|
|
}
|
2012-02-23 12:59:45 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-02-17 06:16:59 -08:00
|
|
|
|
2010-05-24 16:24:15 -07:00
|
|
|
nsCOMPtr<nsISupportsString> data;
|
|
|
|
PRUint32 dataSize;
|
|
|
|
rv = transferable->GetTransferData(kHTMLMime, getter_AddRefs(data), &dataSize);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
data->GetData(mHtmlString);
|
|
|
|
}
|
|
|
|
rv = transferable->GetTransferData(kHTMLContext, getter_AddRefs(data), &dataSize);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
data->GetData(mContextString);
|
|
|
|
}
|
|
|
|
rv = transferable->GetTransferData(kHTMLInfo, getter_AddRefs(data), &dataSize);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
data->GetData(mInfoString);
|
|
|
|
}
|
|
|
|
rv = transferable->GetTransferData(kUnicodeMime, getter_AddRefs(data), &dataSize);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv); // require plain text at a minimum
|
|
|
|
data->GetData(mTitleString);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// default text value is the URL
|
|
|
|
if (mTitleString.IsEmpty()) {
|
|
|
|
mTitleString = mUrlString;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we haven't constructed a html version, make one now
|
|
|
|
if (mHtmlString.IsEmpty() && !mUrlString.IsEmpty())
|
|
|
|
CreateLinkText(mUrlString, mTitleString, mHtmlString);
|
|
|
|
|
2008-08-27 05:07:27 -07:00
|
|
|
// if there is no drag node, which will be the case for a selection, just
|
|
|
|
// use the selection target node.
|
2010-05-24 16:24:15 -07:00
|
|
|
rv = AddStringsToDataTransfer(
|
|
|
|
dragNode ? dragNode : mSelectionTargetNode.get(), aDataTransfer);
|
2008-08-27 05:07:27 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
NS_IF_ADDREF(*aDragNode = dragNode);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-04-18 11:27:18 -07:00
|
|
|
DragDataProducer::AddString(nsDOMDataTransfer* aDataTransfer,
|
|
|
|
const nsAString& aFlavor,
|
|
|
|
const nsAString& aData,
|
|
|
|
nsIPrincipal* aPrincipal)
|
2008-08-27 05:07:27 -07:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIWritableVariant> variant = do_CreateInstance(NS_VARIANT_CONTRACTID);
|
|
|
|
if (variant) {
|
|
|
|
variant->SetAsAString(aData);
|
|
|
|
aDataTransfer->SetDataWithPrincipal(aFlavor, variant, 0, aPrincipal);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2010-04-18 11:27:18 -07:00
|
|
|
DragDataProducer::AddStringsToDataTransfer(nsIContent* aDragNode,
|
|
|
|
nsDOMDataTransfer* aDataTransfer)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-08-27 05:07:27 -07:00
|
|
|
NS_ASSERTION(aDragNode, "adding strings for null node");
|
|
|
|
|
|
|
|
// set all of the data to have the principal of the node where the data came from
|
|
|
|
nsIPrincipal* principal = aDragNode->NodePrincipal();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// add a special flavor if we're an anchor to indicate that we have
|
|
|
|
// a URL in the drag data
|
|
|
|
if (!mUrlString.IsEmpty() && mIsAnchor) {
|
|
|
|
nsAutoString dragData(mUrlString);
|
|
|
|
dragData.AppendLiteral("\n");
|
|
|
|
dragData += mTitleString;
|
|
|
|
|
2008-08-27 05:07:27 -07:00
|
|
|
AddString(aDataTransfer, NS_LITERAL_STRING(kURLMime), dragData, principal);
|
|
|
|
AddString(aDataTransfer, NS_LITERAL_STRING(kURLDataMime), mUrlString, principal);
|
|
|
|
AddString(aDataTransfer, NS_LITERAL_STRING(kURLDescriptionMime), mTitleString, principal);
|
|
|
|
AddString(aDataTransfer, NS_LITERAL_STRING("text/uri-list"), mUrlString, principal);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-02-17 06:16:59 -08:00
|
|
|
// add a special flavor for the html context data
|
|
|
|
if (!mContextString.IsEmpty())
|
|
|
|
AddString(aDataTransfer, NS_LITERAL_STRING(kHTMLContext), mContextString, principal);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// add a special flavor if we have html info data
|
2008-08-27 05:07:27 -07:00
|
|
|
if (!mInfoString.IsEmpty())
|
|
|
|
AddString(aDataTransfer, NS_LITERAL_STRING(kHTMLInfo), mInfoString, principal);
|
2008-08-25 02:56:29 -07:00
|
|
|
|
2008-08-25 08:08:28 -07:00
|
|
|
// add the full html
|
2012-02-17 06:16:59 -08:00
|
|
|
if (!mHtmlString.IsEmpty())
|
|
|
|
AddString(aDataTransfer, NS_LITERAL_STRING(kHTMLMime), mHtmlString, principal);
|
2008-08-27 05:07:27 -07:00
|
|
|
|
|
|
|
// add the plain text. we use the url for text/plain data if an anchor is
|
|
|
|
// being dragged, rather than the title text of the link or the alt text for
|
|
|
|
// an anchor image.
|
|
|
|
AddString(aDataTransfer, NS_LITERAL_STRING(kTextMime),
|
|
|
|
mIsAnchor ? mUrlString : mTitleString, principal);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// add image data, if present. For now, all we're going to do with
|
|
|
|
// this is turn it into a native data flavor, so indicate that with
|
|
|
|
// a new flavor so as not to confuse anyone who is really registered
|
|
|
|
// for image/gif or image/jpg.
|
|
|
|
if (mImage) {
|
2008-08-27 05:07:27 -07:00
|
|
|
nsCOMPtr<nsIWritableVariant> variant = do_CreateInstance(NS_VARIANT_CONTRACTID);
|
|
|
|
if (variant) {
|
|
|
|
variant->SetAsISupports(mImage);
|
|
|
|
aDataTransfer->SetDataWithPrincipal(NS_LITERAL_STRING(kNativeImageMime),
|
|
|
|
variant, 0, principal);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// assume the image comes from a file, and add a file promise. We
|
|
|
|
// register ourselves as a nsIFlavorDataProvider, and will use the
|
|
|
|
// GetFlavorData callback to save the image to disk.
|
|
|
|
|
2008-08-27 05:07:27 -07:00
|
|
|
nsCOMPtr<nsIFlavorDataProvider> dataProvider =
|
|
|
|
new nsContentAreaDragDropDataProvider();
|
|
|
|
if (dataProvider) {
|
|
|
|
nsCOMPtr<nsIWritableVariant> variant = do_CreateInstance(NS_VARIANT_CONTRACTID);
|
|
|
|
if (variant) {
|
|
|
|
variant->SetAsISupports(dataProvider);
|
|
|
|
aDataTransfer->SetDataWithPrincipal(NS_LITERAL_STRING(kFilePromiseMime),
|
|
|
|
variant, 0, principal);
|
|
|
|
}
|
|
|
|
}
|
2008-08-25 08:08:28 -07:00
|
|
|
|
2008-08-27 05:07:27 -07:00
|
|
|
AddString(aDataTransfer, NS_LITERAL_STRING(kFilePromiseURLMime),
|
|
|
|
mImageSourceString, principal);
|
|
|
|
AddString(aDataTransfer, NS_LITERAL_STRING(kFilePromiseDestFilename),
|
|
|
|
mImageDestFileName, principal);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// if not an anchor, add the image url
|
|
|
|
if (!mIsAnchor) {
|
2008-08-27 05:07:27 -07:00
|
|
|
AddString(aDataTransfer, NS_LITERAL_STRING(kURLDataMime), mUrlString, principal);
|
|
|
|
AddString(aDataTransfer, NS_LITERAL_STRING("text/uri-list"), mUrlString, principal);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// note that this can return NS_OK, but a null out param (by design)
|
|
|
|
// static
|
|
|
|
nsresult
|
2010-04-18 11:27:18 -07:00
|
|
|
DragDataProducer::GetDraggableSelectionData(nsISelection* inSelection,
|
|
|
|
nsIContent* inRealTargetNode,
|
|
|
|
nsIContent **outImageOrLinkNode,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool* outDragSelectedText)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(inSelection);
|
|
|
|
NS_ENSURE_ARG(inRealTargetNode);
|
|
|
|
NS_ENSURE_ARG_POINTER(outImageOrLinkNode);
|
|
|
|
|
|
|
|
*outImageOrLinkNode = nsnull;
|
2011-10-17 07:59:28 -07:00
|
|
|
*outDragSelectedText = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool selectionContainsTarget = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isCollapsed = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
inSelection->GetIsCollapsed(&isCollapsed);
|
|
|
|
if (!isCollapsed) {
|
2008-08-27 05:07:27 -07:00
|
|
|
nsCOMPtr<nsIDOMNode> realTargetNode = do_QueryInterface(inRealTargetNode);
|
2011-10-17 07:59:28 -07:00
|
|
|
inSelection->ContainsNode(realTargetNode, false,
|
2007-03-22 10:30:00 -07:00
|
|
|
&selectionContainsTarget);
|
|
|
|
|
|
|
|
if (selectionContainsTarget) {
|
|
|
|
// track down the anchor node, if any, for the url
|
|
|
|
nsCOMPtr<nsIDOMNode> selectionStart;
|
|
|
|
inSelection->GetAnchorNode(getter_AddRefs(selectionStart));
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> selectionEnd;
|
|
|
|
inSelection->GetFocusNode(getter_AddRefs(selectionEnd));
|
|
|
|
|
|
|
|
// look for a selection around a single node, like an image.
|
|
|
|
// in this case, drag the image, rather than a serialization of the HTML
|
|
|
|
// XXX generalize this to other draggable element types?
|
|
|
|
if (selectionStart == selectionEnd) {
|
2011-09-28 23:19:26 -07:00
|
|
|
bool hasChildren;
|
2007-03-22 10:30:00 -07:00
|
|
|
selectionStart->HasChildNodes(&hasChildren);
|
|
|
|
if (hasChildren) {
|
|
|
|
// see if just one node is selected
|
|
|
|
PRInt32 anchorOffset, focusOffset;
|
|
|
|
inSelection->GetAnchorOffset(&anchorOffset);
|
|
|
|
inSelection->GetFocusOffset(&focusOffset);
|
|
|
|
if (abs(anchorOffset - focusOffset) == 1) {
|
|
|
|
nsCOMPtr<nsIContent> selStartContent =
|
|
|
|
do_QueryInterface(selectionStart);
|
|
|
|
|
|
|
|
if (selStartContent) {
|
|
|
|
PRInt32 childOffset =
|
|
|
|
(anchorOffset < focusOffset) ? anchorOffset : focusOffset;
|
|
|
|
nsIContent *childContent =
|
|
|
|
selStartContent->GetChildAt(childOffset);
|
|
|
|
// if we find an image, we'll fall into the node-dragging code,
|
|
|
|
// rather the the selection-dragging code
|
|
|
|
if (nsContentUtils::IsDraggableImage(childContent)) {
|
2008-08-27 05:07:27 -07:00
|
|
|
NS_ADDREF(*outImageOrLinkNode = childContent);
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// see if the selection is a link; if so, its node will be returned
|
|
|
|
GetSelectedLink(inSelection, outImageOrLinkNode);
|
|
|
|
|
|
|
|
// indicate that a link or text is selected
|
2011-10-17 07:59:28 -07:00
|
|
|
*outDragSelectedText = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2010-04-18 11:27:18 -07:00
|
|
|
void
|
|
|
|
DragDataProducer::GetSelectedLink(nsISelection* inSelection,
|
|
|
|
nsIContent **outLinkNode)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
*outLinkNode = nsnull;
|
|
|
|
|
2008-08-27 05:07:27 -07:00
|
|
|
nsCOMPtr<nsIDOMNode> selectionStartNode;
|
|
|
|
inSelection->GetAnchorNode(getter_AddRefs(selectionStartNode));
|
|
|
|
nsCOMPtr<nsIDOMNode> selectionEndNode;
|
|
|
|
inSelection->GetFocusNode(getter_AddRefs(selectionEndNode));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// simple case: only one node is selected
|
|
|
|
// see if it or its parent is an anchor, then exit
|
|
|
|
|
2008-08-27 05:07:27 -07:00
|
|
|
if (selectionStartNode == selectionEndNode) {
|
|
|
|
nsCOMPtr<nsIContent> selectionStart = do_QueryInterface(selectionStartNode);
|
|
|
|
nsCOMPtr<nsIContent> link = FindParentLinkNode(selectionStart);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (link) {
|
|
|
|
link.swap(*outLinkNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// more complicated case: multiple nodes are selected
|
|
|
|
|
|
|
|
// Unless you use the Alt key while selecting anchor text, it is
|
|
|
|
// nearly impossible to avoid overlapping into adjacent nodes.
|
|
|
|
// Deal with this by trimming off the leading and/or trailing
|
|
|
|
// nodes of the selection if the strings they produce are empty.
|
|
|
|
|
|
|
|
// first, use a range determine if the selection was marked LTR or RTL;
|
|
|
|
// if the latter, swap endpoints so we trim in the right direction
|
|
|
|
|
|
|
|
PRInt32 startOffset, endOffset;
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMRange> range;
|
|
|
|
inSelection->GetRangeAt(0, getter_AddRefs(range));
|
|
|
|
if (!range) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> tempNode;
|
|
|
|
range->GetStartContainer( getter_AddRefs(tempNode));
|
2008-08-27 05:07:27 -07:00
|
|
|
if (tempNode != selectionStartNode) {
|
|
|
|
selectionEndNode = selectionStartNode;
|
|
|
|
selectionStartNode = tempNode;
|
2007-03-22 10:30:00 -07:00
|
|
|
inSelection->GetAnchorOffset(&endOffset);
|
|
|
|
inSelection->GetFocusOffset(&startOffset);
|
|
|
|
} else {
|
|
|
|
inSelection->GetAnchorOffset(&startOffset);
|
|
|
|
inSelection->GetFocusOffset(&endOffset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// trim leading node if the string is empty or
|
|
|
|
// the selection starts at the end of the text
|
|
|
|
|
|
|
|
nsAutoString nodeStr;
|
2008-08-27 05:07:27 -07:00
|
|
|
selectionStartNode->GetNodeValue(nodeStr);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (nodeStr.IsEmpty() ||
|
2007-07-08 00:08:04 -07:00
|
|
|
startOffset+1 >= static_cast<PRInt32>(nodeStr.Length())) {
|
2008-08-27 05:07:27 -07:00
|
|
|
nsCOMPtr<nsIDOMNode> curr = selectionStartNode;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIDOMNode* next;
|
|
|
|
|
|
|
|
while (curr) {
|
|
|
|
curr->GetNextSibling(&next);
|
|
|
|
|
|
|
|
if (next) {
|
2008-08-27 05:07:27 -07:00
|
|
|
selectionStartNode = dont_AddRef(next);
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
curr->GetParentNode(&next);
|
|
|
|
curr = dont_AddRef(next);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// trim trailing node if the selection ends before its text begins
|
|
|
|
|
|
|
|
if (endOffset == 0) {
|
2008-08-27 05:07:27 -07:00
|
|
|
nsCOMPtr<nsIDOMNode> curr = selectionEndNode;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIDOMNode* next;
|
|
|
|
|
|
|
|
while (curr) {
|
|
|
|
curr->GetPreviousSibling(&next);
|
|
|
|
|
|
|
|
if (next){
|
2008-08-27 05:07:27 -07:00
|
|
|
selectionEndNode = dont_AddRef(next);
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
curr->GetParentNode(&next);
|
|
|
|
curr = dont_AddRef(next);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// see if the leading & trailing nodes are part of the
|
|
|
|
// same anchor - if so, return the anchor node
|
2008-08-27 05:07:27 -07:00
|
|
|
nsCOMPtr<nsIContent> selectionStart = do_QueryInterface(selectionStartNode);
|
|
|
|
nsCOMPtr<nsIContent> link = FindParentLinkNode(selectionStart);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (link) {
|
2008-08-27 05:07:27 -07:00
|
|
|
nsCOMPtr<nsIContent> selectionEnd = do_QueryInterface(selectionEndNode);
|
|
|
|
nsCOMPtr<nsIContent> link2 = FindParentLinkNode(selectionEnd);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (link == link2) {
|
|
|
|
NS_IF_ADDREF(*outLinkNode = link);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|