Invoke the context menu delegate callback right before we return the menu
authorandersca@apple.com <andersca@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sat, 24 Oct 2015 00:26:21 +0000 (00:26 +0000)
committerandersca@apple.com <andersca@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sat, 24 Oct 2015 00:26:21 +0000 (00:26 +0000)
https://bugs.webkit.org/show_bug.cgi?id=150521

Reviewed by Tim Horton.

Move the getCustomMenuFromDefaultItems to WebHTMLView wholesale. This will allow us to get rid of the ContextMenuClient function from WebCore.

* WebCoreSupport/WebContextMenuClient.mm:
(WebContextMenuClient::getCustomMenuFromDefaultItems):
(isPreVersion3Client): Deleted.
(isPreInspectElementTagClient): Deleted.
(fixMenusToSendToOldClients): Deleted.
(fixMenusReceivedFromOldClients): Deleted.
* WebView/WebHTMLView.mm:
(isPreVersion3Client):
(isPreInspectElementTagClient):
(fixMenusToSendToOldClients):
(fixMenusReceivedFromOldClients):
(customMenuFromDefaultItems):
(-[WebHTMLView menuForEvent:]):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@191520 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebKit/mac/ChangeLog
Source/WebKit/mac/WebCoreSupport/WebContextMenuClient.mm
Source/WebKit/mac/WebView/WebHTMLView.mm

index 0e0fe17faecae8b76928e9a05df1bda716b69b0c..319162d9d0ed652b4a44eafb17921146f16ee4ec 100644 (file)
@@ -1,3 +1,26 @@
+2015-10-23  Anders Carlsson  <andersca@apple.com>
+
+        Invoke the context menu delegate callback right before we return the menu
+        https://bugs.webkit.org/show_bug.cgi?id=150521
+
+        Reviewed by Tim Horton.
+
+        Move the getCustomMenuFromDefaultItems to WebHTMLView wholesale. This will allow us to get rid of the ContextMenuClient function from WebCore.
+
+        * WebCoreSupport/WebContextMenuClient.mm:
+        (WebContextMenuClient::getCustomMenuFromDefaultItems):
+        (isPreVersion3Client): Deleted.
+        (isPreInspectElementTagClient): Deleted.
+        (fixMenusToSendToOldClients): Deleted.
+        (fixMenusReceivedFromOldClients): Deleted.
+        * WebView/WebHTMLView.mm:
+        (isPreVersion3Client):
+        (isPreInspectElementTagClient):
+        (fixMenusToSendToOldClients):
+        (fixMenusReceivedFromOldClients):
+        (customMenuFromDefaultItems):
+        (-[WebHTMLView menuForEvent:]):
+
 2015-10-23  Anders Carlsson  <andersca@apple.com>
 
         More context menu simplification
index 52d2674e8445c316f674d3b9bbb1043c8a9a84d0..5e5c3da7244ee4ae028cd99740c15adfc2440171 100644 (file)
@@ -89,237 +89,9 @@ void WebContextMenuClient::contextMenuDestroyed()
     delete this;
 }
 
