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
) {
28 if (!aVisitor
.mPresContext
) {
32 if (aVisitor
.mEventStatus
== nsEventStatus_eConsumeNoDefault
) {
36 if (!IsMainSummary()) {
40 WidgetEvent
* const event
= aVisitor
.mEvent
;
41 nsCOMPtr
<Element
> target
=
42 do_QueryInterface(event
->GetOriginalDOMEventTarget());
43 if (nsContentUtils::IsInInteractiveHTMLContent(target
, this)) {
47 if (event
->HasMouseEventMessage()) {
48 WidgetMouseEvent
* mouseEvent
= event
->AsMouseEvent();
50 if (mouseEvent
->IsLeftClickEvent()) {
51 RefPtr
<HTMLDetailsElement
> details
= GetDetails();
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
;
63 } // event->HasMouseEventMessage()
65 if (event
->HasKeyEventMessage() && event
->IsTrusted()) {
66 HandleKeyboardActivation(aVisitor
);
71 bool HTMLSummaryElement::IsHTMLFocusable(bool aWithMouse
, bool* aIsFocusable
,
73 bool disallowOverridingFocusability
= nsGenericHTMLElement::IsHTMLFocusable(
74 aWithMouse
, aIsFocusable
, aTabIndex
);
76 if (disallowOverridingFocusability
|| !IsMainSummary()) {
77 return disallowOverridingFocusability
;
80 // The main summary element is focusable.
83 // Give a chance to allow the subclass to override aIsFocusable.
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();
99 return details
->GetFirstSummary() == this ||
100 GetContainingShadow() == details
->GetShadowRoot();
103 HTMLDetailsElement
* HTMLSummaryElement::GetDetails() const {
104 if (auto* details
= HTMLDetailsElement::FromNodeOrNull(GetParent())) {
107 if (!HasBeenInUAWidget()) {
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