From 2e5aeec95e031eea6f34e2d4ef338d195d3a0003 Mon Sep 17 00:00:00 2001 From: "jst%netscape.com" Date: Fri, 28 Apr 2000 14:02:29 +0000 Subject: [PATCH] Hooking up window.getSelection() to return a selection object. --- dom/public/base/nsIDOMWindow.h | 5 ++++ dom/public/idl/base/Window.idl | 1 + dom/public/nsDOMPropEnums.h | 1 + dom/public/nsDOMPropNames.h | 1 + dom/src/base/nsGlobalWindow.cpp | 28 +++++++++++++++++++++ dom/src/base/nsJSWindow.cpp | 43 +++++++++++++++++++++++++++++++-- 6 files changed, 77 insertions(+), 2 deletions(-) diff --git a/dom/public/base/nsIDOMWindow.h b/dom/public/base/nsIDOMWindow.h index 9d2c9d12036a..3980cf2e9300 100644 --- a/dom/public/base/nsIDOMWindow.h +++ b/dom/public/base/nsIDOMWindow.h @@ -31,6 +31,7 @@ class nsIDOMNavigator; class nsIDOMDocument; +class nsIDOMSelection; class nsIDOMBarProp; class nsIDOMScreen; class nsIDOMHistory; @@ -201,6 +202,8 @@ public: NS_IMETHOD Escape(const nsString& aStr, nsString& aReturn)=0; NS_IMETHOD Unescape(const nsString& aStr, nsString& aReturn)=0; + + NS_IMETHOD GetSelection(nsIDOMSelection** aReturn)=0; }; @@ -288,6 +291,7 @@ public: NS_IMETHOD UpdateCommands(const nsString& aAction); \ NS_IMETHOD Escape(const nsString& aStr, nsString& aReturn); \ NS_IMETHOD Unescape(const nsString& aStr, nsString& aReturn); \ + NS_IMETHOD GetSelection(nsIDOMSelection** aReturn); \ @@ -375,6 +379,7 @@ public: NS_IMETHOD UpdateCommands(const nsString& aAction) { return _to UpdateCommands(aAction); } \ NS_IMETHOD Escape(const nsString& aStr, nsString& aReturn) { return _to Escape(aStr, aReturn); } \ NS_IMETHOD Unescape(const nsString& aStr, nsString& aReturn) { return _to Unescape(aStr, aReturn); } \ + NS_IMETHOD GetSelection(nsIDOMSelection** aReturn) { return _to GetSelection(aReturn); } \ extern nsresult NS_InitWindowClass(nsIScriptContext *aContext, nsIScriptGlobalObject *aGlobal); diff --git a/dom/public/idl/base/Window.idl b/dom/public/idl/base/Window.idl index 96dfc038d469..9a90782f39ed 100644 --- a/dom/public/idl/base/Window.idl +++ b/dom/public/idl/base/Window.idl @@ -80,6 +80,7 @@ interface Window { DOMString escape(in DOMString str); DOMString unescape(in DOMString str); + Selection getSelection(); }; interface EventTarget { diff --git a/dom/public/nsDOMPropEnums.h b/dom/public/nsDOMPropEnums.h index 37ae7d3f2ecb..d316140590cd 100644 --- a/dom/public/nsDOMPropEnums.h +++ b/dom/public/nsDOMPropEnums.h @@ -896,6 +896,7 @@ enum nsDOMProp { NS_DOM_PROP_WINDOW_FORWARD, NS_DOM_PROP_WINDOW_FRAMES, NS_DOM_PROP_WINDOW_GETATTENTION, + NS_DOM_PROP_WINDOW_GETSELECTION, NS_DOM_PROP_WINDOW_HISTORY, NS_DOM_PROP_WINDOW_HOME, NS_DOM_PROP_WINDOW_INNERHEIGHT, diff --git a/dom/public/nsDOMPropNames.h b/dom/public/nsDOMPropNames.h index 4cb84e60a092..a248576013ae 100644 --- a/dom/public/nsDOMPropNames.h +++ b/dom/public/nsDOMPropNames.h @@ -895,6 +895,7 @@ "window.forward", \ "window.frames", \ "window.getattention", \ + "window.getselection", \ "window.history", \ "window.home", \ "window.innerheight", \ diff --git a/dom/src/base/nsGlobalWindow.cpp b/dom/src/base/nsGlobalWindow.cpp index 2124ac5623e4..956066fefa7c 100644 --- a/dom/src/base/nsGlobalWindow.cpp +++ b/dom/src/base/nsGlobalWindow.cpp @@ -80,6 +80,8 @@ #include "nsIScriptGlobalObjectOwner.h" #include "nsIScriptSecurityManager.h" #include "nsISelectionController.h" +#include "nsIDOMSelection.h" +#include "nsIFrameSelection.h" #include "nsISidebar.h" // XXX for sidebar HACK, see bug 20721 #include "nsIStyleContext.h" #include "nsIWebNavigation.h" @@ -1809,6 +1811,32 @@ NS_IMETHODIMP GlobalWindowImpl::Unescape(const nsString& aStr, nsString& aReturn return NS_OK; } +NS_IMETHODIMP +GlobalWindowImpl::GetSelection(nsIDOMSelection** aSelection) +{ + NS_ENSURE_ARG_POINTER(aSelection); + *aSelection = nsnull; + + if (!mDocShell) + return NS_OK; + + nsCOMPtr presShell; + mDocShell->GetPresShell(getter_AddRefs(presShell)); + + if (!presShell) + return NS_OK; + + nsCOMPtr selection; + presShell->GetFrameSelection(getter_AddRefs(selection)); + + if (!selection) + return NS_OK; + + return selection->GetSelection(nsISelectionController::SELECTION_NORMAL, + aSelection); +} + + //***************************************************************************** // GlobalWindowImpl::nsIJSScriptObject //***************************************************************************** diff --git a/dom/src/base/nsJSWindow.cpp b/dom/src/base/nsJSWindow.cpp index 0368f1bf929e..d55e9a61deb0 100644 --- a/dom/src/base/nsJSWindow.cpp +++ b/dom/src/base/nsJSWindow.cpp @@ -37,13 +37,14 @@ #include "nsIDOMNavigator.h" #include "nsIDOMDocumentView.h" #include "nsIDOMDocument.h" +#include "nsIDOMSelection.h" #include "nsIDOMBarProp.h" #include "nsIDOMAbstractView.h" #include "nsIDOMScreen.h" #include "nsIDOMHistory.h" -#include "nsIDOMEventListener.h" #include "nsIDOMWindowCollection.h" #include "nsIDOMEvent.h" +#include "nsIDOMEventListener.h" #include "nsIDOMEventTarget.h" #include "nsISidebar.h" #include "nsIDOMWindow.h" @@ -56,13 +57,14 @@ static NS_DEFINE_IID(kIScriptGlobalObjectIID, NS_ISCRIPTGLOBALOBJECT_IID); static NS_DEFINE_IID(kINavigatorIID, NS_IDOMNAVIGATOR_IID); static NS_DEFINE_IID(kIDocumentViewIID, NS_IDOMDOCUMENTVIEW_IID); static NS_DEFINE_IID(kIDocumentIID, NS_IDOMDOCUMENT_IID); +static NS_DEFINE_IID(kISelectionIID, NS_IDOMSELECTION_IID); static NS_DEFINE_IID(kIBarPropIID, NS_IDOMBARPROP_IID); static NS_DEFINE_IID(kIAbstractViewIID, NS_IDOMABSTRACTVIEW_IID); static NS_DEFINE_IID(kIScreenIID, NS_IDOMSCREEN_IID); static NS_DEFINE_IID(kIHistoryIID, NS_IDOMHISTORY_IID); -static NS_DEFINE_IID(kIEventListenerIID, NS_IDOMEVENTLISTENER_IID); static NS_DEFINE_IID(kIWindowCollectionIID, NS_IDOMWINDOWCOLLECTION_IID); static NS_DEFINE_IID(kIEventIID, NS_IDOMEVENT_IID); +static NS_DEFINE_IID(kIEventListenerIID, NS_IDOMEVENTLISTENER_IID); static NS_DEFINE_IID(kIEventTargetIID, NS_IDOMEVENTTARGET_IID); static NS_DEFINE_IID(kISidebarIID, NS_ISIDEBAR_IID); static NS_DEFINE_IID(kIWindowIID, NS_IDOMWINDOW_IID); @@ -2420,6 +2422,42 @@ WindowUnescape(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rva } +// +// Native method GetSelection +// +PR_STATIC_CALLBACK(JSBool) +WindowGetSelection(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) +{ + nsIDOMWindow *nativeThis = (nsIDOMWindow*)nsJSUtils::nsGetNativeThis(cx, obj); + nsresult result = NS_OK; + nsIDOMSelection* nativeRet; + // If there's no private data, this must be the prototype, so ignore + if (nsnull == nativeThis) { + return JS_TRUE; + } + + { + *rval = JSVAL_NULL; + nsIScriptSecurityManager *secMan = nsJSUtils::nsGetSecurityManager(cx, obj); + if (!secMan) + return PR_FALSE; + result = secMan->CheckScriptAccess(cx, obj, NS_DOM_PROP_WINDOW_GETSELECTION, PR_FALSE); + if (NS_FAILED(result)) { + return nsJSUtils::nsReportError(cx, obj, result); + } + + result = nativeThis->GetSelection(&nativeRet); + if (NS_FAILED(result)) { + return nsJSUtils::nsReportError(cx, obj, result); + } + + nsJSUtils::nsConvertObjectToJSVal(nativeRet, cx, obj, rval); + } + + return JS_TRUE; +} + + // // Native method AddEventListener // @@ -2639,6 +2677,7 @@ static JSFunctionSpec WindowMethods[] = {"updateCommands", WindowUpdateCommands, 1}, {"escape", WindowEscape, 1}, {"unescape", WindowUnescape, 1}, + {"getSelection", WindowGetSelection, 0}, {"addEventListener", EventTargetAddEventListener, 3}, {"removeEventListener", EventTargetRemoveEventListener, 3}, {0}