-static BOOL isPreVersion3Client(void)
-{
-    static BOOL preVersion3Client = !WebKitLinkedOnOrAfter(WEBKIT_FIRST_VERSION_WITH_3_0_CONTEXT_MENU_TAGS);
-    return preVersion3Client;
-}
-
-static BOOL isPreInspectElementTagClient(void)
-{
-    static BOOL preInspectElementTagClient = !WebKitLinkedOnOrAfter(WEBKIT_FIRST_VERSION_WITH_INSPECT_ELEMENT_MENU_TAG);
-    return preInspectElementTagClient;
-}
-
-static RetainPtr<NSArray> fixMenusToSendToOldClients(NSMutableArray *defaultMenuItems)
-{
-    auto savedItems = adoptNS([[NSMutableArray alloc] init]);
-
-    unsigned defaultItemsCount = [defaultMenuItems count];
-
-    if (isPreInspectElementTagClient() && defaultItemsCount >= 2) {
-        NSMenuItem *secondToLastItem = [defaultMenuItems objectAtIndex:defaultItemsCount - 2];
-        NSMenuItem *lastItem = [defaultMenuItems objectAtIndex:defaultItemsCount - 1];
-
-        if ([secondToLastItem isSeparatorItem] && [lastItem tag] == WebMenuItemTagInspectElement) {
-            savedItems = adoptNS([[NSMutableArray alloc] initWithCapacity:2]);
-            [savedItems addObject:secondToLastItem];
-            [savedItems addObject:lastItem];
-
-            [defaultMenuItems removeObject:secondToLastItem];
-            [defaultMenuItems removeObject:lastItem];
-            defaultItemsCount -= 2;
-        }
-    }
-
-    BOOL preVersion3Client = isPreVersion3Client();
-    if (!preVersion3Client)
-        return savedItems;
-
-    for (NSMenuItem *item in defaultMenuItems) {
-        int tag = item.tag;
-        int oldStyleTag = tag;
-
-        if (tag >= WEBMENUITEMTAG_WEBKIT_3_0_SPI_START) {
-            // Change all editing-related SPI tags listed in WebUIDelegatePrivate.h to WebMenuItemTagOther
-            // to match our old WebKit context menu behavior.
-            oldStyleTag = WebMenuItemTagOther;
-        } else {
-            // All items are expected to have useful tags coming into this method.
-            ASSERT(tag != WebMenuItemTagOther);
-            
-            // Use the pre-3.0 tags for the few items that changed tags as they moved from SPI to API. We
-            // do this only for old clients; new Mail already expects the new symbols in this case.
-            if (preVersion3Client) {
-                switch (tag) {
-                    case WebMenuItemTagSearchInSpotlight:
-                        oldStyleTag = OldWebMenuItemTagSearchInSpotlight;
-                        break;
-                    case WebMenuItemTagSearchWeb:
-                        oldStyleTag = OldWebMenuItemTagSearchWeb;
-                        break;
-                    case WebMenuItemTagLookUpInDictionary:
-                        oldStyleTag = OldWebMenuItemTagLookUpInDictionary;
-                        break;
-                    default:
-                        break;
-                }
-            }
-        }
-
-        item.tag = oldStyleTag;
-    }
-
-    return savedItems;
-}
-
-// FIXME: This should return NSArray.
-static RetainPtr<NSMutableArray> fixMenusReceivedFromOldClients(NSArray *delegateSuppliedItems, NSArray *savedItems)
-{
-    auto newMenuItems = adoptNS([delegateSuppliedItems mutableCopy]);
-
-    if (savedItems)
-        [newMenuItems addObjectsFromArray:savedItems];
-
-    BOOL preVersion3Client = isPreVersion3Client();
-    if (!preVersion3Client)
-        return newMenuItems;
-    
-    // Restore the modern tags to the menu items whose tags we altered in fixMenusToSendToOldClients. 
-    unsigned newItemsCount = [newMenuItems count];
-    for (unsigned i = 0; i < newItemsCount; ++i) {
-        NSMenuItem *item = [newMenuItems objectAtIndex:i];
-        
-        int tag = [item tag];
-        int modernTag = tag;
-        
-        if (tag == WebMenuItemTagOther) {
-            // Restore the specific tag for items on which we temporarily set WebMenuItemTagOther to match old behavior.
-            NSString *title = [item title];
-            if ([title isEqualToString:contextMenuItemTagOpenLink()])
-                modernTag = WebMenuItemTagOpenLink;
-            else if ([title isEqualToString:contextMenuItemTagIgnoreGrammar()])
-                modernTag = WebMenuItemTagIgnoreGrammar;
-            else if ([title isEqualToString:contextMenuItemTagSpellingMenu()])
-                modernTag = WebMenuItemTagSpellingMenu;
-            else if ([title isEqualToString:contextMenuItemTagShowSpellingPanel(true)]
-                     || [title isEqualToString:contextMenuItemTagShowSpellingPanel(false)])
-                modernTag = WebMenuItemTagShowSpellingPanel;
-            else if ([title isEqualToString:contextMenuItemTagCheckSpelling()])
-                modernTag = WebMenuItemTagCheckSpelling;
-            else if ([title isEqualToString:contextMenuItemTagCheckSpellingWhileTyping()])
-                modernTag = WebMenuItemTagCheckSpellingWhileTyping;
-            else if ([title isEqualToString:contextMenuItemTagCheckGrammarWithSpelling()])
-                modernTag = WebMenuItemTagCheckGrammarWithSpelling;
-            else if ([title isEqualToString:contextMenuItemTagFontMenu()])
-                modernTag = WebMenuItemTagFontMenu;
-            else if ([title isEqualToString:contextMenuItemTagShowFonts()])
-                modernTag = WebMenuItemTagShowFonts;
-            else if ([title isEqualToString:contextMenuItemTagBold()])
-                modernTag = WebMenuItemTagBold;
-            else if ([title isEqualToString:contextMenuItemTagItalic()])
-                modernTag = WebMenuItemTagItalic;
-            else if ([title isEqualToString:contextMenuItemTagUnderline()])
-                modernTag = WebMenuItemTagUnderline;
-            else if ([title isEqualToString:contextMenuItemTagOutline()])
-                modernTag = WebMenuItemTagOutline;
-            else if ([title isEqualToString:contextMenuItemTagStyles()])
-                modernTag = WebMenuItemTagStyles;
-            else if ([title isEqualToString:contextMenuItemTagShowColors()])
-                modernTag = WebMenuItemTagShowColors;
-            else if ([title isEqualToString:contextMenuItemTagSpeechMenu()])
-                modernTag = WebMenuItemTagSpeechMenu;
-            else if ([title isEqualToString:contextMenuItemTagStartSpeaking()])
-                modernTag = WebMenuItemTagStartSpeaking;
-            else if ([title isEqualToString:contextMenuItemTagStopSpeaking()])
-                modernTag = WebMenuItemTagStopSpeaking;
-            else if ([title isEqualToString:contextMenuItemTagWritingDirectionMenu()])
-                modernTag = WebMenuItemTagWritingDirectionMenu;
-            else if ([title isEqualToString:contextMenuItemTagDefaultDirection()])
-                modernTag = WebMenuItemTagDefaultDirection;
-            else if ([title isEqualToString:contextMenuItemTagLeftToRight()])
-                modernTag = WebMenuItemTagLeftToRight;
-            else if ([title isEqualToString:contextMenuItemTagRightToLeft()])
-                modernTag = WebMenuItemTagRightToLeft;
-            else if ([title isEqualToString:contextMenuItemTagInspectElement()])
-                modernTag = WebMenuItemTagInspectElement;
-            else if ([title isEqualToString:contextMenuItemTagCorrectSpellingAutomatically()])
-                modernTag = WebMenuItemTagCorrectSpellingAutomatically;
-            else if ([title isEqualToString:contextMenuItemTagSubstitutionsMenu()])
-                modernTag = WebMenuItemTagSubstitutionsMenu;
-            else if ([title isEqualToString:contextMenuItemTagShowSubstitutions(true)]
-                     || [title isEqualToString:contextMenuItemTagShowSubstitutions(false)])
-                modernTag = WebMenuItemTagShowSubstitutions;
-            else if ([title isEqualToString:contextMenuItemTagSmartCopyPaste()])
-                modernTag = WebMenuItemTagSmartCopyPaste;
-            else if ([title isEqualToString:contextMenuItemTagSmartQuotes()])
-                modernTag = WebMenuItemTagSmartQuotes;
-            else if ([title isEqualToString:contextMenuItemTagSmartDashes()])
-                modernTag = WebMenuItemTagSmartDashes;
-            else if ([title isEqualToString:contextMenuItemTagSmartLinks()])
-                modernTag = WebMenuItemTagSmartLinks;
-            else if ([title isEqualToString:contextMenuItemTagTextReplacement()])
-                modernTag = WebMenuItemTagTextReplacement;
-            else if ([title isEqualToString:contextMenuItemTagTransformationsMenu()])
-                modernTag = WebMenuItemTagTransformationsMenu;
-            else if ([title isEqualToString:contextMenuItemTagMakeUpperCase()])
-                modernTag = WebMenuItemTagMakeUpperCase;
-            else if ([title isEqualToString:contextMenuItemTagMakeLowerCase()])
-                modernTag = WebMenuItemTagMakeLowerCase;
-            else if ([title isEqualToString:contextMenuItemTagCapitalize()])
-                modernTag = WebMenuItemTagCapitalize;
-            else {
-            // We don't expect WebMenuItemTagOther for any items other than the ones we explicitly handle.
-            // There's nothing to prevent an app from applying this tag, but they are supposed to only
-            // use tags in the range starting with WebMenuItemBaseApplicationTag=10000
-                ASSERT_NOT_REACHED();
-            }
-        } else if (preVersion3Client) {
-            // Restore the new API tag for items on which we temporarily set the old SPI tag. The old SPI tag was
-            // needed to avoid confusing clients linked against earlier WebKits; the new API tag is needed for
-            // WebCore to handle the menu items appropriately (without needing to know about the old SPI tags).
-            switch (tag) {
-                case OldWebMenuItemTagSearchInSpotlight:
-                    modernTag = WebMenuItemTagSearchInSpotlight;
-                    break;
-                case OldWebMenuItemTagSearchWeb:
-                    modernTag = WebMenuItemTagSearchWeb;
-                    break;
-                case OldWebMenuItemTagLookUpInDictionary:
-                    modernTag = WebMenuItemTagLookUpInDictionary;
-                    break;
-                default:
-                    break;
-            }
-        }
-        
-        if (modernTag != tag)
-            [item setTag:modernTag];        
-    }
-
-    return newMenuItems;
-}
-
 NSMutableArray* WebContextMenuClient::getCustomMenuFromDefaultItems(ContextMenu* defaultMenu)
 {
-    id delegate = [m_webView UIDelegate];
-    SEL selector = @selector(webView:contextMenuItemsForElement:defaultMenuItems:);
-    if (![delegate respondsToSelector:selector])
-        return defaultMenu->platformDescription();
-
-    NSDictionary *element = [[[WebElementDictionary alloc] initWithHitTestResult:[m_webView page]->contextMenuController().hitTestResult()] autorelease];
-
-    BOOL preVersion3Client = isPreVersion3Client();
-    if (preVersion3Client) {
-        DOMNode *node = [element objectForKey:WebElementDOMNodeKey];
-        if ([node isKindOfClass:[DOMHTMLInputElement class]] && [(DOMHTMLInputElement *)node _isTextField])
-            return defaultMenu->platformDescription();
-        if ([node isKindOfClass:[DOMHTMLTextAreaElement class]])
-            return defaultMenu->platformDescription();
-    }
-
-    NSMutableArray *defaultMenuItems = defaultMenu->platformDescription();
-
-    for (NSMenuItem *menuItem in defaultMenuItems) {
-        if (!menuItem.representedObject)
-            menuItem.representedObject = element;
-    }
-
-    auto savedItems = fixMenusToSendToOldClients(defaultMenuItems);
-
-    NSArray *delegateSuppliedItems = CallUIDelegate(m_webView, selector, element, defaultMenuItems);
-
-    return fixMenusReceivedFromOldClients(delegateSuppliedItems, savedItems.get()).autorelease();
+    return defaultMenu->platformDescription();
 }
 
 void WebContextMenuClient::contextMenuItemSelected(ContextMenuItem* item, const ContextMenu* parentMenu)
