2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-08-08 21:31:32 +04:00
|
|
|
|
2013-08-14 10:55:46 +04:00
|
|
|
#include "mozilla/dom/HTMLMenuElement.h"
|
|
|
|
|
2013-09-25 15:21:22 +04:00
|
|
|
#include "mozilla/BasicEvents.h"
|
2014-03-18 08:48:21 +04:00
|
|
|
#include "mozilla/EventDispatcher.h"
|
2013-02-18 15:59:08 +04:00
|
|
|
#include "mozilla/dom/HTMLMenuElementBinding.h"
|
2013-08-14 10:55:46 +04:00
|
|
|
#include "mozilla/dom/HTMLMenuItemElement.h"
|
2014-12-16 19:21:11 +03:00
|
|
|
#include "nsIMenuBuilder.h"
|
2013-02-18 15:59:08 +04:00
|
|
|
#include "nsAttrValueInlines.h"
|
2013-08-14 10:55:46 +04:00
|
|
|
#include "nsContentUtils.h"
|
2013-08-24 06:42:40 +04:00
|
|
|
#include "nsIURI.h"
|
2013-02-18 15:59:08 +04:00
|
|
|
|
2014-12-16 19:21:11 +03:00
|
|
|
#define HTMLMENUBUILDER_CONTRACTID "@mozilla.org/content/html-menu-builder;1"
|
|
|
|
|
2013-02-18 15:59:08 +04:00
|
|
|
NS_IMPL_NS_NEW_HTML_ELEMENT(Menu)
|
2013-02-06 23:19:20 +04:00
|
|
|
|
2013-02-18 15:59:08 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2012-11-15 02:10:08 +04:00
|
|
|
|
2016-09-07 05:20:17 +03:00
|
|
|
enum MenuType : uint8_t
|
2011-08-08 21:31:32 +04:00
|
|
|
{
|
|
|
|
MENU_TYPE_CONTEXT = 1,
|
2017-05-25 05:11:07 +03:00
|
|
|
MENU_TYPE_TOOLBAR
|
2011-08-08 21:31:32 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
static const nsAttrValue::EnumTable kMenuTypeTable[] = {
|
|
|
|
{ "context", MENU_TYPE_CONTEXT },
|
|
|
|
{ "toolbar", MENU_TYPE_TOOLBAR },
|
2016-09-07 05:20:17 +03:00
|
|
|
{ nullptr, 0 }
|
2011-08-08 21:31:32 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
static const nsAttrValue::EnumTable* kMenuDefaultType =
|
2017-05-25 05:11:07 +03:00
|
|
|
&kMenuTypeTable[1];
|
2011-08-08 21:31:32 +04:00
|
|
|
|
|
|
|
enum SeparatorType
|
|
|
|
{
|
|
|
|
ST_TRUE_INIT = -1,
|
|
|
|
ST_FALSE = 0,
|
|
|
|
ST_TRUE = 1
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-06-20 06:01:40 +04:00
|
|
|
HTMLMenuElement::HTMLMenuElement(already_AddRefed<mozilla::dom::NodeInfo>& aNodeInfo)
|
2017-05-25 05:11:07 +03:00
|
|
|
: nsGenericHTMLElement(aNodeInfo), mType(MENU_TYPE_TOOLBAR)
|
2011-08-08 21:31:32 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-02-18 15:59:08 +04:00
|
|
|
HTMLMenuElement::~HTMLMenuElement()
|
2011-08-08 21:31:32 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-08-14 19:31:47 +03:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED0(HTMLMenuElement, nsGenericHTMLElement)
|
2011-08-08 21:31:32 +04:00
|
|
|
|
2013-02-18 15:59:08 +04:00
|
|
|
NS_IMPL_ELEMENT_CLONE(HTMLMenuElement)
|
2011-08-08 21:31:32 +04:00
|
|
|
|
|
|
|
|
2016-11-15 08:18:33 +03:00
|
|
|
void
|
2013-02-18 15:59:08 +04:00
|
|
|
HTMLMenuElement::SendShowEvent()
|
2011-08-08 21:31:32 +04:00
|
|
|
{
|
2014-10-02 23:07:24 +04:00
|
|
|
nsCOMPtr<nsIDocument> document = GetComposedDoc();
|
2011-08-08 21:31:32 +04:00
|
|
|
if (!document) {
|
2016-11-15 08:18:33 +03:00
|
|
|
return;
|
2011-08-08 21:31:32 +04:00
|
|
|
}
|
|
|
|
|
2015-09-14 18:14:35 +03:00
|
|
|
WidgetEvent event(true, eShow);
|
2012-12-16 05:26:03 +04:00
|
|
|
event.mFlags.mBubbles = false;
|
|
|
|
event.mFlags.mCancelable = false;
|
2011-08-08 21:31:32 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIPresShell> shell = document->GetShell();
|
|
|
|
if (!shell) {
|
2016-11-15 08:18:33 +03:00
|
|
|
return;
|
2011-08-08 21:31:32 +04:00
|
|
|
}
|
2017-05-25 05:11:07 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsPresContext> presContext = shell->GetPresContext();
|
2011-08-08 21:31:32 +04:00
|
|
|
nsEventStatus status = nsEventStatus_eIgnore;
|
2014-03-18 08:48:21 +04:00
|
|
|
EventDispatcher::Dispatch(static_cast<nsIContent*>(this), presContext,
|
|
|
|
&event, nullptr, &status);
|
2011-08-08 21:31:32 +04:00
|
|
|
}
|
|
|
|
|
2013-02-18 15:59:08 +04:00
|
|
|
already_AddRefed<nsIMenuBuilder>
|
|
|
|
HTMLMenuElement::CreateBuilder()
|
|
|
|
{
|
|
|
|
if (mType != MENU_TYPE_CONTEXT) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-12-16 19:21:11 +03:00
|
|
|
nsCOMPtr<nsIMenuBuilder> builder = do_CreateInstance(HTMLMENUBUILDER_CONTRACTID);
|
2016-09-01 08:01:16 +03:00
|
|
|
NS_WARNING_ASSERTION(builder, "No builder available");
|
2014-12-16 19:21:11 +03:00
|
|
|
return builder.forget();
|
2013-02-18 15:59:08 +04:00
|
|
|
}
|
2011-08-08 21:31:32 +04:00
|
|
|
|
2016-11-15 08:18:33 +03:00
|
|
|
void
|
2013-02-18 15:59:08 +04:00
|
|
|
HTMLMenuElement::Build(nsIMenuBuilder* aBuilder)
|
2011-08-08 21:31:32 +04:00
|
|
|
{
|
|
|
|
if (!aBuilder) {
|
2016-11-15 08:18:33 +03:00
|
|
|
return;
|
2011-08-08 21:31:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
BuildSubmenu(EmptyString(), this, aBuilder);
|
|
|
|
}
|
|
|
|
|
2017-04-20 07:35:19 +03:00
|
|
|
nsresult
|
2017-10-03 01:05:19 +03:00
|
|
|
HTMLMenuElement::AfterSetAttr(int32_t aNameSpaceID, nsAtom* aName,
|
2017-05-19 00:09:01 +03:00
|
|
|
const nsAttrValue* aValue,
|
2017-10-10 00:33:38 +03:00
|
|
|
const nsAttrValue* aOldValue,
|
|
|
|
nsIPrincipal* aSubjectPrincipal,
|
|
|
|
bool aNotify)
|
2017-04-20 07:35:19 +03:00
|
|
|
{
|
|
|
|
if (aNameSpaceID == kNameSpaceID_None && aName == nsGkAtoms::type) {
|
|
|
|
if (aValue) {
|
|
|
|
mType = aValue->GetEnumValue();
|
|
|
|
} else {
|
|
|
|
mType = kMenuDefaultType->value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nsGenericHTMLElement::AfterSetAttr(aNameSpaceID, aName, aValue,
|
2017-10-10 00:33:38 +03:00
|
|
|
aOldValue, aSubjectPrincipal, aNotify);
|
2017-04-20 07:35:19 +03:00
|
|
|
}
|
2011-08-08 21:31:32 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2013-02-18 15:59:08 +04:00
|
|
|
HTMLMenuElement::ParseAttribute(int32_t aNamespaceID,
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom* aAttribute,
|
2013-02-18 15:59:08 +04:00
|
|
|
const nsAString& aValue,
|
2017-11-02 06:35:52 +03:00
|
|
|
nsIPrincipal* aMaybeScriptedPrincipal,
|
2013-02-18 15:59:08 +04:00
|
|
|
nsAttrValue& aResult)
|
2011-08-08 21:31:32 +04:00
|
|
|
{
|
|
|
|
if (aNamespaceID == kNameSpaceID_None && aAttribute == nsGkAtoms::type) {
|
2017-04-20 07:35:19 +03:00
|
|
|
return aResult.ParseEnumValue(aValue, kMenuTypeTable, false,
|
|
|
|
kMenuDefaultType);
|
2011-08-08 21:31:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nsGenericHTMLElement::ParseAttribute(aNamespaceID, aAttribute, aValue,
|
2017-11-02 06:35:52 +03:00
|
|
|
aMaybeScriptedPrincipal, aResult);
|
2011-08-08 21:31:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-02-18 15:59:08 +04:00
|
|
|
HTMLMenuElement::BuildSubmenu(const nsAString& aLabel,
|
|
|
|
nsIContent* aContent,
|
|
|
|
nsIMenuBuilder* aBuilder)
|
2011-08-08 21:31:32 +04:00
|
|
|
{
|
|
|
|
aBuilder->OpenContainer(aLabel);
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int8_t separator = ST_TRUE_INIT;
|
2011-08-08 21:31:32 +04:00
|
|
|
TraverseContent(aContent, aBuilder, separator);
|
|
|
|
|
|
|
|
if (separator == ST_TRUE) {
|
|
|
|
aBuilder->UndoAddSeparator();
|
|
|
|
}
|
|
|
|
|
|
|
|
aBuilder->CloseContainer();
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2013-02-18 15:59:08 +04:00
|
|
|
HTMLMenuElement::CanLoadIcon(nsIContent* aContent, const nsAString& aIcon)
|
2011-08-08 21:31:32 +04:00
|
|
|
{
|
|
|
|
if (aIcon.IsEmpty()) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2011-08-08 21:31:32 +04:00
|
|
|
}
|
|
|
|
|
2011-10-18 14:53:36 +04:00
|
|
|
nsIDocument* doc = aContent->OwnerDoc();
|
2011-08-08 21:31:32 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> baseURI = aContent->GetBaseURI();
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
nsContentUtils::NewURIWithDocumentCharset(getter_AddRefs(uri), aIcon, doc,
|
|
|
|
baseURI);
|
|
|
|
|
|
|
|
if (!uri) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2011-08-08 21:31:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nsContentUtils::CanLoadImage(uri, aContent, doc,
|
|
|
|
aContent->NodePrincipal());
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-02-18 15:59:08 +04:00
|
|
|
HTMLMenuElement::TraverseContent(nsIContent* aContent,
|
|
|
|
nsIMenuBuilder* aBuilder,
|
|
|
|
int8_t& aSeparator)
|
2011-08-08 21:31:32 +04:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIContent> child;
|
|
|
|
for (child = aContent->GetFirstChild(); child;
|
|
|
|
child = child->GetNextSibling()) {
|
|
|
|
nsGenericHTMLElement* element = nsGenericHTMLElement::FromContent(child);
|
|
|
|
if (!element) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-03-03 14:09:00 +03:00
|
|
|
if (child->IsHTMLElement(nsGkAtoms::menuitem)) {
|
2013-02-18 14:05:52 +04:00
|
|
|
HTMLMenuItemElement* menuitem = HTMLMenuItemElement::FromContent(child);
|
2011-08-08 21:31:32 +04:00
|
|
|
|
|
|
|
if (menuitem->IsHidden()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString label;
|
|
|
|
menuitem->GetLabel(label);
|
|
|
|
if (label.IsEmpty()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString icon;
|
|
|
|
menuitem->GetIcon(icon);
|
|
|
|
|
|
|
|
aBuilder->AddItemFor(menuitem, CanLoadIcon(child, icon));
|
|
|
|
|
|
|
|
aSeparator = ST_FALSE;
|
2016-08-20 02:57:54 +03:00
|
|
|
} else if (child->IsHTMLElement(nsGkAtoms::hr)) {
|
|
|
|
aBuilder->AddSeparator();
|
2015-03-03 14:09:00 +03:00
|
|
|
} else if (child->IsHTMLElement(nsGkAtoms::menu) && !element->IsHidden()) {
|
2017-12-07 21:13:50 +03:00
|
|
|
if (child->AsElement()->HasAttr(kNameSpaceID_None, nsGkAtoms::label)) {
|
2011-08-08 21:31:32 +04:00
|
|
|
nsAutoString label;
|
2017-12-07 21:13:50 +03:00
|
|
|
child->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::label, label);
|
2011-08-08 21:31:32 +04:00
|
|
|
|
|
|
|
BuildSubmenu(label, child, aBuilder);
|
|
|
|
|
|
|
|
aSeparator = ST_FALSE;
|
|
|
|
} else {
|
|
|
|
AddSeparator(aBuilder, aSeparator);
|
|
|
|
|
|
|
|
TraverseContent(child, aBuilder, aSeparator);
|
|
|
|
|
|
|
|
AddSeparator(aBuilder, aSeparator);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
2013-02-18 15:59:08 +04:00
|
|
|
HTMLMenuElement::AddSeparator(nsIMenuBuilder* aBuilder, int8_t& aSeparator)
|
2011-08-08 21:31:32 +04:00
|
|
|
{
|
|
|
|
if (aSeparator) {
|
|
|
|
return;
|
|
|
|
}
|
2017-05-25 05:11:07 +03:00
|
|
|
|
2011-08-08 21:31:32 +04:00
|
|
|
aBuilder->AddSeparator();
|
|
|
|
aSeparator = ST_TRUE;
|
|
|
|
}
|
2013-02-18 15:59:08 +04:00
|
|
|
|
2013-02-18 15:59:08 +04:00
|
|
|
JSObject*
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
|
|
|
HTMLMenuElement::WrapNode(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
2013-02-18 15:59:08 +04:00
|
|
|
{
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
|
|
|
return HTMLMenuElementBinding::Wrap(aCx, this, aGivenProto);
|
2013-02-18 15:59:08 +04:00
|
|
|
}
|
|
|
|
|
2013-02-18 15:59:08 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|