Bug 1732219 - Add API for fetching the preview image. r=geckoview-reviewers,agi,mconley
[gecko.git] / dom / html / HTMLMenuElement.cpp
blobb146e3bea18a654b6cf2855c82f01bbb114f0559
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/dom/HTMLMenuElement.h"
9 #include "mozilla/BasicEvents.h"
10 #include "mozilla/EventDispatcher.h"
11 #include "mozilla/dom/DocumentInlines.h"
12 #include "mozilla/dom/HTMLMenuElementBinding.h"
13 #include "mozilla/dom/HTMLMenuItemElement.h"
14 #include "nsIMenuBuilder.h"
15 #include "nsAttrValueInlines.h"
16 #include "nsComponentManagerUtils.h"
17 #include "nsContentUtils.h"
18 #include "nsIURI.h"
20 #define HTMLMENUBUILDER_CONTRACTID "@mozilla.org/content/html-menu-builder;1"
22 NS_IMPL_NS_NEW_HTML_ELEMENT(Menu)
24 namespace mozilla::dom {
26 enum MenuType : uint8_t { MENU_TYPE_CONTEXT = 1, MENU_TYPE_TOOLBAR };
28 static const nsAttrValue::EnumTable kMenuTypeTable[] = {
29 {"context", MENU_TYPE_CONTEXT},
30 {"toolbar", MENU_TYPE_TOOLBAR},
31 {nullptr, 0}};
33 static const nsAttrValue::EnumTable* kMenuDefaultType = &kMenuTypeTable[1];
35 enum SeparatorType { ST_TRUE_INIT = -1, ST_FALSE = 0, ST_TRUE = 1 };
37 HTMLMenuElement::HTMLMenuElement(
38 already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo)
39 : nsGenericHTMLElement(std::move(aNodeInfo)), mType(MENU_TYPE_TOOLBAR) {}
41 HTMLMenuElement::~HTMLMenuElement() = default;
43 NS_IMPL_ELEMENT_CLONE(HTMLMenuElement)
45 void HTMLMenuElement::SendShowEvent() {
46 nsCOMPtr<Document> document = GetComposedDoc();
47 if (!document) {
48 return;
51 WidgetEvent event(true, eShow);
52 event.mFlags.mBubbles = false;
53 event.mFlags.mCancelable = false;
55 RefPtr<nsPresContext> presContext = document->GetPresContext();
56 if (!presContext) {
57 return;
60 nsEventStatus status = nsEventStatus_eIgnore;
61 EventDispatcher::Dispatch(static_cast<nsIContent*>(this), presContext, &event,
62 nullptr, &status);
65 already_AddRefed<nsIMenuBuilder> HTMLMenuElement::CreateBuilder() {
66 if (mType != MENU_TYPE_CONTEXT) {
67 return nullptr;
70 nsCOMPtr<nsIMenuBuilder> builder =
71 do_CreateInstance(HTMLMENUBUILDER_CONTRACTID);
72 NS_WARNING_ASSERTION(builder, "No builder available");
73 return builder.forget();
76 void HTMLMenuElement::Build(nsIMenuBuilder* aBuilder) {
77 if (!aBuilder) {
78 return;
81 BuildSubmenu(u""_ns, this, aBuilder);
84 nsresult HTMLMenuElement::AfterSetAttr(int32_t aNameSpaceID, nsAtom* aName,
85 const nsAttrValue* aValue,
86 const nsAttrValue* aOldValue,
87 nsIPrincipal* aSubjectPrincipal,
88 bool aNotify) {
89 if (aNameSpaceID == kNameSpaceID_None && aName == nsGkAtoms::type &&
90 StaticPrefs::dom_menuitem_enabled()) {
91 if (aValue) {
92 mType = aValue->GetEnumValue();
93 } else {
94 mType = kMenuDefaultType->value;
98 return nsGenericHTMLElement::AfterSetAttr(
99 aNameSpaceID, aName, aValue, aOldValue, aSubjectPrincipal, aNotify);
102 bool HTMLMenuElement::ParseAttribute(int32_t aNamespaceID, nsAtom* aAttribute,
103 const nsAString& aValue,
104 nsIPrincipal* aMaybeScriptedPrincipal,
105 nsAttrValue& aResult) {
106 if (aNamespaceID == kNameSpaceID_None && aAttribute == nsGkAtoms::type &&
107 StaticPrefs::dom_menuitem_enabled()) {
108 return aResult.ParseEnumValue(aValue, kMenuTypeTable, false,
109 kMenuDefaultType);
112 return nsGenericHTMLElement::ParseAttribute(aNamespaceID, aAttribute, aValue,
113 aMaybeScriptedPrincipal, aResult);
116 void HTMLMenuElement::BuildSubmenu(const nsAString& aLabel,
117 nsIContent* aContent,
118 nsIMenuBuilder* aBuilder) {
119 aBuilder->OpenContainer(aLabel);
121 int8_t separator = ST_TRUE_INIT;
122 TraverseContent(aContent, aBuilder, separator);
124 if (separator == ST_TRUE) {
125 aBuilder->UndoAddSeparator();
128 aBuilder->CloseContainer();
131 // static
132 bool HTMLMenuElement::CanLoadIcon(nsIContent* aContent,
133 const nsAString& aIcon) {
134 if (aIcon.IsEmpty()) {
135 return false;
138 Document* doc = aContent->OwnerDoc();
140 nsCOMPtr<nsIURI> uri;
141 nsContentUtils::NewURIWithDocumentCharset(getter_AddRefs(uri), aIcon, doc,
142 aContent->GetBaseURI());
144 if (!uri) {
145 return false;
148 return nsContentUtils::CanLoadImage(uri, aContent, doc,
149 aContent->NodePrincipal());
152 void HTMLMenuElement::TraverseContent(nsIContent* aContent,
153 nsIMenuBuilder* aBuilder,
154 int8_t& aSeparator) {
155 nsCOMPtr<nsIContent> child;
156 for (child = aContent->GetFirstChild(); child;
157 child = child->GetNextSibling()) {
158 nsGenericHTMLElement* element = nsGenericHTMLElement::FromNode(child);
159 if (!element) {
160 continue;
163 if (child->IsHTMLElement(nsGkAtoms::menuitem)) {
164 HTMLMenuItemElement* menuitem = HTMLMenuItemElement::FromNode(child);
166 if (menuitem->IsHidden()) {
167 continue;
170 nsAutoString label;
171 menuitem->GetLabel(label);
172 if (label.IsEmpty()) {
173 continue;
176 nsAutoString icon;
177 menuitem->GetIcon(icon);
179 aBuilder->AddItemFor(menuitem, CanLoadIcon(child, icon));
181 aSeparator = ST_FALSE;
182 } else if (child->IsHTMLElement(nsGkAtoms::hr)) {
183 aBuilder->AddSeparator();
184 } else if (child->IsHTMLElement(nsGkAtoms::menu) && !element->IsHidden()) {
185 if (child->AsElement()->HasAttr(kNameSpaceID_None, nsGkAtoms::label)) {
186 nsAutoString label;
187 child->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::label, label);
189 BuildSubmenu(label, child, aBuilder);
191 aSeparator = ST_FALSE;
192 } else {
193 AddSeparator(aBuilder, aSeparator);
195 TraverseContent(child, aBuilder, aSeparator);
197 AddSeparator(aBuilder, aSeparator);
203 inline void HTMLMenuElement::AddSeparator(nsIMenuBuilder* aBuilder,
204 int8_t& aSeparator) {
205 if (aSeparator) {
206 return;
209 aBuilder->AddSeparator();
210 aSeparator = ST_TRUE;
213 JSObject* HTMLMenuElement::WrapNode(JSContext* aCx,
214 JS::Handle<JSObject*> aGivenProto) {
215 return HTMLMenuElement_Binding::Wrap(aCx, this, aGivenProto);
218 } // namespace mozilla::dom