EventHandler.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. /*
  2. * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Max Wipfli <mail@maxwipfli.ch>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibGUI/Event.h>
  8. #include <LibWeb/DOM/Range.h>
  9. #include <LibWeb/DOM/Text.h>
  10. #include <LibWeb/HTML/BrowsingContext.h>
  11. #include <LibWeb/HTML/HTMLAnchorElement.h>
  12. #include <LibWeb/HTML/HTMLIFrameElement.h>
  13. #include <LibWeb/HTML/HTMLImageElement.h>
  14. #include <LibWeb/Layout/InitialContainingBlock.h>
  15. #include <LibWeb/Page/EventHandler.h>
  16. #include <LibWeb/Page/Page.h>
  17. #include <LibWeb/Painting/PaintableBox.h>
  18. #include <LibWeb/UIEvents/EventNames.h>
  19. #include <LibWeb/UIEvents/KeyboardEvent.h>
  20. #include <LibWeb/UIEvents/MouseEvent.h>
  21. namespace Web {
  22. static Gfx::StandardCursor cursor_css_to_gfx(Optional<CSS::Cursor> cursor)
  23. {
  24. if (!cursor.has_value()) {
  25. return Gfx::StandardCursor::None;
  26. }
  27. switch (cursor.value()) {
  28. case CSS::Cursor::Crosshair:
  29. case CSS::Cursor::Cell:
  30. return Gfx::StandardCursor::Crosshair;
  31. case CSS::Cursor::Grab:
  32. case CSS::Cursor::Grabbing:
  33. return Gfx::StandardCursor::Drag;
  34. case CSS::Cursor::Pointer:
  35. return Gfx::StandardCursor::Hand;
  36. case CSS::Cursor::Help:
  37. return Gfx::StandardCursor::Help;
  38. case CSS::Cursor::None:
  39. return Gfx::StandardCursor::Hidden;
  40. case CSS::Cursor::Text:
  41. case CSS::Cursor::VerticalText:
  42. return Gfx::StandardCursor::IBeam;
  43. case CSS::Cursor::Move:
  44. case CSS::Cursor::AllScroll:
  45. return Gfx::StandardCursor::Move;
  46. case CSS::Cursor::Progress:
  47. case CSS::Cursor::Wait:
  48. return Gfx::StandardCursor::Wait;
  49. case CSS::Cursor::ColResize:
  50. return Gfx::StandardCursor::ResizeColumn;
  51. case CSS::Cursor::EResize:
  52. case CSS::Cursor::WResize:
  53. case CSS::Cursor::EwResize:
  54. return Gfx::StandardCursor::ResizeHorizontal;
  55. case CSS::Cursor::RowResize:
  56. return Gfx::StandardCursor::ResizeRow;
  57. case CSS::Cursor::NResize:
  58. case CSS::Cursor::SResize:
  59. case CSS::Cursor::NsResize:
  60. return Gfx::StandardCursor::ResizeVertical;
  61. case CSS::Cursor::NeResize:
  62. case CSS::Cursor::SwResize:
  63. case CSS::Cursor::NeswResize:
  64. return Gfx::StandardCursor::ResizeDiagonalBLTR;
  65. case CSS::Cursor::NwResize:
  66. case CSS::Cursor::SeResize:
  67. case CSS::Cursor::NwseResize:
  68. return Gfx::StandardCursor::ResizeDiagonalTLBR;
  69. case CSS::Cursor::ZoomIn:
  70. case CSS::Cursor::ZoomOut:
  71. return Gfx::StandardCursor::Zoom;
  72. default:
  73. return Gfx::StandardCursor::None;
  74. }
  75. }
  76. static Gfx::IntPoint compute_mouse_event_offset(Gfx::IntPoint const& position, Layout::Node const& layout_node)
  77. {
  78. auto top_left_of_layout_node = layout_node.box_type_agnostic_position();
  79. return {
  80. position.x() - static_cast<int>(top_left_of_layout_node.x()),
  81. position.y() - static_cast<int>(top_left_of_layout_node.y())
  82. };
  83. }
  84. EventHandler::EventHandler(Badge<HTML::BrowsingContext>, HTML::BrowsingContext& browsing_context)
  85. : m_browsing_context(browsing_context)
  86. , m_edit_event_handler(make<EditEventHandler>(browsing_context))
  87. {
  88. }
  89. EventHandler::~EventHandler() = default;
  90. Layout::InitialContainingBlock const* EventHandler::layout_root() const
  91. {
  92. if (!m_browsing_context.active_document())
  93. return nullptr;
  94. return m_browsing_context.active_document()->layout_node();
  95. }
  96. Layout::InitialContainingBlock* EventHandler::layout_root()
  97. {
  98. if (!m_browsing_context.active_document())
  99. return nullptr;
  100. return m_browsing_context.active_document()->layout_node();
  101. }
  102. Painting::PaintableBox* EventHandler::paint_root()
  103. {
  104. if (!m_browsing_context.active_document())
  105. return nullptr;
  106. return const_cast<Painting::PaintableBox*>(m_browsing_context.active_document()->paint_box());
  107. }
  108. Painting::PaintableBox const* EventHandler::paint_root() const
  109. {
  110. if (!m_browsing_context.active_document())
  111. return nullptr;
  112. return const_cast<Painting::PaintableBox*>(m_browsing_context.active_document()->paint_box());
  113. }
  114. bool EventHandler::handle_mousewheel(Gfx::IntPoint const& position, unsigned int buttons, unsigned int modifiers, int wheel_delta_x, int wheel_delta_y)
  115. {
  116. if (m_browsing_context.active_document())
  117. m_browsing_context.active_document()->update_layout();
  118. if (!paint_root())
  119. return false;
  120. if (modifiers & KeyModifier::Mod_Shift)
  121. swap(wheel_delta_x, wheel_delta_y);
  122. // FIXME: Support wheel events in nested browsing contexts.
  123. auto result = paint_root()->hit_test(position.to_type<float>(), Painting::HitTestType::Exact);
  124. if (result.has_value() && result->paintable->handle_mousewheel({}, position, buttons, modifiers, wheel_delta_x, wheel_delta_y))
  125. return true;
  126. if (auto* page = m_browsing_context.page()) {
  127. page->client().page_did_request_scroll(wheel_delta_x * 20, wheel_delta_y * 20);
  128. return true;
  129. }
  130. return false;
  131. }
  132. bool EventHandler::handle_mouseup(Gfx::IntPoint const& position, unsigned button, unsigned modifiers)
  133. {
  134. if (m_browsing_context.active_document())
  135. m_browsing_context.active_document()->update_layout();
  136. if (!paint_root())
  137. return false;
  138. bool handled_event = false;
  139. RefPtr<Painting::Paintable> paintable;
  140. if (m_mouse_event_tracking_layout_node) {
  141. paintable = m_mouse_event_tracking_layout_node->paintable();
  142. } else {
  143. if (auto result = paint_root()->hit_test(position.to_type<float>(), Painting::HitTestType::Exact); result.has_value())
  144. paintable = result->paintable;
  145. }
  146. if (paintable && paintable->wants_mouse_events()) {
  147. if (paintable->handle_mouseup({}, position, button, modifiers) == Painting::Paintable::DispatchEventOfSameName::No)
  148. return false;
  149. // Things may have changed as a consequence of Layout::Node::handle_mouseup(). Hit test again.
  150. if (!paint_root())
  151. return true;
  152. if (auto result = paint_root()->hit_test(position.to_type<float>(), Painting::HitTestType::Exact); result.has_value())
  153. paintable = result->paintable;
  154. }
  155. if (paintable) {
  156. JS::GCPtr<DOM::Node> node = paintable->mouse_event_target();
  157. if (!node)
  158. node = paintable->dom_node();
  159. if (node) {
  160. if (is<HTML::HTMLIFrameElement>(*node)) {
  161. if (auto* nested_browsing_context = static_cast<HTML::HTMLIFrameElement&>(*node).nested_browsing_context())
  162. return nested_browsing_context->event_handler().handle_mouseup(position.translated(compute_mouse_event_offset({}, paintable->layout_node())), button, modifiers);
  163. return false;
  164. }
  165. // Search for the first parent of the hit target that's an element.
  166. // "The click event type MUST be dispatched on the topmost event target indicated by the pointer." (https://www.w3.org/TR/uievents/#event-type-click)
  167. // "The topmost event target MUST be the element highest in the rendering order which is capable of being an event target." (https://www.w3.org/TR/uievents/#topmost-event-target)
  168. auto* layout_node = &paintable->layout_node();
  169. while (layout_node && node && !node->is_element() && layout_node->parent()) {
  170. layout_node = layout_node->parent();
  171. node = layout_node->dom_node();
  172. }
  173. if (!node || !layout_node) {
  174. // FIXME: This is pretty ugly but we need to bail out here.
  175. goto after_node_use;
  176. }
  177. auto offset = compute_mouse_event_offset(position, *layout_node);
  178. node->dispatch_event(*UIEvents::MouseEvent::create_from_platform_event(node->realm(), UIEvents::EventNames::mouseup, offset.x(), offset.y(), position.x(), position.y(), button));
  179. handled_event = true;
  180. bool run_activation_behavior = true;
  181. if (node.ptr() == m_mousedown_target && button == GUI::MouseButton::Primary) {
  182. run_activation_behavior = node->dispatch_event(*UIEvents::MouseEvent::create_from_platform_event(node->realm(), UIEvents::EventNames::click, offset.x(), offset.y(), position.x(), position.y(), button));
  183. }
  184. if (run_activation_behavior) {
  185. // FIXME: This is ad-hoc and incorrect. The reason this exists is
  186. // because we are missing browsing context navigation:
  187. //
  188. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#navigate
  189. //
  190. // Additionally, we currently cannot spawn a new top-level
  191. // browsing context for new tab operations, because the new
  192. // top-level browsing context would be in another process. To
  193. // fix this, there needs to be some way to be able to
  194. // communicate with browsing contexts in remote WebContent
  195. // processes, and then step 8 of this algorithm needs to be
  196. // implemented in BrowsingContext::choose_a_browsing_context:
  197. //
  198. // https://html.spec.whatwg.org/multipage/browsers.html#the-rules-for-choosing-a-browsing-context-given-a-browsing-context-name
  199. if (JS::GCPtr<HTML::HTMLAnchorElement> link = node->enclosing_link_element()) {
  200. JS::NonnullGCPtr<DOM::Document> document = *m_browsing_context.active_document();
  201. auto href = link->href();
  202. auto url = document->parse_url(href);
  203. dbgln("Web::EventHandler: Clicking on a link to {}", url);
  204. if (button == GUI::MouseButton::Primary) {
  205. if (href.starts_with("javascript:"sv)) {
  206. document->run_javascript(href.substring_view(11, href.length() - 11));
  207. } else if (!url.fragment().is_null() && url.equals(document->url(), AK::URL::ExcludeFragment::Yes)) {
  208. m_browsing_context.scroll_to_anchor(url.fragment());
  209. } else {
  210. if (m_browsing_context.is_top_level()) {
  211. if (auto* page = m_browsing_context.page())
  212. page->client().page_did_click_link(url, link->target(), modifiers);
  213. }
  214. }
  215. } else if (button == GUI::MouseButton::Middle) {
  216. if (auto* page = m_browsing_context.page())
  217. page->client().page_did_middle_click_link(url, link->target(), modifiers);
  218. } else if (button == GUI::MouseButton::Secondary) {
  219. if (auto* page = m_browsing_context.page())
  220. page->client().page_did_request_link_context_menu(m_browsing_context.to_top_level_position(position), url, link->target(), modifiers);
  221. }
  222. } else if (button == GUI::MouseButton::Secondary) {
  223. if (is<HTML::HTMLImageElement>(*node)) {
  224. auto& image_element = verify_cast<HTML::HTMLImageElement>(*node);
  225. auto image_url = image_element.document().parse_url(image_element.src());
  226. if (auto* page = m_browsing_context.page())
  227. page->client().page_did_request_image_context_menu(m_browsing_context.to_top_level_position(position), image_url, "", modifiers, image_element.bitmap());
  228. } else if (auto* page = m_browsing_context.page()) {
  229. page->client().page_did_request_context_menu(m_browsing_context.to_top_level_position(position));
  230. }
  231. }
  232. }
  233. }
  234. }
  235. after_node_use:
  236. if (button == GUI::MouseButton::Primary)
  237. m_in_mouse_selection = false;
  238. return handled_event;
  239. }
  240. bool EventHandler::handle_mousedown(Gfx::IntPoint const& position, unsigned button, unsigned modifiers)
  241. {
  242. if (m_browsing_context.active_document())
  243. m_browsing_context.active_document()->update_layout();
  244. if (!paint_root())
  245. return false;
  246. JS::NonnullGCPtr<DOM::Document> document = *m_browsing_context.active_document();
  247. JS::GCPtr<DOM::Node> node;
  248. {
  249. RefPtr<Painting::Paintable> paintable;
  250. if (m_mouse_event_tracking_layout_node) {
  251. paintable = m_mouse_event_tracking_layout_node->paintable();
  252. } else {
  253. auto result = paint_root()->hit_test(position.to_type<float>(), Painting::HitTestType::Exact);
  254. if (!result.has_value())
  255. return false;
  256. paintable = result->paintable;
  257. }
  258. auto pointer_events = paintable->computed_values().pointer_events();
  259. // FIXME: Handle other values for pointer-events.
  260. VERIFY(pointer_events != CSS::PointerEvents::None);
  261. node = paintable->mouse_event_target();
  262. if (!node)
  263. node = paintable->dom_node();
  264. document->set_hovered_node(node);
  265. if (paintable->wants_mouse_events()) {
  266. if (paintable->handle_mousedown({}, position, button, modifiers) == Painting::Paintable::DispatchEventOfSameName::No)
  267. return false;
  268. }
  269. if (!node)
  270. return false;
  271. if (is<HTML::HTMLIFrameElement>(*node)) {
  272. if (auto* nested_browsing_context = static_cast<HTML::HTMLIFrameElement&>(*node).nested_browsing_context())
  273. return nested_browsing_context->event_handler().handle_mousedown(position.translated(compute_mouse_event_offset({}, paintable->layout_node())), button, modifiers);
  274. return false;
  275. }
  276. if (auto* page = m_browsing_context.page())
  277. page->set_focused_browsing_context({}, m_browsing_context);
  278. // Search for the first parent of the hit target that's an element.
  279. // "The click event type MUST be dispatched on the topmost event target indicated by the pointer." (https://www.w3.org/TR/uievents/#event-type-click)
  280. // "The topmost event target MUST be the element highest in the rendering order which is capable of being an event target." (https://www.w3.org/TR/uievents/#topmost-event-target)
  281. auto* layout_node = &paintable->layout_node();
  282. while (layout_node && node && !node->is_element() && layout_node->parent()) {
  283. layout_node = layout_node->parent();
  284. node = layout_node->dom_node();
  285. }
  286. if (!node || !layout_node)
  287. return false;
  288. m_mousedown_target = node.ptr();
  289. auto offset = compute_mouse_event_offset(position, *layout_node);
  290. node->dispatch_event(*UIEvents::MouseEvent::create_from_platform_event(node->realm(), UIEvents::EventNames::mousedown, offset.x(), offset.y(), position.x(), position.y(), button));
  291. }
  292. // NOTE: Dispatching an event may have disturbed the world.
  293. if (!paint_root() || paint_root() != node->document().paint_box())
  294. return true;
  295. if (button == GUI::MouseButton::Primary) {
  296. if (auto result = paint_root()->hit_test(position.to_type<float>(), Painting::HitTestType::TextCursor); result.has_value()) {
  297. auto paintable = result->paintable;
  298. if (paintable->dom_node()) {
  299. // See if we want to focus something.
  300. bool did_focus_something = false;
  301. for (auto candidate = node; candidate; candidate = candidate->parent()) {
  302. if (candidate->is_focusable()) {
  303. document->set_focused_element(verify_cast<DOM::Element>(candidate.ptr()));
  304. did_focus_something = true;
  305. break;
  306. }
  307. }
  308. // If we didn't focus anything, place the document text cursor at the mouse position.
  309. // FIXME: This is all rather strange. Find a better solution.
  310. if (!did_focus_something) {
  311. m_browsing_context.set_cursor_position(DOM::Position(*paintable->dom_node(), result->index_in_node));
  312. layout_root()->set_selection({ { paintable->layout_node(), result->index_in_node }, {} });
  313. m_in_mouse_selection = true;
  314. }
  315. }
  316. }
  317. }
  318. return true;
  319. }
  320. bool EventHandler::handle_mousemove(Gfx::IntPoint const& position, unsigned buttons, unsigned modifiers)
  321. {
  322. if (m_browsing_context.active_document())
  323. m_browsing_context.active_document()->update_layout();
  324. if (!paint_root())
  325. return false;
  326. auto& document = *m_browsing_context.active_document();
  327. bool hovered_node_changed = false;
  328. bool is_hovering_link = false;
  329. Gfx::StandardCursor hovered_node_cursor = Gfx::StandardCursor::None;
  330. RefPtr<Painting::Paintable> paintable;
  331. Optional<int> start_index;
  332. if (m_mouse_event_tracking_layout_node) {
  333. paintable = m_mouse_event_tracking_layout_node->paintable();
  334. } else {
  335. if (auto result = paint_root()->hit_test(position.to_type<float>(), Painting::HitTestType::Exact); result.has_value()) {
  336. paintable = result->paintable;
  337. start_index = result->index_in_node;
  338. }
  339. }
  340. const HTML::HTMLAnchorElement* hovered_link_element = nullptr;
  341. if (paintable) {
  342. if (paintable->wants_mouse_events()) {
  343. document.set_hovered_node(paintable->dom_node());
  344. if (paintable->handle_mousemove({}, position, buttons, modifiers) == Painting::Paintable::DispatchEventOfSameName::No)
  345. return false;
  346. // FIXME: It feels a bit aggressive to always update the cursor like this.
  347. if (auto* page = m_browsing_context.page())
  348. page->client().page_did_request_cursor_change(Gfx::StandardCursor::None);
  349. }
  350. JS::GCPtr<DOM::Node> node = paintable->mouse_event_target();
  351. if (!node)
  352. node = paintable->dom_node();
  353. if (node && is<HTML::HTMLIFrameElement>(*node)) {
  354. if (auto* nested_browsing_context = static_cast<HTML::HTMLIFrameElement&>(*node).nested_browsing_context())
  355. return nested_browsing_context->event_handler().handle_mousemove(position.translated(compute_mouse_event_offset({}, paintable->layout_node())), buttons, modifiers);
  356. return false;
  357. }
  358. auto pointer_events = paintable->computed_values().pointer_events();
  359. // FIXME: Handle other values for pointer-events.
  360. VERIFY(pointer_events != CSS::PointerEvents::None);
  361. hovered_node_changed = node.ptr() != document.hovered_node();
  362. document.set_hovered_node(node);
  363. if (node) {
  364. hovered_link_element = node->enclosing_link_element();
  365. if (hovered_link_element)
  366. is_hovering_link = true;
  367. if (node->is_text()) {
  368. auto cursor = paintable->computed_values().cursor();
  369. if (cursor == CSS::Cursor::Auto)
  370. hovered_node_cursor = Gfx::StandardCursor::IBeam;
  371. else
  372. hovered_node_cursor = cursor_css_to_gfx(cursor);
  373. } else if (node->is_element()) {
  374. auto cursor = paintable->computed_values().cursor();
  375. if (cursor == CSS::Cursor::Auto)
  376. hovered_node_cursor = Gfx::StandardCursor::Arrow;
  377. else
  378. hovered_node_cursor = cursor_css_to_gfx(cursor);
  379. }
  380. // Search for the first parent of the hit target that's an element.
  381. // "The click event type MUST be dispatched on the topmost event target indicated by the pointer." (https://www.w3.org/TR/uievents/#event-type-click)
  382. // "The topmost event target MUST be the element highest in the rendering order which is capable of being an event target." (https://www.w3.org/TR/uievents/#topmost-event-target)
  383. auto* layout_node = &paintable->layout_node();
  384. while (layout_node && node && !node->is_element() && layout_node->parent()) {
  385. layout_node = layout_node->parent();
  386. node = layout_node->dom_node();
  387. }
  388. if (!node || !layout_node) {
  389. // FIXME: This is pretty ugly but we need to bail out here.
  390. goto after_node_use;
  391. }
  392. auto offset = compute_mouse_event_offset(position, *layout_node);
  393. node->dispatch_event(*UIEvents::MouseEvent::create_from_platform_event(node->realm(), UIEvents::EventNames::mousemove, offset.x(), offset.y(), position.x(), position.y()));
  394. // NOTE: Dispatching an event may have disturbed the world.
  395. if (!paint_root() || paint_root() != node->document().paint_box())
  396. return true;
  397. }
  398. after_node_use:
  399. if (m_in_mouse_selection) {
  400. auto hit = paint_root()->hit_test(position.to_type<float>(), Painting::HitTestType::TextCursor);
  401. if (start_index.has_value() && hit.has_value() && hit->dom_node()) {
  402. m_browsing_context.set_cursor_position(DOM::Position(*hit->dom_node(), *start_index));
  403. layout_root()->set_selection_end({ hit->paintable->layout_node(), hit->index_in_node });
  404. m_browsing_context.set_needs_display();
  405. }
  406. if (auto* page = m_browsing_context.page())
  407. page->client().page_did_change_selection();
  408. }
  409. }
  410. if (auto* page = m_browsing_context.page()) {
  411. page->client().page_did_request_cursor_change(hovered_node_cursor);
  412. if (hovered_node_changed) {
  413. JS::GCPtr<HTML::HTMLElement> hovered_html_element = document.hovered_node() ? document.hovered_node()->enclosing_html_element_with_attribute(HTML::AttributeNames::title) : nullptr;
  414. if (hovered_html_element && !hovered_html_element->title().is_null()) {
  415. page->client().page_did_enter_tooltip_area(m_browsing_context.to_top_level_position(position), hovered_html_element->title());
  416. } else {
  417. page->client().page_did_leave_tooltip_area();
  418. }
  419. if (is_hovering_link)
  420. page->client().page_did_hover_link(document.parse_url(hovered_link_element->href()));
  421. else
  422. page->client().page_did_unhover_link();
  423. }
  424. }
  425. return true;
  426. }
  427. bool EventHandler::handle_doubleclick(Gfx::IntPoint const& position, unsigned button, unsigned modifiers)
  428. {
  429. if (m_browsing_context.active_document())
  430. m_browsing_context.active_document()->update_layout();
  431. if (!paint_root())
  432. return false;
  433. RefPtr<Painting::Paintable> paintable;
  434. if (m_mouse_event_tracking_layout_node) {
  435. paintable = m_mouse_event_tracking_layout_node->paintable();
  436. } else {
  437. auto result = paint_root()->hit_test(position.to_type<float>(), Painting::HitTestType::Exact);
  438. if (!result.has_value())
  439. return false;
  440. paintable = result->paintable;
  441. }
  442. auto pointer_events = paintable->computed_values().pointer_events();
  443. // FIXME: Handle other values for pointer-events.
  444. if (pointer_events == CSS::PointerEvents::None)
  445. return false;
  446. JS::GCPtr<DOM::Node> node = paintable->mouse_event_target();
  447. if (!node)
  448. node = paintable->dom_node();
  449. if (paintable->wants_mouse_events()) {
  450. // FIXME: Handle double clicks.
  451. }
  452. if (!node)
  453. return false;
  454. if (is<HTML::HTMLIFrameElement>(*node)) {
  455. if (auto* nested_browsing_context = static_cast<HTML::HTMLIFrameElement&>(*node).nested_browsing_context())
  456. return nested_browsing_context->event_handler().handle_doubleclick(position.translated(compute_mouse_event_offset({}, paintable->layout_node())), button, modifiers);
  457. return false;
  458. }
  459. // Search for the first parent of the hit target that's an element.
  460. // "The topmost event target MUST be the element highest in the rendering order which is capable of being an event target." (https://www.w3.org/TR/uievents/#topmost-event-target)
  461. auto* layout_node = &paintable->layout_node();
  462. while (layout_node && node && !node->is_element() && layout_node->parent()) {
  463. layout_node = layout_node->parent();
  464. node = layout_node->dom_node();
  465. }
  466. if (!node || !layout_node)
  467. return false;
  468. auto offset = compute_mouse_event_offset(position, *layout_node);
  469. node->dispatch_event(*UIEvents::MouseEvent::create_from_platform_event(node->realm(), UIEvents::EventNames::dblclick, offset.x(), offset.y(), position.x(), position.y(), button));
  470. // NOTE: Dispatching an event may have disturbed the world.
  471. if (!paint_root() || paint_root() != node->document().paint_box())
  472. return true;
  473. if (button == GUI::MouseButton::Primary) {
  474. if (auto result = paint_root()->hit_test(position.to_type<float>(), Painting::HitTestType::TextCursor); result.has_value()) {
  475. auto paintable = result->paintable;
  476. if (!paintable->dom_node())
  477. return true;
  478. auto const& layout_node = paintable->layout_node();
  479. if (!layout_node.is_text_node())
  480. return true;
  481. auto const& text_for_rendering = verify_cast<Layout::TextNode>(layout_node).text_for_rendering();
  482. int first_word_break_before = [&] {
  483. // Start from one before the index position to prevent selecting only spaces between words, caused by the addition below.
  484. // This also helps us dealing with cases where index is equal to the string length.
  485. for (int i = result->index_in_node - 1; i >= 0; --i) {
  486. if (is_ascii_space(text_for_rendering[i])) {
  487. // Don't include the space in the selection
  488. return i + 1;
  489. }
  490. }
  491. return 0;
  492. }();
  493. int first_word_break_after = [&] {
  494. for (size_t i = result->index_in_node; i < text_for_rendering.length(); ++i) {
  495. if (is_ascii_space(text_for_rendering[i]))
  496. return i;
  497. }
  498. return text_for_rendering.length();
  499. }();
  500. m_browsing_context.set_cursor_position(DOM::Position(*paintable->dom_node(), first_word_break_after));
  501. layout_root()->set_selection({ { paintable->layout_node(), first_word_break_before }, { paintable->layout_node(), first_word_break_after } });
  502. }
  503. }
  504. return true;
  505. }
  506. bool EventHandler::focus_next_element()
  507. {
  508. if (!m_browsing_context.active_document())
  509. return false;
  510. auto* element = m_browsing_context.active_document()->focused_element();
  511. if (!element) {
  512. element = m_browsing_context.active_document()->first_child_of_type<DOM::Element>();
  513. if (element && element->is_focusable()) {
  514. m_browsing_context.active_document()->set_focused_element(element);
  515. return true;
  516. }
  517. }
  518. for (element = element->next_element_in_pre_order(); element && !element->is_focusable(); element = element->next_element_in_pre_order())
  519. ;
  520. m_browsing_context.active_document()->set_focused_element(element);
  521. return element;
  522. }
  523. bool EventHandler::focus_previous_element()
  524. {
  525. if (!m_browsing_context.active_document())
  526. return false;
  527. auto* element = m_browsing_context.active_document()->focused_element();
  528. if (!element) {
  529. element = m_browsing_context.active_document()->last_child_of_type<DOM::Element>();
  530. if (element && element->is_focusable()) {
  531. m_browsing_context.active_document()->set_focused_element(element);
  532. return true;
  533. }
  534. }
  535. for (element = element->previous_element_in_pre_order(); element && !element->is_focusable(); element = element->previous_element_in_pre_order())
  536. ;
  537. m_browsing_context.active_document()->set_focused_element(element);
  538. return element;
  539. }
  540. constexpr bool should_ignore_keydown_event(u32 code_point)
  541. {
  542. // FIXME: There are probably also keys with non-zero code points that should be filtered out.
  543. return code_point == 0 || code_point == 27;
  544. }
  545. bool EventHandler::handle_keydown(KeyCode key, unsigned modifiers, u32 code_point)
  546. {
  547. if (!m_browsing_context.active_document())
  548. return false;
  549. JS::NonnullGCPtr<DOM::Document> document = *m_browsing_context.active_document();
  550. if (!document->layout_node())
  551. return false;
  552. NonnullRefPtr<Layout::InitialContainingBlock> layout_root = *document->layout_node();
  553. if (key == KeyCode::Key_Tab) {
  554. if (modifiers & KeyModifier::Mod_Shift)
  555. return focus_previous_element();
  556. return focus_next_element();
  557. }
  558. if (layout_root->selection().is_valid()) {
  559. auto range = layout_root->selection().to_dom_range()->normalized();
  560. if (range->start_container()->is_editable()) {
  561. layout_root->set_selection({});
  562. // FIXME: This doesn't work for some reason?
  563. m_browsing_context.set_cursor_position({ *range->start_container(), range->start_offset() });
  564. if (key == KeyCode::Key_Backspace || key == KeyCode::Key_Delete) {
  565. m_edit_event_handler->handle_delete(*range);
  566. return true;
  567. }
  568. if (!should_ignore_keydown_event(code_point)) {
  569. m_edit_event_handler->handle_delete(*range);
  570. m_edit_event_handler->handle_insert(m_browsing_context.cursor_position(), code_point);
  571. m_browsing_context.increment_cursor_position_offset();
  572. return true;
  573. }
  574. }
  575. }
  576. if (m_browsing_context.cursor_position().is_valid() && m_browsing_context.cursor_position().node()->is_editable()) {
  577. if (key == KeyCode::Key_Backspace) {
  578. if (!m_browsing_context.decrement_cursor_position_offset()) {
  579. // FIXME: Move to the previous node and delete the last character there.
  580. return true;
  581. }
  582. m_edit_event_handler->handle_delete_character_after(m_browsing_context.cursor_position());
  583. return true;
  584. }
  585. if (key == KeyCode::Key_Delete) {
  586. if (m_browsing_context.cursor_position().offset_is_at_end_of_node()) {
  587. // FIXME: Move to the next node and delete the first character there.
  588. return true;
  589. }
  590. m_edit_event_handler->handle_delete_character_after(m_browsing_context.cursor_position());
  591. return true;
  592. }
  593. if (key == KeyCode::Key_Right) {
  594. if (!m_browsing_context.increment_cursor_position_offset()) {
  595. // FIXME: Move to the next node.
  596. }
  597. return true;
  598. }
  599. if (key == KeyCode::Key_Left) {
  600. if (!m_browsing_context.decrement_cursor_position_offset()) {
  601. // FIXME: Move to the previous node.
  602. }
  603. return true;
  604. }
  605. if (key == KeyCode::Key_Home) {
  606. auto& node = *static_cast<DOM::Text*>(const_cast<DOM::Node*>(m_browsing_context.cursor_position().node()));
  607. m_browsing_context.set_cursor_position(DOM::Position { node, 0 });
  608. return true;
  609. }
  610. if (key == KeyCode::Key_End) {
  611. auto& node = *static_cast<DOM::Text*>(const_cast<DOM::Node*>(m_browsing_context.cursor_position().node()));
  612. m_browsing_context.set_cursor_position(DOM::Position { node, (unsigned)node.data().length() });
  613. return true;
  614. }
  615. if (!should_ignore_keydown_event(code_point)) {
  616. m_edit_event_handler->handle_insert(m_browsing_context.cursor_position(), code_point);
  617. m_browsing_context.increment_cursor_position_offset();
  618. return true;
  619. }
  620. // NOTE: Because modifier keys should be ignored, we need to return true.
  621. return true;
  622. }
  623. auto event = UIEvents::KeyboardEvent::create_from_platform_event(document->realm(), UIEvents::EventNames::keydown, key, modifiers, code_point);
  624. if (JS::GCPtr<DOM::Element> focused_element = document->focused_element())
  625. return focused_element->dispatch_event(*event);
  626. if (JS::GCPtr<HTML::HTMLElement> body = m_browsing_context.active_document()->body())
  627. return body->dispatch_event(*event);
  628. return document->root().dispatch_event(*event);
  629. }
  630. bool EventHandler::handle_keyup(KeyCode key, unsigned modifiers, u32 code_point)
  631. {
  632. JS::GCPtr<DOM::Document> document = m_browsing_context.active_document();
  633. if (!document)
  634. return false;
  635. auto event = UIEvents::KeyboardEvent::create_from_platform_event(document->realm(), UIEvents::EventNames::keyup, key, modifiers, code_point);
  636. if (JS::GCPtr<DOM::Element> focused_element = document->focused_element())
  637. return document->focused_element()->dispatch_event(*event);
  638. if (JS::GCPtr<HTML::HTMLElement> body = document->body())
  639. return body->dispatch_event(*event);
  640. return document->root().dispatch_event(*event);
  641. }
  642. void EventHandler::set_mouse_event_tracking_layout_node(Layout::Node* layout_node)
  643. {
  644. m_mouse_event_tracking_layout_node = layout_node;
  645. }
  646. }