Bug 1356303 - Process background image into data: URL. r=aceman a=jorgk
--- a/mail/components/compose/content/MsgComposeCommands.js
+++ b/mail/components/compose/content/MsgComposeCommands.js
@@ -5442,16 +5442,35 @@ function InitEditor()
}
}
else {
// For file:, and references to parts of random messages, show the
// blocked content notification.
gComposeNotificationBar.setBlockedContent(src);
}
}, true);
+
+ // Convert mailnews URL back to data: URL.
+ let background = editor.document.body.background;
+ if (background && gMsgCompose.originalMsgURI) {
+ // Check that background has the same URL as the message itself.
+ let msgSvc = Components.classes["@mozilla.org/messenger;1"]
+ .createInstance(Components.interfaces.nsIMessenger)
+ .messageServiceFromURI(gMsgCompose.originalMsgURI);
+ let originalMsgNeckoURI = {};
+ msgSvc.GetUrlForUri(gMsgCompose.originalMsgURI, originalMsgNeckoURI, null);
+ if (background.startsWith(originalMsgNeckoURI.value.spec)) {
+ try {
+ editor.document.body.background = loadBlockedImage(background, true);
+ } catch (e) {
+ // Couldn't load the referenced image.
+ Components.utils.reportError(e);
+ }
+ }
+ }
}
// This is used as event listener to spellcheck-changed event to update
// document language.
function updateDocumentLanguage(e)
{
document.documentElement.setAttribute("lang", e.detail.dictionary);
}
@@ -5654,21 +5673,22 @@ function onUnblockResource(aURL, aNode)
}
}
}
/**
* Convert the blocked content to a data URL and swap the src to that for the
* elements that were using it.
*
- * @param {String} aURL - (necko) URL to unblock
- *
- * @throw Error() if reading the data failed
+ * @param {String} aURL - (necko) URL to unblock
+ * @param {Bool} aReturnDataURL - return data: URL instead of processing image
+ * @return {String} the image as data: URL.
+ * @throw Error() if reading the data failed
*/
-function loadBlockedImage(aURL) {
+function loadBlockedImage(aURL, aReturnDataURL = false) {
let filename;
if (/^file:/i.test(aURL)) {
filename = aURL.substr(aURL.lastIndexOf("/") + 1);
}
else {
let fnMatch = /[?&;]filename=([^?&]+)/.exec(aURL);
filename = (fnMatch && fnMatch[1]) || "";
}
@@ -5709,16 +5729,19 @@ function loadBlockedImage(aURL) {
throw new Error("Couln't read all data from URL=" + aURL + " (" + e +")");
}
stream.close();
let encoded = btoa(streamData);
let dataURL = "data:" + contentType +
(filename ? ";filename=" + encodeURIComponent(filename) : "") +
";base64," + encoded;
+ if (aReturnDataURL)
+ return dataURL;
+
let editor = GetCurrentEditor();
for (let img of editor.document.images) {
if (img.src == aURL) {
img.src = dataURL; // Swap to data URL.
img.classList.remove("loading-internal");
}
}
}