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

Side by Side Diff: Source/core/page/ContextMenuController.cpp

Issue 243403006: Implement contextmenu attribute with basic support of <menu> (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Addressed review comments 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 unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2006, 2007 Apple Inc. All rights reserved. 2 * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
3 * Copyright (C) 2010 Igalia S.L 3 * Copyright (C) 2010 Igalia S.L
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions 6 * modification, are permitted provided that the following conditions
7 * are met: 7 * are met:
8 * 1. Redistributions of source code must retain the above copyright 8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright 10 * 2. Redistributions in binary form must reproduce the above copyright
(...skipping 10 matching lines...) Expand all
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */ 25 */
26 26
27 #include "config.h" 27 #include "config.h"
28 #include "core/page/ContextMenuController.h" 28 #include "core/page/ContextMenuController.h"
29 29
30 #include "core/dom/Document.h" 30 #include "core/dom/Document.h"
31 #include "core/dom/ElementTraversal.h"
32 #include "core/dom/Node.h"
31 #include "core/events/Event.h" 33 #include "core/events/Event.h"
34 #include "core/events/EventDispatchMediator.h"
35 #include "core/events/EventDispatcher.h"
32 #include "core/events/MouseEvent.h" 36 #include "core/events/MouseEvent.h"
33 #include "core/dom/Node.h" 37 #include "core/events/RelatedEvent.h"
34 #include "core/frame/LocalFrame.h" 38 #include "core/frame/LocalFrame.h"
39 #include "core/html/HTMLMenuElement.h"
35 #include "core/page/ContextMenuClient.h" 40 #include "core/page/ContextMenuClient.h"
36 #include "core/page/ContextMenuProvider.h" 41 #include "core/page/ContextMenuProvider.h"
42 #include "core/page/CustomContextMenuProvider.h"
37 #include "core/page/EventHandler.h" 43 #include "core/page/EventHandler.h"
38 #include "platform/ContextMenu.h" 44 #include "platform/ContextMenu.h"
39 #include "platform/ContextMenuItem.h" 45 #include "platform/ContextMenuItem.h"
40 46
41 namespace blink { 47 namespace blink {
42 48
49 using namespace HTMLNames;
50
43 ContextMenuController::ContextMenuController(Page*, ContextMenuClient* client) 51 ContextMenuController::ContextMenuController(Page*, ContextMenuClient* client)
44 : m_client(client) 52 : m_client(client)
45 { 53 {
46 ASSERT_ARG(client, client); 54 ASSERT_ARG(client, client);
47 } 55 }
48 56
49 ContextMenuController::~ContextMenuController() 57 ContextMenuController::~ContextMenuController()
50 { 58 {
51 } 59 }
52 60
(...skipping 14 matching lines...) Expand all
67 75
68 void ContextMenuController::documentDetached(Document* document) 76 void ContextMenuController::documentDetached(Document* document)
69 { 77 {
70 if (Node* innerNode = m_hitTestResult.innerNode()) { 78 if (Node* innerNode = m_hitTestResult.innerNode()) {
71 // Invalidate the context menu info if its target document is detached. 79 // Invalidate the context menu info if its target document is detached.
72 if (innerNode->document() == document) 80 if (innerNode->document() == document)
73 clearContextMenu(); 81 clearContextMenu();
74 } 82 }
75 } 83 }
76 84
85 void ContextMenuController::populateContextMenuItems(const HTMLMenuElement& menu , ContextMenu& contextMenu)
86 {
87 HTMLElement* nextElement = Traversal<HTMLElement>::firstWithin(menu);
88 while (nextElement) {
89 if (isHTMLMenuElement(*nextElement)) {
90 ContextMenu subMenu;
91 populateContextMenuItems(*toHTMLMenuElement(nextElement), subMenu);
92 contextMenu.appendItem(ContextMenuItem(SubmenuType, ContextMenuItemC ustomTagNoAction, nextElement->fastGetAttribute(labelAttr), &subMenu));
93 nextElement = Traversal<HTMLElement>::nextSibling(*nextElement);
94 } else if (isHTMLMenuItemElement(*nextElement)) {
95 m_menuItems.append(nextElement);
96 contextMenu.appendItem(ContextMenuItem(ActionType, static_cast<Conte xtMenuAction>(ContextMenuItemBaseCustomTag + m_menuItems.size() - 1), nextElemen t->fastGetAttribute(labelAttr)));
97 if (ContextMenuItemBaseCustomTag + m_menuItems.size() >= ContextMenu ItemLastCustomTag)
98 break;
99 nextElement = Traversal<HTMLElement>::next(*nextElement, &menu);
100 } else {
101 nextElement = Traversal<HTMLElement>::next(*nextElement, &menu);
102 }
103 }
104 }
105
106 HTMLElement* ContextMenuController::menuItemAt(unsigned menuId)
107 {
108 int itemIndex = menuId - ContextMenuItemBaseCustomTag;
109 if (itemIndex < 0 || static_cast<unsigned long>(itemIndex) >= m_menuItems.si ze())
110 return 0;
111 return m_menuItems[itemIndex];
112 }
113
77 void ContextMenuController::handleContextMenuEvent(Event* event) 114 void ContextMenuController::handleContextMenuEvent(Event* event)
78 { 115 {
79 m_contextMenu = createContextMenu(event); 116 m_contextMenu = createContextMenu(event);
80 if (!m_contextMenu) 117 if (!m_contextMenu)
81 return; 118 return;
82 119
120 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.
121 if (node && node->isHTMLElement() && RuntimeEnabledFeatures::contextMenuEnab led()) {
122 const HTMLElement& element = toHTMLElement(*node);
123 if (RefPtrWillBeRawPtr<HTMLMenuElement> menuElement = element.contextMen u()) {
124 RefPtr<RelatedEvent> relatedEvent = RelatedEvent::create(EventTypeNa mes::show);
125 relatedEvent->setRelatedTarget(node);
126 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.
127 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.
128 if (notCanceled) {
129 ContextMenu menu;
tkent 2014/07/31 06:07:56 Because dispatchEvent runs arbitrary JavaScript co
pals 2014/08/01 10:03:08 Done.
130 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
131 m_menuProvider = CustomContextMenuProvider::create(&menuElement- >document(), menu.items());
132 m_menuProvider->populateContextMenu(m_contextMenu.get());
133 }
134 }
135 }
83 showContextMenu(event); 136 showContextMenu(event);
84 } 137 }
85 138
86 void ContextMenuController::showContextMenu(Event* event, PassRefPtr<ContextMenu Provider> menuProvider) 139 void ContextMenuController::showContextMenu(Event* event, PassRefPtr<ContextMenu Provider> menuProvider)
87 { 140 {
88 m_menuProvider = menuProvider; 141 m_menuProvider = menuProvider;
89 142
90 m_contextMenu = createContextMenu(event); 143 m_contextMenu = createContextMenu(event);
91 if (!m_contextMenu) { 144 if (!m_contextMenu) {
92 clearContextMenu(); 145 clearContextMenu();
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 ASSERT(item->type() == ActionType || item->type() == CheckableActionType); 203 ASSERT(item->type() == ActionType || item->type() == CheckableActionType);
151 204
152 if (item->action() < ContextMenuItemBaseCustomTag || item->action() > Contex tMenuItemLastCustomTag) 205 if (item->action() < ContextMenuItemBaseCustomTag || item->action() > Contex tMenuItemLastCustomTag)
153 return; 206 return;
154 207
155 ASSERT(m_menuProvider); 208 ASSERT(m_menuProvider);
156 m_menuProvider->contextMenuItemSelected(item); 209 m_menuProvider->contextMenuItemSelected(item);
157 } 210 }
158 211
159 } // namespace blink 212 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698