44 * SPDX-License-Identifier: BSD-2-Clause
55 */
66
7+ #include < LibWeb/DOM/Document.h>
78#include < LibWeb/DOM/Element.h>
9+ #include < LibWeb/DOM/Node.h>
810#include < LibWeb/HTML/AttributeNames.h>
11+ #include < LibWeb/HTML/FormAssociatedElement.h>
12+ #include < LibWeb/HTML/HTMLElement.h>
913#include < LibWeb/HTML/PopoverInvokerElement.h>
1014
1115namespace Web ::HTML {
@@ -29,4 +33,93 @@ void PopoverInvokerElement::visit_edges(JS::Cell::Visitor& visitor)
2933 visitor.visit (m_popover_target_element);
3034}
3135
36+ // https://html.spec.whatwg.org/multipage/popover.html#popover-target-attribute-activation-behavior
37+ void PopoverInvokerElement::popover_target_activation_behaviour (GC::Ref<DOM::Node> node, GC::Ref<DOM::Node> event_target)
38+ {
39+ // To run the popover target attribute activation behavior given a Node node and a Node eventTarget:
40+
41+ // 1. Let popover be node's popover target element.
42+ auto popover = PopoverInvokerElement::get_the_popover_target_element (node);
43+
44+ // 2. If popover is null, then return.
45+ if (!popover)
46+ return ;
47+
48+ // 3. If eventTarget is a shadow-including inclusive descendant of popover and popover is a shadow-including descendant of node, then return.
49+ if (event_target->is_shadow_including_inclusive_descendant_of (*popover)
50+ && popover->is_shadow_including_descendant_of (node))
51+ return ;
52+
53+ // 4. If node's popovertargetaction attribute is in the show state and popover's popover visibility state is showing, then return.
54+ if (verify_cast<DOM::Element>(*node).get_attribute_value (HTML::AttributeNames::popovertargetaction).equals_ignoring_ascii_case (" show" sv)
55+ && popover->popover_visibility_state () == HTMLElement::PopoverVisibilityState::Showing)
56+ return ;
57+
58+ // 5. If node's popovertargetaction attribute is in the hide state and popover's popover visibility state is hidden, then return.
59+ if (verify_cast<DOM::Element>(*node).get_attribute_value (HTML::AttributeNames::popovertargetaction).equals_ignoring_ascii_case (" hide" sv)
60+ && popover->popover_visibility_state () == HTMLElement::PopoverVisibilityState::Hidden)
61+ return ;
62+
63+ // 6. If popover's popover visibility state is showing, then run the hide popover algorithm given popover, true, true, false, and false.
64+ if (popover->popover_visibility_state () == HTMLElement::PopoverVisibilityState::Showing) {
65+ popover->hide_popover (FocusPreviousElement::Yes, FireEvents::Yes, ThrowExceptions::No, IgnoreDomState::No).release_value_but_fixme_should_propagate_errors ();
66+ }
67+
68+ // 7. Otherwise, if popover's popover visibility state is hidden and the result of running check popover validity given popover, false, false, null, and false is true, then run show popover given popover, false, and node.
69+ else if (popover->popover_visibility_state () == HTMLElement::PopoverVisibilityState::Hidden
70+ && popover->check_popover_validity (ExpectedToBeShowing::No, ThrowExceptions::No, nullptr , IgnoreDomState::No).release_value_but_fixme_should_propagate_errors ()) {
71+ popover->show_popover (ThrowExceptions::No, verify_cast<HTMLElement>(*node)).release_value_but_fixme_should_propagate_errors ();
72+ }
73+ }
74+
75+ // https://html.spec.whatwg.org/multipage/popover.html#popover-target-element
76+ GC::Ptr<HTMLElement> PopoverInvokerElement::get_the_popover_target_element (GC::Ref<DOM::Node> node)
77+ {
78+ // To get the popover target element given a Node node, perform the following steps. They return an HTML element or null.
79+
80+ auto const * form_associated_element = dynamic_cast <FormAssociatedElement const *>(node.ptr ());
81+ if (!form_associated_element)
82+ return {};
83+
84+ // 1. If node is not a button, then return null.
85+ if (!form_associated_element->is_button ())
86+ return {};
87+
88+ // 2. If node is disabled, then return null.
89+ if (!form_associated_element->enabled ())
90+ return {};
91+
92+ // 3. If node has a form owner and node is a submit button, then return null.
93+ if (form_associated_element->form () != nullptr && form_associated_element->is_submit_button ())
94+ return {};
95+
96+ // 4. Let popoverElement be the result of running node's get the popovertarget-associated element.
97+ auto const * popover_invoker_element = dynamic_cast <PopoverInvokerElement const *>(node.ptr ());
98+ GC::Ptr<HTMLElement> popover_element = verify_cast<HTMLElement>(popover_invoker_element->m_popover_target_element .ptr ());
99+
100+ if (!popover_element) {
101+ auto target_id = verify_cast<HTMLElement>(*node).attribute (" popovertarget" _fly_string);
102+ if (target_id.has_value ()) {
103+ node->root ().for_each_in_inclusive_subtree_of_type <HTMLElement>([&](auto & candidate) {
104+ if (candidate.attribute (HTML::AttributeNames::id) == target_id.value ()) {
105+ popover_element = &candidate;
106+ return TraversalDecision::Break;
107+ }
108+ return TraversalDecision::Continue;
109+ });
110+ }
111+ }
112+
113+ // 5. If popoverElement is null, then return null.
114+ if (!popover_element)
115+ return {};
116+
117+ // 6. If popoverElement's popover attribute is in the no popover state, then return null.
118+ if (!popover_element->popover ().has_value ())
119+ return {};
120+
121+ // 7. Return popoverElement.
122+ return popover_element;
123+ }
124+
32125}
0 commit comments