From cd20864e1a8f9974492b699ca1bbf9d9e5518008 Mon Sep 17 00:00:00 2001 From: Joey Arhar Date: Wed, 21 Jun 2023 13:53:52 -0700 Subject: [PATCH] Make togglePopover throw more exceptions This patch makes sure that togglePopover will throw exceptions when it is disconnected from the document or doesn't have a popover attribute. This is being discussed here: https://github.com/whatwg/html/issues/8999 Change-Id: Iac7a486cd64b09b5657a157dbf3e9e54c1be2a27 --- html/semantics/popovers/togglePopover.html | 29 ++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/html/semantics/popovers/togglePopover.html b/html/semantics/popovers/togglePopover.html index 115db8e2ac02c3..f17220f2ea4a15 100644 --- a/html/semantics/popovers/togglePopover.html +++ b/html/semantics/popovers/togglePopover.html @@ -44,4 +44,33 @@ // but every way to prevent that from hiding the popover also throws an // exception, so the return value is not testable. }, `togglePopover's return value should reflect what the end state is, not just the force parameter.`); + +test(() => { + const popover = document.createElement('div'); + document.body.appendChild(popover); + + assert_throws_dom('NotSupportedError', () => popover.togglePopover(), + 'togglePopover() should throw an exception when the element has no popover attribute.'); + assert_throws_dom('NotSupportedError', () => popover.togglePopover(true), + 'togglePopover(true) should throw an exception when the element has no popover attribute.'); + assert_throws_dom('NotSupportedError', () => popover.togglePopover(false), + 'togglePopover(false) should throw an exception when the element has no popover attribute.'); + + popover.setAttribute('popover', 'auto'); + popover.remove(); + + assert_throws_dom('InvalidStateError', () => popover.togglePopover(), + 'togglePopover() should throw an exception when the element is disconnected.'); + assert_throws_dom('InvalidStateError', () => popover.togglePopover(true), + 'togglePopover(true) should throw an exception when the element is disconnected.'); + assert_throws_dom('InvalidStateError', () => popover.togglePopover(false), + 'togglePopover(false) should throw an exception when the element is disconnected.'); + + document.body.appendChild(popover); + // togglePopover(false) should not throw just because the popover is already hidden. + popover.togglePopover(false); + popover.showPopover(); + // togglePopover(true) should not throw just because the popover is already showing. + popover.togglePopover(true); +}, 'togglePopover should throw an exception when there is no popover attribute.');