EventHandler.cpp 26 KB

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