EventHandler.cpp 34 KB

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