EventHandler.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  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 Gfx::StandardCursor cursor_css_to_gfx(Optional<CSS::Cursor> cursor)
  34. {
  35. if (!cursor.has_value()) {
  36. return Gfx::StandardCursor::None;
  37. }
  38. switch (cursor.value()) {
  39. case CSS::Cursor::Crosshair:
  40. case CSS::Cursor::Cell:
  41. return Gfx::StandardCursor::Crosshair;
  42. case CSS::Cursor::Grab:
  43. case CSS::Cursor::Grabbing:
  44. return Gfx::StandardCursor::Drag;
  45. case CSS::Cursor::Pointer:
  46. return Gfx::StandardCursor::Hand;
  47. case CSS::Cursor::Help:
  48. return Gfx::StandardCursor::Help;
  49. case CSS::Cursor::None:
  50. return Gfx::StandardCursor::Hidden;
  51. case CSS::Cursor::Text:
  52. case CSS::Cursor::VerticalText:
  53. return Gfx::StandardCursor::IBeam;
  54. case CSS::Cursor::Move:
  55. case CSS::Cursor::AllScroll:
  56. return Gfx::StandardCursor::Move;
  57. case CSS::Cursor::Progress:
  58. case CSS::Cursor::Wait:
  59. return Gfx::StandardCursor::Wait;
  60. case CSS::Cursor::ColResize:
  61. return Gfx::StandardCursor::ResizeColumn;
  62. case CSS::Cursor::EResize:
  63. case CSS::Cursor::WResize:
  64. case CSS::Cursor::EwResize:
  65. return Gfx::StandardCursor::ResizeHorizontal;
  66. case CSS::Cursor::RowResize:
  67. return Gfx::StandardCursor::ResizeRow;
  68. case CSS::Cursor::NResize:
  69. case CSS::Cursor::SResize:
  70. case CSS::Cursor::NsResize:
  71. return Gfx::StandardCursor::ResizeVertical;
  72. case CSS::Cursor::NeResize:
  73. case CSS::Cursor::SwResize:
  74. case CSS::Cursor::NeswResize:
  75. return Gfx::StandardCursor::ResizeDiagonalBLTR;
  76. case CSS::Cursor::NwResize:
  77. case CSS::Cursor::SeResize:
  78. case CSS::Cursor::NwseResize:
  79. return Gfx::StandardCursor::ResizeDiagonalTLBR;
  80. case CSS::Cursor::ZoomIn:
  81. case CSS::Cursor::ZoomOut:
  82. return Gfx::StandardCursor::Zoom;
  83. default:
  84. return Gfx::StandardCursor::None;
  85. }
  86. }
  87. static Gfx::IntPoint compute_mouse_event_offset(Gfx::IntPoint const& position, Layout::Node const& layout_node)
  88. {
  89. auto top_left_of_layout_node = layout_node.box_type_agnostic_position();
  90. return {
  91. position.x() - static_cast<int>(top_left_of_layout_node.x()),
  92. position.y() - static_cast<int>(top_left_of_layout_node.y())
  93. };
  94. }
  95. EventHandler::EventHandler(Badge<HTML::BrowsingContext>, HTML::BrowsingContext& browsing_context)
  96. : m_browsing_context(browsing_context)
  97. , m_edit_event_handler(make<EditEventHandler>(browsing_context))
  98. {
  99. }
  100. EventHandler::~EventHandler() = default;
  101. Layout::InitialContainingBlock const* EventHandler::layout_root() const
  102. {
  103. if (!m_browsing_context.active_document())
  104. return nullptr;
  105. return m_browsing_context.active_document()->layout_node();
  106. }
  107. Layout::InitialContainingBlock* EventHandler::layout_root()
  108. {
  109. if (!m_browsing_context.active_document())
  110. return nullptr;
  111. return m_browsing_context.active_document()->layout_node();
  112. }
  113. Painting::PaintableBox* EventHandler::paint_root()
  114. {
  115. if (!m_browsing_context.active_document())
  116. return nullptr;
  117. return const_cast<Painting::PaintableBox*>(m_browsing_context.active_document()->paint_box());
  118. }
  119. Painting::PaintableBox const* EventHandler::paint_root() const
  120. {
  121. if (!m_browsing_context.active_document())
  122. return nullptr;
  123. return const_cast<Painting::PaintableBox*>(m_browsing_context.active_document()->paint_box());
  124. }
  125. bool EventHandler::handle_mousewheel(Gfx::IntPoint const& position, unsigned button, unsigned buttons, unsigned int modifiers, int wheel_delta_x, int wheel_delta_y)
  126. {
  127. if (m_browsing_context.active_document())
  128. m_browsing_context.active_document()->update_layout();
  129. if (!paint_root())
  130. return false;
  131. if (modifiers & KeyModifier::Mod_Shift)
  132. swap(wheel_delta_x, wheel_delta_y);
  133. bool handled_event = false;
  134. RefPtr<Painting::Paintable> paintable;
  135. if (m_mouse_event_tracking_layout_node) {
  136. paintable = m_mouse_event_tracking_layout_node->paintable();
  137. } else {
  138. if (auto result = paint_root()->hit_test(position.to_type<float>(), Painting::HitTestType::Exact); result.has_value())
  139. paintable = result->paintable;
  140. }
  141. if (paintable) {
  142. paintable->handle_mousewheel({}, position, buttons, modifiers, wheel_delta_x, wheel_delta_y);
  143. auto node = dom_node_for_event_dispatch(*paintable);
  144. if (node) {
  145. // FIXME: Support wheel events in nested browsing contexts.
  146. if (is<HTML::HTMLIFrameElement>(*node)) {
  147. return false;
  148. }
  149. // Search for the first parent of the hit target that's an element.
  150. auto* layout_node = &paintable->layout_node();
  151. while (layout_node && node && !node->is_element() && layout_node->parent()) {
  152. layout_node = layout_node->parent();
  153. node = layout_node->dom_node();
  154. }
  155. if (!node || !layout_node) {
  156. return false;
  157. }
  158. auto offset = compute_mouse_event_offset(position, *layout_node);
  159. 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))) {
  160. if (auto* page = m_browsing_context.page()) {
  161. page->client().page_did_request_scroll(wheel_delta_x * 20, wheel_delta_y * 20);
  162. }
  163. }
  164. handled_event = true;
  165. }
  166. }
  167. return handled_event;
  168. }
  169. bool EventHandler::handle_mouseup(Gfx::IntPoint const& position, unsigned button, unsigned buttons, unsigned modifiers)
  170. {
  171. if (m_browsing_context.active_document())
  172. m_browsing_context.active_document()->update_layout();
  173. if (!paint_root())
  174. return false;
  175. bool handled_event = false;
  176. RefPtr<Painting::Paintable> paintable;
  177. if (m_mouse_event_tracking_layout_node) {
  178. paintable = m_mouse_event_tracking_layout_node->paintable();
  179. } else {
  180. if (auto result = paint_root()->hit_test(position.to_type<float>(), Painting::HitTestType::Exact); result.has_value())
  181. paintable = result->paintable;
  182. }
  183. if (paintable && paintable->wants_mouse_events()) {
  184. if (paintable->handle_mouseup({}, position, button, modifiers) == Painting::Paintable::DispatchEventOfSameName::No)
  185. return false;
  186. // Things may have changed as a consequence of Layout::Node::handle_mouseup(). Hit test again.
  187. if (!paint_root())
  188. return true;
  189. if (auto result = paint_root()->hit_test(position.to_type<float>(), Painting::HitTestType::Exact); result.has_value())
  190. paintable = result->paintable;
  191. }
  192. if (paintable) {
  193. auto node = dom_node_for_event_dispatch(*paintable);
  194. if (node) {
  195. if (is<HTML::HTMLIFrameElement>(*node)) {
  196. if (auto* nested_browsing_context = static_cast<HTML::HTMLIFrameElement&>(*node).nested_browsing_context())
  197. return nested_browsing_context->event_handler().handle_mouseup(position.translated(compute_mouse_event_offset({}, paintable->layout_node())), button, buttons, modifiers);
  198. return false;
  199. }
  200. // Search for the first parent of the hit target that's an element.
  201. // "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)
  202. // "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)
  203. auto* layout_node = &paintable->layout_node();
  204. while (layout_node && node && !node->is_element() && layout_node->parent()) {
  205. layout_node = layout_node->parent();
  206. node = layout_node->dom_node();
  207. }
  208. if (!node || !layout_node) {
  209. // FIXME: This is pretty ugly but we need to bail out here.
  210. goto after_node_use;
  211. }
  212. auto offset = compute_mouse_event_offset(position, *layout_node);
  213. node->dispatch_event(*UIEvents::MouseEvent::create_from_platform_event(node->realm(), UIEvents::EventNames::mouseup, offset.x(), offset.y(), position.x(), position.y(), buttons, button));
  214. handled_event = true;
  215. bool run_activation_behavior = true;
  216. if (node.ptr() == m_mousedown_target && button == GUI::MouseButton::Primary) {
  217. 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));
  218. }
  219. if (run_activation_behavior) {
  220. // FIXME: This is ad-hoc and incorrect. The reason this exists is
  221. // because we are missing browsing context navigation:
  222. //
  223. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#navigate
  224. //
  225. // Additionally, we currently cannot spawn a new top-level
  226. // browsing context for new tab operations, because the new
  227. // top-level browsing context would be in another process. To
  228. // fix this, there needs to be some way to be able to
  229. // communicate with browsing contexts in remote WebContent
  230. // processes, and then step 8 of this algorithm needs to be
  231. // implemented in BrowsingContext::choose_a_browsing_context:
  232. //
  233. // https://html.spec.whatwg.org/multipage/browsers.html#the-rules-for-choosing-a-browsing-context-given-a-browsing-context-name
  234. if (JS::GCPtr<HTML::HTMLAnchorElement> link = node->enclosing_link_element()) {
  235. JS::NonnullGCPtr<DOM::Document> document = *m_browsing_context.active_document();
  236. auto href = link->href();
  237. auto url = document->parse_url(href);
  238. dbgln("Web::EventHandler: Clicking on a link to {}", url);
  239. if (button == GUI::MouseButton::Primary) {
  240. if (href.starts_with("javascript:"sv)) {
  241. document->run_javascript(href.substring_view(11, href.length() - 11));
  242. } else if (!url.fragment().is_null() && url.equals(document->url(), AK::URL::ExcludeFragment::Yes)) {
  243. m_browsing_context.scroll_to_anchor(url.fragment());
  244. } else {
  245. if (m_browsing_context.is_top_level()) {
  246. if (auto* page = m_browsing_context.page())
  247. page->client().page_did_click_link(url, link->target(), modifiers);
  248. }
  249. }
  250. } else if (button == GUI::MouseButton::Middle) {
  251. if (auto* page = m_browsing_context.page())
  252. page->client().page_did_middle_click_link(url, link->target(), modifiers);
  253. } else if (button == GUI::MouseButton::Secondary) {
  254. if (auto* page = m_browsing_context.page())
  255. page->client().page_did_request_link_context_menu(m_browsing_context.to_top_level_position(position), url, link->target(), modifiers);
  256. }
  257. } else if (button == GUI::MouseButton::Secondary) {
  258. if (is<HTML::HTMLImageElement>(*node)) {
  259. auto& image_element = verify_cast<HTML::HTMLImageElement>(*node);
  260. auto image_url = image_element.document().parse_url(image_element.src());
  261. if (auto* page = m_browsing_context.page())
  262. page->client().page_did_request_image_context_menu(m_browsing_context.to_top_level_position(position), image_url, "", modifiers, image_element.bitmap());
  263. } else if (auto* page = m_browsing_context.page()) {
  264. page->client().page_did_request_context_menu(m_browsing_context.to_top_level_position(position));
  265. }
  266. }
  267. }
  268. }
  269. }
  270. after_node_use:
  271. if (button == GUI::MouseButton::Primary)
  272. m_in_mouse_selection = false;
  273. return handled_event;
  274. }
  275. bool EventHandler::handle_mousedown(Gfx::IntPoint const& position, unsigned button, unsigned buttons, unsigned modifiers)
  276. {
  277. if (m_browsing_context.active_document())
  278. m_browsing_context.active_document()->update_layout();
  279. if (!paint_root())
  280. return false;
  281. JS::NonnullGCPtr<DOM::Document> document = *m_browsing_context.active_document();
  282. JS::GCPtr<DOM::Node> node;
  283. {
  284. RefPtr<Painting::Paintable> paintable;
  285. if (m_mouse_event_tracking_layout_node) {
  286. paintable = m_mouse_event_tracking_layout_node->paintable();
  287. } else {
  288. auto result = paint_root()->hit_test(position.to_type<float>(), Painting::HitTestType::Exact);
  289. if (!result.has_value())
  290. return false;
  291. paintable = result->paintable;
  292. }
  293. auto pointer_events = paintable->computed_values().pointer_events();
  294. // FIXME: Handle other values for pointer-events.
  295. VERIFY(pointer_events != CSS::PointerEvents::None);
  296. node = dom_node_for_event_dispatch(*paintable);
  297. document->set_hovered_node(node);
  298. if (paintable->wants_mouse_events()) {
  299. if (paintable->handle_mousedown({}, position, button, modifiers) == Painting::Paintable::DispatchEventOfSameName::No)
  300. return false;
  301. }
  302. if (!node)
  303. return false;
  304. if (is<HTML::HTMLIFrameElement>(*node)) {
  305. if (auto* nested_browsing_context = static_cast<HTML::HTMLIFrameElement&>(*node).nested_browsing_context())
  306. return nested_browsing_context->event_handler().handle_mousedown(position.translated(compute_mouse_event_offset({}, paintable->layout_node())), button, buttons, modifiers);
  307. return false;
  308. }
  309. if (auto* page = m_browsing_context.page())
  310. page->set_focused_browsing_context({}, m_browsing_context);
  311. // Search for the first parent of the hit target that's an element.
  312. // "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)
  313. // "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)
  314. auto* layout_node = &paintable->layout_node();
  315. while (layout_node && node && !node->is_element() && layout_node->parent()) {
  316. layout_node = layout_node->parent();
  317. node = layout_node->dom_node();
  318. }
  319. if (!node || !layout_node)
  320. return false;
  321. m_mousedown_target = node.ptr();
  322. auto offset = compute_mouse_event_offset(position, *layout_node);
  323. node->dispatch_event(*UIEvents::MouseEvent::create_from_platform_event(node->realm(), UIEvents::EventNames::mousedown, offset.x(), offset.y(), position.x(), position.y(), buttons, button));
  324. }
  325. // NOTE: Dispatching an event may have disturbed the world.
  326. if (!paint_root() || paint_root() != node->document().paint_box())
  327. return true;
  328. if (button == GUI::MouseButton::Primary) {
  329. if (auto result = paint_root()->hit_test(position.to_type<float>(), Painting::HitTestType::TextCursor); result.has_value()) {
  330. auto paintable = result->paintable;
  331. if (paintable->dom_node()) {
  332. // See if we want to focus something.
  333. bool did_focus_something = false;
  334. for (auto candidate = node; candidate; candidate = candidate->parent()) {
  335. if (candidate->is_focusable()) {
  336. document->set_focused_element(verify_cast<DOM::Element>(candidate.ptr()));
  337. did_focus_something = true;
  338. break;
  339. }
  340. }
  341. // If we didn't focus anything, place the document text cursor at the mouse position.
  342. // FIXME: This is all rather strange. Find a better solution.
  343. if (!did_focus_something) {
  344. m_browsing_context.set_cursor_position(DOM::Position(*paintable->dom_node(), result->index_in_node));
  345. layout_root()->set_selection({ { paintable->layout_node(), result->index_in_node }, {} });
  346. m_in_mouse_selection = true;
  347. }
  348. }
  349. }
  350. }
  351. return true;
  352. }
  353. bool EventHandler::handle_mousemove(Gfx::IntPoint const& position, unsigned buttons, unsigned modifiers)
  354. {
  355. if (m_browsing_context.active_document())
  356. m_browsing_context.active_document()->update_layout();
  357. if (!paint_root())
  358. return false;
  359. auto& document = *m_browsing_context.active_document();
  360. bool hovered_node_changed = false;
  361. bool is_hovering_link = false;
  362. Gfx::StandardCursor hovered_node_cursor = Gfx::StandardCursor::None;
  363. RefPtr<Painting::Paintable> paintable;
  364. Optional<int> start_index;
  365. if (m_mouse_event_tracking_layout_node) {
  366. paintable = m_mouse_event_tracking_layout_node->paintable();
  367. } else {
  368. if (auto result = paint_root()->hit_test(position.to_type<float>(), Painting::HitTestType::Exact); result.has_value()) {
  369. paintable = result->paintable;
  370. start_index = result->index_in_node;
  371. }
  372. }
  373. const HTML::HTMLAnchorElement* hovered_link_element = nullptr;
  374. if (paintable) {
  375. if (paintable->wants_mouse_events()) {
  376. document.set_hovered_node(paintable->dom_node());
  377. if (paintable->handle_mousemove({}, position, buttons, modifiers) == Painting::Paintable::DispatchEventOfSameName::No)
  378. return false;
  379. // FIXME: It feels a bit aggressive to always update the cursor like this.
  380. if (auto* page = m_browsing_context.page())
  381. page->client().page_did_request_cursor_change(Gfx::StandardCursor::None);
  382. }
  383. auto node = dom_node_for_event_dispatch(*paintable);
  384. if (node && is<HTML::HTMLIFrameElement>(*node)) {
  385. if (auto* nested_browsing_context = static_cast<HTML::HTMLIFrameElement&>(*node).nested_browsing_context())
  386. return nested_browsing_context->event_handler().handle_mousemove(position.translated(compute_mouse_event_offset({}, paintable->layout_node())), buttons, modifiers);
  387. return false;
  388. }
  389. auto pointer_events = paintable->computed_values().pointer_events();
  390. // FIXME: Handle other values for pointer-events.
  391. VERIFY(pointer_events != CSS::PointerEvents::None);
  392. hovered_node_changed = node.ptr() != document.hovered_node();
  393. document.set_hovered_node(node);
  394. if (node) {
  395. hovered_link_element = node->enclosing_link_element();
  396. if (hovered_link_element)
  397. is_hovering_link = true;
  398. if (node->is_text()) {
  399. auto cursor = paintable->computed_values().cursor();
  400. if (cursor == CSS::Cursor::Auto)
  401. hovered_node_cursor = Gfx::StandardCursor::IBeam;
  402. else
  403. hovered_node_cursor = cursor_css_to_gfx(cursor);
  404. } else if (node->is_element()) {
  405. auto cursor = paintable->computed_values().cursor();
  406. if (cursor == CSS::Cursor::Auto)
  407. hovered_node_cursor = Gfx::StandardCursor::Arrow;
  408. else
  409. hovered_node_cursor = cursor_css_to_gfx(cursor);
  410. }
  411. // Search for the first parent of the hit target that's an element.
  412. // "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)
  413. // "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)
  414. auto* layout_node = &paintable->layout_node();
  415. while (layout_node && node && !node->is_element() && layout_node->parent()) {
  416. layout_node = layout_node->parent();
  417. node = layout_node->dom_node();
  418. }
  419. if (!node || !layout_node) {
  420. // FIXME: This is pretty ugly but we need to bail out here.
  421. goto after_node_use;
  422. }
  423. auto offset = compute_mouse_event_offset(position, *layout_node);
  424. node->dispatch_event(*UIEvents::MouseEvent::create_from_platform_event(node->realm(), UIEvents::EventNames::mousemove, offset.x(), offset.y(), position.x(), position.y(), buttons));
  425. // NOTE: Dispatching an event may have disturbed the world.
  426. if (!paint_root() || paint_root() != node->document().paint_box())
  427. return true;
  428. }
  429. after_node_use:
  430. if (m_in_mouse_selection) {
  431. auto hit = paint_root()->hit_test(position.to_type<float>(), Painting::HitTestType::TextCursor);
  432. if (start_index.has_value() && hit.has_value() && hit->dom_node()) {
  433. m_browsing_context.set_cursor_position(DOM::Position(*hit->dom_node(), *start_index));
  434. layout_root()->set_selection_end({ hit->paintable->layout_node(), hit->index_in_node });
  435. m_browsing_context.set_needs_display();
  436. }
  437. if (auto* page = m_browsing_context.page())
  438. page->client().page_did_change_selection();
  439. }
  440. }
  441. if (auto* page = m_browsing_context.page()) {
  442. page->client().page_did_request_cursor_change(hovered_node_cursor);
  443. if (hovered_node_changed) {
  444. JS::GCPtr<HTML::HTMLElement> hovered_html_element = document.hovered_node() ? document.hovered_node()->enclosing_html_element_with_attribute(HTML::AttributeNames::title) : nullptr;
  445. if (hovered_html_element && !hovered_html_element->title().is_null()) {
  446. page->client().page_did_enter_tooltip_area(m_browsing_context.to_top_level_position(position), hovered_html_element->title());
  447. } else {
  448. page->client().page_did_leave_tooltip_area();
  449. }
  450. if (is_hovering_link)
  451. page->client().page_did_hover_link(document.parse_url(hovered_link_element->href()));
  452. else
  453. page->client().page_did_unhover_link();
  454. }
  455. }
  456. return true;
  457. }
  458. bool EventHandler::handle_doubleclick(Gfx::IntPoint const& position, unsigned button, unsigned buttons, unsigned modifiers)
  459. {
  460. if (m_browsing_context.active_document())
  461. m_browsing_context.active_document()->update_layout();
  462. if (!paint_root())
  463. return false;
  464. RefPtr<Painting::Paintable> paintable;
  465. if (m_mouse_event_tracking_layout_node) {
  466. paintable = m_mouse_event_tracking_layout_node->paintable();
  467. } else {
  468. auto result = paint_root()->hit_test(position.to_type<float>(), Painting::HitTestType::Exact);
  469. if (!result.has_value())
  470. return false;
  471. paintable = result->paintable;
  472. }
  473. auto pointer_events = paintable->computed_values().pointer_events();
  474. // FIXME: Handle other values for pointer-events.
  475. if (pointer_events == CSS::PointerEvents::None)
  476. return false;
  477. auto node = dom_node_for_event_dispatch(*paintable);
  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. }