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/. */
6 
7 #include "mozilla/dom/HTMLSummaryElement.h"
8 
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"
17 
18 NS_IMPL_NS_NEW_HTML_ELEMENT(Summary)
19 
20 namespace mozilla::dom {
21 
22 HTMLSummaryElement::~HTMLSummaryElement() = default;
23 
NS_IMPL_ELEMENT_CLONE(HTMLSummaryElement)24 NS_IMPL_ELEMENT_CLONE(HTMLSummaryElement)
25 
26 nsresult HTMLSummaryElement::PostHandleEvent(EventChainPostVisitor& aVisitor) {
27   nsresult rv = NS_OK;
28   if (!aVisitor.mPresContext) {
29     return rv;
30   }
31 
32   if (aVisitor.mEventStatus == nsEventStatus_eConsumeNoDefault) {
33     return rv;
34   }
35 
36   if (!IsMainSummary()) {
37     return rv;
38   }
39 
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;
45   }
46 
47   if (event->HasMouseEventMessage()) {
48     WidgetMouseEvent* mouseEvent = event->AsMouseEvent();
49 
50     if (mouseEvent->IsLeftClickEvent()) {
51       RefPtr<HTMLDetailsElement> details = GetDetails();
52       MOZ_ASSERT(details,
53                  "Expected to find details since this is the main summary!");
54 
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;
62     }
63   }  // event->HasMouseEventMessage()
64 
65   HandleKeyboardActivation(aVisitor);
66   return rv;
67 }
68 
IsHTMLFocusable(bool aWithMouse,bool * aIsFocusable,int32_t * aTabIndex)69 bool HTMLSummaryElement::IsHTMLFocusable(bool aWithMouse, bool* aIsFocusable,
70                                          int32_t* aTabIndex) {
71   bool disallowOverridingFocusability = nsGenericHTMLElement::IsHTMLFocusable(
72       aWithMouse, aIsFocusable, aTabIndex);
73 
74   if (disallowOverridingFocusability || !IsMainSummary()) {
75     return disallowOverridingFocusability;
76   }
77 
78   // The main summary element is focusable.
79   *aIsFocusable = true;
80 
81   // Give a chance to allow the subclass to override aIsFocusable.
82   return false;
83 }
84 
TabIndexDefault()85 int32_t HTMLSummaryElement::TabIndexDefault() {
86   // Make the main summary be able to navigate via tab, and be focusable.
87   // See nsGenericHTMLElement::IsHTMLFocusable().
88   return IsMainSummary() ? 0 : nsGenericHTMLElement::TabIndexDefault();
89 }
90 
IsMainSummary() const91 bool HTMLSummaryElement::IsMainSummary() const {
92   HTMLDetailsElement* details = GetDetails();
93   if (!details) {
94     return false;
95   }
96 
97   return details->GetFirstSummary() == this || IsRootOfNativeAnonymousSubtree();
98 }
99 
GetDetails() const100 HTMLDetailsElement* HTMLSummaryElement::GetDetails() const {
101   return HTMLDetailsElement::FromNodeOrNull(GetParent());
102 }
103 
WrapNode(JSContext * aCx,JS::Handle<JSObject * > aGivenProto)104 JSObject* HTMLSummaryElement::WrapNode(JSContext* aCx,
105                                        JS::Handle<JSObject*> aGivenProto) {
106   return HTMLElement_Binding::Wrap(aCx, this, aGivenProto);
107 }
108 
109 }  // namespace mozilla::dom
110