зеркало из https://github.com/mozilla/gecko-dev.git
Support receiving data from Mac OS X services into text and HTML editors. b=525389 r=josh r/sr=smaug
This commit is contained in:
Родитель
f35b20b502
Коммит
0f154b629d
|
@ -153,6 +153,7 @@
|
|||
#endif
|
||||
#include "nsIFocusController.h"
|
||||
#include "nsIController.h"
|
||||
#include "nsICommandParams.h"
|
||||
|
||||
#ifdef XP_MACOSX
|
||||
#import <ApplicationServices/ApplicationServices.h>
|
||||
|
@ -1343,6 +1344,7 @@ nsEventStateManager::PreHandleEvent(nsPresContext* aPresContext,
|
|||
case NS_CONTENT_COMMAND_DELETE:
|
||||
case NS_CONTENT_COMMAND_UNDO:
|
||||
case NS_CONTENT_COMMAND_REDO:
|
||||
case NS_CONTENT_COMMAND_PASTE_TRANSFERABLE:
|
||||
{
|
||||
DoContentCommandEvent(static_cast<nsContentCommandEvent*>(aEvent));
|
||||
}
|
||||
|
@ -4543,6 +4545,9 @@ nsEventStateManager::DoContentCommandEvent(nsContentCommandEvent* aEvent)
|
|||
case NS_CONTENT_COMMAND_REDO:
|
||||
cmd = "cmd_redo";
|
||||
break;
|
||||
case NS_CONTENT_COMMAND_PASTE_TRANSFERABLE:
|
||||
cmd = "cmd_pasteTransferable";
|
||||
break;
|
||||
default:
|
||||
return NS_ERROR_NOT_IMPLEMENTED;
|
||||
}
|
||||
|
@ -4559,7 +4564,25 @@ nsEventStateManager::DoContentCommandEvent(nsContentCommandEvent* aEvent)
|
|||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
aEvent->mIsEnabled = canDoIt;
|
||||
if (canDoIt && !aEvent->mOnlyEnabledCheck) {
|
||||
rv = controller->DoCommand(cmd);
|
||||
switch (aEvent->message) {
|
||||
case NS_CONTENT_COMMAND_PASTE_TRANSFERABLE: {
|
||||
nsCOMPtr<nsICommandController> commandController = do_QueryInterface(controller);
|
||||
NS_ENSURE_STATE(commandController);
|
||||
|
||||
nsCOMPtr<nsICommandParams> params = do_CreateInstance("@mozilla.org/embedcomp/command-params;1", &rv);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
rv = params->SetISupportsValue("transferable", aEvent->mTransferable);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
rv = commandController->DoCommandWithParams(cmd, params);
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
rv = controller->DoCommand(cmd);
|
||||
break;
|
||||
}
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -946,6 +946,47 @@ nsDOMWindowUtils::DispatchDOMEventViaPresShell(nsIDOMNode* aTarget,
|
|||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsDOMWindowUtils::SendContentCommandEvent(const nsAString& aType,
|
||||
nsITransferable * aTransferable)
|
||||
{
|
||||
PRBool hasCap = PR_FALSE;
|
||||
if (NS_FAILED(nsContentUtils::GetSecurityManager()->IsCapabilityEnabled("UniversalXPConnect", &hasCap))
|
||||
|| !hasCap)
|
||||
return NS_ERROR_DOM_SECURITY_ERR;
|
||||
|
||||
// get the widget to send the event to
|
||||
nsCOMPtr<nsIWidget> widget = GetWidget();
|
||||
if (!widget)
|
||||
return NS_ERROR_FAILURE;
|
||||
|
||||
PRInt32 msg;
|
||||
if (aType.EqualsLiteral("cut"))
|
||||
msg = NS_CONTENT_COMMAND_CUT;
|
||||
else if (aType.EqualsLiteral("copy"))
|
||||
msg = NS_CONTENT_COMMAND_COPY;
|
||||
else if (aType.EqualsLiteral("paste"))
|
||||
msg = NS_CONTENT_COMMAND_PASTE;
|
||||
else if (aType.EqualsLiteral("delete"))
|
||||
msg = NS_CONTENT_COMMAND_DELETE;
|
||||
else if (aType.EqualsLiteral("undo"))
|
||||
msg = NS_CONTENT_COMMAND_UNDO;
|
||||
else if (aType.EqualsLiteral("redo"))
|
||||
msg = NS_CONTENT_COMMAND_REDO;
|
||||
else if (aType.EqualsLiteral("pasteTransferable"))
|
||||
msg = NS_CONTENT_COMMAND_PASTE_TRANSFERABLE;
|
||||
else
|
||||
return NS_ERROR_FAILURE;
|
||||
|
||||
nsContentCommandEvent event(PR_TRUE, msg, widget);
|
||||
if (msg == NS_CONTENT_COMMAND_PASTE_TRANSFERABLE) {
|
||||
event.mTransferable = aTransferable;
|
||||
}
|
||||
|
||||
nsEventStatus status;
|
||||
return widget->DispatchEvent(&event, status);
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsDOMWindowUtils::GetClassName(char **aName)
|
||||
{
|
||||
|
|
|
@ -49,8 +49,9 @@ interface nsIDOMNode;
|
|||
interface nsIDOMElement;
|
||||
interface nsIDOMHTMLCanvasElement;
|
||||
interface nsIDOMEvent;
|
||||
interface nsITransferable;
|
||||
|
||||
[scriptable, uuid(0e13d9a6-bcd1-4d61-9986-c32f78f99d1a)]
|
||||
[scriptable, uuid(5ab44028-20ed-499a-bbe4-1805a1f350c8)]
|
||||
interface nsIDOMWindowUtils : nsISupports {
|
||||
|
||||
/**
|
||||
|
@ -427,4 +428,19 @@ interface nsIDOMWindowUtils : nsISupports {
|
|||
* wrapper) of aObj.
|
||||
*/
|
||||
string getClassName(/*in JSObjectPtr aObj*/);
|
||||
|
||||
/**
|
||||
* Generate a content command event.
|
||||
*
|
||||
* Cannot be accessed from unprivileged context (not content-accessible)
|
||||
* Will throw a DOM security error if called without UniversalXPConnect
|
||||
* privileges.
|
||||
*
|
||||
* @param aType Type of command content event to send. Can be one of "cut",
|
||||
* "copy", "paste", "delete", "undo", "redo", or "pasteTransferable".
|
||||
* @param aTransferable an instance of nsITransferable when aType is
|
||||
* "pasteTransferable"
|
||||
*/
|
||||
void sendContentCommandEvent(in AString aType,
|
||||
[optional] in nsITransferable aTransferable);
|
||||
};
|
||||
|
|
|
@ -53,6 +53,7 @@ interface nsITransaction;
|
|||
interface nsIEditorObserver;
|
||||
interface nsIEditActionListener;
|
||||
interface nsIInlineSpellChecker;
|
||||
interface nsITransferable;
|
||||
|
||||
%{C++
|
||||
class nsIPresShell;
|
||||
|
@ -61,7 +62,7 @@ typedef short EDirection;
|
|||
|
||||
[ptr] native nsIPresShellPtr(nsIPresShell);
|
||||
|
||||
[scriptable, uuid(96b60ba0-634a-41e4-928e-78ab0b3c4b46)]
|
||||
[scriptable, uuid(63084019-4cd0-45f8-8b10-d9f0231b2f63)]
|
||||
|
||||
interface nsIEditor : nsISupports
|
||||
{
|
||||
|
@ -339,11 +340,23 @@ interface nsIEditor : nsISupports
|
|||
*/
|
||||
void paste(in long aSelectionType);
|
||||
|
||||
/** Paste the text in |aTransferable| at the cursor position, replacing the
|
||||
* selected text (if any).
|
||||
*/
|
||||
void pasteTransferable(in nsITransferable aTransferable);
|
||||
|
||||
/** Can we paste? True if the doc is modifiable, and we have
|
||||
* pasteable data in the clipboard.
|
||||
*/
|
||||
boolean canPaste(in long aSelectionType);
|
||||
|
||||
/** Can we paste |aTransferable| or, if |aTransferable| is null, will a call
|
||||
* to pasteTransferable later possibly succeed if given an instance of
|
||||
* nsITransferable then? True if the doc is modifiable, and, if
|
||||
* |aTransfeable| is non-null, we have pasteable data in |aTransfeable|.
|
||||
*/
|
||||
boolean canPasteTransferable([optional] in nsITransferable aTransferable);
|
||||
|
||||
/* ------------ Selection methods -------------- */
|
||||
|
||||
/** sets the document selection to the entire contents of the document */
|
||||
|
|
|
@ -112,6 +112,8 @@
|
|||
#include "nsIHTMLDocument.h"
|
||||
#include "nsIParserService.h"
|
||||
|
||||
#include "nsITransferable.h"
|
||||
|
||||
#define NS_ERROR_EDITOR_NO_SELECTION NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_EDITOR,1)
|
||||
#define NS_ERROR_EDITOR_NO_TEXTNODE NS_ERROR_GENERATE_FAILURE(NS_ERROR_MODULE_EDITOR,2)
|
||||
|
||||
|
@ -1258,12 +1260,24 @@ nsEditor::Paste(PRInt32 aSelectionType)
|
|||
return NS_ERROR_NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsEditor::PasteTransferable(nsITransferable *aTransferable)
|
||||
{
|
||||
return NS_ERROR_NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsEditor::CanPaste(PRInt32 aSelectionType, PRBool *aCanPaste)
|
||||
{
|
||||
return NS_ERROR_NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsEditor::CanPasteTransferable(nsITransferable *aTransferable, PRBool *aCanPaste)
|
||||
{
|
||||
return NS_ERROR_NOT_IMPLEMENTED;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsEditor::CanDrag(nsIDOMEvent *aEvent, PRBool *aCanDrag)
|
||||
{
|
||||
|
|
|
@ -436,6 +436,73 @@ nsPasteCommand::GetCommandStateParams(const char *aCommandName,
|
|||
return aParams->SetBooleanValue(STATE_ENABLED,canUndo);
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsPasteTransferableCommand::IsCommandEnabled(const char *aCommandName,
|
||||
nsISupports *aCommandRefCon,
|
||||
PRBool *outCmdEnabled)
|
||||
{
|
||||
NS_ENSURE_ARG_POINTER(outCmdEnabled);
|
||||
nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
|
||||
if (editor)
|
||||
return editor->CanPasteTransferable(nsnull, outCmdEnabled);
|
||||
|
||||
*outCmdEnabled = PR_FALSE;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsPasteTransferableCommand::DoCommand(const char *aCommandName, nsISupports *aCommandRefCon)
|
||||
{
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsPasteTransferableCommand::DoCommandParams(const char *aCommandName,
|
||||
nsICommandParams *aParams,
|
||||
nsISupports *aCommandRefCon)
|
||||
{
|
||||
nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
|
||||
if (!editor)
|
||||
return NS_ERROR_FAILURE;
|
||||
|
||||
nsCOMPtr<nsISupports> supports;
|
||||
aParams->GetISupportsValue("transferable", getter_AddRefs(supports));
|
||||
if (!supports)
|
||||
return NS_ERROR_FAILURE;
|
||||
|
||||
nsCOMPtr<nsITransferable> trans = do_QueryInterface(supports);
|
||||
if (!trans)
|
||||
return NS_ERROR_FAILURE;
|
||||
|
||||
return editor->PasteTransferable(trans);
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsPasteTransferableCommand::GetCommandStateParams(const char *aCommandName,
|
||||
nsICommandParams *aParams,
|
||||
nsISupports *aCommandRefCon)
|
||||
{
|
||||
nsCOMPtr<nsIEditor> editor = do_QueryInterface(aCommandRefCon);
|
||||
if (!editor)
|
||||
return NS_ERROR_FAILURE;
|
||||
|
||||
nsCOMPtr<nsITransferable> trans;
|
||||
|
||||
nsCOMPtr<nsISupports> supports;
|
||||
aParams->GetISupportsValue("transferable", getter_AddRefs(supports));
|
||||
if (supports) {
|
||||
trans = do_QueryInterface(supports);
|
||||
if (!trans)
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
PRBool canPaste;
|
||||
nsresult rv = editor->CanPasteTransferable(trans, &canPaste);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
return aParams->SetBooleanValue(STATE_ENABLED, canPaste);
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsSwitchTextDirectionCommand::IsCommandEnabled(const char *aCommandName,
|
||||
nsISupports *aCommandRefCon,
|
||||
|
|
|
@ -82,6 +82,7 @@ NS_DECL_EDITOR_COMMAND(nsCutOrDeleteCommand)
|
|||
NS_DECL_EDITOR_COMMAND(nsCopyCommand)
|
||||
NS_DECL_EDITOR_COMMAND(nsCopyOrDeleteCommand)
|
||||
NS_DECL_EDITOR_COMMAND(nsPasteCommand)
|
||||
NS_DECL_EDITOR_COMMAND(nsPasteTransferableCommand)
|
||||
NS_DECL_EDITOR_COMMAND(nsSwitchTextDirectionCommand)
|
||||
NS_DECL_EDITOR_COMMAND(nsDeleteCommand)
|
||||
NS_DECL_EDITOR_COMMAND(nsSelectAllCommand)
|
||||
|
|
|
@ -91,6 +91,8 @@ nsresult nsEditorController::RegisterEditorCommands(nsIControllerCommandTable *i
|
|||
NS_REGISTER_ONE_COMMAND(nsSelectAllCommand, "cmd_selectAll");
|
||||
|
||||
NS_REGISTER_ONE_COMMAND(nsPasteCommand, "cmd_paste");
|
||||
NS_REGISTER_ONE_COMMAND(nsPasteTransferableCommand, "cmd_pasteTransferable");
|
||||
|
||||
NS_REGISTER_ONE_COMMAND(nsSwitchTextDirectionCommand, "cmd_switchTextDirection");
|
||||
|
||||
NS_REGISTER_FIRST_COMMAND(nsDeleteCommand, "cmd_delete");
|
||||
|
|
|
@ -1933,6 +1933,30 @@ NS_IMETHODIMP nsHTMLEditor::Paste(PRInt32 aSelectionType)
|
|||
return rv;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP nsHTMLEditor::PasteTransferable(nsITransferable *aTransferable)
|
||||
{
|
||||
ForceCompositionEnd();
|
||||
|
||||
PRBool preventDefault;
|
||||
nsresult rv = FireClipboardEvent(NS_PASTE, &preventDefault);
|
||||
if (NS_FAILED(rv) || preventDefault)
|
||||
return rv;
|
||||
|
||||
// handle transferable hooks
|
||||
nsCOMPtr<nsIDOMDocument> domdoc;
|
||||
GetDocument(getter_AddRefs(domdoc));
|
||||
if (!nsEditorHookUtils::DoInsertionHook(domdoc, nsnull, aTransferable))
|
||||
return NS_OK;
|
||||
|
||||
// Beware! This may flush notifications via synchronous
|
||||
// ScrollSelectionIntoView.
|
||||
nsAutoString contextStr, infoStr;
|
||||
rv = InsertFromTransferable(aTransferable, nsnull, contextStr, infoStr,
|
||||
nsnull, 0, PR_TRUE);
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
//
|
||||
// HTML PasteNoFormatting. Ignore any HTML styles and formating in paste source
|
||||
//
|
||||
|
@ -1967,6 +1991,14 @@ NS_IMETHODIMP nsHTMLEditor::PasteNoFormatting(PRInt32 aSelectionType)
|
|||
}
|
||||
|
||||
|
||||
// The following arrays contain the MIME types that we can paste. The arrays
|
||||
// are used by CanPaste() and CanPasteTransferable() below.
|
||||
|
||||
static const char* textEditorFlavors[] = { kUnicodeMime };
|
||||
static const char* textHtmlEditorFlavors[] = { kUnicodeMime, kHTMLMime,
|
||||
kJPEGImageMime, kPNGImageMime,
|
||||
kGIFImageMime };
|
||||
|
||||
NS_IMETHODIMP nsHTMLEditor::CanPaste(PRInt32 aSelectionType, PRBool *aCanPaste)
|
||||
{
|
||||
NS_ENSURE_ARG_POINTER(aCanPaste);
|
||||
|
@ -1980,11 +2012,6 @@ NS_IMETHODIMP nsHTMLEditor::CanPaste(PRInt32 aSelectionType, PRBool *aCanPaste)
|
|||
nsCOMPtr<nsIClipboard> clipboard(do_GetService("@mozilla.org/widget/clipboard;1", &rv));
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
|
||||
// the flavors that we can deal with (preferred order selectable for k*ImageMime)
|
||||
const char* textEditorFlavors[] = { kUnicodeMime };
|
||||
const char* textHtmlEditorFlavors[] = { kUnicodeMime, kHTMLMime,
|
||||
kJPEGImageMime, kPNGImageMime, kGIFImageMime };
|
||||
|
||||
PRUint32 editorFlags;
|
||||
GetFlags(&editorFlags);
|
||||
|
||||
|
@ -2006,6 +2033,54 @@ NS_IMETHODIMP nsHTMLEditor::CanPaste(PRInt32 aSelectionType, PRBool *aCanPaste)
|
|||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP nsHTMLEditor::CanPasteTransferable(nsITransferable *aTransferable, PRBool *aCanPaste)
|
||||
{
|
||||
NS_ENSURE_ARG_POINTER(aCanPaste);
|
||||
|
||||
// can't paste if readonly
|
||||
if (!IsModifiable()) {
|
||||
*aCanPaste = PR_FALSE;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
// If |aTransferable| is null, assume that a paste will succeed.
|
||||
if (!aTransferable) {
|
||||
*aCanPaste = PR_TRUE;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
// Peek in |aTransferable| to see if it contains a supported MIME type.
|
||||
|
||||
PRUint32 editorFlags;
|
||||
GetFlags(&editorFlags);
|
||||
|
||||
// Use the flavors depending on the current editor mask
|
||||
const char ** flavors;
|
||||
unsigned length;
|
||||
if ((editorFlags & eEditorPlaintextMask)) {
|
||||
flavors = textEditorFlavors;
|
||||
length = NS_ARRAY_LENGTH(textEditorFlavors);
|
||||
} else {
|
||||
flavors = textHtmlEditorFlavors;
|
||||
length = NS_ARRAY_LENGTH(textHtmlEditorFlavors);
|
||||
}
|
||||
|
||||
for (unsigned int i = 0; i < length; i++, flavors++) {
|
||||
nsCOMPtr<nsISupports> data;
|
||||
PRUint32 dataLen;
|
||||
nsresult rv = aTransferable->GetTransferData(*flavors,
|
||||
getter_AddRefs(data),
|
||||
&dataLen);
|
||||
if (NS_SUCCEEDED(rv) && data) {
|
||||
*aCanPaste = PR_TRUE;
|
||||
return NS_OK;
|
||||
}
|
||||
}
|
||||
|
||||
*aCanPaste = PR_FALSE;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// HTML PasteAsQuotation: Paste in a blockquote type=cite
|
||||
|
|
|
@ -319,6 +319,9 @@ public:
|
|||
NS_IMETHOD Paste(PRInt32 aSelectionType);
|
||||
NS_IMETHOD CanPaste(PRInt32 aSelectionType, PRBool *aCanPaste);
|
||||
|
||||
NS_IMETHOD PasteTransferable(nsITransferable *aTransferable);
|
||||
NS_IMETHOD CanPasteTransferable(nsITransferable *aTransferable, PRBool *aCanPaste);
|
||||
|
||||
NS_IMETHOD DebugUnitTests(PRInt32 *outNumTests, PRInt32 *outNumTestsFailed);
|
||||
|
||||
/** All editor operations which alter the doc should be prefaced
|
||||
|
|
|
@ -52,6 +52,7 @@ _TEST_FILES = \
|
|||
test_bug478725.html \
|
||||
test_bug480972.html \
|
||||
test_bug487524.html \
|
||||
test_bug525389.html \
|
||||
test_select_all_without_body.html \
|
||||
file_select_all_without_body.html \
|
||||
$(NULL)
|
||||
|
|
|
@ -0,0 +1,189 @@
|
|||
<!DOCTYPE HTML>
|
||||
<html><head>
|
||||
<title>Test for bug 525389</title>
|
||||
<style src="/tests/SimpleTest/test.css" type="text/css"></style>
|
||||
<script type="application/javascript" src="/MochiKit/packed.js"></script>
|
||||
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
|
||||
<script type="text/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
|
||||
|
||||
<script class="testbody" type="application/javascript">
|
||||
|
||||
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
|
||||
var utils = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor).getInterface(Components.interfaces.nsIDOMWindowUtils);
|
||||
var Cc = Components.classes;
|
||||
var Ci = Components.interfaces;
|
||||
|
||||
function runTest() {
|
||||
var pasteCount = 0;
|
||||
var pasteFunc = function (event) { pasteCount++; };
|
||||
|
||||
function verifyContent(s) {
|
||||
var e = document.getElementById('i1');
|
||||
var doc = e.contentDocument;
|
||||
is(doc.body.innerHTML, s, "");
|
||||
}
|
||||
|
||||
function pasteInto(trans, html, target_id) {
|
||||
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
|
||||
var e = document.getElementById('i1');
|
||||
var doc = e.contentDocument;
|
||||
doc.designMode = "on";
|
||||
doc.body.innerHTML = html;
|
||||
doc.defaultView.focus();
|
||||
if (target_id)
|
||||
e = doc.getElementById(target_id);
|
||||
else
|
||||
e = doc.body;
|
||||
var selection = doc.defaultView.getSelection();
|
||||
selection.removeAllRanges();
|
||||
selection.selectAllChildren(e);
|
||||
selection.collapseToEnd();
|
||||
|
||||
pasteCount = 0;
|
||||
e.addEventListener("paste", pasteFunc, false);
|
||||
utils.sendContentCommandEvent("pasteTransferable", trans);
|
||||
e.removeEventListener("paste", pasteFunc, false);
|
||||
|
||||
return e;
|
||||
}
|
||||
|
||||
function getTransferableFromClipboard(asHTML) {
|
||||
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
|
||||
var trans = Cc["@mozilla.org/widget/transferable;1"].createInstance(Ci.nsITransferable);
|
||||
if (asHTML) {
|
||||
trans.addDataFlavor("text/html");
|
||||
} else {
|
||||
trans.addDataFlavor("text/unicode");
|
||||
}
|
||||
var clip = Cc["@mozilla.org/widget/clipboard;1"].getService(Ci.nsIClipboard);
|
||||
clip.getData(trans, Ci.nsIClipboard.kGlobalClipboard);
|
||||
return trans;
|
||||
}
|
||||
|
||||
function makeTransferable(s,asHTML,target_id) {
|
||||
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
|
||||
|
||||
var e = document.getElementById('i2');
|
||||
var doc = e.contentDocument;
|
||||
if (asHTML) {
|
||||
doc.body.innerHTML = s;
|
||||
} else {
|
||||
var text = doc.createTextNode(s);
|
||||
doc.body.appendChild(text);
|
||||
}
|
||||
doc.designMode = "on";
|
||||
doc.defaultView.focus();
|
||||
var selection = doc.defaultView.getSelection();
|
||||
selection.removeAllRanges();
|
||||
if (!target_id) {
|
||||
selection.selectAllChildren(doc.body);
|
||||
} else {
|
||||
var range = document.createRange();
|
||||
range.selectNode(doc.getElementById(target_id));
|
||||
selection.addRange(range);
|
||||
}
|
||||
|
||||
// We cannot use plain strings, we have to use nsSupportsString.
|
||||
var supportsStringClass = Components.classes["@mozilla.org/supports-string;1"];
|
||||
var ssData = supportsStringClass.createInstance(Ci.nsISupportsString);
|
||||
|
||||
// Create the transferable.
|
||||
var trans = Cc["@mozilla.org/widget/transferable;1"].createInstance(Ci.nsITransferable);
|
||||
|
||||
// Add the data to the transferable.
|
||||
if (asHTML) {
|
||||
trans.addDataFlavor("text/html");
|
||||
ssData.data = doc.body.innerHTML;
|
||||
trans.setTransferData("text/html", ssData, ssData.length * 2);
|
||||
} else {
|
||||
trans.addDataFlavor("text/unicode");
|
||||
ssData.data = doc.body.innerHTML;
|
||||
trans.setTransferData("text/unicode", ssData, ssData.length * 2);
|
||||
}
|
||||
|
||||
return trans;
|
||||
}
|
||||
|
||||
function copyToClipBoard(s,asHTML,target_id) {
|
||||
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
|
||||
var e = document.getElementById('i2');
|
||||
var doc = e.contentDocument;
|
||||
if (asHTML) {
|
||||
doc.body.innerHTML = s;
|
||||
} else {
|
||||
var text = doc.createTextNode(s);
|
||||
doc.body.appendChild(text);
|
||||
}
|
||||
doc.designMode = "on";
|
||||
doc.defaultView.focus();
|
||||
var selection = doc.defaultView.getSelection();
|
||||
selection.removeAllRanges();
|
||||
if (!target_id) {
|
||||
selection.selectAllChildren(doc.body);
|
||||
} else {
|
||||
var range = document.createRange();
|
||||
range.selectNode(doc.getElementById(target_id));
|
||||
selection.addRange(range);
|
||||
}
|
||||
doc.execCommand("copy", false, null);
|
||||
return e;
|
||||
}
|
||||
|
||||
copyToClipBoard('<span>Hello</span><span>Kitty</span>', true);
|
||||
var trans = getTransferableFromClipboard(true);
|
||||
pasteInto(trans, '');
|
||||
verifyContent('<span>Hello</span><span>Kitty</span>');
|
||||
is(pasteCount, 1, "paste event was not triggered");
|
||||
|
||||
// this test is not working out exactly like the clipboard test
|
||||
// has to do with generating the nsITransferable above
|
||||
//trans = makeTransferable('<span>Hello</span><span>Kitty</span>', true);
|
||||
//pasteInto(trans, '');
|
||||
//verifyContent('<span>Hello</span><span>Kitty</span>');
|
||||
|
||||
copyToClipBoard("<dl><dd>Hello Kitty</dd></dl><span>Hello</span><span>Kitty</span>", true);
|
||||
trans = getTransferableFromClipboard(true);
|
||||
pasteInto(trans, '<ol><li id="paste_here">X</li></ol>',"paste_here");
|
||||
verifyContent('<ol><li id="paste_here">X<dl><dd>Hello Kitty</dd></dl><span>Hello</span><span>Kitty</span></li></ol>');
|
||||
is(pasteCount, 1, "paste event was not triggered");
|
||||
|
||||
// The following test doesn't do what I expected, because the special handling
|
||||
// of IsList nodes in nsHTMLEditor::InsertHTMLWithContext simply removes
|
||||
// non-list/item children. See bug 481177.
|
||||
// copyToClipBoard("<ol><li>Hello Kitty</li><span>Hello</span></ol>", true);
|
||||
// pasteInto('<ol><li id="paste_here">X</li></ol>',"paste_here");
|
||||
// verifyContent('<ol><li id="paste_here">X</li><li>Hello Kitty</li><span>Hello</span></ol>');
|
||||
|
||||
copyToClipBoard("<pre>Kitty</pre><span>Hello</span>", true);
|
||||
trans = getTransferableFromClipboard(true);
|
||||
pasteInto(trans, '<pre id="paste_here">Hello </pre>',"paste_here");
|
||||
verifyContent('<pre id="paste_here">Hello Kitty<span>Hello</span></pre>');
|
||||
is(pasteCount, 1, "paste event was not triggered");
|
||||
|
||||
// test that we can preventDefault pastes
|
||||
pasteFunc = function (event) { event.preventDefault(); return false; };
|
||||
copyToClipBoard("<pre>Kitty</pre><span>Hello</span>", true);
|
||||
trans = getTransferableFromClipboard(true);
|
||||
pasteInto(trans, '<pre id="paste_here">Hello </pre>',"paste_here");
|
||||
verifyContent('<pre id="paste_here">Hello </pre>');
|
||||
is(pasteCount, 0, "paste event was triggered");
|
||||
|
||||
SimpleTest.finish();
|
||||
}
|
||||
|
||||
SimpleTest.waitForExplicitFinish();
|
||||
addLoadEvent(runTest);
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=525389">Mozilla Bug 525389</a>
|
||||
<p id="display"></p>
|
||||
|
||||
<pre id="test">
|
||||
</pre>
|
||||
|
||||
<iframe id="i1" width="200" height="100" src="about:blank"></iframe><br>
|
||||
<iframe id="i2" width="200" height="100" src="about:blank"></iframe><br>
|
||||
|
||||
</body>
|
||||
</html>
|
|
@ -455,6 +455,30 @@ NS_IMETHODIMP nsPlaintextEditor::Paste(PRInt32 aSelectionType)
|
|||
return rv;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP nsPlaintextEditor::PasteTransferable(nsITransferable *aTransferable)
|
||||
{
|
||||
ForceCompositionEnd();
|
||||
|
||||
PRBool preventDefault;
|
||||
nsresult rv = FireClipboardEvent(NS_PASTE, &preventDefault);
|
||||
if (NS_FAILED(rv) || preventDefault)
|
||||
return rv;
|
||||
|
||||
if (!IsModifiable())
|
||||
return NS_OK;
|
||||
|
||||
// handle transferable hooks
|
||||
nsCOMPtr<nsIDOMDocument> domdoc;
|
||||
GetDocument(getter_AddRefs(domdoc));
|
||||
if (!nsEditorHookUtils::DoInsertionHook(domdoc, nsnull, aTransferable))
|
||||
return NS_OK;
|
||||
|
||||
// Beware! This may flush notifications via synchronous
|
||||
// ScrollSelectionIntoView.
|
||||
rv = InsertTextFromTransferable(aTransferable, nsnull, nsnull, PR_TRUE);
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP nsPlaintextEditor::CanPaste(PRInt32 aSelectionType, PRBool *aCanPaste)
|
||||
{
|
||||
|
@ -482,6 +506,37 @@ NS_IMETHODIMP nsPlaintextEditor::CanPaste(PRInt32 aSelectionType, PRBool *aCanPa
|
|||
return NS_OK;
|
||||
}
|
||||
|
||||
|
||||
NS_IMETHODIMP nsPlaintextEditor::CanPasteTransferable(nsITransferable *aTransferable, PRBool *aCanPaste)
|
||||
{
|
||||
NS_ENSURE_ARG_POINTER(aCanPaste);
|
||||
|
||||
// can't paste if readonly
|
||||
if (!IsModifiable()) {
|
||||
*aCanPaste = PR_FALSE;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
// If |aTransferable| is null, assume that a paste will succeed.
|
||||
if (!aTransferable) {
|
||||
*aCanPaste = PR_TRUE;
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsCOMPtr<nsISupports> data;
|
||||
PRUint32 dataLen;
|
||||
nsresult rv = aTransferable->GetTransferData(kUnicodeMime,
|
||||
getter_AddRefs(data),
|
||||
&dataLen);
|
||||
if (NS_SUCCEEDED(rv) && data)
|
||||
*aCanPaste = PR_TRUE;
|
||||
else
|
||||
*aCanPaste = PR_FALSE;
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
|
||||
nsresult
|
||||
nsPlaintextEditor::SetupDocEncoder(nsIDocumentEncoder **aDocEncoder)
|
||||
{
|
||||
|
|
|
@ -124,6 +124,8 @@ public:
|
|||
NS_IMETHOD CanCopy(PRBool *aCanCopy);
|
||||
NS_IMETHOD Paste(PRInt32 aSelectionType);
|
||||
NS_IMETHOD CanPaste(PRInt32 aSelectionType, PRBool *aCanPaste);
|
||||
NS_IMETHOD PasteTransferable(nsITransferable *aTransferable);
|
||||
NS_IMETHOD CanPasteTransferable(nsITransferable *aTransferable, PRBool *aCanPaste);
|
||||
|
||||
NS_IMETHOD CanDrag(nsIDOMEvent *aDragEvent, PRBool *aCanDrag);
|
||||
NS_IMETHOD DoDrag(nsIDOMEvent *aDragEvent);
|
||||
|
|
|
@ -439,6 +439,7 @@ class nsHashKey;
|
|||
#define NS_CONTENT_COMMAND_DELETE (NS_CONTENT_COMMAND_EVENT_START+3)
|
||||
#define NS_CONTENT_COMMAND_UNDO (NS_CONTENT_COMMAND_EVENT_START+4)
|
||||
#define NS_CONTENT_COMMAND_REDO (NS_CONTENT_COMMAND_EVENT_START+5)
|
||||
#define NS_CONTENT_COMMAND_PASTE_TRANSFERABLE (NS_CONTENT_COMMAND_EVENT_START+6)
|
||||
|
||||
// Event to gesture notification
|
||||
#define NS_GESTURENOTIFY_EVENT_START 3900
|
||||
|
@ -1234,6 +1235,7 @@ public:
|
|||
{
|
||||
}
|
||||
|
||||
nsCOMPtr<nsITransferable> mTransferable; // [in]
|
||||
PRPackedBool mOnlyEnabledCheck; // [in]
|
||||
|
||||
PRPackedBool mSucceeded; // [out]
|
||||
|
|
|
@ -2200,8 +2200,8 @@ NSEvent* gLastDragMouseDownEvent = nil;
|
|||
// that we can handle.
|
||||
|
||||
NSArray *sendTypes = [[NSArray alloc] initWithObjects:NSStringPboardType,NSHTMLPboardType,nil];
|
||||
NSArray *returnTypes = [[NSArray alloc] init];
|
||||
|
||||
NSArray *returnTypes = [[NSArray alloc] initWithObjects:NSStringPboardType,NSHTMLPboardType,nil];
|
||||
|
||||
[NSApp registerServicesMenuSendTypes:sendTypes returnTypes:returnTypes];
|
||||
|
||||
[sendTypes release];
|
||||
|
@ -6102,26 +6102,47 @@ static BOOL keyUpAlreadySentKeyDown = NO;
|
|||
// returnType is nil if the service will not return any data.
|
||||
//
|
||||
// The following condition thus triggers when the service expects a string
|
||||
// or HTML from us or no data at all AND when the service will not send back
|
||||
// any data to us.
|
||||
// or HTML from us or no data at all AND when the service will either not
|
||||
// send back any data to us or will send a string or HTML back to us.
|
||||
|
||||
if ((!sendType || [sendType isEqual:NSStringPboardType] ||
|
||||
[sendType isEqual:NSHTMLPboardType]) && !returnType) {
|
||||
// Query the Gecko window to determine if there is a current selection.
|
||||
#define IsSupportedType(typeStr) ([typeStr isEqual:NSStringPboardType] || [typeStr isEqual:NSHTMLPboardType])
|
||||
|
||||
id result = nil;
|
||||
|
||||
if ((!sendType || IsSupportedType(sendType)) &&
|
||||
(!returnType || IsSupportedType(returnType))) {
|
||||
if (mGeckoChild) {
|
||||
// Assume that this object will be able to handle this request.
|
||||
result = self;
|
||||
|
||||
// Keep the ChildView alive during this operation.
|
||||
nsAutoRetainCocoaObject kungFuDeathGrip(self);
|
||||
|
||||
// Determine if there is a selection (if sending to the service).
|
||||
if (sendType) {
|
||||
nsQueryContentEvent event(PR_TRUE, NS_QUERY_CONTENT_STATE, mGeckoChild);
|
||||
mGeckoChild->DispatchWindowEvent(event);
|
||||
if (!event.mSucceeded || !event.mReply.mHasSelection)
|
||||
result = nil;
|
||||
}
|
||||
|
||||
nsQueryContentEvent event(PR_TRUE, NS_QUERY_CONTENT_STATE, mGeckoChild);
|
||||
mGeckoChild->DispatchWindowEvent(event);
|
||||
|
||||
// Return this object if it can handle the request.
|
||||
if ((!sendType || (event.mSucceeded && event.mReply.mHasSelection)) &&
|
||||
!returnType)
|
||||
return self;
|
||||
// Determine if we can paste (if receiving data from the service).
|
||||
if (returnType) {
|
||||
nsContentCommandEvent command(PR_TRUE, NS_CONTENT_COMMAND_PASTE_TRANSFERABLE, mGeckoChild, PR_TRUE);
|
||||
mGeckoChild->DispatchWindowEvent(command);
|
||||
if (!command.mSucceeded || !command.mIsEnabled)
|
||||
result = nil;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return [super validRequestorForSendType:sendType returnType:returnType];
|
||||
#undef IsSupportedType
|
||||
|
||||
// Give the superclass a chance if this object will not handle this request.
|
||||
if (!result)
|
||||
result = [super validRequestorForSendType:sendType returnType:returnType];
|
||||
|
||||
return result;
|
||||
|
||||
NS_OBJC_END_TRY_ABORT_BLOCK_NIL;
|
||||
}
|
||||
|
@ -6185,11 +6206,31 @@ static BOOL keyUpAlreadySentKeyDown = NO;
|
|||
NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(NO);
|
||||
}
|
||||
|
||||
// Called if the service wants us to replace the current selection. We do
|
||||
// not currently support replacing the current selection so just return NO.
|
||||
// Called if the service wants us to replace the current selection.
|
||||
- (BOOL)readSelectionFromPasteboard:(NSPasteboard *)pboard
|
||||
{
|
||||
return NO;
|
||||
nsresult rv;
|
||||
nsCOMPtr<nsITransferable> trans = do_CreateInstance("@mozilla.org/widget/transferable;1", &rv);
|
||||
if (NS_FAILED(rv))
|
||||
return NO;
|
||||
|
||||
trans->AddDataFlavor(kUnicodeMime);
|
||||
trans->AddDataFlavor(kHTMLMime);
|
||||
|
||||
rv = nsClipboard::TransferableFromPasteboard(trans, pboard);
|
||||
if (NS_FAILED(rv))
|
||||
return NO;
|
||||
|
||||
if (!mGeckoChild)
|
||||
return NO;
|
||||
|
||||
nsContentCommandEvent command(PR_TRUE,
|
||||
NS_CONTENT_COMMAND_PASTE_TRANSFERABLE,
|
||||
mGeckoChild);
|
||||
command.mTransferable = trans;
|
||||
mGeckoChild->DispatchWindowEvent(command);
|
||||
|
||||
return command.mSucceeded && command.mIsEnabled;
|
||||
}
|
||||
|
||||
#pragma mark -
|
||||
|
|
|
@ -61,6 +61,7 @@ public:
|
|||
static NSDictionary* PasteboardDictFromTransferable(nsITransferable *aTransferable);
|
||||
static PRBool IsStringType(const nsCString& aMIMEType, const NSString** aPasteboardType);
|
||||
static NSString* WrapHtmlForSystemPasteboard(NSString* aString);
|
||||
static nsresult TransferableFromPasteboard(nsITransferable *aTransferable, NSPasteboard *pboard);
|
||||
|
||||
protected:
|
||||
|
||||
|
|
|
@ -132,18 +132,11 @@ nsClipboard::SetNativeClipboardData(PRInt32 aWhichClipboard)
|
|||
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsClipboard::GetNativeClipboardData(nsITransferable* aTransferable, PRInt32 aWhichClipboard)
|
||||
nsresult
|
||||
nsClipboard::TransferableFromPasteboard(nsITransferable *aTransferable, NSPasteboard *cocoaPasteboard)
|
||||
{
|
||||
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
||||
|
||||
if ((aWhichClipboard != kGlobalClipboard) || !aTransferable)
|
||||
return NS_ERROR_FAILURE;
|
||||
|
||||
NSPasteboard* cocoaPasteboard = [NSPasteboard generalPasteboard];
|
||||
if (!cocoaPasteboard)
|
||||
return NS_ERROR_FAILURE;
|
||||
|
||||
// get flavor list that includes all acceptable flavors (including ones obtained through conversion)
|
||||
nsCOMPtr<nsISupportsArray> flavorList;
|
||||
nsresult rv = aTransferable->FlavorsTransferableCanImport(getter_AddRefs(flavorList));
|
||||
|
@ -153,37 +146,6 @@ nsClipboard::GetNativeClipboardData(nsITransferable* aTransferable, PRInt32 aWhi
|
|||
PRUint32 flavorCount;
|
||||
flavorList->Count(&flavorCount);
|
||||
|
||||
// If we were the last ones to put something on the pasteboard, then just use the cached
|
||||
// transferable. Otherwise clear it because it isn't relevant any more.
|
||||
if (mChangeCount == [cocoaPasteboard changeCount]) {
|
||||
if (mTransferable) {
|
||||
for (PRUint32 i = 0; i < flavorCount; i++) {
|
||||
nsCOMPtr<nsISupports> genericFlavor;
|
||||
flavorList->GetElementAt(i, getter_AddRefs(genericFlavor));
|
||||
nsCOMPtr<nsISupportsCString> currentFlavor(do_QueryInterface(genericFlavor));
|
||||
if (!currentFlavor)
|
||||
continue;
|
||||
|
||||
nsXPIDLCString flavorStr;
|
||||
currentFlavor->ToString(getter_Copies(flavorStr));
|
||||
|
||||
nsCOMPtr<nsISupports> dataSupports;
|
||||
PRUint32 dataSize = 0;
|
||||
rv = mTransferable->GetTransferData(flavorStr, getter_AddRefs(dataSupports), &dataSize);
|
||||
if (NS_SUCCEEDED(rv)) {
|
||||
aTransferable->SetTransferData(flavorStr, dataSupports, dataSize);
|
||||
return NS_OK; // maybe try to fill in more types? Is there a point?
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
nsBaseClipboard::EmptyClipboard(kGlobalClipboard);
|
||||
}
|
||||
|
||||
// at this point we can't satisfy the request from cache data so let's look
|
||||
// for things other people put on the system clipboard
|
||||
|
||||
for (PRUint32 i = 0; i < flavorCount; i++) {
|
||||
nsCOMPtr<nsISupports> genericFlavor;
|
||||
flavorList->GetElementAt(i, getter_AddRefs(genericFlavor));
|
||||
|
@ -297,6 +259,63 @@ nsClipboard::GetNativeClipboardData(nsITransferable* aTransferable, PRInt32 aWhi
|
|||
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsClipboard::GetNativeClipboardData(nsITransferable* aTransferable, PRInt32 aWhichClipboard)
|
||||
{
|
||||
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
||||
|
||||
if ((aWhichClipboard != kGlobalClipboard) || !aTransferable)
|
||||
return NS_ERROR_FAILURE;
|
||||
|
||||
NSPasteboard* cocoaPasteboard = [NSPasteboard generalPasteboard];
|
||||
if (!cocoaPasteboard)
|
||||
return NS_ERROR_FAILURE;
|
||||
|
||||
// get flavor list that includes all acceptable flavors (including ones obtained through conversion)
|
||||
nsCOMPtr<nsISupportsArray> flavorList;
|
||||
nsresult rv = aTransferable->FlavorsTransferableCanImport(getter_AddRefs(flavorList));
|
||||
if (NS_FAILED(rv))
|
||||
return NS_ERROR_FAILURE;
|
||||
|
||||
PRUint32 flavorCount;
|
||||
flavorList->Count(&flavorCount);
|
||||
|
||||
// If we were the last ones to put something on the pasteboard, then just use the cached
|
||||
// transferable. Otherwise clear it because it isn't relevant any more.
|
||||
if (mChangeCount == [cocoaPasteboard changeCount]) {
|
||||
if (mTransferable) {
|
||||
for (PRUint32 i = 0; i < flavorCount; i++) {
|
||||
nsCOMPtr<nsISupports> genericFlavor;
|
||||
flavorList->GetElementAt(i, getter_AddRefs(genericFlavor));
|
||||
nsCOMPtr<nsISupportsCString> currentFlavor(do_QueryInterface(genericFlavor));
|
||||
if (!currentFlavor)
|
||||
continue;
|
||||
|
||||
nsXPIDLCString flavorStr;
|
||||
currentFlavor->ToString(getter_Copies(flavorStr));
|
||||
|
||||
nsCOMPtr<nsISupports> dataSupports;
|
||||
PRUint32 dataSize = 0;
|
||||
rv = mTransferable->GetTransferData(flavorStr, getter_AddRefs(dataSupports), &dataSize);
|
||||
if (NS_SUCCEEDED(rv)) {
|
||||
aTransferable->SetTransferData(flavorStr, dataSupports, dataSize);
|
||||
return NS_OK; // maybe try to fill in more types? Is there a point?
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
nsBaseClipboard::EmptyClipboard(kGlobalClipboard);
|
||||
}
|
||||
|
||||
// at this point we can't satisfy the request from cache data so let's look
|
||||
// for things other people put on the system clipboard
|
||||
|
||||
return nsClipboard::TransferableFromPasteboard(aTransferable, cocoaPasteboard);
|
||||
|
||||
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
||||
}
|
||||
|
||||
// returns true if we have *any* of the passed in flavors available for pasting
|
||||
NS_IMETHODIMP
|
||||
nsClipboard::HasDataMatchingFlavors(const char** aFlavorList, PRUint32 aLength,
|
||||
|
|
Загрузка…
Ссылка в новой задаче