Bug 490879 - Pasting images into rich text editors creates temporary moz-screenshot.jpg; r+sr=roc

--HG--
extra : rebase_source : e8a5e10ed3067548366574d64406c33bee3b455e
This commit is contained in:
Ehsan Akhgari 2009-05-15 13:14:16 +04:30
parent ba2d2c2bde
commit c0351272c1
4 changed files with 100 additions and 51 deletions

View File

@ -125,6 +125,9 @@
#include "nsIPrefService.h"
#include "nsIContentFilter.h"
#include "nsEventDispatcher.h"
#include "plbase64.h"
#include "prmem.h"
#include "nsStreamUtils.h"
const PRUnichar nbsp = 160;
@ -1402,60 +1405,25 @@ NS_IMETHODIMP nsHTMLEditor::InsertFromTransferable(nsITransferable *transferable
nsCOMPtr<nsIInputStream> imageStream(do_QueryInterface(genericDataObj));
NS_ENSURE_TRUE(imageStream, NS_ERROR_FAILURE);
nsCOMPtr<nsIFile> fileToUse;
NS_GetSpecialDirectory(NS_OS_TEMP_DIR, getter_AddRefs(fileToUse));
if (0 == nsCRT::strcmp(bestFlavor, kJPEGImageMime))
fileToUse->Append(NS_LITERAL_STRING("moz-screenshot.jpg"));
else if (0 == nsCRT::strcmp(bestFlavor, kPNGImageMime))
fileToUse->Append(NS_LITERAL_STRING("moz-screenshot.png"));
else if (0 == nsCRT::strcmp(bestFlavor, kGIFImageMime))
fileToUse->Append(NS_LITERAL_STRING("moz-screenshot.gif"));
nsCOMPtr<nsILocalFile> path = do_QueryInterface(fileToUse);
path->CreateUnique(nsIFile::NORMAL_FILE_TYPE, 0600);
nsCOMPtr<nsIOutputStream> outputStream;
rv = NS_NewLocalFileOutputStream(getter_AddRefs(outputStream), fileToUse);
nsCString imageData;
rv = NS_ConsumeStream(imageStream, PR_UINT32_MAX, imageData);
NS_ENSURE_SUCCESS(rv, rv);
PRUint32 length;
imageStream->Available(&length);
char * base64 = PL_Base64Encode(imageData.get(), imageData.Length(), nsnull);
NS_ENSURE_TRUE(base64, NS_ERROR_OUT_OF_MEMORY);
nsCOMPtr<nsIOutputStream> bufferedOutputStream;
rv = NS_NewBufferedOutputStream(getter_AddRefs(bufferedOutputStream), outputStream, length);
NS_ENSURE_SUCCESS(rv, rv);
PRUint32 numWritten;
rv = bufferedOutputStream->WriteFrom(imageStream, length, &numWritten);
NS_ENSURE_SUCCESS(rv, rv);
// force the stream close before we try to insert the image
// into the document.
rv = bufferedOutputStream->Close();
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIURI> uri;
rv = NS_NewFileURI(getter_AddRefs(uri), fileToUse);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIURL> fileURL(do_QueryInterface(uri));
if (fileURL)
{
nsCAutoString urltext;
rv = fileURL->GetSpec(urltext);
if (NS_SUCCEEDED(rv) && !urltext.IsEmpty())
{
stuffToPaste.AssignLiteral("<IMG src=\"");
AppendUTF8toUTF16(urltext, stuffToPaste);
stuffToPaste.AppendLiteral("\" alt=\"\" >");
nsAutoEditBatch beginBatching(this);
rv = InsertHTMLWithContext(stuffToPaste, EmptyString(), EmptyString(),
NS_LITERAL_STRING(kFileMime),
aSourceDoc,
aDestinationNode, aDestOffset,
aDoDeleteSelection);
}
}
stuffToPaste.AssignLiteral("<IMG src=\"data:");
AppendUTF8toUTF16(bestFlavor, stuffToPaste);
stuffToPaste.AppendLiteral(";base64,");
AppendUTF8toUTF16(base64, stuffToPaste);
stuffToPaste.AppendLiteral("\" alt=\"\" >");
nsAutoEditBatch beginBatching(this);
rv = InsertHTMLWithContext(stuffToPaste, EmptyString(), EmptyString(),
NS_LITERAL_STRING(kFileMime),
aSourceDoc,
aDestinationNode, aDestOffset,
aDoDeleteSelection);
PR_Free(base64);
}
}

View File

@ -74,6 +74,11 @@ _DATA_FILES = \
data/cfhtml-ooo.txt \
$(NULL)
_CHROME_TEST_FILES = \
test_bug490879.xul \
green.png \
$(NULL)
libs:: $(_TEST_FILES)
$(INSTALL) $(foreach f,$^,"$f") $(DEPTH)/_tests/testing/mochitest/tests/$(relativesrcdir)
(cd $(srcdir) && tar $(TAR_CREATE_FLAGS) - browserscope 2> /dev/null) | (cd $(DEPTH)/_tests/testing/mochitest/tests/$(relativesrcdir) && tar -xf -)
@ -81,3 +86,6 @@ libs:: $(_TEST_FILES)
libs:: $(_DATA_FILES)
$(INSTALL) $(foreach f,$^,"$f") $(DEPTH)/_tests/testing/mochitest/tests/$(relativesrcdir)/data
libs:: $(_CHROME_TEST_FILES)
$(INSTALL) $(foreach f,$^,"$f") $(DEPTH)/_tests/testing/mochitest/chrome/$(relativesrcdir)

Binary file not shown.

After

Width:  |  Height:  |  Size: 334 B

View File

@ -0,0 +1,73 @@
<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin"
type="text/css"?>
<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css"
type="text/css"?>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=490879
-->
<window xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
title="Mozilla Bug 490879" onload="runTest();">
<script type="application/javascript"
src="chrome://mochikit/content/MochiKit/packed.js"/>
<script type="application/javascript"
src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
<body xmlns="http://www.w3.org/1999/xhtml">
<a href="https://bugzilla.mozilla.org/show_bug.cgi?id=490879"
target="_blank">Mozilla Bug 490879</a>
<p/>
<iframe id="i1" width="200" height="100" src="about:blank" /><br />
<img id="i" src="green.png" />
<p/>
<pre id="test">
</pre>
</body>
<script class="testbody" type="application/javascript">
<![CDATA[
function runTest() {
function verifyContent() {
const kExpectedImgSpec = "data:image/png;base64,";
var e = document.getElementById('i1');
var doc = e.contentDocument;
is(doc.getElementsByTagName("img")[0].src.substring(0, kExpectedImgSpec.length),
kExpectedImgSpec, "The pasted image is a base64-encoded data: URI");
}
function pasteInto() {
var e = document.getElementById('i1');
var doc = e.contentDocument;
doc.designMode = "on";
doc.defaultView.focus();
var selection = doc.defaultView.getSelection();
selection.removeAllRanges();
selection.selectAllChildren(doc.body);
selection.collapseToEnd();
doc.execCommand("paste", false, null);
}
function copyToClipBoard() {
var tmpNode = document.popupNode;
document.popupNode = document.getElementById("i");
const kCmd = "cmd_copyImageContents";
var controller = top.document.commandDispatcher
.getControllerForCommand(kCmd);
ok((controller && controller.isCommandEnabled(kCmd)), "have copy command");
controller.doCommand(kCmd);
document.popupNode = tmpNode;
}
copyToClipBoard();
pasteInto();
verifyContent();
SimpleTest.finish();
}
SimpleTest.waitForExplicitFinish();
]]>
</script>
</window>