Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org) | 2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org) |
| 3 * (C) 1999 Antti Koivisto (koivisto@kde.org) | 3 * (C) 1999 Antti Koivisto (koivisto@kde.org) |
| 4 * Copyright (C) 2010 Apple Inc. All rights reserved. | 4 * Copyright (C) 2010 Apple Inc. All rights reserved. |
| 5 * | 5 * |
| 6 * This library is free software; you can redistribute it and/or | 6 * This library is free software; you can redistribute it and/or |
| 7 * modify it under the terms of the GNU Library General Public | 7 * modify it under the terms of the GNU Library General Public |
| 8 * License as published by the Free Software Foundation; either | 8 * License as published by the Free Software Foundation; either |
| 9 * version 2 of the License, or (at your option) any later version. | 9 * version 2 of the License, or (at your option) any later version. |
| 10 * | 10 * |
| 11 * This library is distributed in the hope that it will be useful, | 11 * This library is distributed in the hope that it will be useful, |
| 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 * Library General Public License for more details. | 14 * Library General Public License for more details. |
| 15 * | 15 * |
| 16 * You should have received a copy of the GNU Library General Public License | 16 * You should have received a copy of the GNU Library General Public License |
| 17 * along with this library; see the file COPYING.LIB. If not, write to | 17 * along with this library; see the file COPYING.LIB. If not, write to |
| 18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | 18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 19 * Boston, MA 02110-1301, USA. | 19 * Boston, MA 02110-1301, USA. |
| 20 * | 20 * |
| 21 */ | 21 */ |
| 22 | 22 |
| 23 #include "config.h" | 23 #include "config.h" |
| 24 | |
| 24 #include "core/html/HTMLMenuElement.h" | 25 #include "core/html/HTMLMenuElement.h" |
| 25 | 26 |
| 26 #include "HTMLNames.h" | 27 #include "core/dom/ElementTraversal.h" |
| 28 #include "core/events/EventListener.h" | |
| 29 #include "core/events/RelatedEvent.h" | |
| 30 #include "core/html/HTMLMenuitemElement.h" | |
| 31 #include "core/page/ContextMenuController.h" | |
| 32 #include "core/page/Page.h" | |
| 27 | 33 |
| 28 namespace WebCore { | 34 namespace WebCore { |
| 29 | 35 |
| 30 using namespace HTMLNames; | 36 using namespace HTMLNames; |
| 31 | 37 |
| 38 class HTMLMenuElementEventListener : public EventListener { | |
|
esprehn
2014/05/20 05:51:34
FINAL
This class should also have it's own .h and
pals
2014/05/21 07:55:12
Done.
| |
| 39 public: | |
| 40 static PassRefPtr<HTMLMenuElementEventListener> create(HTMLMenuElement* menu ) { return adoptRef(new HTMLMenuElementEventListener(menu)); } | |
| 41 static const HTMLMenuElementEventListener* cast(const EventListener* listene r) | |
|
esprehn
2014/05/20 05:51:34
This should be an instance method, toHTMLMenuEleme
pals
2014/05/21 07:55:12
Done.
| |
| 42 { | |
| 43 return listener->type() == NativeEventListenerType | |
| 44 ? static_cast<const HTMLMenuElementEventListener*>(listener) | |
| 45 : 0; | |
| 46 } | |
| 47 | |
| 48 virtual bool operator==(const EventListener& other); | |
| 49 | |
| 50 private: | |
| 51 HTMLMenuElementEventListener(HTMLMenuElement* menu) | |
|
esprehn
2014/05/20 05:51:34
missing explicit
pals
2014/05/21 07:55:12
Done.
| |
| 52 : EventListener(NativeEventListenerType) | |
| 53 , m_menu(menu) | |
| 54 { | |
| 55 } | |
| 56 | |
| 57 virtual void handleEvent(ExecutionContext*, Event*); | |
| 58 | |
| 59 HTMLMenuElement* m_menu; | |
| 60 }; | |
| 61 | |
| 62 void HTMLMenuElementEventListener::handleEvent(ExecutionContext*, Event* event) | |
| 63 { | |
| 64 if (event->type() == EventTypeNames::show && toRelatedEvent(event)->isRelate dEvent()) { | |
| 65 Node* node = toRelatedEvent(event)->relatedTarget()->toNode(); | |
| 66 if (node && node->isElementNode()) { | |
| 67 const HTMLElement& element = toHTMLElement(*node); | |
| 68 HTMLMenuElement* menuElement = element.menuElement(); | |
| 69 if (menuElement) { | |
| 70 menuElement->buildCustomProvider(); | |
| 71 event->setDefaultHandled(); | |
| 72 } | |
| 73 } | |
| 74 } | |
| 75 } | |
| 76 | |
| 77 bool HTMLMenuElementEventListener::operator==(const EventListener& listener) | |
| 78 { | |
| 79 if (const HTMLMenuElementEventListener* menuEventListener = HTMLMenuElementE ventListener::cast(&listener)) | |
| 80 return m_menu == menuEventListener->m_menu; | |
| 81 return false; | |
| 82 } | |
| 83 | |
| 32 inline HTMLMenuElement::HTMLMenuElement(Document& document) | 84 inline HTMLMenuElement::HTMLMenuElement(Document& document) |
| 33 : HTMLElement(menuTag, document) | 85 : HTMLElement(menuTag, document) |
| 34 { | 86 { |
| 35 ScriptWrappable::init(this); | 87 ScriptWrappable::init(this); |
| 88 // Add event listeners | |
|
esprehn
2014/05/20 05:51:34
remove comment, it doesn't add value.
pals
2014/05/21 07:55:12
Done.
| |
| 89 RefPtr<EventListener> listener = HTMLMenuElementEventListener::create(this); | |
| 90 this->addEventListener("show", listener.release(), false); | |
|
esprehn
2014/05/20 05:51:34
Remove this->, you don't need it.
pals
2014/05/21 07:55:12
Done.
| |
| 36 } | 91 } |
| 37 | 92 |
| 38 PassRefPtr<HTMLMenuElement> HTMLMenuElement::create(Document& document) | 93 PassRefPtr<HTMLMenuElement> HTMLMenuElement::create(Document& document) |
| 39 { | 94 { |
| 40 return adoptRef(new HTMLMenuElement(document)); | 95 return adoptRef(new HTMLMenuElement(document)); |
| 41 } | 96 } |
| 42 | 97 |
| 98 void HTMLMenuElement::buildCustomProvider() | |
| 99 { | |
| 100 m_menuItems.clear(); | |
| 101 ContextMenu menu; | |
| 102 populateContextMenuItems(this, menu); | |
| 103 m_menuProvider = CustomContextMenuProvider::create(this, menu.items()); | |
| 43 } | 104 } |
| 105 | |
| 106 void HTMLMenuElement::populateContextMenuItems(HTMLMenuElement* menu, ContextMen u& contextMenu) | |
| 107 { | |
| 108 for (Element* nextElement = ElementTraversal::firstWithin(*menu); nextElemen t; nextElement = ElementTraversal::next(*nextElement)) { | |
|
esprehn
2014/05/20 05:51:34
Missing scoping node, this will traverse all the w
pals
2014/05/21 07:55:12
Done.
| |
| 109 if (nextElement->hasTagName(HTMLNames::menuTag)) { | |
| 110 ContextMenu subMenu; | |
| 111 populateContextMenuItems(toHTMLMenuElement(nextElement), subMenu); | |
| 112 contextMenu.appendItem(ContextMenuItem(SubmenuType, ContextMenuItemC ustomTagNoAction, nextElement->fastGetAttribute(HTMLNames::labelAttr), &subMenu) ); | |
| 113 } else { | |
| 114 m_menuItems.append(toHTMLElement(nextElement)); | |
|
esprehn
2014/05/20 05:51:34
Does the spec really say that anything is a menu i
pals
2014/05/21 07:55:12
Done.
| |
| 115 contextMenu.appendItem(ContextMenuItem(ActionType, static_cast<Conte xtMenuAction>(ContextMenuItemBaseCustomTag + m_menuItems.size() - 1), nextElemen t->fastGetAttribute(HTMLNames::labelAttr))); | |
|
esprehn
2014/05/20 05:51:34
This enum stuff doesn't look right.
pals
2014/05/21 07:55:12
I mostly followed the custom contextmenu implement
| |
| 116 } | |
| 117 } | |
| 118 } | |
| 119 | |
| 120 HTMLElement* HTMLMenuElement::getSelectedMenuItem(unsigned menuId) | |
| 121 { | |
| 122 return m_menuItems[menuId - ContextMenuItemBaseCustomTag]; | |
| 123 } | |
| 124 | |
| 125 } | |
| OLD | NEW |