Title: [294113] trunk
Revision
294113
Author
katherine_che...@apple.com
Date
2022-05-12 11:22:57 -0700 (Thu, 12 May 2022)

Log Message

Mail compose: right clicking an image attachment selects it
https://bugs.webkit.org/show_bug.cgi?id=240315
rdar://45454933

Reviewed by Wenson Hsieh.

* wtf/cocoa/RuntimeApplicationChecksCocoa.h:
Mail compose: right clicking an image attachment selects it
https://bugs.webkit.org/show_bug.cgi?id=240315
rdar://45454933

Reviewed by Wenson Hsieh.

If the page is editable, don't select if the element is an image.

* page/EventHandler.cpp:
(WebCore::EventHandler::sendContextMenuEvent):
Mail compose: right clicking an image attachment selects it
https://bugs.webkit.org/show_bug.cgi?id=240315
rdar://45454933

Reviewed by Wenson Hsieh.

API test coverage.

* TestWebKitAPI/Tests/mac/ContextMenuTests.mm:
(TestWebKitAPI::rightClick):
(TestWebKitAPI::TEST):

Canonical link: https://commits.webkit.org/250494@main

Modified Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (294112 => 294113)


--- trunk/Source/WTF/ChangeLog	2022-05-12 18:14:51 UTC (rev 294112)
+++ trunk/Source/WTF/ChangeLog	2022-05-12 18:22:57 UTC (rev 294113)
@@ -1,3 +1,13 @@
+2022-05-11  Kate Cheney  <katherine_che...@apple.com>
+
+        Mail compose: right clicking an image attachment selects it
+        https://bugs.webkit.org/show_bug.cgi?id=240315
+        rdar://45454933
+
+        Reviewed by Wenson Hsieh.
+
+        * wtf/cocoa/RuntimeApplicationChecksCocoa.h:
+
 2022-05-10  Brent Fulgham  <bfulg...@apple.com>
 
         Remove abandoned CSSDeferredParser implementation and feature flag

Modified: trunk/Source/WTF/wtf/cocoa/RuntimeApplicationChecksCocoa.h (294112 => 294113)


--- trunk/Source/WTF/wtf/cocoa/RuntimeApplicationChecksCocoa.h	2022-05-12 18:14:51 UTC (rev 294112)
+++ trunk/Source/WTF/wtf/cocoa/RuntimeApplicationChecksCocoa.h	2022-05-12 18:22:57 UTC (rev 294113)
@@ -72,6 +72,7 @@
     NullOriginForNonSpecialSchemedURLs,
     ObservesClassProperty,
     PictureInPictureMediaPlayback,
+    PreventImageSelectionOnContextualMenuClickInEditablePage,
     ProcessSwapOnCrossSiteNavigation,
     RequiresUserGestureToLoadVideo,
     RestrictsBaseURLSchemes,

Modified: trunk/Source/WebCore/ChangeLog (294112 => 294113)


--- trunk/Source/WebCore/ChangeLog	2022-05-12 18:14:51 UTC (rev 294112)
+++ trunk/Source/WebCore/ChangeLog	2022-05-12 18:22:57 UTC (rev 294113)
@@ -1,3 +1,16 @@
+2022-05-11  Kate Cheney  <katherine_che...@apple.com>
+
+        Mail compose: right clicking an image attachment selects it
+        https://bugs.webkit.org/show_bug.cgi?id=240315
+        rdar://45454933
+
+        Reviewed by Wenson Hsieh.
+
+        If the page is editable, don't select if the element is an image.
+
+        * page/EventHandler.cpp:
+        (WebCore::EventHandler::sendContextMenuEvent):
+
 2022-05-12  Eric Carlson  <eric.carl...@apple.com>
 
         [macOS] Remove support for deprecated ScreenCaptureKit API

Modified: trunk/Source/WebCore/page/EventHandler.cpp (294112 => 294113)


--- trunk/Source/WebCore/page/EventHandler.cpp	2022-05-12 18:14:51 UTC (rev 294112)
+++ trunk/Source/WebCore/page/EventHandler.cpp	2022-05-12 18:22:57 UTC (rev 294113)
@@ -127,6 +127,10 @@
 #include <wtf/SetForScope.h>
 #include <wtf/StdLibExtras.h>
 
+#if PLATFORM(COCOA)
+#include <wtf/cocoa/RuntimeApplicationChecksCocoa.h>
+#endif
+
 #if ENABLE(IOS_TOUCH_EVENTS)
 #include "PlatformTouchEventIOS.h"
 #endif
@@ -3277,8 +3281,17 @@
     if (mouseEvent.scrollbar() || view->scrollbarAtPoint(event.position()))
         return false;
 