index 589ca4a3a1584d56880206ae50979bb5460e4ef0..1f4c3c19cbcf91003a0ffe4cefe5bb7db4543fe2 100644 (file)
 #import <WebCore/Image.h>
 #import <WebCore/KeyboardEvent.h>
 #import <WebCore/LegacyWebArchive.h>
+#import <WebCore/LocalizedStrings.h>
 #import <WebCore/MIMETypeRegistry.h>
 #import <WebCore/MainFrame.h>
 #import <WebCore/NSURLFileTypeMappingsSPI.h>
@@ -3329,6 +3330,236 @@ static void setMenuTargets(NSMenu* menu)
     }
 }
 
+static BOOL isPreVersion3Client(void)
+{
+    static BOOL preVersion3Client = !WebKitLinkedOnOrAfter(WEBKIT_FIRST_VERSION_WITH_3_0_CONTEXT_MENU_TAGS);
+    return preVersion3Client;
+}
+
+static BOOL isPreInspectElementTagClient(void)
+{
+    static BOOL preInspectElementTagClient = !WebKitLinkedOnOrAfter(WEBKIT_FIRST_VERSION_WITH_INSPECT_ELEMENT_MENU_TAG);
+    return preInspectElementTagClient;
+}
+
+static RetainPtr<NSArray> fixMenusToSendToOldClients(NSMutableArray *defaultMenuItems)
+{
+    auto savedItems = adoptNS([[NSMutableArray alloc] init]);
+
+    unsigned defaultItemsCount = [defaultMenuItems count];
+
+    if (isPreInspectElementTagClient() && defaultItemsCount >= 2) {
+        NSMenuItem *secondToLastItem = [defaultMenuItems objectAtIndex:defaultItemsCount - 2];
+        NSMenuItem *lastItem = [defaultMenuItems objectAtIndex:defaultItemsCount - 1];
+
+        if ([secondToLastItem isSeparatorItem] && [lastItem tag] == WebMenuItemTagInspectElement) {
+            savedItems = adoptNS([[NSMutableArray alloc] initWithCapacity:2]);
+            [savedItems addObject:secondToLastItem];
+            [savedItems addObject:lastItem];
+
+            [defaultMenuItems removeObject:secondToLastItem];
+            [defaultMenuItems removeObject:lastItem];
+            defaultItemsCount -= 2;
+        }
+    }
+
+    BOOL preVersion3Client = isPreVersion3Client();
+    if (!preVersion3Client)
+        return savedItems;
+
+    for (NSMenuItem *item in defaultMenuItems) {
+        int tag = item.tag;
+        int oldStyleTag = tag;
+
+        if (tag >= WEBMENUITEMTAG_WEBKIT_3_0_SPI_START) {
+            // Change all editing-related SPI tags listed in WebUIDelegatePrivate.h to WebMenuItemTagOther
+            // to match our old WebKit context menu behavior.
+            oldStyleTag = WebMenuItemTagOther;
+        } else {
+            // All items are expected to have useful tags coming into this method.
+            ASSERT(tag != WebMenuItemTagOther);
+            
+            // Use the pre-3.0 tags for the few items that changed tags as they moved from SPI to API. We
+            // do this only for old clients; new Mail already expects the new symbols in this case.
+            if (preVersion3Client) {
+                switch (tag) {
+                case WebMenuItemTagSearchInSpotlight:
+                    oldStyleTag = OldWebMenuItemTagSearchInSpotlight;
+                    break;
+                case WebMenuItemTagSearchWeb:
+                    oldStyleTag = OldWebMenuItemTagSearchWeb;
+                    break;
+                case WebMenuItemTagLookUpInDictionary:
+                    oldStyleTag = OldWebMenuItemTagLookUpInDictionary;
+                    break;
+                default:
+                    break;
+                }
+            }
+        }
+
+        item.tag = oldStyleTag;
+    }
+
+    return savedItems;
+}
+
+static RetainPtr<NSArray> fixMenusReceivedFromOldClients(NSArray *delegateSuppliedItems, NSArray *savedItems)
+{
+    auto newMenuItems = adoptNS([delegateSuppliedItems mutableCopy]);
+
+    if (savedItems)
+        [newMenuItems addObjectsFromArray:savedItems];
+
+    BOOL preVersion3Client = isPreVersion3Client();
+    if (!preVersion3Client)
+        return newMenuItems;
+    
+    // Restore the modern tags to the menu items whose tags we altered in fixMenusToSendToOldClients. 
+    unsigned newItemsCount = [newMenuItems count];
+    for (unsigned i = 0; i < newItemsCount; ++i) {
+        NSMenuItem *item = [newMenuItems objectAtIndex:i];
+        
+        int tag = [item tag];
+        int modernTag = tag;
+        
+        if (tag == WebMenuItemTagOther) {
+            // Restore the specific tag for items on which we temporarily set WebMenuItemTagOther to match old behavior.
+            NSString *title = [item title];
+            if ([title isEqualToString:contextMenuItemTagOpenLink()])
+                modernTag = WebMenuItemTagOpenLink;
+            else if ([title isEqualToString:contextMenuItemTagIgnoreGrammar()])
+                modernTag = WebMenuItemTagIgnoreGrammar;
+            else if ([title isEqualToString:contextMenuItemTagSpellingMenu()])
+                modernTag = WebMenuItemTagSpellingMenu;
+            else if ([title isEqualToString:contextMenuItemTagShowSpellingPanel(true)] || [title isEqualToString:contextMenuItemTagShowSpellingPanel(false)])
+                modernTag = WebMenuItemTagShowSpellingPanel;
+            else if ([title isEqualToString:contextMenuItemTagCheckSpelling()])
+                modernTag = WebMenuItemTagCheckSpelling;
+            else if ([title isEqualToString:contextMenuItemTagCheckSpellingWhileTyping()])
+                modernTag = WebMenuItemTagCheckSpellingWhileTyping;
+            else if ([title isEqualToString:contextMenuItemTagCheckGrammarWithSpelling()])
+                modernTag = WebMenuItemTagCheckGrammarWithSpelling;
+            else if ([title isEqualToString:contextMenuItemTagFontMenu()])
+                modernTag = WebMenuItemTagFontMenu;
+            else if ([title isEqualToString:contextMenuItemTagShowFonts()])
+                modernTag = WebMenuItemTagShowFonts;
+            else if ([title isEqualToString:contextMenuItemTagBold()])
+                modernTag = WebMenuItemTagBold;
+            else if ([title isEqualToString:contextMenuItemTagItalic()])
+                modernTag = WebMenuItemTagItalic;
+            else if ([title isEqualToString:contextMenuItemTagUnderline()])
+                modernTag = WebMenuItemTagUnderline;
+            else if ([title isEqualToString:contextMenuItemTagOutline()])
+                modernTag = WebMenuItemTagOutline;
+            else if ([title isEqualToString:contextMenuItemTagStyles()])
+                modernTag = WebMenuItemTagStyles;
+            else if ([title isEqualToString:contextMenuItemTagShowColors()])
+                modernTag = WebMenuItemTagShowColors;
+            else if ([title isEqualToString:contextMenuItemTagSpeechMenu()])
+                modernTag = WebMenuItemTagSpeechMenu;
+            else if ([title isEqualToString:contextMenuItemTagStartSpeaking()])
+                modernTag = WebMenuItemTagStartSpeaking;
+            else if ([title isEqualToString:contextMenuItemTagStopSpeaking()])
+                modernTag = WebMenuItemTagStopSpeaking;
+            else if ([title isEqualToString:contextMenuItemTagWritingDirectionMenu()])
+                modernTag = WebMenuItemTagWritingDirectionMenu;
+            else if ([title isEqualToString:contextMenuItemTagDefaultDirection()])
+                modernTag = WebMenuItemTagDefaultDirection;
+            else if ([title isEqualToString:contextMenuItemTagLeftToRight()])
+                modernTag = WebMenuItemTagLeftToRight;
+            else if ([title isEqualToString:contextMenuItemTagRightToLeft()])
+                modernTag = WebMenuItemTagRightToLeft;
+            else if ([title isEqualToString:contextMenuItemTagInspectElement()])
+                modernTag = WebMenuItemTagInspectElement;
+            else if ([title isEqualToString:contextMenuItemTagCorrectSpellingAutomatically()])
+                modernTag = WebMenuItemTagCorrectSpellingAutomatically;
+            else if ([title isEqualToString:contextMenuItemTagSubstitutionsMenu()])
+                modernTag = WebMenuItemTagSubstitutionsMenu;
+            else if ([title isEqualToString:contextMenuItemTagShowSubstitutions(true)] || [title isEqualToString:contextMenuItemTagShowSubstitutions(false)])
+                modernTag = WebMenuItemTagShowSubstitutions;
+            else if ([title isEqualToString:contextMenuItemTagSmartCopyPaste()])
+                modernTag = WebMenuItemTagSmartCopyPaste;
+            else if ([title isEqualToString:contextMenuItemTagSmartQuotes()])
+                modernTag = WebMenuItemTagSmartQuotes;
+            else if ([title isEqualToString:contextMenuItemTagSmartDashes()])
+                modernTag = WebMenuItemTagSmartDashes;
+            else if ([title isEqualToString:contextMenuItemTagSmartLinks()])
+                modernTag = WebMenuItemTagSmartLinks;
+            else if ([title isEqualToString:contextMenuItemTagTextReplacement()])
+                modernTag = WebMenuItemTagTextReplacement;
+            else if ([title isEqualToString:contextMenuItemTagTransformationsMenu()])
+                modernTag = WebMenuItemTagTransformationsMenu;
+            else if ([title isEqualToString:contextMenuItemTagMakeUpperCase()])
+                modernTag = WebMenuItemTagMakeUpperCase;
+            else if ([title isEqualToString:contextMenuItemTagMakeLowerCase()])
+                modernTag = WebMenuItemTagMakeLowerCase;
+            else if ([title isEqualToString:contextMenuItemTagCapitalize()])
+                modernTag = WebMenuItemTagCapitalize;
+            else {
+            // We don't expect WebMenuItemTagOther for any items other than the ones we explicitly handle.
+            // There's nothing to prevent an app from applying this tag, but they are supposed to only
+            // use tags in the range starting with WebMenuItemBaseApplicationTag=10000
+                ASSERT_NOT_REACHED();
+            }
+        } else if (preVersion3Client) {
+            // Restore the new API tag for items on which we temporarily set the old SPI tag. The old SPI tag was
+            // needed to avoid confusing clients linked against earlier WebKits; the new API tag is needed for
+            // WebCore to handle the menu items appropriately (without needing to know about the old SPI tags).
+            switch (tag) {
+            case OldWebMenuItemTagSearchInSpotlight:
+                modernTag = WebMenuItemTagSearchInSpotlight;
+                break;
+            case OldWebMenuItemTagSearchWeb:
+                modernTag = WebMenuItemTagSearchWeb;
+                break;
+            case OldWebMenuItemTagLookUpInDictionary:
+                modernTag = WebMenuItemTagLookUpInDictionary;
+                break;
+            default:
+                break;
+            }
+        }
+        
+        if (modernTag != tag)
+            [item setTag:modernTag];        
+    }
+
+    return newMenuItems;
+}
+
+static RetainPtr<NSArray> customMenuFromDefaultItems(WebView *webView, const ContextMenu& defaultMenu)
+{
+    id delegate = [webView UIDelegate];
+    SEL selector = @selector(webView:contextMenuItemsForElement:defaultMenuItems:);
+    if (![delegate respondsToSelector:selector])
+        return defaultMenu.platformDescription();
+
+    auto element = adoptNS([[WebElementDictionary alloc] initWithHitTestResult:[webView page]->contextMenuController().hitTestResult()]);
+
+    BOOL preVersion3Client = isPreVersion3Client();
+    if (preVersion3Client) {
+        DOMNode *node = [element objectForKey:WebElementDOMNodeKey];
+        if ([node isKindOfClass:[DOMHTMLInputElement class]] && [(DOMHTMLInputElement *)node _isTextField])
+            return defaultMenu.platformDescription();
+        if ([node isKindOfClass:[DOMHTMLTextAreaElement class]])
+            return defaultMenu.platformDescription();
+    }
+
+    NSMutableArray *defaultMenuItems = defaultMenu.platformDescription();
+
+    for (NSMenuItem *menuItem in defaultMenuItems) {
+        if (!menuItem.representedObject)
+            menuItem.representedObject = element.get();
+    }
+
+    auto savedItems = fixMenusToSendToOldClients(defaultMenuItems);
+
+    NSArray *delegateSuppliedItems = CallUIDelegate(webView, selector, element.get(), defaultMenuItems);
+
+    return fixMenusReceivedFromOldClients(delegateSuppliedItems, savedItems.get()).autorelease();
+}
+
 - (NSMenu *)menuForEvent:(NSEvent *)event
 {
     // There's a chance that if we run a nested event loop the event will be released.
@@ -3361,21 +3592,17 @@ static void setMenuTargets(NSMenu* menu)
     if (!page)
         return nil;
 
-    ContextMenu* coreMenu = page->contextMenuController().contextMenu();
-    if (!coreMenu)
+    ContextMenu* contextMenu = page->contextMenuController().contextMenu();
+    if (!contextMenu)
         return nil;
 
-    NSArray* menuItems = coreMenu->platformDescription();
-    if (!menuItems)
-        return nil;
-
-    NSUInteger count = [menuItems count];
-    if (!count)
+    auto menuItems = customMenuFromDefaultItems([self _webView], *contextMenu);
+    if (![menuItems count])
         return nil;
 
     auto menu = adoptNS([[NSMenu alloc] init]);
 
-    for (NSMenuItem *item in menuItems) {
+    for (NSMenuItem *item in menuItems.get()) {
         [menu addItem:item];
 
         if (item.tag == ContextMenuItemTagShareMenu) {
@@ -3384,8 +3611,8 @@ static void setMenuTargets(NSMenu* menu)
             _private->currentSharingServicePickerController = adoptNS([[WebSharingServicePickerController alloc] initWithSharingServicePicker:item.representedObject client:static_cast<WebContextMenuClient&>(page->contextMenuController().client())]);
 #endif
         }
-
     }
+
     setMenuTargets(menu.get());
     
     [[WebMenuTarget sharedMenuTarget] setMenuController:&page->contextMenuController()];