HTML - added support toggle <details> and <summary> by keyboard

This commit is contained in:
janekptacijarabaci
2017-04-19 18:36:59 +02:00
committed by roytam1
parent 346f3becf3
commit caf3d26cc4
11 changed files with 216 additions and 59 deletions
+86 -20
View File
@@ -19,6 +19,7 @@
#include "mozilla/MouseEvents.h"
#include "mozilla/TextEvents.h"
#include "nsPresState.h"
#include "nsFocusManager.h"
NS_IMPL_NS_NEW_HTML_ELEMENT(Summary)
@@ -48,34 +49,99 @@ HTMLSummaryElement::PostHandleEvent(EventChainPostVisitor& aVisitor)
return rv;
}
auto toggleDetails = false;
auto* event = aVisitor.mEvent;
if (event->HasMouseEventMessage()) {
auto* mouseEvent = event->AsMouseEvent();
toggleDetails = mouseEvent->IsLeftClickEvent();
}
// Todo: Bug 634004: Implement toggle details by keyboard.
if (!toggleDetails || !IsMainSummary()) {
if (!IsMainSummary()) {
return rv;
}
auto* details = GetDetails();
MOZ_ASSERT(details, "Expected to find details since this is the main summary!");
WidgetEvent* const event = aVisitor.mEvent;
// When dispatching a synthesized mouse click event to a details with
// 'display: none', both Chrome and Safari do not toggle the 'open' attribute.
// We follow them by checking whether details has a frame or not.
if (details->GetPrimaryFrame()) {
details->ToggleOpen();
aVisitor.mEventStatus = nsEventStatus_eConsumeNoDefault;
}
if (event->HasMouseEventMessage()) {
WidgetMouseEvent* mouseEvent = event->AsMouseEvent();
if (mouseEvent->IsLeftClickEvent()) {
RefPtr<HTMLDetailsElement> details = GetDetails();
MOZ_ASSERT(details,
"Expected to find details since this is the main summary!");
// When dispatching a synthesized mouse click event to a details element
// with 'display: none', both Chrome and Safari do not toggle the 'open'
// attribute. We follow them by checking whether the details element has a
// frame or not.
if (details->GetPrimaryFrame(Flush_Frames)) {
details->ToggleOpen();
aVisitor.mEventStatus = nsEventStatus_eConsumeNoDefault;
return NS_OK;
}
}
} // event->HasMouseEventMessage()
if (event->HasKeyEventMessage()) {
WidgetKeyboardEvent* keyboardEvent = event->AsKeyboardEvent();
bool dispatchClick = false;
switch (event->message) {
case NS_KEY_PRESS:
if (keyboardEvent->charCode == nsIDOMKeyEvent::DOM_VK_SPACE) {
// Consume 'space' key to prevent scrolling the page down.
aVisitor.mEventStatus = nsEventStatus_eConsumeNoDefault;
}
dispatchClick = keyboardEvent->keyCode == nsIDOMKeyEvent::DOM_VK_RETURN;
break;
case NS_KEY_UP:
dispatchClick = keyboardEvent->keyCode == nsIDOMKeyEvent::DOM_VK_SPACE;
break;
default:
break;
}
if (dispatchClick) {
rv = DispatchSimulatedClick(this, event->mFlags.mIsTrusted,
aVisitor.mPresContext);
if (NS_SUCCEEDED(rv)) {
aVisitor.mEventStatus = nsEventStatus_eConsumeNoDefault;
}
}
} // event->HasKeyEventMessage()
return rv;
}
bool
HTMLSummaryElement::IsHTMLFocusable(bool aWithMouse, bool* aIsFocusable,
int32_t* aTabIndex)
{
bool disallowOverridingFocusability =
nsGenericHTMLElement::IsHTMLFocusable(aWithMouse, aIsFocusable, aTabIndex);
if (disallowOverridingFocusability || !IsMainSummary()) {
return disallowOverridingFocusability;
}
#ifdef XP_MACOSX
// The parent does not have strong opinion about the focusability of this main
// summary element, but we'd like to override it when mouse clicking on Mac OS
// like other form elements.
*aIsFocusable = !aWithMouse || nsFocusManager::sMouseFocusesFormControl;
#else
// The main summary element is focusable on other platforms.
*aIsFocusable = true;
#endif
// Give a chance to allow the subclass to override aIsFocusable.
return false;
}
int32_t
HTMLSummaryElement::TabIndexDefault()
{
// Make the main summary be able to navigate via tab, and be focusable.
// See nsGenericHTMLElement::IsHTMLFocusable().
return IsMainSummary() ? 0 : nsGenericHTMLElement::TabIndexDefault();
}
bool
HTMLSummaryElement::IsMainSummary() const
{