Coverage Report

Created: 2018-09-25 14:53

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