Bug 1478546 - Move HTMLEditor::GetEmbeddedObjects (nsIEditorMailSupport.getEmbeddedObjects) to comm-central. r=jorgk DONTBUILD
authorMakoto Kato <m_kato@ga2.so-net.ne.jp>
Sun, 09 Sep 2018 22:04:00 +0200
changeset 33112 fe6d8f59182d7efcda98f23ec9ccb41f1cbfa710
parent 33111 2cfa24cfd1ac6984457e9bd952fb2e347670faba
child 33113 3c055eb08eb5b451158c54dfee74203c8dc5434f
push id387
push userclokep@gmail.com
push dateMon, 10 Dec 2018 21:30:47 +0000
reviewersjorgk
bugs1478546
Bug 1478546 - Move HTMLEditor::GetEmbeddedObjects (nsIEditorMailSupport.getEmbeddedObjects) to comm-central. r=jorgk DONTBUILD
mailnews/compose/src/nsMsgCompUtils.cpp
mailnews/compose/src/nsMsgCompUtils.h
mailnews/compose/src/nsMsgCompose.cpp
mailnews/compose/src/nsMsgSend.cpp
suite/extensions/debugQA/content/debugQAEditorOverlay.js
suite/extensions/debugQA/content/debugQAEditorOverlay.xul
suite/extensions/debugQA/locales/en-US/debugQAEditorOverlay.dtd
--- a/mailnews/compose/src/nsMsgCompUtils.cpp
+++ b/mailnews/compose/src/nsMsgCompUtils.cpp
@@ -24,21 +24,25 @@
 #include "nsComposeStrings.h"
 #include "nsIMsgCompUtils.h"
 #include "nsIMsgMdnGenerator.h"
 #include "nsServiceManagerUtils.h"
 #include "nsComponentManagerUtils.h"
 #include "nsMemory.h"
 #include "nsCRTGlue.h"
 #include <ctype.h>
+#include "mozilla/dom/Element.h"
 #include "mozilla/mailnews/Services.h"
 #include "mozilla/Services.h"
 #include "mozilla/Unused.h"
+#include "nsIContentIterator.h"
+#include "nsIDocument.h"
 #include "nsIMIMEInfo.h"
 #include "nsIMsgHeaderParser.h"
+#include "nsIMutableArray.h"
 #include "nsIRandomGenerator.h"
 #include "nsID.h"
 
 NS_IMPL_ISUPPORTS(nsMsgCompUtils, nsIMsgCompUtils)
 
 nsMsgCompUtils::nsMsgCompUtils()
 {
 }
@@ -1763,8 +1767,39 @@ void GetSerialiserFlags(const char* char
   if (PL_strcasecmp(charset, "ISO-2022-JP") == 0) {
     // Make sure we honour RFC 1468. For encoding in ISO-2022-JP we need to
     // send short lines to allow 7bit transfer encoding.
     *disallowBreaks = false;
     if (*flowed)
       *delsp = true;
   }
 }
+
+already_AddRefed<nsIArray>
+GetEmbeddedObjects(nsIDocument* aDocument)
+{
+  nsCOMPtr<nsIMutableArray> nodes = do_CreateInstance(NS_ARRAY_CONTRACTID);
+  if (NS_WARN_IF(!nodes)) {
+    return nullptr;
+  }
+
+  nsCOMPtr<nsIContentIterator> iter = NS_NewContentIterator();
+  iter->Init(aDocument->GetRootElement());
+
+  // Loop through the content iterator for each content node.
+  while (!iter->IsDone()) {
+    nsINode* node = iter->GetCurrentNode();
+    if (node->IsElement()) {
+      mozilla::dom::Element* element = node->AsElement();
+
+      // See if it's an image or also include all links.
+      // Let mail decide which link to send or not
+      if (element->IsAnyOfHTMLElements(nsGkAtoms::img, nsGkAtoms::a) ||
+          (element->IsHTMLElement(nsGkAtoms::body) &&
+           element->HasAttr(kNameSpaceID_None, nsGkAtoms::background))) {
+        nodes->AppendElement(node);
+      }
+    }
+    iter->Next();
+  }
+
+  return nodes.forget();
+}
--- a/mailnews/compose/src/nsMsgCompUtils.h
+++ b/mailnews/compose/src/nsMsgCompUtils.h
@@ -7,16 +7,18 @@
 #define _nsMsgCompUtils_H_
 
 #include "nscore.h"
 #include "nsMsgSend.h"
 #include "nsMsgCompFields.h"
 #include "nsIMsgSend.h"
 #include "nsIMsgCompUtils.h"
 
+class nsIArray;
+class nsIDocument;
 class nsIPrompt;
 
 #define ANY_SERVER "anyfolder://"
 
 // these are msg hdr property names for storing the original
 // msg uri's and disposition(replied/forwarded) when queuing
 // messages to send later.
 #define ORIG_URI_PROPERTY "origURIs"
@@ -29,16 +31,18 @@ public:
   NS_DECL_NSIMSGCOMPUTILS
 
   nsMsgCompUtils();
 
 private:
   virtual ~nsMsgCompUtils();
 };
 
