EventHandler.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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. auto offset = compute_mouse_event_offset(position, paintable->layout_node());
  164. node->dispatch_event(UIEvents::MouseEvent::create(UIEvents::EventNames::mouseup, offset.x(), offset.y(), position.x(), position.y()));
  165. handled_event = true;
  166. // FIXME: This is ad-hoc and incorrect. The reason this exists is
  167. // because we are missing browsing context navigation:
  168. //
  169. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#navigate
  170. //
  171. // Additionally, we currently cannot spawn a new top-level
  172. // browsing context for new tab operations, because the new
  173. // top-level browsing context would be in another process. To
  174. // fix this, there needs to be some way to be able to
  175. // communicate with browsing contexts in remote WebContent
  176. // processes, and then step 8 of this algorithm needs to be
  177. // implemented in BrowsingContext::choose_a_browsing_context:
  178. //
  179. // https://html.spec.whatwg.org/multipage/browsers.html#the-rules-for-choosing-a-browsing-context-given-a-browsing-context-name
  180. if (RefPtr<HTML::HTMLAnchorElement> link = node->enclosing_link_element()) {
  181. NonnullRefPtr document = *m_browsing_context.active_document();
  182. auto href = link->href();
  183. auto url = document->parse_url(href);
  184. dbgln("Web::EventHandler: Clicking on a link to {}", url);
  185. if (button == GUI::MouseButton::Primary) {
  186. if (href.starts_with("javascript:")) {
  187. document->run_javascript(href.substring_view(11, href.length() - 11));
  188. } else if (!url.fragment().is_null() && url.equals(document->url(), AK::URL::ExcludeFragment::Yes)) {
  189. m_browsing_context.scroll_to_anchor(url.fragment());
  190. } else {
  191. if (m_browsing_context.is_top_level()) {
  192. if (auto* page = m_browsing_context.page())
  193. page->client().page_did_click_link(url, link->target(), modifiers);
  194. }
  195. }
  196. } else if (button == GUI::MouseButton::Middle) {
  197. if (auto* page = m_browsing_context.page())
  198. page->client().page_did_middle_click_link(url, link->target(), modifiers);
  199. } else if (button == GUI::MouseButton::Secondary) {
  200. if (auto* page = m_browsing_context.page())
  201. page->client().page_did_request_link_context_menu(m_browsing_context.to_top_level_position(position), url, link->target(), modifiers);
  202. }
  203. } else if (button == GUI::MouseButton::Secondary) {
  204. if (is<HTML::HTMLImageElement>(*node)) {
  205. auto& image_element = verify_cast<HTML::HTMLImageElement>(*node);
  206. auto image_url = image_element.document().parse_url(image_element.src());
  207. if (auto* page = m_browsing_context.page())
  208. page->client().page_did_request_image_context_menu(m_browsing_context.to_top_level_position(position), image_url, "", modifiers, image_element.bitmap());
  209. } else if (auto* page = m_browsing_context.page()) {
  210. page->client().page_did_request_context_menu(m_browsing_context.to_top_level_position(position));
  211. }
  212. }
  213. if (node.ptr() == m_mousedown_target && button == GUI::MouseButton::Primary) {
  214. node->dispatch_event(UIEvents::MouseEvent::create(UIEvents::EventNames::click, offset.x(), offset.y(), position.x(), position.y()));
  215. }
  216. }
  217. }
  218. if (button == GUI::MouseButton::Primary)
  219. m_in_mouse_selection = false;
  220. return handled_event;
  221. }
  222. bool EventHandler::handle_mousedown(Gfx::IntPoint const& position, unsigned button, unsigned modifiers)
  223. {
  224. if (m_browsing_context.active_document())
  225. m_browsing_context.active_document()->update_layout();
  226. if (!paint_root())
  227. return false;
  228. NonnullRefPtr document = *m_browsing_context.active_document();
  229. RefPtr<DOM::Node> node;
  230. {
  231. // TODO: Allow selecting element behind if one on top has pointer-events set to none.
  232. RefPtr<Painting::Paintable> paintable;
  233. if (m_mouse_event_tracking_layout_node) {
  234. paintable = m_mouse_event_tracking_layout_node->paintable();
  235. } else {
  236. auto result = paint_root()->hit_test(position.to_type<float>(), Painting::HitTestType::Exact);
  237. if (!result.has_value())
  238. return false;
  239. paintable = result->paintable;
  240. }
  241. auto pointer_events = paintable->computed_values().pointer_events();
  242. // FIXME: Handle other values for pointer-events.
  243. if (pointer_events == CSS::PointerEvents::None)
  244. return false;
  245. node = paintable->mouse_event_target();
  246. if (!node)
  247. node = paintable->dom_node();
  248. document->set_hovered_node(node);
  249. if (paintable->wants_mouse_events()) {
  250. if (paintable->handle_mousedown({}, position, button, modifiers) == Painting::Paintable::DispatchEventOfSameName::No)
  251. return false;
  252. }
  253. if (!node)
  254. return false;
  255. if (is<HTML::HTMLIFrameElement>(*node)) {
  256. if (auto* nested_browsing_context = static_cast<HTML::HTMLIFrameElement&>(*node).nested_browsing_context())
  257. return nested_browsing_context->event_handler().handle_mousedown(position.translated(compute_mouse_event_offset({}, paintable->layout_node())), button, modifiers);
  258. return false;
  259. }
  260. if (auto* page = m_browsing_context.page())
  261. page->set_focused_browsing_context({}, m_browsing_context);
  262. m_mousedown_target = node;
  263. auto offset = compute_mouse_event_offset(position, paintable->layout_node());
  264. node->dispatch_event(UIEvents::MouseEvent::create(UIEvents::EventNames::mousedown, offset.x(), offset.y(), position.x(), position.y()));
  265. }
  266. // NOTE: Dispatching an event may have disturbed the world.
  267. if (!paint_root() || paint_root() != node->document().paint_box())
  268. return true;
  269. if (button == GUI::MouseButton::Primary) {
  270. if (auto result = paint_root()->hit_test(position.to_type<float>(), Painting::HitTestType::TextCursor); result.has_value()) {
  271. auto paintable = result->paintable;
  272. if (paintable->dom_node()) {
  273. // See if we want to focus something.
  274. bool did_focus_something = false;
  275. for (auto candidate = node; candidate; candidate = candidate->parent()) {
  276. if (candidate->is_focusable()) {
  277. document->set_focused_element(verify_cast<DOM::Element>(candidate.ptr()));
  278. did_focus_something = true;
  279. break;
  280. }
  281. }
  282. // If we didn't focus anything, place the document text cursor at the mouse position.
  283. // FIXME: This is all rather strange. Find a better solution.
  284. if (!did_focus_something) {
  285. m_browsing_context.set_cursor_position(DOM::Position(*paintable->dom_node(), result->index_in_node));
  286. layout_root()->set_selection({ { paintable->layout_node(), result->index_in_node }, {} });
  287. m_in_mouse_selection = true;
  288. }
  289. }
  290. }
  291. }
  292. return true;
  293. }
  294. bool EventHandler::handle_mousemove(Gfx::IntPoint const& position, unsigned buttons, unsigned modifiers)
  295. {
  296. if (m_browsing_context.active_document())
  297. m_browsing_context.active_document()->update_layout();
  298. if (!paint_root())
  299. return false;
  300. auto& document = *m_browsing_context.active_document();
  301. bool hovered_node_changed = false;
  302. bool is_hovering_link = false;
  303. Gfx::StandardCursor hovered_node_cursor = Gfx::StandardCursor::None;
  304. RefPtr<Painting::Paintable> paintable;
  305. Optional<int> start_index;
  306. if (m_mouse_event_tracking_layout_node) {
  307. paintable = m_mouse_event_tracking_layout_node->paintable();
  308. } else {
  309. if (auto result = paint_root()->hit_test(position.to_type<float>(), Painting::HitTestType::Exact); result.has_value()) {
  310. paintable = result->paintable;
  311. start_index = result->index_in_node;
  312. }
  313. }
  314. const HTML::HTMLAnchorElement* hovered_link_element = nullptr;
  315. if (paintable) {
  316. if (paintable->wants_mouse_events()) {
  317. document.set_hovered_node(paintable->dom_node());
  318. if (paintable->handle_mousemove({}, position, buttons, modifiers) == Painting::Paintable::DispatchEventOfSameName::No)
  319. return false;
  320. // FIXME: It feels a bit aggressive to always update the cursor like this.
  321. if (auto* page = m_browsing_context.page())
  322. page->client().page_did_request_cursor_change(Gfx::StandardCursor::None);
  323. }
  324. RefPtr<DOM::Node> node = paintable->mouse_event_target();
  325. if (!node)
  326. node = paintable->dom_node();
  327. if (node && is<HTML::HTMLIFrameElement>(*node)) {
  328. if (auto* nested_browsing_context = static_cast<HTML::HTMLIFrameElement&>(*node).nested_browsing_context())
  329. return nested_browsing_context->event_handler().handle_mousemove(position.translated(compute_mouse_event_offset({}, paintable->layout_node())), buttons, modifiers);
  330. return false;
  331. }
  332. auto pointer_events = paintable->computed_values().pointer_events();
  333. // FIXME: Handle other values for pointer-events.
  334. if (pointer_events == CSS::PointerEvents::None)
  335. return false;
  336. hovered_node_changed = node != document.hovered_node();
  337. document.set_hovered_node(node);
  338. if (node) {
  339. hovered_link_element = node->enclosing_link_element();
  340. if (hovered_link_element)
  341. is_hovering_link = true;
  342. if (node->is_text()) {
  343. auto cursor = paintable->computed_values().cursor();
  344. if (cursor == CSS::Cursor::Auto)
  345. hovered_node_cursor = Gfx::StandardCursor::IBeam;
  346. else
  347. hovered_node_cursor = cursor_css_to_gfx(cursor);
  348. } else if (node->is_element()) {
  349. auto cursor = paintable->computed_values().cursor();
  350. if (cursor == CSS::Cursor::Auto)
  351. hovered_node_cursor = Gfx::StandardCursor::Arrow;
  352. else
  353. hovered_node_cursor = cursor_css_to_gfx(cursor);
  354. }
  355. auto offset = compute_mouse_event_offset(position, paintable->layout_node());
  356. node->dispatch_event(UIEvents::MouseEvent::create(UIEvents::EventNames::mousemove, offset.x(), offset.y(), position.x(), position.y()));
  357. // NOTE: Dispatching an event may have disturbed the world.
  358. if (!paint_root() || paint_root() != node->document().paint_box())
  359. return true;
  360. }
  361. if (m_in_mouse_selection) {
  362. auto hit = paint_root()->hit_test(position.to_type<float>(), Painting::HitTestType::TextCursor);
  363. if (start_index.has_value() && hit.has_value() && hit->dom_node()) {
  364. m_browsing_context.set_cursor_position(DOM::Position(*hit->dom_node(), *start_index));
  365. layout_root()->set_selection_end({ hit->paintable->layout_node(), hit->index_in_node });
  366. }
  367. if (auto* page = m_browsing_context.page())
  368. page->client().page_did_change_selection();
  369. }
  370. }
  371. if (auto* page = m_browsing_context.page()) {
  372. page->client().page_did_request_cursor_change(hovered_node_cursor);
  373. if (hovered_node_changed) {
  374. RefPtr<HTML::HTMLElement> hovered_html_element = document.hovered_node() ? document.hovered_node()->enclosing_html_element_with_attribute(HTML::AttributeNames::title) : nullptr;
  375. if (hovered_html_element && !hovered_html_element->title().is_null()) {
  376. page->client().page_did_enter_tooltip_area(m_browsing_context.to_top_level_position(position), hovered_html_element->title());
  377. } else {
  378. page->client().page_did_leave_tooltip_area();
  379. }
  380. if (is_hovering_link)
  381. page->client().page_did_hover_link(document.parse_url(hovered_link_element->href()));
  382. else
  383. page->client().page_did_unhover_link();
  384. }
  385. }
  386. return true;
  387. }
  388. bool EventHandler::focus_next_element()
  389. {
  390. if (!m_browsing_context.active_document())
  391. return false;
  392. auto* element = m_browsing_context.active_document()->focused_element();
  393. if (!element) {
  394. element = m_browsing_context.active_document()->first_child_of_type<DOM::Element>();
  395. if (element && element->is_focusable()) {
  396. m_browsing_context.active_document()->set_focused_element(element);
  397. return true;
  398. }
  399. }
  400. for (element = element->next_element_in_pre_order(); element && !element->is_focusable(); element = element->next_element_in_pre_order())
  401. ;
  402. m_browsing_context.active_document()->set_focused_element(element);
  403. return element;
  404. }
  405. bool EventHandler::focus_previous_element()
  406. {
  407. if (!m_browsing_context.active_document())
  408. return false;
  409. auto* element = m_browsing_context.active_document()->focused_element();
  410. if (!element) {
  411. element = m_browsing_context.active_document()->last_child_of_type<DOM::Element>();
  412. if (element && element->is_focusable()) {
  413. m_browsing_context.active_document()->set_focused_element(element);
  414. return true;
  415. }
  416. }
  417. for (element = element->previous_element_in_pre_order(); element && !element->is_focusable(); element = element->previous_element_in_pre_order())
  418. ;
  419. m_browsing_context.active_document()->set_focused_element(element);
  420. return element;
  421. }
  422. constexpr bool should_ignore_keydown_event(u32 code_point)
  423. {
  424. // FIXME: There are probably also keys with non-zero code points that should be filtered out.
  425. return code_point == 0 || code_point == 27;
  426. }
  427. bool EventHandler::handle_keydown(KeyCode key, unsigned modifiers, u32 code_point)
  428. {
  429. if (!m_browsing_context.active_document())
  430. return false;
  431. NonnullRefPtr<DOM::Document> document = *m_browsing_context.active_document();
  432. if (!document->layout_node())
  433. return false;
  434. NonnullRefPtr<Layout::InitialContainingBlock> layout_root = *document->layout_node();
  435. if (key == KeyCode::Key_Tab) {
  436. if (modifiers & KeyModifier::Mod_Shift)
  437. return focus_previous_element();
  438. return focus_next_element();
  439. }
  440. if (layout_root->selection().is_valid()) {
  441. auto range = layout_root->selection().to_dom_range()->normalized();
  442. if (range->start_container()->is_editable()) {
  443. layout_root->set_selection({});
  444. // FIXME: This doesn't work for some reason?
  445. m_browsing_context.set_cursor_position({ *range->start_container(), range->start_offset() });
  446. if (key == KeyCode::Key_Backspace || key == KeyCode::Key_Delete) {
  447. m_edit_event_handler->handle_delete(range);
  448. return true;
  449. }
  450. if (!should_ignore_keydown_event(code_point)) {
  451. m_edit_event_handler->handle_delete(range);
  452. m_edit_event_handler->handle_insert(m_browsing_context.cursor_position(), code_point);
  453. m_browsing_context.increment_cursor_position_offset();
  454. return true;
  455. }
  456. }
  457. }
  458. if (m_browsing_context.cursor_position().is_valid() && m_browsing_context.cursor_position().node()->is_editable()) {
  459. if (key == KeyCode::Key_Backspace) {
  460. if (!m_browsing_context.decrement_cursor_position_offset()) {
  461. // FIXME: Move to the previous node and delete the last character there.
  462. return true;
  463. }
  464. m_edit_event_handler->handle_delete_character_after(m_browsing_context.cursor_position());
  465. return true;
  466. }
  467. if (key == KeyCode::Key_Delete) {
  468. if (m_browsing_context.cursor_position().offset_is_at_end_of_node()) {
  469. // FIXME: Move to the next node and delete the first character there.
  470. return true;
  471. }
  472. m_edit_event_handler->handle_delete_character_after(m_browsing_context.cursor_position());
  473. return true;
  474. }
  475. if (key == KeyCode::Key_Right) {
  476. if (!m_browsing_context.increment_cursor_position_offset()) {
  477. // FIXME: Move to the next node.
  478. }
  479. return true;
  480. }
  481. if (key == KeyCode::Key_Left) {
  482. if (!m_browsing_context.decrement_cursor_position_offset()) {
  483. // FIXME: Move to the previous node.
  484. }
  485. return true;
  486. }
  487. if (key == KeyCode::Key_Home) {
  488. auto& node = *static_cast<DOM::Text*>(const_cast<DOM::Node*>(m_browsing_context.cursor_position().node()));
  489. m_browsing_context.set_cursor_position(DOM::Position { node, 0 });
  490. return true;
  491. }
  492. if (key == KeyCode::Key_End) {
  493. auto& node = *static_cast<DOM::Text*>(const_cast<DOM::Node*>(m_browsing_context.cursor_position().node()));
  494. m_browsing_context.set_cursor_position(DOM::Position { node, (unsigned)node.data().length() });
  495. return true;
  496. }
  497. if (!should_ignore_keydown_event(code_point)) {
  498. m_edit_event_handler->handle_insert(m_browsing_context.cursor_position(), code_point);
  499. m_browsing_context.increment_cursor_position_offset();
  500. return true;
  501. }
  502. // NOTE: Because modifier keys should be ignored, we need to return true.
  503. return true;
  504. }
  505. auto event = UIEvents::KeyboardEvent::create_from_platform_event(UIEvents::EventNames::keydown, key, modifiers, code_point);
  506. if (RefPtr<DOM::Element> focused_element = document->focused_element())
  507. return focused_element->dispatch_event(move(event));
  508. if (RefPtr<HTML::HTMLElement> body = m_browsing_context.active_document()->body())
  509. return body->dispatch_event(move(event));
  510. return document->root().dispatch_event(move(event));
  511. }
  512. bool EventHandler::handle_keyup(KeyCode key, unsigned modifiers, u32 code_point)
  513. {
  514. RefPtr<DOM::Document> document = m_browsing_context.active_document();
  515. if (!document)
  516. return false;
  517. auto event = UIEvents::KeyboardEvent::create_from_platform_event(UIEvents::EventNames::keyup, key, modifiers, code_point);
  518. if (RefPtr<DOM::Element> focused_element = document->focused_element())
  519. return document->focused_element()->dispatch_event(move(event));
  520. if (RefPtr<HTML::HTMLElement> body = document->body())
  521. return body->dispatch_event(move(event));
  522. return document->root().dispatch_event(move(event));
  523. }
  524. void EventHandler::set_mouse_event_tracking_layout_node(Layout::Node* layout_node)
  525. {
  526. if (layout_node)
  527. m_mouse_event_tracking_layout_node = layout_node->make_weak_ptr();
  528. else
  529. m_mouse_event_tracking_layout_node = nullptr;
  530. }
  531. }