no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / dom / html / HTMLSummaryElement.cpp
blobd1fcf225986395ef9a1652094eb0ee2c7b6f924b
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/HTMLSummaryElement.h"
9 #include "mozilla/dom/HTMLDetailsElement.h"
10 #include "mozilla/dom/HTMLElementBinding.h"
11 #include "mozilla/dom/HTMLUnknownElement.h"
12 #include "mozilla/EventDispatcher.h"
13 #include "mozilla/MouseEvents.h"
14 #include "mozilla/Preferences.h"
15 #include "mozilla/TextEvents.h"
16 #include "nsFocusManager.h"
18 NS_IMPL_NS_NEW_HTML_ELEMENT(Summary)
20 namespace mozilla::dom {
22 HTMLSummaryElement::~HTMLSummaryElement() = default;
24 NS_IMPL_ELEMENT_CLONE(HTMLSummaryElement)
26 nsresult HTMLSummaryElement::PostHandleEvent(EventChainPostVisitor& aVisitor) {
27 nsresult rv = NS_OK;
28 if (!aVisitor.mPresContext) {
29 return rv;
32 if (aVisitor.mEventStatus == nsEventStatus_eConsumeNoDefault) {
33 return rv;
36 if (!IsMainSummary()) {
37 return rv;
40 WidgetEvent* const event = aVisitor.mEvent;
41 nsCOMPtr<Element> target =
42 do_QueryInterface(event->GetOriginalDOMEventTarget());
43 if (nsContentUtils::IsInInteractiveHTMLContent(target, this)) {
44 return NS_OK;
47 if (event->HasMouseEventMessage()) {
48 WidgetMouseEvent* mouseEvent = event->AsMouseEvent();
50 if (mouseEvent->IsLeftClickEvent()) {
51 RefPtr<HTMLDetailsElement> details = GetDetails();
52 MOZ_ASSERT(details,
53 "Expected to find details since this is the main summary!");
55 // When dispatching a synthesized mouse click event to a details element
56 // with 'display: none', both Chrome and Safari do not toggle the 'open'
57 // attribute. We had tried to be compatible with this behavior, but found
58 // more inconsistency in test cases in bug 1245424. So we stop doing that.
59 details->ToggleOpen();
60 aVisitor.mEventStatus = nsEventStatus_eConsumeNoDefault;
61 return NS_OK;
63 } // event->HasMouseEventMessage()
65 if (event->HasKeyEventMessage() && event->IsTrusted()) {
66 HandleKeyboardActivation(aVisitor);
68 return rv;
71 bool HTMLSummaryElement::IsHTMLFocusable(bool aWithMouse, bool* aIsFocusable,
72 int32_t* aTabIndex) {
73 bool disallowOverridingFocusability = nsGenericHTMLElement::IsHTMLFocusable(
74 aWithMouse, aIsFocusable, aTabIndex);
76 if (disallowOverridingFocusability || !IsMainSummary()) {
77 return disallowOverridingFocusability;
80 // The main summary element is focusable.
81 *aIsFocusable = true;
83 // Give a chance to allow the subclass to override aIsFocusable.
84 return false;
87 int32_t HTMLSummaryElement::TabIndexDefault() {
88 // Make the main summary be able to navigate via tab, and be focusable.
89 // See nsGenericHTMLElement::IsHTMLFocusable().
90 return IsMainSummary() ? 0 : nsGenericHTMLElement::TabIndexDefault();
93 bool HTMLSummaryElement::IsMainSummary() const {
94 HTMLDetailsElement* details = GetDetails();
95 if (!details) {
96 return false;
99 return details->GetFirstSummary() == this ||
100 GetContainingShadow() == details->GetShadowRoot();
103 HTMLDetailsElement* HTMLSummaryElement::GetDetails() const {
104 if (auto* details = HTMLDetailsElement::FromNodeOrNull(GetParent())) {
105 return details;
107 if (!HasBeenInUAWidget()) {
108 return nullptr;
110 return HTMLDetailsElement::FromNodeOrNull(GetContainingShadowHost());
113 JSObject* HTMLSummaryElement::WrapNode(JSContext* aCx,
114 JS::Handle<JSObject*> aGivenProto) {
115 return HTMLElement_Binding::Wrap(aCx, this, aGivenProto);
118 } // namespace mozilla::dom