+already_AddRefed<nsIArray> GetEmbeddedObjects(nsIDocument* aDocument);
+
 PR_BEGIN_EXTERN_C
 
 //
 // Create a file spec or file name using the name passed
 // in as a template
 //
 nsresult    nsMsgCreateTempFile(const char *tFileName, nsIFile **tFile);
 char        *nsMsgCreateTempFileName(const char *tFileName);
@@ -130,14 +134,13 @@ char        *GenerateFileNameFromURI(nsI
 //
 // Folder calls...
 //
 void GetFolderURIFromUserPrefs(nsMsgDeliverMode   aMode, nsIMsgIdentity *identity, nsCString& uri);
 
 // Check if we should use format=flowed
 void GetSerialiserFlags(const char *charset, bool *flowed, bool *delsp, bool *formatted, bool *disallowBreaks);
 
-
 PR_END_EXTERN_C
 
 
 #endif /* _nsMsgCompUtils_H_ */
 
--- a/mailnews/compose/src/nsMsgCompose.cpp
+++ b/mailnews/compose/src/nsMsgCompose.cpp
@@ -294,34 +294,36 @@ bool nsMsgCompose::IsEmbeddedObjectSafe(
   return false;
 }
 
 /* Reset the uri's of embedded objects because we've saved the draft message, and the
    original message doesn't exist anymore.
  */
 nsresult nsMsgCompose::ResetUrisForEmbeddedObjects()
 {
-  nsCOMPtr<nsIArray> aNodeList;
   uint32_t numNodes;
   uint32_t i;
 
-  nsCOMPtr<nsIEditorMailSupport> mailEditor (do_QueryInterface(m_editor));
-  if (!mailEditor)
+  if (!m_editor)
     return NS_ERROR_FAILURE;
 
-  nsresult rv = mailEditor->GetEmbeddedObjects(getter_AddRefs(aNodeList));
-  if (NS_FAILED(rv) || !aNodeList)
+  nsCOMPtr<nsIDocument> document;
+  m_editor->GetDocument(getter_AddRefs(document));
+  if (!document)
+    return NS_ERROR_FAILURE;
+  nsCOMPtr<nsIArray> aNodeList = GetEmbeddedObjects(document);
+  if (!aNodeList)
     return NS_ERROR_FAILURE;
 
   if (NS_FAILED(aNodeList->GetLength(&numNodes)))
     return NS_ERROR_FAILURE;
 
   nsCString curDraftIdURL;
 
-  rv = m_compFields->GetDraftId(getter_Copies(curDraftIdURL));
+  nsresult rv = m_compFields->GetDraftId(getter_Copies(curDraftIdURL));
 
   // Skip if no draft id (probably a new draft msg).
   if (NS_SUCCEEDED(rv) && mMsgSend && !curDraftIdURL.IsEmpty())
   {
     nsCOMPtr <nsIMsgDBHdr> msgDBHdr;
     rv = GetMsgDBHdrFromURI(curDraftIdURL.get(), getter_AddRefs(msgDBHdr));
     NS_ASSERTION(NS_SUCCEEDED(rv), "RemoveCurrentDraftMessage can't get msg header DB interface pointer.");
     if (NS_SUCCEEDED(rv) && msgDBHdr)
@@ -436,25 +438,32 @@ nsresult nsMsgCompose::ResetUrisForEmbed
 /* The purpose of this function is to mark any embedded object that wasn't a RFC822 part
    of the original message as moz-do-not-send.
    That will prevent us to attach data not specified by the user or not present in the
    original message.
 */
 nsresult nsMsgCompose::TagEmbeddedObjects(nsIEditorMailSupport *aEditor)
 {
   nsresult rv = NS_OK;
-  nsCOMPtr<nsIArray> aNodeList;
   uint32_t count;
   uint32_t i;
 
   if (!aEditor)
     return NS_ERROR_FAILURE;
 
-  rv = aEditor->GetEmbeddedObjects(getter_AddRefs(aNodeList));
-  if (NS_FAILED(rv) || !aNodeList)
+  nsCOMPtr<nsIEditor> editor = do_QueryInterface(aEditor);
+  if (!editor)
+    return NS_ERROR_FAILURE;
+
+  nsCOMPtr<nsIDocument> document;
+  editor->GetDocument(getter_AddRefs(document));
+  if (!document)
+    return NS_ERROR_FAILURE;
+  nsCOMPtr<nsIArray> aNodeList = GetEmbeddedObjects(document);
+  if (!aNodeList)
     return NS_ERROR_FAILURE;
 
   if (NS_FAILED(aNodeList->GetLength(&count)))
     return NS_ERROR_FAILURE;
 
   nsCOMPtr<nsIURI> originalUrl;
   nsCString originalScheme;
   nsCString originalHost;
--- a/mailnews/compose/src/nsMsgSend.cpp
+++ b/mailnews/compose/src/nsMsgSend.cpp
@@ -1426,23 +1426,21 @@ nsMsgComposeAndSend::GetMultipartRelated
 
   if (mMultipartRelatedAttachmentCount != -1 && !forceToBeCalculated)
     return (uint32_t)mMultipartRelatedAttachmentCount;
 
   //First time here, let's calculate the correct number of related part we need to generate
   mMultipartRelatedAttachmentCount = 0;
   if (mEditor)
   {
-    nsCOMPtr<nsIEditorMailSupport> mailEditor (do_QueryInterface(mEditor));
-    if (!mailEditor)
+    nsCOMPtr<nsIDocument> document;
+    mEditor->GetDocument(getter_AddRefs(document));
+    if (!document)
       return 0;
-
-    rv = mailEditor->GetEmbeddedObjects(getter_AddRefs(mEmbeddedObjectList));
-    if (NS_FAILED(rv))
-      return 0;
+    mEmbeddedObjectList = GetEmbeddedObjects(document);
   }
   if (!mEmbeddedObjectList)
     return 0;
 
   if (NS_SUCCEEDED(mEmbeddedObjectList->GetLength(&count)))
   {
     if (count > 0)
     {
--- a/suite/extensions/debugQA/content/debugQAEditorOverlay.js
+++ b/suite/extensions/debugQA/content/debugQAEditorOverlay.js
@@ -210,28 +210,16 @@ function EditorTestTableLayout()
     }
   }
   dump("Counted during scan: Number of rows="+rowCount+" Number of Columns="+maxColCount+"\n");
   rowCount = editor.getTableRowCount(table);
   maxColCount = editor.getTableColumnCount(table);
   dump("From nsITableLayout: Number of rows="+rowCount+" Number of Columns="+maxColCount+"\n****** End of Table Layout Test *****\n\n");
 }
 
-function EditorShowEmbeddedObjects()
-{
-  dump("\nEmbedded Objects:\n");
-  try {
-    var objectArray = GetCurrentEditor().getEmbeddedObjects();
-    dump(objectArray.length + " embedded objects\n");
-    // FIXME nsIDOMNode has been removed.
-    // for (let i = 0; i < objectArray.length; ++i)
-    //   dump(objectArray.queryElementAt(i, Ci.nsIDOMNode) + "\n");
-  } catch(e) {}
-}
-
 function EditorUnitTests()
 {
   dump("Running Unit Tests\n");
   var numTests       = { value:0 };
   var numTestsFailed = { value:0 };
   GetCurrentEditor().debugUnitTests(numTests, numTestsFailed);
 }
 
--- a/suite/extensions/debugQA/content/debugQAEditorOverlay.xul
+++ b/suite/extensions/debugQA/content/debugQAEditorOverlay.xul
@@ -64,18 +64,16 @@
             <menuitem accesskey="&insertIsIndexCmd.accesskey;"
                       command="cmd_isindex" label="&insertIsIndexCmd.label;"/>
           </menupopup>
         </menu>
         <menuitem label="&testSelectionCmd.label;"
                   oncommand="EditorTestSelection()"/>
         <menuitem label="&testTableLayoutCmd.label;"
                   oncommand="EditorTestTableLayout()"/>
-        <menuitem label="&showEmbeddedCmd.label;"
-                  oncommand="EditorShowEmbeddedObjects()"/>
         <menuitem label="&dumpContentCmd.label;"
                   oncommand="EditorDumpContent()"/>
         <menuitem label="&testDocumentCmd.label;"
                   oncommand="EditorTestDocument()"/>
         <menuitem label="&runUnitTestsCmd.label;"
                   oncommand="EditorUnitTests()"/>
         <menuseparator/>
         <menuitem label="&dumpUndoStack.label;"
--- a/suite/extensions/debugQA/locales/en-US/debugQAEditorOverlay.dtd
+++ b/suite/extensions/debugQA/locales/en-US/debugQAEditorOverlay.dtd
@@ -12,17 +12,16 @@
 <!ENTITY newEditorTestPage.label "Composer with Test Page">
 <!ENTITY textEditorCmd.label "Plaintext Editor">
 <!ENTITY outputTextCmd.label "Output Text">
 <!ENTITY outputHTMLCmd.label "Output HTML">
 <!ENTITY insertTextCmd.label "Insert Text">
 <!ENTITY testSelectionCmd.label "Test Selection">
 <!ENTITY testTableLayoutCmd.label "Test Table Layout">
 <!ENTITY testDocumentCmd.label "Test Document">
-<!ENTITY showEmbeddedCmd.label "Show Embedded Objects">
 <!ENTITY dumpContentCmd.label "Dump Content Tree">
 <!ENTITY runUnitTestsCmd.label "Run Unit Tests">
 <!ENTITY dumpUndoStack.label "Dump Undo Stack">
 <!ENTITY dumpRedoStack.label "Dump Redo Stack">
 <!ENTITY executeJSTransactionViaTxmgr.label "Execute JS Transaction Via Transaction Manager">
 <!ENTITY executeJSTransactionViaEditor.label "Execute JS Transaction Via Editor">
 <!ENTITY startLogCmd.label "Start Log">
 <!ENTITY stopLogCmd.label "Stop Log">