EventHandler.cpp 33 KB

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