2015-05-03 19:32:37 +00: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 11:12:37 +00: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 17:31:32 +00:00
|
|
|
|
2013-08-14 06:55:46 +00:00
|
|
|
#include "mozilla/dom/HTMLMenuElement.h"
|
|
|
|
|
2013-09-25 11:21:22 +00:00
|
|
|
#include "mozilla/BasicEvents.h"
|
2014-03-18 04:48:21 +00:00
|
|
|
#include "mozilla/EventDispatcher.h"
|
2013-02-18 11:59:08 +00:00
|
|
|
#include "mozilla/dom/HTMLMenuElementBinding.h"
|
2013-08-14 06:55:46 +00:00
|
|
|
#include "mozilla/dom/HTMLMenuItemElement.h"
|
2014-12-16 16:21:11 +00:00
|
|
|
#include "nsIMenuBuilder.h"
|
2013-02-18 11:59:08 +00:00
|
|
|
#include "nsAttrValueInlines.h"
|
2013-08-14 06:55:46 +00:00
|
|
|
#include "nsContentUtils.h"
|
2013-08-24 02:42:40 +00:00
|
|
|
#include "nsIURI.h"
|
2013-02-18 11:59:08 +00:00
|
|
|
|
2014-12-16 16:21:11 +00:00
|
|
|
#define HTMLMENUBUILDER_CONTRACTID "@mozilla.org/content/html-menu-builder;1"
|
|
|
|
|
2013-02-18 11:59:08 +00:00
|
|
|
NS_IMPL_NS_NEW_HTML_ELEMENT(Menu)
|
2013-02-06 19:19:20 +00:00
|
|
|
|
2013-02-18 11:59:08 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2012-11-14 22:10:08 +00:00
|
|
|
|
2011-08-08 17:31:32 +00:00
|
|
|
enum MenuType
|
|
|
|
{
|
|
|
|
MENU_TYPE_CONTEXT = 1,
|
|
|
|
MENU_TYPE_TOOLBAR,
|
|
|
|
MENU_TYPE_LIST
|
|
|
|
};
|
|
|
|
|
|
|
|
static const nsAttrValue::EnumTable kMenuTypeTable[] = {
|
|
|
|
{ "context", MENU_TYPE_CONTEXT },
|
|
|
|
{ "toolbar", MENU_TYPE_TOOLBAR },
|
|
|
|
{ "list", MENU_TYPE_LIST },
|
|
|
|
{ 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
static const nsAttrValue::EnumTable* kMenuDefaultType =
|
|
|
|
&kMenuTypeTable[2];
|
|
|
|
|
|
|
|
enum SeparatorType
|
|
|
|
{
|
|
|
|
ST_TRUE_INIT = -1,
|
|
|
|
ST_FALSE = 0,
|
|
|
|
ST_TRUE = 1
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-06-20 02:01:40 +00:00
|
|
|
HTMLMenuElement::HTMLMenuElement(already_AddRefed<mozilla::dom::NodeInfo>& aNodeInfo)
|
2011-08-08 17:31:32 +00:00
|
|
|
: nsGenericHTMLElement(aNodeInfo), mType(MENU_TYPE_LIST)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-02-18 11:59:08 +00:00
|
|
|
HTMLMenuElement::~HTMLMenuElement()
|
2011-08-08 17:31:32 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-04-27 07:06:00 +00:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED(HTMLMenuElement, nsGenericHTMLElement,
|
|
|
|
nsIDOMHTMLMenuElement, nsIHTMLMenu)
|
2011-08-08 17:31:32 +00:00
|
|
|
|
2013-02-18 11:59:08 +00:00
|
|
|
NS_IMPL_ELEMENT_CLONE(HTMLMenuElement)
|
2011-08-08 17:31:32 +00:00
|
|
|
|
2013-02-18 11:59:08 +00:00
|
|
|
NS_IMPL_BOOL_ATTR(HTMLMenuElement, Compact, compact)
|
|
|
|
NS_IMPL_ENUM_ATTR_DEFAULT_VALUE(HTMLMenuElement, Type, type,
|
2011-08-08 17:31:32 +00:00
|
|
|
kMenuDefaultType->tag)
|
2013-02-18 11:59:08 +00:00
|
|
|
NS_IMPL_STRING_ATTR(HTMLMenuElement, Label, label)
|
2011-08-08 17:31:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-02-18 11:59:08 +00:00
|
|
|
HTMLMenuElement::SendShowEvent()
|
2011-08-08 17:31:32 +00:00
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(nsContentUtils::IsCallerChrome(), NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
|
2014-10-02 19:07:24 +00:00
|
|
|
nsCOMPtr<nsIDocument> document = GetComposedDoc();
|
2011-08-08 17:31:32 +00:00
|
|
|
if (!document) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-09-14 15:14:35 +00:00
|
|
|
WidgetEvent event(true, eShow);
|
2012-12-16 01:26:03 +00:00
|
|
|
event.mFlags.mBubbles = false;
|
|
|
|
event.mFlags.mCancelable = false;
|
2011-08-08 17:31:32 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIPresShell> shell = document->GetShell();
|
|
|
|
if (!shell) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<nsPresContext> presContext = shell->GetPresContext();
|
2011-08-08 17:31:32 +00:00
|
|
|
nsEventStatus status = nsEventStatus_eIgnore;
|
2014-03-18 04:48:21 +00:00
|
|
|
EventDispatcher::Dispatch(static_cast<nsIContent*>(this), presContext,
|
|
|
|
&event, nullptr, &status);
|
2011-08-08 17:31:32 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-02-18 11:59:08 +00:00
|
|
|
HTMLMenuElement::CreateBuilder(nsIMenuBuilder** _retval)
|
2011-08-08 17:31:32 +00:00
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(nsContentUtils::IsCallerChrome(), NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
|
2014-12-16 16:21:11 +00:00
|
|
|
nsCOMPtr<nsIMenuBuilder> builder = CreateBuilder();
|
|
|
|
builder.swap(*_retval);
|
2011-08-08 17:31:32 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-02-18 11:59:08 +00:00
|
|
|
already_AddRefed<nsIMenuBuilder>
|
|
|
|
HTMLMenuElement::CreateBuilder()
|
|
|
|
{
|
|
|
|
if (mType != MENU_TYPE_CONTEXT) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-12-16 16:21:11 +00:00
|
|
|
nsCOMPtr<nsIMenuBuilder> builder = do_CreateInstance(HTMLMENUBUILDER_CONTRACTID);
|
2014-12-22 18:29:23 +00:00
|
|
|
NS_WARN_IF_FALSE(builder, "No builder available");
|
2014-12-16 16:21:11 +00:00
|
|
|
return builder.forget();
|
2013-02-18 11:59:08 +00:00
|
|
|
}
|
2011-08-08 17:31:32 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-02-18 11:59:08 +00:00
|
|
|
HTMLMenuElement::Build(nsIMenuBuilder* aBuilder)
|
2011-08-08 17:31:32 +00:00
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(nsContentUtils::IsCallerChrome(), NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
|
|
|
|
if (!aBuilder) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
BuildSubmenu(EmptyString(), this, aBuilder);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2013-02-18 11:59:08 +00:00
|
|
|
HTMLMenuElement::ParseAttribute(int32_t aNamespaceID,
|
|
|
|
nsIAtom* aAttribute,
|
|
|
|
const nsAString& aValue,
|
|
|
|
nsAttrValue& aResult)
|
2011-08-08 17:31:32 +00:00
|
|
|
{
|
|
|
|
if (aNamespaceID == kNameSpaceID_None && aAttribute == nsGkAtoms::type) {
|
2011-09-29 06:19:26 +00:00
|
|
|
bool success = aResult.ParseEnumValue(aValue, kMenuTypeTable,
|
2011-10-17 14:59:28 +00:00
|
|
|
false);
|
2011-08-08 17:31:32 +00:00
|
|
|
if (success) {
|
|
|
|
mType = aResult.GetEnumValue();
|
|
|
|
} else {
|
|
|
|
mType = kMenuDefaultType->value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nsGenericHTMLElement::ParseAttribute(aNamespaceID, aAttribute, aValue,
|
|
|
|
aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-02-18 11:59:08 +00:00
|
|
|
HTMLMenuElement::BuildSubmenu(const nsAString& aLabel,
|
|
|
|
nsIContent* aContent,
|
|
|
|
nsIMenuBuilder* aBuilder)
|
2011-08-08 17:31:32 +00:00
|
|
|
{
|
|
|
|
aBuilder->OpenContainer(aLabel);
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int8_t separator = ST_TRUE_INIT;
|
2011-08-08 17:31:32 +00:00
|
|
|
TraverseContent(aContent, aBuilder, separator);
|
|
|
|
|
|
|
|
if (separator == ST_TRUE) {
|
|
|
|
aBuilder->UndoAddSeparator();
|
|
|
|
}
|
|
|
|
|
|
|
|
aBuilder->CloseContainer();
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2013-02-18 11:59:08 +00:00
|
|
|
HTMLMenuElement::CanLoadIcon(nsIContent* aContent, const nsAString& aIcon)
|
2011-08-08 17:31:32 +00:00
|
|
|
{
|
|
|
|
if (aIcon.IsEmpty()) {
|
2011-10-17 14:59:28 +00:00
|
|
|
return false;
|
2011-08-08 17:31:32 +00:00
|
|
|
}
|
|
|
|
|
2011-10-18 10:53:36 +00:00
|
|
|
nsIDocument* doc = aContent->OwnerDoc();
|
2011-08-08 17:31:32 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> baseURI = aContent->GetBaseURI();
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
nsContentUtils::NewURIWithDocumentCharset(getter_AddRefs(uri), aIcon, doc,
|
|
|
|
baseURI);
|
|
|
|
|
|
|
|
if (!uri) {
|
2011-10-17 14:59:28 +00:00
|
|
|
return false;
|
2011-08-08 17:31:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nsContentUtils::CanLoadImage(uri, aContent, doc,
|
|
|
|
aContent->NodePrincipal());
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-02-18 11:59:08 +00:00
|
|
|
HTMLMenuElement::TraverseContent(nsIContent* aContent,
|
|
|
|
nsIMenuBuilder* aBuilder,
|
|
|
|
int8_t& aSeparator)
|
2011-08-08 17:31:32 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIContent> child;
|
|
|
|
for (child = aContent->GetFirstChild(); child;
|
|
|
|
child = child->GetNextSibling()) {
|
|
|
|
nsGenericHTMLElement* element = nsGenericHTMLElement::FromContent(child);
|
|
|
|
if (!element) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-03-03 11:09:00 +00:00
|
|
|
if (child->IsHTMLElement(nsGkAtoms::menuitem)) {
|
2013-02-18 10:05:52 +00:00
|
|
|
HTMLMenuItemElement* menuitem = HTMLMenuItemElement::FromContent(child);
|
2011-08-08 17:31:32 +00: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;
|
2015-03-03 11:09:00 +00:00
|
|
|
} else if (child->IsHTMLElement(nsGkAtoms::menu) && !element->IsHidden()) {
|
2011-08-08 17:31:32 +00:00
|
|
|
if (child->HasAttr(kNameSpaceID_None, nsGkAtoms::label)) {
|
|
|
|
nsAutoString label;
|
|
|
|
child->GetAttr(kNameSpaceID_None, nsGkAtoms::label, label);
|
|
|
|
|
|
|
|
BuildSubmenu(label, child, aBuilder);
|
|
|
|
|
|
|
|
aSeparator = ST_FALSE;
|
|
|
|
} else {
|
|
|
|
AddSeparator(aBuilder, aSeparator);
|
|
|
|
|
|
|
|
TraverseContent(child, aBuilder, aSeparator);
|
|
|
|
|
|
|
|
AddSeparator(aBuilder, aSeparator);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
2013-02-18 11:59:08 +00:00
|
|
|
HTMLMenuElement::AddSeparator(nsIMenuBuilder* aBuilder, int8_t& aSeparator)
|
2011-08-08 17:31:32 +00:00
|
|
|
{
|
|
|
|
if (aSeparator) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
aBuilder->AddSeparator();
|
|
|
|
aSeparator = ST_TRUE;
|
|
|
|
}
|
2013-02-18 11:59:08 +00:00
|
|
|
|
2013-02-18 11:59:08 +00: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 14:13:33 +00:00
|
|
|
HTMLMenuElement::WrapNode(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
2013-02-18 11:59:08 +00: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 14:13:33 +00:00
|
|
|
return HTMLMenuElementBinding::Wrap(aCx, this, aGivenProto);
|
2013-02-18 11:59:08 +00:00
|
|
|
}
|
|
|
|
|
2013-02-18 11:59:08 +00:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|