EventHandler.cpp 34 KB

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