Index: Source/core/page/ContextMenuController.cpp |
diff --git a/Source/core/page/ContextMenuController.cpp b/Source/core/page/ContextMenuController.cpp |
index 9e97e69bafd3d7dd0439fc22ff6960a5bce5e70a..deff996cb9f7276036e0caa6463ee895f8add4eb 100644 |
--- a/Source/core/page/ContextMenuController.cpp |
+++ b/Source/core/page/ContextMenuController.cpp |
@@ -28,18 +28,26 @@ |
#include "core/page/ContextMenuController.h" |
#include "core/dom/Document.h" |
+#include "core/dom/ElementTraversal.h" |
+#include "core/dom/Node.h" |
#include "core/events/Event.h" |
+#include "core/events/EventDispatchMediator.h" |
+#include "core/events/EventDispatcher.h" |
#include "core/events/MouseEvent.h" |
-#include "core/dom/Node.h" |
+#include "core/events/RelatedEvent.h" |
#include "core/frame/LocalFrame.h" |
+#include "core/html/HTMLMenuElement.h" |
#include "core/page/ContextMenuClient.h" |
#include "core/page/ContextMenuProvider.h" |
+#include "core/page/CustomContextMenuProvider.h" |
#include "core/page/EventHandler.h" |
#include "platform/ContextMenu.h" |
#include "platform/ContextMenuItem.h" |
namespace blink { |
+using namespace HTMLNames; |
+ |
ContextMenuController::ContextMenuController(Page*, ContextMenuClient* client) |
: m_client(client) |
{ |
@@ -74,12 +82,57 @@ void ContextMenuController::documentDetached(Document* document) |
} |
} |
+void ContextMenuController::populateContextMenuItems(const HTMLMenuElement& menu, ContextMenu& contextMenu) |
+{ |
+ HTMLElement* nextElement = Traversal<HTMLElement>::firstWithin(menu); |
+ while (nextElement) { |
+ if (isHTMLMenuElement(*nextElement)) { |
+ ContextMenu subMenu; |
+ populateContextMenuItems(*toHTMLMenuElement(nextElement), subMenu); |
+ contextMenu.appendItem(ContextMenuItem(SubmenuType, ContextMenuItemCustomTagNoAction, nextElement->fastGetAttribute(labelAttr), &subMenu)); |
+ nextElement = Traversal<HTMLElement>::nextSibling(*nextElement); |
+ } else if (isHTMLMenuItemElement(*nextElement)) { |
+ m_menuItems.append(nextElement); |
+ contextMenu.appendItem(ContextMenuItem(ActionType, static_cast<ContextMenuAction>(ContextMenuItemBaseCustomTag + m_menuItems.size() - 1), nextElement->fastGetAttribute(labelAttr))); |
+ if (ContextMenuItemBaseCustomTag + m_menuItems.size() >= ContextMenuItemLastCustomTag) |
+ break; |
+ nextElement = Traversal<HTMLElement>::next(*nextElement, &menu); |
+ } else { |
+ nextElement = Traversal<HTMLElement>::next(*nextElement, &menu); |
+ } |
+ } |
+} |
+ |
+HTMLElement* ContextMenuController::menuItemAt(unsigned menuId) |
+{ |
+ int itemIndex = menuId - ContextMenuItemBaseCustomTag; |
+ if (itemIndex < 0 || static_cast<unsigned long>(itemIndex) >= m_menuItems.size()) |
+ return 0; |
+ return m_menuItems[itemIndex]; |
+} |
+ |
void ContextMenuController::handleContextMenuEvent(Event* event) |
{ |
m_contextMenu = createContextMenu(event); |
if (!m_contextMenu) |
return; |
+ Node* node = event->target()->toNode(); |
tkent
2014/07/31 06:07:56
I recommend to make new function for this block of
pals
2014/08/01 10:03:08
Done.
|
+ if (node && node->isHTMLElement() && RuntimeEnabledFeatures::contextMenuEnabled()) { |
+ const HTMLElement& element = toHTMLElement(*node); |
+ if (RefPtrWillBeRawPtr<HTMLMenuElement> menuElement = element.contextMenu()) { |
+ RefPtr<RelatedEvent> relatedEvent = RelatedEvent::create(EventTypeNames::show); |
+ relatedEvent->setRelatedTarget(node); |
+ m_subject = toHTMLElement(node); |
tkent
2014/07/31 06:07:56
toHTMLElement is unnecessary we already have |elem
pals
2014/08/01 10:03:08
Done.
|
+ bool notCanceled = menuElement->dispatchEvent(relatedEvent.release()); |
tkent
2014/07/31 06:07:56
|notCanceled| is unnecessary.
if (menuElement->d
pals
2014/08/01 10:03:08
Done.
|
+ if (notCanceled) { |
+ ContextMenu menu; |
tkent
2014/07/31 06:07:56
Because dispatchEvent runs arbitrary JavaScript co
pals
2014/08/01 10:03:08
Done.
|
+ populateContextMenuItems(*menuElement, menu); |
tkent
2014/07/31 06:07:56
I'm sorry I changed my mind. populateContextMenuI
pals
2014/08/01 10:03:08
Done. Moved to CustomContextMenuProvider. After th
|
+ m_menuProvider = CustomContextMenuProvider::create(&menuElement->document(), menu.items()); |
+ m_menuProvider->populateContextMenu(m_contextMenu.get()); |
+ } |
+ } |
+ } |
showContextMenu(event); |
} |