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:
Tom Dyas 2009-12-30 15:58:33 -05:00
parent fa34110016
commit 13780ad38c
18 changed files with 631 additions and 66 deletions

View File

@ -154,6 +154,7 @@
#endif #endif
#include "nsIFocusController.h" #include "nsIFocusController.h"
#include "nsIController.h" #include "nsIController.h"
#include "nsICommandParams.h"
#ifdef XP_MACOSX #ifdef XP_MACOSX
#import <ApplicationServices/ApplicationServices.h> #import <ApplicationServices/ApplicationServices.h>
@ -1344,6 +1345,7 @@ nsEventStateManager::PreHandleEvent(nsPresContext* aPresContext,
case NS_CONTENT_COMMAND_DELETE: case NS_CONTENT_COMMAND_DELETE:
case NS_CONTENT_COMMAND_UNDO: case NS_CONTENT_COMMAND_UNDO:
case NS_CONTENT_COMMAND_REDO: case NS_CONTENT_COMMAND_REDO:
case NS_CONTENT_COMMAND_PASTE_TRANSFERABLE:
{ {
DoContentCommandEvent(static_cast<nsContentCommandEvent*>(aEvent)); DoContentCommandEvent(static_cast<nsContentCommandEvent*>(aEvent));
} }
@ -4546,6 +4548,9 @@ nsEventStateManager::DoContentCommandEvent(nsContentCommandEvent* aEvent)
case NS_CONTENT_COMMAND_REDO: case NS_CONTENT_COMMAND_REDO:
cmd = "cmd_redo"; cmd = "cmd_redo";
break; break;
case NS_CONTENT_COMMAND_PASTE_TRANSFERABLE:
cmd = "cmd_pasteTransferable";
break;
default: default:
return NS_ERROR_NOT_IMPLEMENTED; return NS_ERROR_NOT_IMPLEMENTED;
} }
@ -4562,7 +4567,25 @@ nsEventStateManager::DoContentCommandEvent(nsContentCommandEvent* aEvent)
NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_SUCCESS(rv, rv);
aEvent->mIsEnabled = canDoIt; aEvent->mIsEnabled = canDoIt;
if (canDoIt && !aEvent->mOnlyEnabledCheck) { 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); NS_ENSURE_SUCCESS(rv, rv);
} }
} }

View File

