HTMLMenuElement.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "mozilla/dom/HTMLMenuElement.h"
  6. #include "mozilla/BasicEvents.h"
  7. #include "mozilla/EventDispatcher.h"
  8. #include "mozilla/dom/HTMLMenuElementBinding.h"
  9. #include "mozilla/dom/HTMLMenuItemElement.h"
  10. #include "nsIMenuBuilder.h"
  11. #include "nsAttrValueInlines.h"
  12. #include "nsContentUtils.h"
  13. #include "nsIURI.h"
  14. #define HTMLMENUBUILDER_CONTRACTID "@mozilla.org/content/html-menu-builder;1"
  15. NS_IMPL_NS_NEW_HTML_ELEMENT(Menu)
  16. namespace mozilla {
  17. namespace dom {
  18. enum MenuType : uint8_t
  19. {
  20. MENU_TYPE_CONTEXT = 1,
  21. MENU_TYPE_TOOLBAR,
  22. MENU_TYPE_LIST
  23. };
  24. static const nsAttrValue::EnumTable kMenuTypeTable[] = {
  25. { "context", MENU_TYPE_CONTEXT },
  26. { "toolbar", MENU_TYPE_TOOLBAR },
  27. { "list", MENU_TYPE_LIST },
  28. { nullptr, 0 }
  29. };
  30. static const nsAttrValue::EnumTable* kMenuDefaultType =
  31. &kMenuTypeTable[2];
  32. enum SeparatorType
  33. {
  34. ST_TRUE_INIT = -1,
  35. ST_FALSE = 0,
  36. ST_TRUE = 1
  37. };
  38. HTMLMenuElement::HTMLMenuElement(already_AddRefed<mozilla::dom::NodeInfo>& aNodeInfo)
  39. : nsGenericHTMLElement(aNodeInfo), mType(MENU_TYPE_LIST)
  40. {
  41. }
  42. HTMLMenuElement::~HTMLMenuElement()
  43. {
  44. }
  45. NS_IMPL_ISUPPORTS_INHERITED(HTMLMenuElement, nsGenericHTMLElement,
  46. nsIDOMHTMLMenuElement, nsIHTMLMenu)
  47. NS_IMPL_ELEMENT_CLONE(HTMLMenuElement)
  48. NS_IMPL_BOOL_ATTR(HTMLMenuElement, Compact, compact)
  49. NS_IMPL_ENUM_ATTR_DEFAULT_VALUE(HTMLMenuElement, Type, type,
  50. kMenuDefaultType->tag)
  51. NS_IMPL_STRING_ATTR(HTMLMenuElement, Label, label)
  52. NS_IMETHODIMP
  53. HTMLMenuElement::SendShowEvent()
  54. {
  55. NS_ENSURE_TRUE(nsContentUtils::IsCallerChrome(), NS_ERROR_DOM_SECURITY_ERR);
  56. nsCOMPtr<nsIDocument> document = GetComposedDoc();
  57. if (!document) {
  58. return NS_ERROR_FAILURE;
  59. }
  60. WidgetEvent event(true, eShow);
  61. event.mFlags.mBubbles = false;
  62. event.mFlags.mCancelable = false;
  63. nsCOMPtr<nsIPresShell> shell = document->GetShell();
  64. if (!shell) {
  65. return NS_ERROR_FAILURE;
  66. }
  67. RefPtr<nsPresContext> presContext = shell->GetPresContext();
  68. nsEventStatus status = nsEventStatus_eIgnore;
  69. EventDispatcher::Dispatch(static_cast<nsIContent*>(this), presContext,
  70. &event, nullptr, &status);
  71. return NS_OK;
  72. }
  73. NS_IMETHODIMP
  74. HTMLMenuElement::CreateBuilder(nsIMenuBuilder** _retval)
  75. {
  76. NS_ENSURE_TRUE(nsContentUtils::IsCallerChrome(), NS_ERROR_DOM_SECURITY_ERR);
  77. nsCOMPtr<nsIMenuBuilder> builder = CreateBuilder();
  78. builder.swap(*_retval);
  79. return NS_OK;
  80. }
  81. already_AddRefed<nsIMenuBuilder>
  82. HTMLMenuElement::CreateBuilder()
  83. {
  84. if (mType != MENU_TYPE_CONTEXT) {
  85. return nullptr;
  86. }
  87. nsCOMPtr<nsIMenuBuilder> builder = do_CreateInstance(HTMLMENUBUILDER_CONTRACTID);
  88. NS_WARNING_ASSERTION(builder, "No builder available");
  89. return builder.forget();
  90. }
  91. NS_IMETHODIMP
  92. HTMLMenuElement::Build(nsIMenuBuilder* aBuilder)
  93. {
  94. NS_ENSURE_TRUE(nsContentUtils::IsCallerChrome(), NS_ERROR_DOM_SECURITY_ERR);
  95. if (!aBuilder) {
  96. return NS_OK;
  97. }
  98. BuildSubmenu(EmptyString(), this, aBuilder);
  99. return NS_OK;
  100. }
  101. bool
  102. HTMLMenuElement::ParseAttribute(int32_t aNamespaceID,
  103. nsIAtom* aAttribute,
  104. const nsAString& aValue,
  105. nsAttrValue& aResult)
  106. {
  107. if (aNamespaceID == kNameSpaceID_None && aAttribute == nsGkAtoms::type &&
  108. Preferences::GetBool("dom.menuitem.enabled")) {
  109. bool success = aResult.ParseEnumValue(aValue, kMenuTypeTable, false);
  110. if (success) {
  111. mType = aResult.GetEnumValue();
  112. } else {
  113. mType = kMenuDefaultType->value;
  114. }
  115. return success;
  116. }
  117. return nsGenericHTMLElement::ParseAttribute(aNamespaceID, aAttribute, aValue,
  118. aResult);
  119. }
  120. void
  121. HTMLMenuElement::BuildSubmenu(const nsAString& aLabel,
  122. nsIContent* aContent,
  123. nsIMenuBuilder* aBuilder)
  124. {
  125. aBuilder->OpenContainer(aLabel);
  126. int8_t separator = ST_TRUE_INIT;
  127. TraverseContent(aContent, aBuilder, separator);
  128. if (separator == ST_TRUE) {
  129. aBuilder->UndoAddSeparator();
  130. }
  131. aBuilder->CloseContainer();
  132. }
  133. // static
  134. bool
  135. HTMLMenuElement::CanLoadIcon(nsIContent* aContent, const nsAString& aIcon)
  136. {
  137. if (aIcon.IsEmpty()) {
  138. return false;
  139. }
  140. nsIDocument* doc = aContent->OwnerDoc();
  141. nsCOMPtr<nsIURI> baseURI = aContent->GetBaseURI();
  142. nsCOMPtr<nsIURI> uri;
  143. nsContentUtils::NewURIWithDocumentCharset(getter_AddRefs(uri), aIcon, doc,
  144. baseURI);
  145. if (!uri) {
  146. return false;
  147. }
  148. return nsContentUtils::CanLoadImage(uri, aContent, doc,
  149. aContent->NodePrincipal());
  150. }
  151. void
  152. HTMLMenuElement::TraverseContent(nsIContent* aContent,
  153. nsIMenuBuilder* aBuilder,
  154. int8_t& aSeparator)
  155. {
  156. nsCOMPtr<nsIContent> child;
  157. for (child = aContent->GetFirstChild(); child;
  158. child = child->GetNextSibling()) {
  159. nsGenericHTMLElement* element = nsGenericHTMLElement::FromContent(child);
  160. if (!element) {
  161. continue;
  162. }
  163. if (child->IsHTMLElement(nsGkAtoms::menuitem)) {
  164. HTMLMenuItemElement* menuitem = HTMLMenuItemElement::FromContent(child);
  165. if (menuitem->IsHidden()) {
  166. continue;
  167. }
  168. nsAutoString label;
  169. menuitem->GetLabel(label);
  170. if (label.IsEmpty()) {
  171. continue;
  172. }
  173. nsAutoString icon;
  174. menuitem->GetIcon(icon);
  175. aBuilder->AddItemFor(menuitem, CanLoadIcon(child, icon));
  176. aSeparator = ST_FALSE;
  177. } else if (child->IsHTMLElement(nsGkAtoms::hr)) {
  178. aBuilder->AddSeparator();
  179. } else if (child->IsHTMLElement(nsGkAtoms::menu) && !element->IsHidden()) {
  180. if (child->HasAttr(kNameSpaceID_None, nsGkAtoms::label)) {
  181. nsAutoString label;
  182. child->GetAttr(kNameSpaceID_None, nsGkAtoms::label, label);
  183. BuildSubmenu(label, child, aBuilder);
  184. aSeparator = ST_FALSE;
  185. } else {
  186. AddSeparator(aBuilder, aSeparator);
  187. TraverseContent(child, aBuilder, aSeparator);
  188. AddSeparator(aBuilder, aSeparator);
  189. }
  190. }
  191. }
  192. }
  193. inline void
  194. HTMLMenuElement::AddSeparator(nsIMenuBuilder* aBuilder, int8_t& aSeparator)
  195. {
  196. if (aSeparator) {
  197. return;
  198. }
  199. aBuilder->AddSeparator();
  200. aSeparator = ST_TRUE;
  201. }
  202. JSObject*
  203. HTMLMenuElement::WrapNode(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
  204. {
  205. return HTMLMenuElementBinding::Wrap(aCx, this, aGivenProto);
  206. }
  207. } // namespace dom
  208. } // namespace mozilla