diff --git a/dom/html/HTMLButtonElement.cpp b/dom/html/HTMLButtonElement.cpp
index 0c359b9bb81a..7f190a33f62a 100644
--- a/dom/html/HTMLButtonElement.cpp
+++ b/dom/html/HTMLButtonElement.cpp
@@ -233,7 +233,11 @@ nsresult HTMLButtonElement::PostHandleEvent(EventChainPostVisitor& aVisitor) {
}
if (nsEventStatus_eIgnore == aVisitor.mEventStatus) {
- HandleKeyboardActivation(aVisitor);
+ WidgetKeyboardEvent* keyEvent = aVisitor.mEvent->AsKeyboardEvent();
+ if (keyEvent && keyEvent->IsTrusted()) {
+ HandleKeyboardActivation(aVisitor);
+ }
+
if (aVisitor.mItemFlags & NS_OUTER_ACTIVATE_EVENT) {
if (mForm) {
// Hold a strong ref while dispatching
diff --git a/dom/html/HTMLSummaryElement.cpp b/dom/html/HTMLSummaryElement.cpp
index 37b52b373072..0a489626f143 100644
--- a/dom/html/HTMLSummaryElement.cpp
+++ b/dom/html/HTMLSummaryElement.cpp
@@ -62,7 +62,9 @@ nsresult HTMLSummaryElement::PostHandleEvent(EventChainPostVisitor& aVisitor) {
}
} // event->HasMouseEventMessage()
- HandleKeyboardActivation(aVisitor);
+ if (event->HasKeyEventMessage() && event->IsTrusted()) {
+ HandleKeyboardActivation(aVisitor);
+ }
return rv;
}
diff --git a/dom/html/nsGenericHTMLElement.cpp b/dom/html/nsGenericHTMLElement.cpp
index 8ed003acb071..ce218aa3b616 100644
--- a/dom/html/nsGenericHTMLElement.cpp
+++ b/dom/html/nsGenericHTMLElement.cpp
@@ -2246,11 +2246,10 @@ Result nsGenericHTMLElement::PerformAccesskey(
void nsGenericHTMLElement::HandleKeyboardActivation(
EventChainPostVisitor& aVisitor) {
- const auto message = aVisitor.mEvent->mMessage;
- if (message != eKeyDown && message != eKeyUp && message != eKeyPress) {
- return;
- }
+ MOZ_ASSERT(aVisitor.mEvent->HasKeyEventMessage());
+ MOZ_ASSERT(aVisitor.mEvent->IsTrusted());
+ const auto message = aVisitor.mEvent->mMessage;
const WidgetKeyboardEvent* keyEvent = aVisitor.mEvent->AsKeyboardEvent();
if (nsEventStatus_eIgnore != aVisitor.mEventStatus) {
if (message == eKeyUp && keyEvent->mKeyCode == NS_VK_SPACE) {
diff --git a/layout/generic/test/mochitest.ini b/layout/generic/test/mochitest.ini
index 448f8bab86ef..2bff6fb5e56e 100644
--- a/layout/generic/test/mochitest.ini
+++ b/layout/generic/test/mochitest.ini
@@ -111,6 +111,10 @@ support-files =
[test_image_selection_3.html]
[test_image_selection_in_contenteditable.html]
[test_intrinsic_size_on_loading.html]
+[test_key_enter_open_second_summary.html]
+[test_key_enter_prevent_default.html]
+[test_key_enter_single_summary.html]
+[test_key_space_single_summary.html]
[test_movement_by_characters.html]
[test_movement_by_words.html]
# Disable the caret movement by word test on Linux because the shortcut keys
diff --git a/layout/generic/test/test_key_enter_open_second_summary.html b/layout/generic/test/test_key_enter_open_second_summary.html
new file mode 100644
index 000000000000..6c05ac41d45d
--- /dev/null
+++ b/layout/generic/test/test_key_enter_open_second_summary.html
@@ -0,0 +1,28 @@
+
+
+
+
+
+
+
+
+
+ Summary
+ Second Summary
+ This is the details.
+
+
+
diff --git a/layout/generic/test/test_key_enter_prevent_default.html b/layout/generic/test/test_key_enter_prevent_default.html
new file mode 100644
index 000000000000..2d5793fe8fed
--- /dev/null
+++ b/layout/generic/test/test_key_enter_prevent_default.html
@@ -0,0 +1,31 @@
+
+
+
+
+
+
+
+
+
+ Summary
+ This is the details.
+
+
+
diff --git a/layout/generic/test/test_key_enter_single_summary.html b/layout/generic/test/test_key_enter_single_summary.html
new file mode 100644
index 000000000000..a9022596a822
--- /dev/null
+++ b/layout/generic/test/test_key_enter_single_summary.html
@@ -0,0 +1,27 @@
+
+
+
+
+
+
+
+
+
+ Summary
+ This is the details.
+
+
+
diff --git a/layout/generic/test/test_key_space_single_summary.html b/layout/generic/test/test_key_space_single_summary.html
new file mode 100644
index 000000000000..b393888ab6a5
--- /dev/null
+++ b/layout/generic/test/test_key_space_single_summary.html
@@ -0,0 +1,26 @@
+
+
+
+
+
+
+
+
+
+ Summary
+ This is the details.
+
+
+
diff --git a/layout/reftests/details-summary/key-enter-open-second-summary.html b/layout/reftests/details-summary/key-enter-open-second-summary.html
deleted file mode 100644
index 4d5899787f74..000000000000
--- a/layout/reftests/details-summary/key-enter-open-second-summary.html
+++ /dev/null
@@ -1,21 +0,0 @@
-
-
-
-
-
-
-
- Summary
- Second Summary
- This is the details.
-
-
-
diff --git a/layout/reftests/details-summary/key-enter-prevent-default.html b/layout/reftests/details-summary/key-enter-prevent-default.html
deleted file mode 100644
index 6e657a64343d..000000000000
--- a/layout/reftests/details-summary/key-enter-prevent-default.html
+++ /dev/null
@@ -1,24 +0,0 @@
-
-
-
-
-
-
-
- Summary
- This is the details.
-
-
-
diff --git a/layout/reftests/details-summary/key-enter-single-summary.html b/layout/reftests/details-summary/key-enter-single-summary.html
deleted file mode 100644
index 668573e90e03..000000000000
--- a/layout/reftests/details-summary/key-enter-single-summary.html
+++ /dev/null
@@ -1,20 +0,0 @@
-
-
-
-
-
-
-
- Summary
- This is the details.
-
-
-
diff --git a/layout/reftests/details-summary/key-space-single-summary.html b/layout/reftests/details-summary/key-space-single-summary.html
deleted file mode 100644
index 8f8cecdb18b2..000000000000
--- a/layout/reftests/details-summary/key-space-single-summary.html
+++ /dev/null
@@ -1,21 +0,0 @@
-
-
-
-
-
-
-
- Summary
- This is the details.
-
-
-
diff --git a/layout/reftests/details-summary/reftest.list b/layout/reftests/details-summary/reftest.list
index 71aa8e59ea62..c7c5f57b73af 100644
--- a/layout/reftests/details-summary/reftest.list
+++ b/layout/reftests/details-summary/reftest.list
@@ -84,12 +84,6 @@ fuzzy(0-1,0-172) == mouse-click-overflow-auto-details.html overflow-auto-open-de
fuzzy-if(geckoview,0-7,0-1) == mouse-click-float-details.html open-float-details.html
fuzzy(0-4,0-1) == mouse-click-twice-float-details.html float-details.html # Bug 1316430
-# Dispatch keyboard event to summary
-== key-enter-single-summary.html open-single-summary.html
-== key-enter-open-second-summary.html open-multiple-summary.html
-== key-enter-prevent-default.html single-summary.html
-== key-space-single-summary.html open-single-summary.html
-
# Generated content bits
== details-after.html single-summary.html
== details-before.html single-summary.html
diff --git a/testing/web-platform/tests/html/semantics/forms/the-button-element/button-untrusted-key-event.html b/testing/web-platform/tests/html/semantics/forms/the-button-element/button-untrusted-key-event.html
new file mode 100644
index 000000000000..dd34d3123313
--- /dev/null
+++ b/testing/web-platform/tests/html/semantics/forms/the-button-element/button-untrusted-key-event.html
@@ -0,0 +1,112 @@
+
+
+
+Forms
+
+
+
+
+
+
+
+
+
diff --git a/testing/web-platform/tests/html/semantics/interactive-elements/the-summary-element/summary-untrusted-key-event.html b/testing/web-platform/tests/html/semantics/interactive-elements/the-summary-element/summary-untrusted-key-event.html
new file mode 100644
index 000000000000..21b66d52e7b8
--- /dev/null
+++ b/testing/web-platform/tests/html/semantics/interactive-elements/the-summary-element/summary-untrusted-key-event.html
@@ -0,0 +1,104 @@
+
+
+
+Summary
+
+
+
+
+
+
+ Summary
+ Details
+
+
+
+