@ -946,6 +946,47 @@ nsDOMWindowUtils::DispatchDOMEventViaPresShell(nsIDOMNode* aTarget,
return NS_OK; 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 NS_IMETHODIMP
nsDOMWindowUtils::GetClassName(char **aName) nsDOMWindowUtils::GetClassName(char **aName)
{ {

View File

@ -49,8 +49,9 @@ interface nsIDOMNode;
interface nsIDOMElement; interface nsIDOMElement;
interface nsIDOMHTMLCanvasElement; interface nsIDOMHTMLCanvasElement;
interface nsIDOMEvent; interface nsIDOMEvent;
interface nsITransferable;
[scriptable, uuid(0e13d9a6-bcd1-4d61-9986-c32f78f99d1a)] [scriptable, uuid(5ab44028-20ed-499a-bbe4-1805a1f350c8)]
interface nsIDOMWindowUtils : nsISupports { interface nsIDOMWindowUtils : nsISupports {
/** /**
@ -427,4 +428,19 @@ interface nsIDOMWindowUtils : nsISupports {
* wrapper) of aObj. * wrapper) of aObj.
*/ */
string getClassName(/*in JSObjectPtr 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);
}; };

View File

@ -53,6 +53,7 @@ interface nsITransaction;
interface nsIEditorObserver; interface nsIEditorObserver;
interface nsIEditActionListener; interface nsIEditActionListener;
interface nsIInlineSpellChecker; interface nsIInlineSpellChecker;
interface nsITransferable;
%{C++ %{C++
class nsIPresShell; class nsIPresShell;
@ -61,7 +62,7 @@ typedef short EDirection;
[ptr] native nsIPresShellPtr(nsIPresShell); [ptr] native nsIPresShellPtr(nsIPresShell);
[scriptable, uuid(96b60ba0-634a-41e4-928e-78ab0b3c4b46)] [scriptable, uuid(63084019-4cd0-45f8-8b10-d9f0231b2f63)]
interface nsIEditor : nsISupports interface nsIEditor : nsISupports
{ {
@ -339,11 +340,23 @@ interface nsIEditor : nsISupports
*/ */
void paste(in long aSelectionType); 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 /** Can we paste? True if the doc is modifiable, and we have
* pasteable data in the clipboard. * pasteable data in the clipboard.
*/ */
boolean canPaste(in long aSelectionType); 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 -------------- */ /* ------------ Selection methods -------------- */
/** sets the document selection to the entire contents of the document */ /** sets the document selection to the entire contents of the document */

View File

@ -112,6 +112,8 @@
#include "nsIHTMLDocument.h" #include "nsIHTMLDocument.h"
#include "nsIParserService.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_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) #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; return NS_ERROR_NOT_IMPLEMENTED;
} }
NS_IMETHODIMP
nsEditor::PasteTransferable(nsITransferable *aTransferable)
{
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP NS_IMETHODIMP
nsEditor::CanPaste(PRInt32 aSelectionType, PRBool *aCanPaste) nsEditor::CanPaste(PRInt32 aSelectionType, PRBool *aCanPaste)
{ {
return NS_ERROR_NOT_IMPLEMENTED; return NS_ERROR_NOT_IMPLEMENTED;
} }
NS_IMETHODIMP
nsEditor::CanPasteTransferable(nsITransferable *aTransferable, PRBool *aCanPaste)
{
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP NS_IMETHODIMP
nsEditor::CanDrag(nsIDOMEvent *aEvent, PRBool *aCanDrag) nsEditor::CanDrag(nsIDOMEvent *aEvent, PRBool *aCanDrag)
{ {

View File

@ -436,6 +436,73 @@ nsPasteCommand::GetCommandStateParams(const char *aCommandName,
return aParams->SetBooleanValue(STATE_ENABLED,canUndo); 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 NS_IMETHODIMP
nsSwitchTextDirectionCommand::IsCommandEnabled(const char *aCommandName, nsSwitchTextDirectionCommand::IsCommandEnabled(const char *aCommandName,
nsISupports *aCommandRefCon, nsISupports *aCommandRefCon,

View File

@ -82,6 +82,7 @@ NS_DECL_EDITOR_COMMAND(nsCutOrDeleteCommand)
NS_DECL_EDITOR_COMMAND(nsCopyCommand) NS_DECL_EDITOR_COMMAND(nsCopyCommand)
NS_DECL_EDITOR_COMMAND(nsCopyOrDeleteCommand) NS_DECL_EDITOR_COMMAND(nsCopyOrDeleteCommand)
NS_DECL_EDITOR_COMMAND(nsPasteCommand) NS_DECL_EDITOR_COMMAND(nsPasteCommand)
NS_DECL_EDITOR_COMMAND(nsPasteTransferableCommand)
NS_DECL_EDITOR_COMMAND(nsSwitchTextDirectionCommand) NS_DECL_EDITOR_COMMAND(nsSwitchTextDirectionCommand)
NS_DECL_EDITOR_COMMAND(nsDeleteCommand) NS_DECL_EDITOR_COMMAND(nsDeleteCommand)
NS_DECL_EDITOR_COMMAND(nsSelectAllCommand) NS_DECL_EDITOR_COMMAND(nsSelectAllCommand)

View File

@ -91,6 +91,8 @@ nsresult nsEditorController::RegisterEditorCommands(nsIControllerCommandTable *i
NS_REGISTER_ONE_COMMAND(nsSelectAllCommand, "cmd_selectAll"); NS_REGISTER_ONE_COMMAND(nsSelectAllCommand, "cmd_selectAll");
NS_REGISTER_ONE_COMMAND(nsPasteCommand, "cmd_paste"); NS_REGISTER_ONE_COMMAND(nsPasteCommand, "cmd_paste");
NS_REGISTER_ONE_COMMAND(nsPasteTransferableCommand, "cmd_pasteTransferable");
NS_REGISTER_ONE_COMMAND(nsSwitchTextDirectionCommand, "cmd_switchTextDirection"); NS_REGISTER_ONE_COMMAND(nsSwitchTextDirectionCommand, "cmd_switchTextDirection");
NS_REGISTER_FIRST_COMMAND(nsDeleteCommand, "cmd_delete"); NS_REGISTER_FIRST_COMMAND(nsDeleteCommand, "cmd_delete");

View File

@ -1933,6 +1933,30 @@ NS_IMETHODIMP nsHTMLEditor::Paste(PRInt32 aSelectionType)
return rv; 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 // 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_IMETHODIMP nsHTMLEditor::CanPaste(PRInt32 aSelectionType, PRBool *aCanPaste)
{ {
NS_ENSURE_ARG_POINTER(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)); nsCOMPtr<nsIClipboard> clipboard(do_GetService("@mozilla.org/widget/clipboard;1", &rv));
if (NS_FAILED(rv)) return 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; PRUint32 editorFlags;
GetFlags(&editorFlags); GetFlags(&editorFlags);
@ -2006,6 +2033,54 @@ NS_IMETHODIMP nsHTMLEditor::CanPaste(PRInt32 aSelectionType, PRBool *aCanPaste)
return NS_OK; 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 // HTML PasteAsQuotation: Paste in a blockquote type=cite

View File

@ -319,6 +319,9 @@ public:
NS_IMETHOD Paste(PRInt32 aSelectionType); NS_IMETHOD Paste(PRInt32 aSelectionType);
NS_IMETHOD CanPaste(PRInt32 aSelectionType, PRBool *aCanPaste); 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); NS_IMETHOD DebugUnitTests(PRInt32 *outNumTests, PRInt32 *outNumTestsFailed);
/** All editor operations which alter the doc should be prefaced /** All editor operations which alter the doc should be prefaced

View File

@ -52,6 +52,7 @@ _TEST_FILES = \
test_bug478725.html \ test_bug478725.html \
test_bug480972.html \ test_bug480972.html \
test_bug487524.html \ test_bug487524.html \
test_bug525389.html \
test_select_all_without_body.html \ test_select_all_without_body.html \
file_select_all_without_body.html \ file_select_all_without_body.html \
$(NULL) $(NULL)

View File

@ -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>

View File

@ -455,6 +455,30 @@ NS_IMETHODIMP nsPlaintextEditor::Paste(PRInt32 aSelectionType)
return rv; 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) NS_IMETHODIMP nsPlaintextEditor::CanPaste(PRInt32 aSelectionType, PRBool *aCanPaste)
{ {
@ -482,6 +506,37 @@ NS_IMETHODIMP nsPlaintextEditor::CanPaste(PRInt32 aSelectionType, PRBool *aCanPa
return NS_OK; 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 nsresult
nsPlaintextEditor::SetupDocEncoder(nsIDocumentEncoder **aDocEncoder) nsPlaintextEditor::SetupDocEncoder(nsIDocumentEncoder **aDocEncoder)
{ {

View File

@ -124,6 +124,8 @@ public:
NS_IMETHOD CanCopy(PRBool *aCanCopy); NS_IMETHOD CanCopy(PRBool *aCanCopy);
NS_IMETHOD Paste(PRInt32 aSelectionType); NS_IMETHOD Paste(PRInt32 aSelectionType);
NS_IMETHOD CanPaste(PRInt32 aSelectionType, PRBool *aCanPaste); 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 CanDrag(nsIDOMEvent *aDragEvent, PRBool *aCanDrag);
NS_IMETHOD DoDrag(nsIDOMEvent *aDragEvent); NS_IMETHOD DoDrag(nsIDOMEvent *aDragEvent);

View File

@ -439,6 +439,7 @@ class nsHashKey;
#define NS_CONTENT_COMMAND_DELETE (NS_CONTENT_COMMAND_EVENT_START+3) #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_UNDO (NS_CONTENT_COMMAND_EVENT_START+4)
#define NS_CONTENT_COMMAND_REDO (NS_CONTENT_COMMAND_EVENT_START+5) #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 // Event to gesture notification
#define NS_GESTURENOTIFY_EVENT_START 3900 #define NS_GESTURENOTIFY_EVENT_START 3900
@ -1234,6 +1235,7 @@ public:
{ {
} }
nsCOMPtr<nsITransferable> mTransferable; // [in]
PRPackedBool mOnlyEnabledCheck; // [in] PRPackedBool mOnlyEnabledCheck; // [in]
PRPackedBool mSucceeded; // [out] PRPackedBool mSucceeded; // [out]

View File

@ -2198,8 +2198,8 @@ NSEvent* gLastDragMouseDownEvent = nil;
// that we can handle. // that we can handle.
NSArray *sendTypes = [[NSArray alloc] initWithObjects:NSStringPboardType,NSHTMLPboardType,nil]; 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]; [NSApp registerServicesMenuSendTypes:sendTypes returnTypes:returnTypes];
[sendTypes release]; [sendTypes release];
@ -6086,26 +6086,47 @@ static BOOL keyUpAlreadySentKeyDown = NO;
// returnType is nil if the service will not return any data. // returnType is nil if the service will not return any data.
// //
// The following condition thus triggers when the service expects a string // 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 // or HTML from us or no data at all AND when the service will either not
// any data to us. // send back any data to us or will send a string or HTML back to us.
if ((!sendType || [sendType isEqual:NSStringPboardType] || #define IsSupportedType(typeStr) ([typeStr isEqual:NSStringPboardType] || [typeStr isEqual:NSHTMLPboardType])
[sendType isEqual:NSHTMLPboardType]) && !returnType) {
// Query the Gecko window to determine if there is a current selection. id result = nil;
if ((!sendType || IsSupportedType(sendType)) &&
(!returnType || IsSupportedType(returnType))) {
if (mGeckoChild) { 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); 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); // Determine if we can paste (if receiving data from the service).
mGeckoChild->DispatchWindowEvent(event); if (returnType) {
nsContentCommandEvent command(PR_TRUE, NS_CONTENT_COMMAND_PASTE_TRANSFERABLE, mGeckoChild, PR_TRUE);
// Return this object if it can handle the request. mGeckoChild->DispatchWindowEvent(command);
if ((!sendType || (event.mSucceeded && event.mReply.mHasSelection)) && if (!command.mSucceeded || !command.mIsEnabled)
!returnType) result = nil;
return self; }
} }
} }
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; NS_OBJC_END_TRY_ABORT_BLOCK_NIL;
} }
@ -6169,11 +6190,31 @@ static BOOL keyUpAlreadySentKeyDown = NO;
NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(NO); NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(NO);
} }
// Called if the service wants us to replace the current selection. We do // Called if the service wants us to replace the current selection.
// not currently support replacing the current selection so just return NO.
- (BOOL)readSelectionFromPasteboard:(NSPasteboard *)pboard - (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 - #pragma mark -

View File

@ -61,6 +61,7 @@ public:
static NSDictionary* PasteboardDictFromTransferable(nsITransferable *aTransferable); static NSDictionary* PasteboardDictFromTransferable(nsITransferable *aTransferable);
static PRBool IsStringType(const nsCString& aMIMEType, const NSString** aPasteboardType); static PRBool IsStringType(const nsCString& aMIMEType, const NSString** aPasteboardType);
static NSString* WrapHtmlForSystemPasteboard(NSString* aString); static NSString* WrapHtmlForSystemPasteboard(NSString* aString);
static nsresult TransferableFromPasteboard(nsITransferable *aTransferable, NSPasteboard *pboard);
protected: protected:

View File

@ -132,18 +132,11 @@ nsClipboard::SetNativeClipboardData(PRInt32 aWhichClipboard)
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT; NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
} }
NS_IMETHODIMP nsresult
nsClipboard::GetNativeClipboardData(nsITransferable* aTransferable, PRInt32 aWhichClipboard) nsClipboard::TransferableFromPasteboard(nsITransferable *aTransferable, NSPasteboard *cocoaPasteboard)
{ {
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT; 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) // get flavor list that includes all acceptable flavors (including ones obtained through conversion)
nsCOMPtr<nsISupportsArray> flavorList; nsCOMPtr<nsISupportsArray> flavorList;
nsresult rv = aTransferable->FlavorsTransferableCanImport(getter_AddRefs(flavorList)); nsresult rv = aTransferable->FlavorsTransferableCanImport(getter_AddRefs(flavorList));
@ -153,37 +146,6 @@ nsClipboard::GetNativeClipboardData(nsITransferable* aTransferable, PRInt32 aWhi
PRUint32 flavorCount; PRUint32 flavorCount;
flavorList->Count(&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++) { for (PRUint32 i = 0; i < flavorCount; i++) {
nsCOMPtr<nsISupports> genericFlavor; nsCOMPtr<nsISupports> genericFlavor;
flavorList->GetElementAt(i, getter_AddRefs(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_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 // returns true if we have *any* of the passed in flavors available for pasting
NS_IMETHODIMP NS_IMETHODIMP
nsClipboard::HasDataMatchingFlavors(const char** aFlavorList, PRUint32 aLength, nsClipboard::HasDataMatchingFlavors(const char** aFlavorList, PRUint32 aLength,