Bug 1185307 part.1 Add nsISelection::AsSelection() r?smaug
This patch adds nsISelection::AsSelection() for safer "upcast".
MozReview-Commit-ID: LlxoMaU0oE
--- a/dom/base/nsISelection.idl
+++ b/dom/base/nsISelection.idl
@@ -6,16 +6,24 @@
#include "nsISupports.idl"
/* THIS IS A PUBLIC INTERFACE */
interface nsIDOMNode;
interface nsIDOMRange;
interface nsINode;
+%{C++
+namespace mozilla {
+namespace dom {
+class Selection;
+} // namespace dom
+} // namespace mozilla
+%}
+
/**
* Interface for manipulating and querying the current selected range
* of nodes within the document.
*
* @version 1.0
*/
[builtinclass, uuid(e0a4d4b3-f34e-44bd-b1f2-4e3bde9b6915)]
@@ -145,9 +153,18 @@ interface nsISelection : nsISupports
*
* @returns NS_ERROR_NOT_IMPLEMENTED if the granularity is "sentence",
* "sentenceboundary", "paragraph", "paragraphboundary", or
* "documentboundary". Returns NS_ERROR_INVALID_ARG if alter, direction,
* or granularity has an unrecognized value.
*/
void modify(in DOMString alter, in DOMString direction,
in DOMString granularity);
+
+%{C++
+ /**
+ * AsSelection() returns a pointer to Selection class if the instance is
+ * derived from it. Otherwise, nullptr but it should never happen
+ * since Selection is the only class implementing nsISelection.
+ */
+ virtual mozilla::dom::Selection* AsSelection() = 0;
+%}
};
--- a/layout/generic/Selection.h
+++ b/layout/generic/Selection.h
@@ -60,16 +60,18 @@ public:
Selection();
explicit Selection(nsFrameSelection *aList);
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_AMBIGUOUS(Selection, nsISelectionPrivate)
NS_DECL_NSISELECTION
NS_DECL_NSISELECTIONPRIVATE
+ virtual Selection* AsSelection() override { return this; }
+
nsresult EndBatchChangesInternal(int16_t aReason = nsISelectionListener::NO_REASON);
nsIDocument* GetParentObject() const;
// utility methods for scrolling the selection into view
nsPresContext* GetPresContext() const;
nsIPresShell* GetPresShell() const;
nsFrameSelection* GetFrameSelection() const { return mFrameSelection; }