Bug 1460940 - Convert nsIDocumentEncoder to use nsIDocument. r?bz draft
authorAdrian Wielgosik <adrian.wielgosik@gmail.com>
Fri, 11 May 2018 19:46:15 +0200
changeset 794804 d969333e462363ce75fcbc352c3350ccebf783b2
parent 794803 04a053b09a12ce10fa7af61c38304c7940118dda
child 794805 f1ab9f07bd84c13a3828fe06e3c40a0e24d8cef1
push id109776
push userbmo:adrian.wielgosik@gmail.com
push dateMon, 14 May 2018 13:49:41 +0000
reviewersbz
bugs1460940
milestone62.0a1
Bug 1460940 - Convert nsIDocumentEncoder to use nsIDocument. r?bz MozReview-Commit-ID: 1Nla9DnwFiu
dom/base/Selection.cpp
dom/base/nsContentUtils.cpp
dom/base/nsCopySupport.cpp
dom/base/nsDocumentEncoder.cpp
dom/base/nsIDocumentEncoder.idl
--- a/dom/base/Selection.cpp
+++ b/dom/base/Selection.cpp
@@ -45,17 +45,16 @@
 #include "nsContentUtils.h"
 #include "nsThreadUtils.h"
 
 #include "nsPresContext.h"
 #include "nsIPresShell.h"
 #include "nsCaret.h"
 
 #include "nsITimer.h"
-#include "nsIDOMDocument.h"
 #include "nsIDocument.h"
 #include "nsINamed.h"
 
 #include "nsISelectionController.h" //for the enums
 #include "nsAutoCopyListener.h"
 #include "SelectionChangeListener.h"
 #include "nsCopySupport.h"
 #include "nsIClipboard.h"
@@ -449,24 +448,21 @@ Selection::ToStringWithFormat(const nsAS
   nsIPresShell* shell = GetPresShell();
   if (!shell) {
     aRv.Throw(NS_ERROR_FAILURE);
     return;
   }
 
   nsIDocument *doc = shell->GetDocument();
 
-  nsCOMPtr<nsIDOMDocument> domDoc = do_QueryInterface(doc);
-  NS_ASSERTION(domDoc, "Need a document");
-
   // Flags should always include OutputSelectionOnly if we're coming from here:
   aFlags |= nsIDocumentEncoder::OutputSelectionOnly;
   nsAutoString readstring;
   readstring.Assign(aFormatType);
-  rv = encoder->Init(domDoc, readstring, aFlags);
+  rv = encoder->Init(doc, readstring, aFlags);
   if (NS_FAILED(rv)) {
     aRv.Throw(rv);
     return;
   }
 
   encoder->SetSelection(this);
   if (aWrapCol != 0)
     encoder->SetWrapColumn(aWrapCol);
--- a/dom/base/nsContentUtils.cpp
+++ b/dom/base/nsContentUtils.cpp
@@ -5215,25 +5215,24 @@ nsContentUtils::ConvertToPlainText(const
                                   uri,
                                   uri,
                                   principal,
                                   true,
                                   nullptr,
                                   DocumentFlavorHTML);
   NS_ENSURE_SUCCESS(rv, rv);
 
-  nsCOMPtr<nsIDOMDocument> domDocument = do_QueryInterface(document);
   rv = nsContentUtils::ParseDocumentHTML(aSourceBuffer, document,
     !(aFlags & nsIDocumentEncoder::OutputNoScriptContent));
   NS_ENSURE_SUCCESS(rv, rv);
 
   nsCOMPtr<nsIDocumentEncoder> encoder = do_CreateInstance(
     "@mozilla.org/layout/documentEncoder;1?type=text/plain");
 
-  rv = encoder->Init(domDocument, NS_LITERAL_STRING("text/plain"), aFlags);
+  rv = encoder->Init(document, NS_LITERAL_STRING("text/plain"), aFlags);
   NS_ENSURE_SUCCESS(rv, rv);
 
   encoder->SetWrapColumn(aWrapCol);
 
   return encoder->EncodeToString(aResultBuffer);
 }
 
 /* static */
--- a/dom/base/nsCopySupport.cpp
+++ b/dom/base/nsCopySupport.cpp
@@ -26,17 +26,16 @@
 #include "nsIDocShell.h"
 #include "nsIContentViewerEdit.h"
 #include "nsIClipboardHelper.h"
 #include "nsISelectionController.h"
 
 #include "nsPIDOMWindow.h"
 #include "nsIDocument.h"
 #include "nsIDOMNode.h"
-#include "nsIDOMDocument.h"
 #include "nsIHTMLDocument.h"
 #include "nsGkAtoms.h"
 #include "nsIFrame.h"
 #include "nsIURI.h"
 #include "nsIURIMutator.h"
 #include "nsISimpleEnumerator.h"
 
 // image copy stuff
