Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1042)

Unified Diff: Source/core/page/CustomContextMenuProvider.cpp

Issue 243403006: Implement contextmenu attribute with basic support of <menu> (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Reflect HTMLMenuElement as per spec Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « Source/core/page/CustomContextMenuProvider.h ('k') | Source/platform/RuntimeEnabledFeatures.in » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Source/core/page/CustomContextMenuProvider.cpp
diff --git a/Source/core/page/CustomContextMenuProvider.cpp b/Source/core/page/CustomContextMenuProvider.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..e6d6857f76daf649d02bf72a99709683b467991c
--- /dev/null
+++ b/Source/core/page/CustomContextMenuProvider.cpp
@@ -0,0 +1,81 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "config.h"
+#include "core/page/CustomContextMenuProvider.h"
+
+#include "core/dom/Document.h"
+#include "core/dom/ElementTraversal.h"
+#include "core/events/EventDispatcher.h"
+#include "core/events/MouseEvent.h"
+#include "core/html/HTMLMenuElement.h"
+#include "core/html/HTMLMenuItemElement.h"
+#include "core/page/ContextMenuController.h"
+#include "core/page/Page.h"
+#include "platform/ContextMenu.h"
+
+namespace blink {
+
+using namespace HTMLNames;
+
+CustomContextMenuProvider::CustomContextMenuProvider(HTMLMenuElement& menu, HTMLElement& subject)
+ : m_menu(menu)
+ , m_subjectElement(subject)
+{
+}
+
+CustomContextMenuProvider::~CustomContextMenuProvider()
+{
+}
+
+void CustomContextMenuProvider::populateContextMenu(ContextMenu* menu)
+{
+ populateContextMenuItems(*m_menu, *menu);
+}
+
+void CustomContextMenuProvider::contextMenuItemSelected(const ContextMenuItem* item)
+{
+ if (HTMLElement* element = menuItemAt(item->action())) {
+ RefPtrWillBeRawPtr<SimulatedMouseEvent> click = SimulatedMouseEvent::create(EventTypeNames::click, m_menu->document().domWindow(), Event::create());
+ click->setRelatedTarget(m_subjectElement.get());
+ element->dispatchEvent(click.release());
+ }
+}
+
+void CustomContextMenuProvider::contextMenuCleared()
+{
+ m_menuItems.clear();
+ m_subjectElement = nullptr;
+}
+
+void CustomContextMenuProvider::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* CustomContextMenuProvider::menuItemAt(unsigned menuId)
+{
+ int itemIndex = menuId - ContextMenuItemBaseCustomTag;
+ if (itemIndex < 0 || static_cast<unsigned long>(itemIndex) >= m_menuItems.size())
+ return 0;
+ return m_menuItems[itemIndex].get();
+}
+
+} // namespace blink
« no previous file with comments | « Source/core/page/CustomContextMenuProvider.h ('k') | Source/platform/RuntimeEnabledFeatures.in » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698