From d1ed2d7757230f093306253abf0b536ed8f15da2 Mon Sep 17 00:00:00 2001 From: Cameron Kaiser Date: Wed, 13 Feb 2019 20:57:18 -0800 Subject: [PATCH] #164: get selected from <> --- .../applescript/src/MacScripting.mm | 32 +++++++++++++------ 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/toolkit/components/applescript/src/MacScripting.mm b/toolkit/components/applescript/src/MacScripting.mm index 5a1095eea..a1ae5225b 100644 --- a/toolkit/components/applescript/src/MacScripting.mm +++ b/toolkit/components/applescript/src/MacScripting.mm @@ -64,6 +64,8 @@ extern "C" { #include "nsIDOMSerializer.h" #include "nsIDocument.h" #include "nsIDocumentEncoder.h" +#include "nsISelection.h" +#include "nsISelectionController.h" #include "nsIWindowMediator.h" #include "nsISimpleEnumerator.h" #include "nsIBaseWindow.h" @@ -80,8 +82,7 @@ extern "C" { #include "nsObjCExceptions.h" #include "nsToolkitCompsCID.h" #include "nsContentUtils.h" - -class nsLocation; +#include "mozilla/dom/Selection.h" // 10.4 no haz. typedef int NSInteger; @@ -815,16 +816,27 @@ static BOOL didInit = NO; } - (NSString*)selectedText { -#if(0) - nsCOMPtr selection; - if (NS_SUCCEEDED(mContentWindow->GetSelection(getter_AddRefs(selection))) && selection) { - nsXPIDLString selectedTextChars; - if (NS_SUCCEEDED(selection->ToString(getter_Copies(selectedTextChars)))) { - nsAutoString selectedText(selectedTextChars); - return [NSString stringWithUTF8String:NS_ConvertUTF16toUTF8(selectedText).get()]; + NS_WARNING("AppleScript: tab selected"); + nsCOMPtr piWindow = do_QueryInterface(mContentWindow); + if (!piWindow) + return @""; + // We can't just call GetSelection() directly on the nsPIDOMWindow + // since we're not actually an outer, so we have to do this the long + // way through the presshell. + //nsCOMPtr selection = piWindow->GetSelection(); + nsIDocShell* d = piWindow->GetDocShell(); + if (!d) + return @""; + nsIPresShell* s = d->GetPresShell(); + if (!s) + return @""; + nsCOMPtr selection = static_cast(s->GetCurrentSelection(nsISelectionController::SELECTION_NORMAL)); + if (selection) { + nsAutoString selectedTextChars; + if (NS_SUCCEEDED(selection->ToString(selectedTextChars))) { + return [NSString stringWithUTF8String:NS_ConvertUTF16toUTF8(selectedTextChars).get()]; } } -#endif return @""; }