@@ -111,20 +110,17 @@ SelectionCopyHelper(Selection *aSel, nsI
   nsAutoString mimeType;
   mimeType.AssignLiteral(kUnicodeMime);
 
   // Do the first and potentially trial encoding as preformatted and raw.
   uint32_t flags = aFlags | nsIDocumentEncoder::OutputPreformatted
                           | nsIDocumentEncoder::OutputRaw
                           | nsIDocumentEncoder::OutputForPlainTextClipboardCopy;
 
-  nsCOMPtr<nsIDOMDocument> domDoc = do_QueryInterface(aDoc);
-  NS_ASSERTION(domDoc, "Need a document");
-
-  rv = docEncoder->Init(domDoc, mimeType, flags);
+  rv = docEncoder->Init(aDoc, mimeType, flags);
   NS_ENSURE_SUCCESS(rv, rv);
 
   rv = docEncoder->SetSelection(aSel);
   NS_ENSURE_SUCCESS(rv, rv);
 
   // SetSelection set the mime type to text/plain if the selection is inside a
   // text widget.
   rv = docEncoder->GetMimeType(mimeType);
@@ -163,17 +159,17 @@ SelectionCopyHelper(Selection *aSel, nsI
       nsIDocumentEncoder::OutputSelectionOnly |
       nsIDocumentEncoder::OutputAbsoluteLinks |
       nsIDocumentEncoder::SkipInvisibleContent |
       nsIDocumentEncoder::OutputDropInvisibleBreak |
       (aFlags & (nsIDocumentEncoder::OutputNoScriptContent |
                  nsIDocumentEncoder::OutputRubyAnnotation));
 
     mimeType.AssignLiteral(kTextMime);
-    rv = docEncoder->Init(domDoc, mimeType, flags);
+    rv = docEncoder->Init(aDoc, mimeType, flags);
     NS_ENSURE_SUCCESS(rv, rv);
 
     rv = docEncoder->SetSelection(aSel);
     NS_ENSURE_SUCCESS(rv, rv);
 
     rv = docEncoder->EncodeToString(textPlainBuf);
     NS_ENSURE_SUCCESS(rv, rv);
   }
@@ -181,17 +177,17 @@ SelectionCopyHelper(Selection *aSel, nsI
   // Second, prepare the text/html flavor.
   nsAutoString textHTMLBuf;
   nsAutoString htmlParentsBuf;
   nsAutoString htmlInfoBuf;
   if (encodedTextHTML) {
     // Redo the encoding, but this time use the passed-in flags.
     // Don't allow wrapping of CJK strings.
     mimeType.AssignLiteral(kHTMLMime);
-    rv = docEncoder->Init(domDoc, mimeType,
+    rv = docEncoder->Init(aDoc, mimeType,
                           aFlags |
                           nsIDocumentEncoder::OutputDisallowLineBreaking);
     NS_ENSURE_SUCCESS(rv, rv);
 
     rv = docEncoder->SetSelection(aSel);
     NS_ENSURE_SUCCESS(rv, rv);
 
     rv = docEncoder->EncodeToStringWithContext(htmlParentsBuf, htmlInfoBuf,
@@ -358,20 +354,17 @@ nsCopySupport::GetContents(const nsACStr
 
   uint32_t flags = aFlags | nsIDocumentEncoder::SkipInvisibleContent;
 
   if (aMimeType.EqualsLiteral("text/plain"))
     flags |= nsIDocumentEncoder::OutputPreformatted;
 
   NS_ConvertASCIItoUTF16 unicodeMimeType(aMimeType);
 
-  nsCOMPtr<nsIDOMDocument> domDoc = do_QueryInterface(aDoc);
-  NS_ASSERTION(domDoc, "Need a document");
-
-  rv = docEncoder->Init(domDoc, unicodeMimeType, flags);
+  rv = docEncoder->Init(aDoc, unicodeMimeType, flags);
   if (NS_FAILED(rv)) return rv;
 
   if (aSel)
   {
     rv = docEncoder->SetSelection(aSel);
     if (NS_FAILED(rv)) return rv;
   }
 
--- a/dom/base/nsDocumentEncoder.cpp
+++ b/dom/base/nsDocumentEncoder.cpp
@@ -16,17 +16,16 @@
 #include "nsISupports.h"
 #include "nsIDocument.h"
 #include "nsIHTMLDocument.h"
 #include "nsCOMPtr.h"
 #include "nsIContentSerializer.h"
 #include "mozilla/Encoding.h"
 #include "nsIOutputStream.h"
 #include "nsRange.h"
-#include "nsIDOMDocument.h"
 #include "nsGkAtoms.h"
 #include "nsIContent.h"
 #include "nsIScriptContext.h"
 #include "nsIScriptGlobalObject.h"
 #include "nsIScriptSecurityManager.h"
 #include "mozilla/dom/Selection.h"
 #include "nsITransferable.h" // for kUnicodeMime
 #include "nsContentUtils.h"
@@ -241,27 +240,21 @@ void nsDocumentEncoder::Initialize(bool 
 nsDocumentEncoder::~nsDocumentEncoder()
 {
   if (mCachedBuffer) {
     mCachedBuffer->Release();
   }
 }
 
 NS_IMETHODIMP
-nsDocumentEncoder::Init(nsIDOMDocument* aDocument,
+nsDocumentEncoder::Init(nsIDocument* aDocument,
                         const nsAString& aMimeType,
                         uint32_t aFlags)
 {
-  if (!aDocument)
-    return NS_ERROR_INVALID_ARG;
-
-  nsCOMPtr<nsIDocument> doc = do_QueryInterface(aDocument);
-  NS_ENSURE_TRUE(doc, NS_ERROR_FAILURE);
-
-  return NativeInit(doc, aMimeType, aFlags);
+  return NativeInit(aDocument, aMimeType, aFlags);
 }
 
 NS_IMETHODIMP
 nsDocumentEncoder::NativeInit(nsIDocument* aDocument,
                               const nsAString& aMimeType,
                               uint32_t aFlags)
 {
   if (!aDocument)
@@ -1147,17 +1140,17 @@ NS_NewTextEncoder(nsIDocumentEncoder** a
 
 class nsHTMLCopyEncoder : public nsDocumentEncoder
 {
 public:
 
   nsHTMLCopyEncoder();
   virtual ~nsHTMLCopyEncoder();
 
-  NS_IMETHOD Init(nsIDOMDocument* aDocument, const nsAString& aMimeType, uint32_t aFlags) override;
+  NS_IMETHOD Init(nsIDocument* aDocument, const nsAString& aMimeType, uint32_t aFlags) override;
 
   // overridden methods from nsDocumentEncoder
   NS_IMETHOD SetSelection(Selection* aSelection) override;
   NS_IMETHOD EncodeToStringWithContext(nsAString& aContextString,
                                        nsAString& aInfoString,
                                        nsAString& aEncodedString) override;
   NS_IMETHOD EncodeToString(nsAString& aOutputString) override;
 
@@ -1195,29 +1188,28 @@ nsHTMLCopyEncoder::nsHTMLCopyEncoder()
   mIsTextWidget = false;
 }
 
 nsHTMLCopyEncoder::~nsHTMLCopyEncoder()
 {
 }
 
 NS_IMETHODIMP
-nsHTMLCopyEncoder::Init(nsIDOMDocument* aDocument,
+nsHTMLCopyEncoder::Init(nsIDocument* aDocument,
                         const nsAString& aMimeType,
                         uint32_t aFlags)
 {
   if (!aDocument)
     return NS_ERROR_INVALID_ARG;
 
   mIsTextWidget = false;
   Initialize();
 
   mIsCopying = true;
-  mDocument = do_QueryInterface(aDocument);
-  NS_ENSURE_TRUE(mDocument, NS_ERROR_FAILURE);
+  mDocument = aDocument;
 
   // Hack, hack! Traditionally, the caller passes text/unicode, which is
   // treated as "guess text/html or text/plain" in this context. (It has a
   // different meaning in other contexts. Sigh.) From now on, "text/plain"
   // means forcing text/plain instead of guessing.
   if (aMimeType.EqualsLiteral("text/plain")) {
     mMimeType.AssignLiteral("text/plain");
   } else {
--- a/dom/base/nsIDocumentEncoder.idl
+++ b/dom/base/nsIDocumentEncoder.idl
@@ -1,21 +1,21 @@
 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "nsISupports.idl"
 
-interface nsIDOMDocument;
 interface nsIDOMRange;
 interface nsIDOMNode;
 interface nsIOutputStream;
 
 webidl Selection;
+webidl Document;
 
 %{ C++
 class nsINode;
 class nsIDocument;
 %}
 [ptr] native nsINodePtr(nsINode);
 [ptr] native nsIDocumentPtr(nsIDocument);
 
@@ -231,17 +231,17 @@ interface nsIDocumentEncoder : nsISuppor
   const unsigned long RequiresReinitAfterOutput = (1 << 28);
 
   /**
    * Initialize with a pointer to the document and the mime type.
    * @param aDocument Document to encode.
    * @param aMimeType MimeType to use. May also be set by SetMimeType.
    * @param aFlags Flags to use while encoding. May also be set by SetFlags.
    */
-  void init(in nsIDOMDocument aDocument,
+  void init(in Document aDocument,
             in AString aMimeType,
             in unsigned long aFlags);
   [noscript] void nativeInit(in nsIDocumentPtr aDocument,
                              in AString aMimeType,
                              in unsigned long aFlags);
 
   /**
    *  If the selection is set to a non-null value, then the