+    bool allowSelection = true;
+#if PLATFORM(COCOA)
+    if (linkedOnOrAfterSDKWithBehavior(SDKAlignedBehavior::PreventImageSelectionOnContextualMenuClickInEditablePage)) {
+        auto* page = m_frame.page();
+        allowSelection = page && (!page->isEditable() || !is<HTMLImageElement>(mouseEvent.hitTestResult().targetElement()));
+    }
+#endif
+
     if (m_frame.editor().behavior().shouldSelectOnContextualMenuClick()
-        && !m_frame.selection().contains(viewportPos)) {
+        && !m_frame.selection().contains(viewportPos)
+        && allowSelection) {
         m_mouseDownMayStartSelect = true; // context menu events are always allowed to perform a selection
         selectClosestContextualWordOrLinkFromHitTestResult(mouseEvent.hitTestResult(), shouldAppendTrailingWhitespace(mouseEvent, m_frame));
     }

Modified: trunk/Tools/ChangeLog (294112 => 294113)


--- trunk/Tools/ChangeLog	2022-05-12 18:14:51 UTC (rev 294112)
+++ trunk/Tools/ChangeLog	2022-05-12 18:22:57 UTC (rev 294113)
@@ -1,3 +1,17 @@
+2022-05-11  Kate Cheney  <katherine_che...@apple.com>
+
+        Mail compose: right clicking an image attachment selects it
+        https://bugs.webkit.org/show_bug.cgi?id=240315
+        rdar://45454933
+
+        Reviewed by Wenson Hsieh.
+
+        API test coverage.
+
+        * TestWebKitAPI/Tests/mac/ContextMenuTests.mm:
+        (TestWebKitAPI::rightClick):
+        (TestWebKitAPI::TEST):
+
 2022-05-11  Wenson Hsieh  <wenson_hs...@apple.com>
 
         ImageAnalysisQueue should extract and analyze images inside of subframes

Modified: trunk/Tools/TestWebKitAPI/Tests/mac/ContextMenuTests.mm (294112 => 294113)


--- trunk/Tools/TestWebKitAPI/Tests/mac/ContextMenuTests.mm	2022-05-12 18:14:51 UTC (rev 294112)
+++ trunk/Tools/TestWebKitAPI/Tests/mac/ContextMenuTests.mm	2022-05-12 18:22:57 UTC (rev 294113)
@@ -33,6 +33,7 @@
 #import "Utilities.h"
 #import <WebKit/WKMenuItemIdentifiersPrivate.h>
 #import <WebKit/WKUIDelegatePrivate.h>
+#import <WebKit/WKWebViewConfigurationPrivate.h>
 
 @interface NSMenu (ContextMenuTests)
 - (NSMenuItem *)itemWithIdentifier:(NSString *)identifier;
@@ -83,6 +84,45 @@
     EXPECT_NOT_NULL([spellingSubmenu itemWithIdentifier:_WKMenuItemIdentifierCheckGrammarWithSpelling]);
 }
 
+NSString *imageWithContextMenuHandler = @""
+    "<script>"
+    "    addEventListener('contextmenu', () => contextmenuSeen = true);"
+    "</script>"
+    "<img src=''>";
+
+static void rightClick(RetainPtr<TestWKWebView> webView)
+{
+    [webView mouseDownAtPoint:NSMakePoint(100, 100) simulatePressure:NO withFlags:0 eventType:NSEventTypeRightMouseDown];
+    [webView mouseUpAtPoint:NSMakePoint(100, 100) withFlags:0 eventType:NSEventTypeRightMouseUp];
+}
+
+TEST(ContextMenuTests, ContextMenuForEditableWebViewDoesNotSelectImage)
+{
+    auto configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
+    [configuration _setAttachmentElementEnabled:YES];
+    auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:CGRectMake(0, 0, 800, 600) configuration:configuration.get()]);
+    [webView _setEditable:YES];
+
+    [webView synchronouslyLoadHTMLString:imageWithContextMenuHandler];
+
+    auto uiDelegate = adoptNS([[TestUIDelegate alloc] init]);
+    [webView setUIDelegate:uiDelegate.get()];
+
+    __block bool done = false;
+    [uiDelegate setGetContextMenuFromProposedMenu:^(NSMenu *menu, _WKContextMenuElementInfo *, id<NSSecureCoding>, void (^completion)(NSMenu *)) {
+        completion(nil);
+        done = true;
+    }];
+
+    rightClick(webView);
+    Util::run(&done);
+
+    rightClick(webView);
+    EXPECT_TRUE([[webView objectByEvaluatingJavaScript:@"window.contextmenuSeen"] boolValue]);
+    NSNumber *result = [webView objectByEvaluatingJavaScript:@"window.getSelection().isCollapsed"];
+    EXPECT_TRUE([result boolValue]);
+}
+
 } // namespace TestWebKitAPI
 
 #endif // PLATFORM(MAC)
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to