Line data Source code
1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* This Source Code Form is subject to the terms of the Mozilla Public
3 : * License, v. 2.0. If a copy of the MPL was not distributed with this
4 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 :
6 : #include "mozilla/dom/HTMLSummaryElement.h"
7 :
8 : #include "mozilla/dom/HTMLDetailsElement.h"
9 : #include "mozilla/dom/HTMLElementBinding.h"
10 : #include "mozilla/dom/HTMLUnknownElement.h"
11 : #include "mozilla/EventDispatcher.h"
12 : #include "mozilla/MouseEvents.h"
13 : #include "mozilla/Preferences.h"
14 : #include "mozilla/TextEvents.h"
15 : #include "nsFocusManager.h"
16 :
17 0 : NS_IMPL_NS_NEW_HTML_ELEMENT(Summary)
18 :
19 : namespace mozilla {
20 : namespace dom {
21 :
22 0 : HTMLSummaryElement::~HTMLSummaryElement()
23 : {
24 0 : }
25 :
26 0 : NS_IMPL_ELEMENT_CLONE(HTMLSummaryElement)
27 :
28 : nsresult
29 0 : HTMLSummaryElement::PostHandleEvent(EventChainPostVisitor& aVisitor)
30 : {
31 0 : nsresult rv = NS_OK;
32 0 : if (!aVisitor.mPresContext) {
33 0 : return rv;
34 : }
35 :
36 0 : if (aVisitor.mEventStatus == nsEventStatus_eConsumeNoDefault) {
37 0 : return rv;
38 : }
39 :
40 0 : if (!IsMainSummary()) {
41 0 : return rv;
42 : }
43 :
44 0 : WidgetEvent* const event = aVisitor.mEvent;
45 :
46 0 : if (event->HasMouseEventMessage()) {
47 0 : WidgetMouseEvent* mouseEvent = event->AsMouseEvent();
48 :
49 0 : if (mouseEvent->IsLeftClickEvent()) {
50 0 : RefPtr<HTMLDetailsElement> details = GetDetails();
51 0 : MOZ_ASSERT(details,
52 : "Expected to find details since this is the main summary!");
53 :
54 : // When dispatching a synthesized mouse click event to a details element
55 : // with 'display: none', both Chrome and Safari do not toggle the 'open'
56 : // attribute. We had tried to be compatible with this behavior, but found
57 : // more inconsistency in test cases in bug 1245424. So we stop doing that.
58 0 : details->ToggleOpen();
59 0 : aVisitor.mEventStatus = nsEventStatus_eConsumeNoDefault;
60 0 : return NS_OK;
61 : }
62 : } // event->HasMouseEventMessage()
63 :
64 0 : if (event->HasKeyEventMessage()) {
65 0 : WidgetKeyboardEvent* keyboardEvent = event->AsKeyboardEvent();
66 0 : bool dispatchClick = false;
67 :
68 0 : switch (event->mMessage) {
69 : case eKeyPress:
70 0 : if (keyboardEvent->mCharCode == ' ') {
71 : // Consume 'space' key to prevent scrolling the page down.
72 0 : aVisitor.mEventStatus = nsEventStatus_eConsumeNoDefault;
73 : }
74 :
75 0 : dispatchClick = keyboardEvent->mKeyCode == NS_VK_RETURN;
76 0 : break;
77 :
78 : case eKeyUp:
79 0 : dispatchClick = keyboardEvent->mKeyCode == NS_VK_SPACE;
80 0 : break;
81 :
82 : default:
83 0 : break;
84 : }
85 :
86 0 : if (dispatchClick) {
87 0 : rv = DispatchSimulatedClick(this, event->mFlags.mIsTrusted,
88 0 : aVisitor.mPresContext);
89 0 : if (NS_SUCCEEDED(rv)) {
90 0 : aVisitor.mEventStatus = nsEventStatus_eConsumeNoDefault;
91 : }
92 : }
93 : } // event->HasKeyEventMessage()
94 :
95 0 : return rv;
96 : }
97 :
98 : bool
99 0 : HTMLSummaryElement::IsHTMLFocusable(bool aWithMouse, bool* aIsFocusable,
100 : int32_t* aTabIndex)
101 : {
102 : bool disallowOverridingFocusability =
103 0 : nsGenericHTMLElement::IsHTMLFocusable(aWithMouse, aIsFocusable, aTabIndex);
104 :
105 0 : if (disallowOverridingFocusability || !IsMainSummary()) {
106 0 : return disallowOverridingFocusability;
107 : }
108 :
109 : #ifdef XP_MACOSX
110 : // The parent does not have strong opinion about the focusability of this main
111 : // summary element, but we'd like to override it when mouse clicking on Mac OS
112 : // like other form elements.
113 : *aIsFocusable = !aWithMouse || nsFocusManager::sMouseFocusesFormControl;
114 : #else
115 : // The main summary element is focusable on other platforms.
116 0 : *aIsFocusable = true;
117 : #endif
118 :
119 : // Give a chance to allow the subclass to override aIsFocusable.
120 0 : return false;
121 : }
122 :
123 : int32_t
124 0 : HTMLSummaryElement::TabIndexDefault()
125 : {
126 : // Make the main summary be able to navigate via tab, and be focusable.
127 : // See nsGenericHTMLElement::IsHTMLFocusable().
128 0 : return IsMainSummary() ? 0 : nsGenericHTMLElement::TabIndexDefault();
129 : }
130 :
131 : bool
132 0 : HTMLSummaryElement::IsMainSummary() const
133 : {
134 0 : HTMLDetailsElement* details = GetDetails();
135 0 : if (!details) {
136 0 : return false;
137 : }
138 :
139 0 : return details->GetFirstSummary() == this || IsRootOfNativeAnonymousSubtree();
140 : }
141 :
142 : HTMLDetailsElement*
143 0 : HTMLSummaryElement::GetDetails() const
144 : {
145 0 : return HTMLDetailsElement::FromContentOrNull(GetParent());
146 : }
147 :
148 : JSObject*
149 0 : HTMLSummaryElement::WrapNode(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
150 : {
151 0 : return HTMLElementBinding::Wrap(aCx, this, aGivenProto);
152 : }
153 :
154 : } // namespace dom
155 : } // namespace mozilla
|