From 0245595d7e823a202215350376ab2eec9ac350cb Mon Sep 17 00:00:00 2001 From: Boris Zbarsky Date: Tue, 27 Mar 2018 00:35:20 -0400 Subject: [PATCH] Bug 1447889 part 1. Change nsCopySupport to work with Selection a bit more. r=mystor MozReview-Commit-ID: B8HePBcalWU --- dom/base/nsCopySupport.cpp | 20 ++++++++++---------- dom/base/nsCopySupport.h | 10 ++++++++-- layout/base/nsDocumentViewer.cpp | 7 +++---- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/dom/base/nsCopySupport.cpp b/dom/base/nsCopySupport.cpp index eafb3e3b9a01..938d09ea9d98 100644 --- a/dom/base/nsCopySupport.cpp +++ b/dom/base/nsCopySupport.cpp @@ -688,7 +688,7 @@ static nsresult AppendImagePromise(nsITransferable* aTransferable, #endif // XP_WIN nsIContent* -nsCopySupport::GetSelectionForCopy(nsIDocument* aDocument, nsISelection** aSelection) +nsCopySupport::GetSelectionForCopy(nsIDocument* aDocument, Selection** aSelection) { *aSelection = nullptr; @@ -704,8 +704,9 @@ nsCopySupport::GetSelectionForCopy(nsIDocument* aDocument, nsISelection** aSelec return nullptr; } - selectionController->GetSelection(nsISelectionController::SELECTION_NORMAL, - aSelection); + RefPtr sel = + selectionController->GetDOMSelection(nsISelectionController::SELECTION_NORMAL); + sel.forget(aSelection); return focusedContent; } @@ -715,13 +716,11 @@ nsCopySupport::CanCopy(nsIDocument* aDocument) if (!aDocument) return false; - nsCOMPtr sel; + RefPtr sel; GetSelectionForCopy(aDocument, getter_AddRefs(sel)); NS_ENSURE_TRUE(sel, false); - bool isCollapsed; - sel->GetIsCollapsed(&isCollapsed); - return !isCollapsed; + return !sel->IsCollapsed(); } static bool @@ -760,7 +759,7 @@ bool nsCopySupport::FireClipboardEvent(EventMessage aEventMessage, int32_t aClipboardType, nsIPresShell* aPresShell, - nsISelection* aSelection, + Selection* aSelection, bool* aActionTaken) { if (aActionTaken) { @@ -790,9 +789,10 @@ nsCopySupport::FireClipboardEvent(EventMessage aEventMessage, // if a selection was not supplied, try to find it nsCOMPtr content; - nsCOMPtr sel = aSelection; - if (!sel) + RefPtr sel = aSelection; + if (!sel) { content = GetSelectionForCopy(doc, getter_AddRefs(sel)); + } // retrieve the event target node from the start of the selection nsresult rv; diff --git a/dom/base/nsCopySupport.h b/dom/base/nsCopySupport.h index b7d215e1564a..bd5bb1affa91 100644 --- a/dom/base/nsCopySupport.h +++ b/dom/base/nsCopySupport.h @@ -20,6 +20,12 @@ class nsITransferable; class nsIPresShell; class nsILoadContext; +namespace mozilla { +namespace dom { +class Selection; +} // namespace dom +} // namespace mozilla + class nsCopySupport { // class of static helper functions for copy support @@ -56,7 +62,7 @@ class nsCopySupport * set to the document's selection and null will be returned. */ static nsIContent* GetSelectionForCopy(nsIDocument* aDocument, - nsISelection** aSelection); + mozilla::dom::Selection** aSelection); /** * Returns true if a copy operation is currently permitted based on the @@ -93,7 +99,7 @@ class nsCopySupport static bool FireClipboardEvent(mozilla::EventMessage aEventMessage, int32_t aClipboardType, nsIPresShell* aPresShell, - nsISelection* aSelection, + mozilla::dom::Selection* aSelection, bool* aActionTaken = nullptr); }; diff --git a/layout/base/nsDocumentViewer.cpp b/layout/base/nsDocumentViewer.cpp index fc548b9490b7..c09d9b6795c2 100644 --- a/layout/base/nsDocumentViewer.cpp +++ b/layout/base/nsDocumentViewer.cpp @@ -2798,15 +2798,14 @@ NS_IMETHODIMP nsDocumentViewer::GetContents(const char *mimeType, bool selection NS_ENSURE_TRUE(mDocument, NS_ERROR_NOT_INITIALIZED); // Now we have the selection. Make sure it's nonzero: - nsCOMPtr sel; + RefPtr sel; if (selectionOnly) { nsCopySupport::GetSelectionForCopy(mDocument, getter_AddRefs(sel)); NS_ENSURE_TRUE(sel, NS_ERROR_FAILURE); - bool isCollapsed; - sel->GetIsCollapsed(&isCollapsed); - if (isCollapsed) + if (sel->IsCollapsed()) { return NS_OK; + } } // call the copy code