| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2022-03-09 23:53:41 +01:00
										 |  |  |  |  * Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org> | 
					
						
							| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  |  |  */ | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-11 09:46:53 +00:00
										 |  |  |  | #include <AK/StringBuilder.h>
 | 
					
						
							| 
									
										
										
										
											2021-02-03 22:47:50 +01:00
										 |  |  |  | #include <LibJS/Interpreter.h>
 | 
					
						
							|  |  |  |  | #include <LibJS/Parser.h>
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |  | #include <LibWeb/Bindings/HTMLElementPrototype.h>
 | 
					
						
							| 
									
										
										
										
											2021-02-20 00:43:08 +01:00
										 |  |  |  | #include <LibWeb/DOM/DOMException.h>
 | 
					
						
							| 
									
										
										
										
											2020-11-11 09:46:53 +00:00
										 |  |  |  | #include <LibWeb/DOM/Document.h>
 | 
					
						
							| 
									
										
										
										
											2021-02-20 00:43:08 +01:00
										 |  |  |  | #include <LibWeb/DOM/ExceptionOr.h>
 | 
					
						
							| 
									
										
										
										
											2022-02-16 20:43:24 +01:00
										 |  |  |  | #include <LibWeb/DOM/IDLEventListener.h>
 | 
					
						
							| 
									
										
										
										
											2022-02-06 18:46:26 +01:00
										 |  |  |  | #include <LibWeb/HTML/BrowsingContext.h>
 | 
					
						
							|  |  |  |  | #include <LibWeb/HTML/BrowsingContextContainer.h>
 | 
					
						
							| 
									
										
										
										
											2021-02-03 22:47:50 +01:00
										 |  |  |  | #include <LibWeb/HTML/EventHandler.h>
 | 
					
						
							| 
									
										
										
										
											2020-11-21 21:53:18 +00:00
										 |  |  |  | #include <LibWeb/HTML/HTMLAnchorElement.h>
 | 
					
						
							| 
									
										
										
										
											2021-04-15 20:48:55 +03:00
										 |  |  |  | #include <LibWeb/HTML/HTMLBodyElement.h>
 | 
					
						
							| 
									
										
										
										
											2020-07-26 15:08:16 +02:00
										 |  |  |  | #include <LibWeb/HTML/HTMLElement.h>
 | 
					
						
							| 
									
										
										
										
											2022-03-07 23:08:26 +01:00
										 |  |  |  | #include <LibWeb/HTML/Window.h>
 | 
					
						
							| 
									
										
										
										
											2021-09-30 01:35:19 +02:00
										 |  |  |  | #include <LibWeb/Layout/Box.h>
 | 
					
						
							| 
									
										
										
										
											2021-01-01 18:55:47 +01:00
										 |  |  |  | #include <LibWeb/Layout/BreakNode.h>
 | 
					
						
							| 
									
										
										
										
											2020-11-22 15:53:01 +01:00
										 |  |  |  | #include <LibWeb/Layout/TextNode.h>
 | 
					
						
							| 
									
										
										
										
											2022-03-10 23:13:37 +01:00
										 |  |  |  | #include <LibWeb/Painting/PaintableBox.h>
 | 
					
						
							| 
									
										
										
										
											2021-02-03 22:47:50 +01:00
										 |  |  |  | #include <LibWeb/UIEvents/EventNames.h>
 | 
					
						
							| 
									
										
										
										
											2022-02-07 00:04:10 +01:00
										 |  |  |  | #include <LibWeb/UIEvents/FocusEvent.h>
 | 
					
						
							| 
									
										
										
										
											2022-03-15 00:50:19 +00:00
										 |  |  |  | #include <LibWeb/UIEvents/MouseEvent.h>
 | 
					
						
							| 
									
										
										
										
											2019-09-29 11:59:38 +02:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-28 18:20:36 +02:00
										 |  |  |  | namespace Web::HTML { | 
					
						
							| 
									
										
										
										
											2020-03-07 10:27:02 +01:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-18 21:00:52 +01:00
										 |  |  |  | HTMLElement::HTMLElement(DOM::Document& document, DOM::QualifiedName qualified_name) | 
					
						
							| 
									
										
										
										
											2021-02-07 11:20:15 +01:00
										 |  |  |  |     : Element(document, move(qualified_name)) | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |  |     , m_dataset(DOMStringMap::create(*this)) | 
					
						
							| 
									
										
										
										
											2019-09-29 11:59:38 +02:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |  |     set_prototype(&window().ensure_web_prototype<Bindings::HTMLElementPrototype>("HTMLElement")); | 
					
						
							| 
									
										
										
										
											2019-09-29 11:59:38 +02:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-14 13:21:51 -06:00
										 |  |  |  | HTMLElement::~HTMLElement() = default; | 
					
						
							| 
									
										
										
										
											2020-03-07 10:27:02 +01:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |  | void HTMLElement::visit_edges(Cell::Visitor& visitor) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     Base::visit_edges(visitor); | 
					
						
							|  |  |  |  |     visitor.visit(m_dataset.ptr()); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-03 02:57:28 +01:00
										 |  |  |  | HTMLElement::ContentEditableState HTMLElement::content_editable_state() const | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     auto contenteditable = attribute(HTML::AttributeNames::contenteditable); | 
					
						
							| 
									
										
										
										
											2021-07-28 22:07:58 +08:00
										 |  |  |  |     // "true", an empty string or a missing value map to the "true" state.
 | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |  |     if ((!contenteditable.is_null() && contenteditable.is_empty()) || contenteditable.equals_ignoring_case("true"sv)) | 
					
						
							| 
									
										
										
										
											2020-08-03 02:57:28 +01:00
										 |  |  |  |         return ContentEditableState::True; | 
					
						
							|  |  |  |  |     // "false" maps to the "false" state.
 | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |  |     if (contenteditable.equals_ignoring_case("false"sv)) | 
					
						
							| 
									
										
										
										
											2020-08-03 02:57:28 +01:00
										 |  |  |  |         return ContentEditableState::False; | 
					
						
							| 
									
										
										
										
											2021-07-31 18:14:58 +08:00
										 |  |  |  |     // Having no such attribute or an invalid value maps to the "inherit" state.
 | 
					
						
							| 
									
										
										
										
											2020-08-03 02:57:28 +01:00
										 |  |  |  |     return ContentEditableState::Inherit; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | bool HTMLElement::is_editable() const | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     switch (content_editable_state()) { | 
					
						
							|  |  |  |  |     case ContentEditableState::True: | 
					
						
							|  |  |  |  |         return true; | 
					
						
							|  |  |  |  |     case ContentEditableState::False: | 
					
						
							|  |  |  |  |         return false; | 
					
						
							|  |  |  |  |     case ContentEditableState::Inherit: | 
					
						
							|  |  |  |  |         return parent() && parent()->is_editable(); | 
					
						
							|  |  |  |  |     default: | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |  |         VERIFY_NOT_REACHED(); | 
					
						
							| 
									
										
										
										
											2020-08-03 02:57:28 +01:00
										 |  |  |  |     } | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | String HTMLElement::content_editable() const | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     switch (content_editable_state()) { | 
					
						
							|  |  |  |  |     case ContentEditableState::True: | 
					
						
							|  |  |  |  |         return "true"; | 
					
						
							|  |  |  |  |     case ContentEditableState::False: | 
					
						
							|  |  |  |  |         return "false"; | 
					
						
							|  |  |  |  |     case ContentEditableState::Inherit: | 
					
						
							|  |  |  |  |         return "inherit"; | 
					
						
							|  |  |  |  |     default: | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |  |         VERIFY_NOT_REACHED(); | 
					
						
							| 
									
										
										
										
											2020-08-03 02:57:28 +01:00
										 |  |  |  |     } | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-20 00:43:08 +01:00
										 |  |  |  | // https://html.spec.whatwg.org/multipage/interaction.html#contenteditable
 | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |  | DOM::ExceptionOr<void> HTMLElement::set_content_editable(String const& content_editable) | 
					
						
							| 
									
										
										
										
											2020-08-03 02:57:28 +01:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |  |     if (content_editable.equals_ignoring_case("inherit"sv)) { | 
					
						
							| 
									
										
										
										
											2020-08-03 02:57:28 +01:00
										 |  |  |  |         remove_attribute(HTML::AttributeNames::contenteditable); | 
					
						
							| 
									
										
										
										
											2021-02-20 00:43:08 +01:00
										 |  |  |  |         return {}; | 
					
						
							| 
									
										
										
										
											2020-08-03 02:57:28 +01:00
										 |  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |  |     if (content_editable.equals_ignoring_case("true"sv)) { | 
					
						
							| 
									
										
										
										
											2020-08-03 02:57:28 +01:00
										 |  |  |  |         set_attribute(HTML::AttributeNames::contenteditable, "true"); | 
					
						
							| 
									
										
										
										
											2021-02-20 00:43:08 +01:00
										 |  |  |  |         return {}; | 
					
						
							| 
									
										
										
										
											2020-08-03 02:57:28 +01:00
										 |  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |  |     if (content_editable.equals_ignoring_case("false"sv)) { | 
					
						
							| 
									
										
										
										
											2020-08-03 02:57:28 +01:00
										 |  |  |  |         set_attribute(HTML::AttributeNames::contenteditable, "false"); | 
					
						
							| 
									
										
										
										
											2021-02-20 00:43:08 +01:00
										 |  |  |  |         return {}; | 
					
						
							| 
									
										
										
										
											2020-08-03 02:57:28 +01:00
										 |  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-02-20 00:43:08 +01:00
										 |  |  |  |     return DOM::SyntaxError::create("Invalid contentEditable value, must be 'true', 'false', or 'inherit'"); | 
					
						
							| 
									
										
										
										
											2020-08-03 02:57:28 +01:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-11 09:46:53 +00:00
										 |  |  |  | void HTMLElement::set_inner_text(StringView text) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     remove_all_children(); | 
					
						
							|  |  |  |  |     append_child(document().create_text_node(text)); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     set_needs_style_update(true); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | String HTMLElement::inner_text() | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     StringBuilder builder; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // innerText for element being rendered takes visibility into account, so force a layout and then walk the layout tree.
 | 
					
						
							| 
									
										
										
										
											2020-12-14 10:39:39 +01:00
										 |  |  |  |     document().update_layout(); | 
					
						
							| 
									
										
										
										
											2020-11-11 09:46:53 +00:00
										 |  |  |  |     if (!layout_node()) | 
					
						
							|  |  |  |  |         return text_content(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |  |     Function<void(Layout::Node const&)> recurse = [&](auto& node) { | 
					
						
							| 
									
										
										
										
											2020-11-11 09:46:53 +00:00
										 |  |  |  |         for (auto* child = node.first_child(); child; child = child->next_sibling()) { | 
					
						
							| 
									
										
										
										
											2021-01-01 18:55:47 +01:00
										 |  |  |  |             if (is<Layout::TextNode>(child)) | 
					
						
							| 
									
										
										
										
											2021-06-24 19:53:42 +02:00
										 |  |  |  |                 builder.append(verify_cast<Layout::TextNode>(*child).text_for_rendering()); | 
					
						
							| 
									
										
										
										
											2021-01-01 18:55:47 +01:00
										 |  |  |  |             if (is<Layout::BreakNode>(child)) | 
					
						
							| 
									
										
										
										
											2020-11-11 09:46:53 +00:00
										 |  |  |  |                 builder.append('\n'); | 
					
						
							|  |  |  |  |             recurse(*child); | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  |     }; | 
					
						
							|  |  |  |  |     recurse(*layout_node()); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     return builder.to_string(); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-30 01:35:19 +02:00
										 |  |  |  | // // https://drafts.csswg.org/cssom-view/#dom-htmlelement-offsettop
 | 
					
						
							|  |  |  |  | int HTMLElement::offset_top() const | 
					
						
							| 
									
										
										
										
											2021-04-15 20:48:55 +03:00
										 |  |  |  | { | 
					
						
							|  |  |  |  |     if (is<HTML::HTMLBodyElement>(this) || !layout_node() || !parent_element() || !parent_element()->layout_node()) | 
					
						
							|  |  |  |  |         return 0; | 
					
						
							|  |  |  |  |     auto position = layout_node()->box_type_agnostic_position(); | 
					
						
							|  |  |  |  |     auto parent_position = parent_element()->layout_node()->box_type_agnostic_position(); | 
					
						
							|  |  |  |  |     return position.y() - parent_position.y(); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-30 01:35:19 +02:00
										 |  |  |  | // https://drafts.csswg.org/cssom-view/#dom-htmlelement-offsetleft
 | 
					
						
							|  |  |  |  | int HTMLElement::offset_left() const | 
					
						
							| 
									
										
										
										
											2021-04-15 20:48:55 +03:00
										 |  |  |  | { | 
					
						
							|  |  |  |  |     if (is<HTML::HTMLBodyElement>(this) || !layout_node() || !parent_element() || !parent_element()->layout_node()) | 
					
						
							|  |  |  |  |         return 0; | 
					
						
							|  |  |  |  |     auto position = layout_node()->box_type_agnostic_position(); | 
					
						
							|  |  |  |  |     auto parent_position = parent_element()->layout_node()->box_type_agnostic_position(); | 
					
						
							|  |  |  |  |     return position.x() - parent_position.x(); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-30 01:35:19 +02:00
										 |  |  |  | // https://drafts.csswg.org/cssom-view/#dom-htmlelement-offsetwidth
 | 
					
						
							|  |  |  |  | int HTMLElement::offset_width() const | 
					
						
							|  |  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-04-10 20:38:16 +02:00
										 |  |  |  |     // NOTE: Ensure that layout is up-to-date before looking at metrics.
 | 
					
						
							|  |  |  |  |     const_cast<DOM::Document&>(document()).update_layout(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 1. If the element does not have any associated CSS layout box return zero and terminate this algorithm.
 | 
					
						
							|  |  |  |  |     if (!paint_box()) | 
					
						
							|  |  |  |  |         return 0; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 2. Return the width of the axis-aligned bounding box of the border boxes of all fragments generated by the element’s principal box,
 | 
					
						
							|  |  |  |  |     //    ignoring any transforms that apply to the element and its ancestors.
 | 
					
						
							|  |  |  |  |     // FIXME: Account for inline boxes.
 | 
					
						
							|  |  |  |  |     return paint_box()->border_box_width(); | 
					
						
							| 
									
										
										
										
											2021-09-30 01:35:19 +02:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // https://drafts.csswg.org/cssom-view/#dom-htmlelement-offsetheight
 | 
					
						
							|  |  |  |  | int HTMLElement::offset_height() const | 
					
						
							|  |  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-04-10 20:38:16 +02:00
										 |  |  |  |     // NOTE: Ensure that layout is up-to-date before looking at metrics.
 | 
					
						
							|  |  |  |  |     const_cast<DOM::Document&>(document()).update_layout(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 1. If the element does not have any associated CSS layout box return zero and terminate this algorithm.
 | 
					
						
							|  |  |  |  |     if (!paint_box()) | 
					
						
							|  |  |  |  |         return 0; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 2. Return the height of the axis-aligned bounding box of the border boxes of all fragments generated by the element’s principal box,
 | 
					
						
							|  |  |  |  |     //    ignoring any transforms that apply to the element and its ancestors.
 | 
					
						
							|  |  |  |  |     // FIXME: Account for inline boxes.
 | 
					
						
							|  |  |  |  |     return paint_box()->border_box_height(); | 
					
						
							| 
									
										
										
										
											2021-09-30 01:35:19 +02:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-21 21:53:18 +00:00
										 |  |  |  | bool HTMLElement::cannot_navigate() const | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     // FIXME: Return true if element's node document is not fully active
 | 
					
						
							|  |  |  |  |     return !is<HTML::HTMLAnchorElement>(this) && !is_connected(); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |  | void HTMLElement::parse_attribute(FlyString const& name, String const& value) | 
					
						
							| 
									
										
										
										
											2021-02-03 22:47:50 +01:00
										 |  |  |  | { | 
					
						
							|  |  |  |  |     Element::parse_attribute(name, value); | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-14 18:03:08 +01:00
										 |  |  |  |     // 1. If namespace is not null, or localName is not the name of an event handler content attribute on element, then return.
 | 
					
						
							|  |  |  |  |     // FIXME: Add the namespace part once we support attribute namespaces.
 | 
					
						
							| 
									
										
										
										
											2021-02-03 22:47:50 +01:00
										 |  |  |  | #undef __ENUMERATE
 | 
					
						
							| 
									
										
										
										
											2021-10-14 18:03:08 +01:00
										 |  |  |  | #define __ENUMERATE(attribute_name, event_name)                     \
 | 
					
						
							|  |  |  |  |     if (name == HTML::AttributeNames::attribute_name) {             \ | 
					
						
							|  |  |  |  |         element_event_handler_attribute_changed(event_name, value); \ | 
					
						
							| 
									
										
										
										
											2021-02-03 22:47:50 +01:00
										 |  |  |  |     } | 
					
						
							|  |  |  |  |     ENUMERATE_GLOBAL_EVENT_HANDLERS(__ENUMERATE) | 
					
						
							|  |  |  |  | #undef __ENUMERATE
 | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-06 18:46:26 +01:00
										 |  |  |  | // https://html.spec.whatwg.org/multipage/interaction.html#focus-update-steps
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |  | static void run_focus_update_steps(Vector<JS::Handle<DOM::Node>> old_chain, Vector<JS::Handle<DOM::Node>> new_chain, DOM::Node& new_focus_target) | 
					
						
							| 
									
										
										
										
											2022-02-06 18:46:26 +01:00
										 |  |  |  | { | 
					
						
							|  |  |  |  |     // 1. If the last entry in old chain and the last entry in new chain are the same,
 | 
					
						
							|  |  |  |  |     //    pop the last entry from old chain and the last entry from new chain and redo this step.
 | 
					
						
							|  |  |  |  |     while (!old_chain.is_empty() | 
					
						
							|  |  |  |  |         && !new_chain.is_empty() | 
					
						
							|  |  |  |  |         && &old_chain.last() == &new_chain.last()) { | 
					
						
							| 
									
										
										
										
											2022-02-07 02:09:17 +01:00
										 |  |  |  |         (void)old_chain.take_last(); | 
					
						
							|  |  |  |  |         (void)new_chain.take_last(); | 
					
						
							| 
									
										
										
										
											2022-02-06 18:46:26 +01:00
										 |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 2. For each entry entry in old chain, in order, run these substeps:
 | 
					
						
							|  |  |  |  |     for (auto& entry : old_chain) { | 
					
						
							|  |  |  |  |         // FIXME: 1. If entry is an input element, and the change event applies to the element,
 | 
					
						
							|  |  |  |  |         //           and the element does not have a defined activation behavior,
 | 
					
						
							|  |  |  |  |         //           and the user has changed the element's value or its list of selected files
 | 
					
						
							|  |  |  |  |         //           while the control was focused without committing that change
 | 
					
						
							|  |  |  |  |         //           (such that it is different to what it was when the control was first focused),
 | 
					
						
							|  |  |  |  |         //           then fire an event named change at the element,
 | 
					
						
							|  |  |  |  |         //           with the bubbles attribute initialized to true.
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |  |         JS::GCPtr<DOM::EventTarget> blur_event_target; | 
					
						
							|  |  |  |  |         if (is<DOM::Element>(*entry)) { | 
					
						
							| 
									
										
										
										
											2022-02-06 18:46:26 +01:00
										 |  |  |  |             // 2. If entry is an element, let blur event target be entry.
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |  |             blur_event_target = entry.ptr(); | 
					
						
							|  |  |  |  |         } else if (is<DOM::Document>(*entry)) { | 
					
						
							| 
									
										
										
										
											2022-02-06 18:46:26 +01:00
										 |  |  |  |             // If entry is a Document object, let blur event target be that Document object's relevant global object.
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |  |             blur_event_target = &static_cast<DOM::Document&>(*entry).window(); | 
					
						
							| 
									
										
										
										
											2022-02-06 18:46:26 +01:00
										 |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         // 3. If entry is the last entry in old chain, and entry is an Element,
 | 
					
						
							|  |  |  |  |         //    and the last entry in new chain is also an Element,
 | 
					
						
							|  |  |  |  |         //    then let related blur target be the last entry in new chain.
 | 
					
						
							|  |  |  |  |         //    Otherwise, let related blur target be null.
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |  |         JS::GCPtr<DOM::EventTarget> related_blur_target; | 
					
						
							| 
									
										
										
										
											2022-02-07 02:10:03 +01:00
										 |  |  |  |         if (!old_chain.is_empty() | 
					
						
							|  |  |  |  |             && &entry == &old_chain.last() | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |  |             && is<DOM::Element>(*entry) | 
					
						
							| 
									
										
										
										
											2022-02-07 02:10:03 +01:00
										 |  |  |  |             && !new_chain.is_empty() | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |  |             && is<DOM::Element>(*new_chain.last())) { | 
					
						
							|  |  |  |  |             related_blur_target = new_chain.last().ptr(); | 
					
						
							| 
									
										
										
										
											2022-02-06 18:46:26 +01:00
										 |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         // 4. If blur event target is not null, fire a focus event named blur at blur event target,
 | 
					
						
							|  |  |  |  |         //    with related blur target as the related target.
 | 
					
						
							|  |  |  |  |         if (blur_event_target) { | 
					
						
							|  |  |  |  |             // FIXME: Implement the "fire a focus event" spec operation.
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |  |             auto blur_event = UIEvents::FocusEvent::create(verify_cast<DOM::Node>(*blur_event_target).document().window(), HTML::EventNames::blur); | 
					
						
							| 
									
										
										
										
											2022-02-06 18:46:26 +01:00
										 |  |  |  |             blur_event->set_related_target(related_blur_target); | 
					
						
							| 
									
										
										
										
											2022-08-08 22:29:40 +02:00
										 |  |  |  |             blur_event_target->dispatch_event(*blur_event); | 
					
						
							| 
									
										
										
										
											2022-02-06 18:46:26 +01:00
										 |  |  |  |         } | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // FIXME: 3. Apply any relevant platform-specific conventions for focusing new focus target.
 | 
					
						
							|  |  |  |  |     //           (For example, some platforms select the contents of a text control when that control is focused.)
 | 
					
						
							|  |  |  |  |     (void)new_focus_target; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 4. For each entry entry in new chain, in reverse order, run these substeps:
 | 
					
						
							| 
									
										
										
										
											2022-02-23 19:45:51 +01:00
										 |  |  |  |     for (auto& entry : new_chain.in_reverse()) { | 
					
						
							| 
									
										
										
										
											2022-02-06 18:46:26 +01:00
										 |  |  |  |         // 1. If entry is a focusable area: designate entry as the focused area of the document.
 | 
					
						
							|  |  |  |  |         // FIXME: This isn't entirely right.
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |  |         if (is<DOM::Element>(*entry)) | 
					
						
							|  |  |  |  |             entry->document().set_focused_element(&static_cast<DOM::Element&>(*entry)); | 
					
						
							| 
									
										
										
										
											2022-02-06 18:46:26 +01:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |  |         JS::GCPtr<DOM::EventTarget> focus_event_target; | 
					
						
							|  |  |  |  |         if (is<DOM::Element>(*entry)) { | 
					
						
							| 
									
										
										
										
											2022-02-06 18:46:26 +01:00
										 |  |  |  |             // 2. If entry is an element, let focus event target be entry.
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |  |             focus_event_target = entry.ptr(); | 
					
						
							|  |  |  |  |         } else if (is<DOM::Document>(*entry)) { | 
					
						
							| 
									
										
										
										
											2022-02-06 18:46:26 +01:00
										 |  |  |  |             // If entry is a Document object, let focus event target be that Document object's relevant global object.
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |  |             focus_event_target = &static_cast<DOM::Document&>(*entry).window(); | 
					
						
							| 
									
										
										
										
											2022-02-06 18:46:26 +01:00
										 |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-07 02:10:03 +01:00
										 |  |  |  |         // 3. If entry is the last entry in new chain, and entry is an Element,
 | 
					
						
							|  |  |  |  |         //    and the last entry in old chain is also an Element,
 | 
					
						
							|  |  |  |  |         //    then let related focus target be the last entry in old chain.
 | 
					
						
							| 
									
										
										
										
											2022-02-06 18:46:26 +01:00
										 |  |  |  |         //    Otherwise, let related focus target be null.
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |  |         JS::GCPtr<DOM::EventTarget> related_focus_target; | 
					
						
							| 
									
										
										
										
											2022-02-07 02:10:03 +01:00
										 |  |  |  |         if (!new_chain.is_empty() | 
					
						
							|  |  |  |  |             && &entry == &new_chain.last() | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |  |             && is<DOM::Element>(*entry) | 
					
						
							| 
									
										
										
										
											2022-02-07 02:10:03 +01:00
										 |  |  |  |             && !old_chain.is_empty() | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |  |             && is<DOM::Element>(*old_chain.last())) { | 
					
						
							|  |  |  |  |             related_focus_target = old_chain.last().ptr(); | 
					
						
							| 
									
										
										
										
											2022-02-06 18:46:26 +01:00
										 |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         // 4. If focus event target is not null, fire a focus event named focus at focus event target,
 | 
					
						
							|  |  |  |  |         //    with related focus target as the related target.
 | 
					
						
							|  |  |  |  |         if (focus_event_target) { | 
					
						
							|  |  |  |  |             // FIXME: Implement the "fire a focus event" spec operation.
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |  |             auto focus_event = UIEvents::FocusEvent::create(verify_cast<DOM::Node>(*focus_event_target).document().window(), HTML::EventNames::focus); | 
					
						
							| 
									
										
										
										
											2022-02-06 18:46:26 +01:00
										 |  |  |  |             focus_event->set_related_target(related_focus_target); | 
					
						
							| 
									
										
										
										
											2022-08-08 22:29:40 +02:00
										 |  |  |  |             focus_event_target->dispatch_event(*focus_event); | 
					
						
							| 
									
										
										
										
											2022-02-06 18:46:26 +01:00
										 |  |  |  |         } | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | // https://html.spec.whatwg.org/multipage/interaction.html#focus-chain
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |  | static Vector<JS::Handle<DOM::Node>> focus_chain(DOM::Node* subject) | 
					
						
							| 
									
										
										
										
											2022-02-06 18:46:26 +01:00
										 |  |  |  | { | 
					
						
							|  |  |  |  |     // FIXME: Move this somewhere more spec-friendly.
 | 
					
						
							|  |  |  |  |     if (!subject) | 
					
						
							|  |  |  |  |         return {}; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 1. Let output be an empty list.
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |  |     Vector<JS::Handle<DOM::Node>> output; | 
					
						
							| 
									
										
										
										
											2022-02-06 18:46:26 +01:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 2. Let currentObject be subject.
 | 
					
						
							|  |  |  |  |     auto* current_object = subject; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 3. While true:
 | 
					
						
							|  |  |  |  |     while (true) { | 
					
						
							|  |  |  |  |         // 1. Append currentObject to output.
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |  |         output.append(JS::make_handle(*current_object)); | 
					
						
							| 
									
										
										
										
											2022-02-06 18:46:26 +01:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |         // FIXME: 2. If currentObject is an area element's shape, then append that area element to output.
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         // FIXME:    Otherwise, if currentObject's DOM anchor is an element that is not currentObject itself, then append currentObject's DOM anchor to output.
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         // FIXME: Everything below needs work. The conditions are not entirely right.
 | 
					
						
							|  |  |  |  |         if (!is<DOM::Document>(*current_object)) { | 
					
						
							|  |  |  |  |             // 3. If currentObject is a focusable area, then set currentObject to currentObject's DOM anchor's node document.
 | 
					
						
							|  |  |  |  |             current_object = ¤t_object->document(); | 
					
						
							| 
									
										
										
										
											2022-02-07 01:43:01 +01:00
										 |  |  |  |         } else if (is<DOM::Document>(*current_object) | 
					
						
							|  |  |  |  |             && static_cast<DOM::Document&>(*current_object).browsing_context() | 
					
						
							|  |  |  |  |             && !static_cast<DOM::Document&>(*current_object).browsing_context()->is_top_level()) { | 
					
						
							| 
									
										
										
										
											2022-02-06 18:46:26 +01:00
										 |  |  |  |             // Otherwise, if currentObject is a Document whose browsing context is a child browsing context,
 | 
					
						
							|  |  |  |  |             // then set currentObject to currentObject's browsing context's container.
 | 
					
						
							| 
									
										
										
										
											2022-02-07 01:43:01 +01:00
										 |  |  |  |             current_object = static_cast<DOM::Document&>(*current_object).browsing_context()->container(); | 
					
						
							| 
									
										
										
										
											2022-02-06 18:46:26 +01:00
										 |  |  |  |         } else { | 
					
						
							|  |  |  |  |             break; | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 4. Return output.
 | 
					
						
							|  |  |  |  |     return output; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // https://html.spec.whatwg.org/multipage/interaction.html#focusing-steps
 | 
					
						
							|  |  |  |  | // FIXME: This should accept more types.
 | 
					
						
							|  |  |  |  | static void run_focusing_steps(DOM::Node* new_focus_target, DOM::Node* fallback_target = nullptr, [[maybe_unused]] Optional<String> focus_trigger = {}) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     // FIXME: 1. If new focus target is not a focusable area, then set new focus target
 | 
					
						
							|  |  |  |  |     //           to the result of getting the focusable area for new focus target,
 | 
					
						
							|  |  |  |  |     //           given focus trigger if it was passed.
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 2. If new focus target is null, then:
 | 
					
						
							|  |  |  |  |     if (!new_focus_target) { | 
					
						
							|  |  |  |  |         // 1. If no fallback target was specified, then return.
 | 
					
						
							|  |  |  |  |         if (!fallback_target) | 
					
						
							|  |  |  |  |             return; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         // 2. Otherwise, set new focus target to the fallback target.
 | 
					
						
							|  |  |  |  |         new_focus_target = fallback_target; | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 3. If new focus target is a browsing context container with non-null nested browsing context,
 | 
					
						
							|  |  |  |  |     //    then set new focus target to the nested browsing context's active document.
 | 
					
						
							|  |  |  |  |     if (is<BrowsingContextContainer>(*new_focus_target)) { | 
					
						
							|  |  |  |  |         auto& browsing_context_container = static_cast<BrowsingContextContainer&>(*new_focus_target); | 
					
						
							|  |  |  |  |         if (auto* nested_browsing_context = browsing_context_container.nested_browsing_context()) | 
					
						
							|  |  |  |  |             new_focus_target = nested_browsing_context->active_document(); | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // FIXME: 4. If new focus target is a focusable area and its DOM anchor is inert, then return.
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 5. If new focus target is the currently focused area of a top-level browsing context, then return.
 | 
					
						
							| 
									
										
										
										
											2022-02-07 01:27:43 +01:00
										 |  |  |  |     if (!new_focus_target->document().browsing_context()) | 
					
						
							| 
									
										
										
										
											2022-02-06 18:46:26 +01:00
										 |  |  |  |         return; | 
					
						
							|  |  |  |  |     auto& top_level_browsing_context = new_focus_target->document().browsing_context()->top_level_browsing_context(); | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |  |     if (new_focus_target == top_level_browsing_context.currently_focused_area().ptr()) | 
					
						
							| 
									
										
										
										
											2022-02-06 18:46:26 +01:00
										 |  |  |  |         return; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 6. Let old chain be the current focus chain of the top-level browsing context in which
 | 
					
						
							|  |  |  |  |     //    new focus target finds itself.
 | 
					
						
							|  |  |  |  |     auto old_chain = focus_chain(top_level_browsing_context.currently_focused_area()); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 7. Let new chain be the focus chain of new focus target.
 | 
					
						
							|  |  |  |  |     auto new_chain = focus_chain(new_focus_target); | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-23 10:21:30 -05:00
										 |  |  |  |     // 8. Run the focus update steps with old chain, new chain, and new focus target respectively.
 | 
					
						
							| 
									
										
										
										
											2022-02-06 18:46:26 +01:00
										 |  |  |  |     run_focus_update_steps(old_chain, new_chain, *new_focus_target); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // https://html.spec.whatwg.org/multipage/interaction.html#dom-focus
 | 
					
						
							|  |  |  |  | void HTMLElement::focus() | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     // 1. If the element is marked as locked for focus, then return.
 | 
					
						
							|  |  |  |  |     if (m_locked_for_focus) | 
					
						
							|  |  |  |  |         return; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 2. Mark the element as locked for focus.
 | 
					
						
							|  |  |  |  |     m_locked_for_focus = true; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 3. Run the focusing steps for the element.
 | 
					
						
							|  |  |  |  |     run_focusing_steps(this); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // FIXME: 4. If the value of the preventScroll dictionary member of options is false,
 | 
					
						
							|  |  |  |  |     //           then scroll the element into view with scroll behavior "auto",
 | 
					
						
							|  |  |  |  |     //           block flow direction position set to an implementation-defined value,
 | 
					
						
							|  |  |  |  |     //           and inline base direction position set to an implementation-defined value.
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 5. Unmark the element as locked for focus.
 | 
					
						
							|  |  |  |  |     m_locked_for_focus = false; | 
					
						
							|  |  |  |  | } | 
					
						
							| 
									
										
										
										
											2022-02-15 00:25:51 +01:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-25 20:45:19 +01:00
										 |  |  |  | // https://html.spec.whatwg.org/multipage/webappapis.html#fire-a-synthetic-pointer-event
 | 
					
						
							|  |  |  |  | bool HTMLElement::fire_a_synthetic_pointer_event(FlyString const& type, DOM::Element& target, bool not_trusted) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     // 1. Let event be the result of creating an event using PointerEvent.
 | 
					
						
							|  |  |  |  |     // 2. Initialize event's type attribute to e.
 | 
					
						
							|  |  |  |  |     // FIXME: Actually create a PointerEvent!
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |  |     auto event = UIEvents::MouseEvent::create(document().window(), type); | 
					
						
							| 
									
										
										
										
											2022-02-25 20:45:19 +01:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 3. Initialize event's bubbles and cancelable attributes to true.
 | 
					
						
							|  |  |  |  |     event->set_bubbles(true); | 
					
						
							|  |  |  |  |     event->set_cancelable(true); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 4. Set event's composed flag.
 | 
					
						
							|  |  |  |  |     event->set_composed(true); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 5. If the not trusted flag is set, initialize event's isTrusted attribute to false.
 | 
					
						
							|  |  |  |  |     if (not_trusted) { | 
					
						
							|  |  |  |  |         event->set_is_trusted(false); | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // FIXME: 6. Initialize event's ctrlKey, shiftKey, altKey, and metaKey attributes according to the current state
 | 
					
						
							|  |  |  |  |     //           of the key input device, if any (false for any keys that are not available).
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // FIXME: 7. Initialize event's view attribute to target's node document's Window object, if any, and null otherwise.
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // FIXME: 8. event's getModifierState() method is to return values appropriately describing the current state of the key input device.
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 9. Return the result of dispatching event at target.
 | 
					
						
							| 
									
										
										
										
											2022-08-08 22:29:40 +02:00
										 |  |  |  |     return target.dispatch_event(*event); | 
					
						
							| 
									
										
										
										
											2022-02-25 20:45:19 +01:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-15 00:25:51 +01:00
										 |  |  |  | // https://html.spec.whatwg.org/multipage/interaction.html#dom-click
 | 
					
						
							|  |  |  |  | void HTMLElement::click() | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     // FIXME: 1. If this element is a form control that is disabled, then return.
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 2. If this element's click in progress flag is set, then return.
 | 
					
						
							|  |  |  |  |     if (m_click_in_progress) | 
					
						
							|  |  |  |  |         return; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 3. Set this element's click in progress flag.
 | 
					
						
							|  |  |  |  |     m_click_in_progress = true; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // FIXME: 4. Fire a synthetic pointer event named click at this element, with the not trusted flag set.
 | 
					
						
							| 
									
										
										
										
											2022-02-25 20:45:19 +01:00
										 |  |  |  |     fire_a_synthetic_pointer_event(HTML::EventNames::click, *this, true); | 
					
						
							| 
									
										
										
										
											2022-02-15 00:25:51 +01:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 5. Unset this element's click in progress flag.
 | 
					
						
							|  |  |  |  |     m_click_in_progress = false; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-07 10:27:02 +01:00
										 |  |  |  | } |