Frame.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/DOM/Document.h>
  7. #include <LibWeb/DOM/Event.h>
  8. #include <LibWeb/DOM/HTMLCollection.h>
  9. #include <LibWeb/DOM/Window.h>
  10. #include <LibWeb/HTML/HTMLAnchorElement.h>
  11. #include <LibWeb/InProcessWebView.h>
  12. #include <LibWeb/Layout/BreakNode.h>
  13. #include <LibWeb/Layout/InitialContainingBlockBox.h>
  14. #include <LibWeb/Layout/TextNode.h>
  15. #include <LibWeb/Page/Frame.h>
  16. #include <LibWeb/UIEvents/EventNames.h>
  17. namespace Web {
  18. Frame::Frame(DOM::Element& host_element, Frame& main_frame)
  19. : m_page(*main_frame.page())
  20. , m_main_frame(main_frame)
  21. , m_loader(*this)
  22. , m_event_handler({}, *this)
  23. , m_host_element(host_element)
  24. {
  25. setup();
  26. }
  27. Frame::Frame(Page& page)
  28. : m_page(page)
  29. , m_main_frame(*this)
  30. , m_loader(*this)
  31. , m_event_handler({}, *this)
  32. {
  33. setup();
  34. }
  35. Frame::~Frame()
  36. {
  37. }
  38. void Frame::setup()
  39. {
  40. m_cursor_blink_timer = Core::Timer::construct(500, [this] {
  41. if (!is_focused_frame())
  42. return;
  43. if (m_cursor_position.node() && m_cursor_position.node()->layout_node()) {
  44. m_cursor_blink_state = !m_cursor_blink_state;
  45. m_cursor_position.node()->layout_node()->set_needs_display();
  46. }
  47. });
  48. }
  49. void Frame::did_edit(Badge<EditEventHandler>)
  50. {
  51. reset_cursor_blink_cycle();
  52. }
  53. void Frame::reset_cursor_blink_cycle()
  54. {
  55. m_cursor_blink_state = true;
  56. m_cursor_blink_timer->restart();
  57. }
  58. bool Frame::is_focused_frame() const
  59. {
  60. return m_page && &m_page->focused_frame() == this;
  61. }
  62. void Frame::set_document(DOM::Document* document)
  63. {
  64. if (m_document == document)
  65. return;
  66. m_cursor_position = {};
  67. if (m_document)
  68. m_document->detach_from_frame({}, *this);
  69. m_document = document;
  70. if (m_document) {
  71. m_document->attach_to_frame({}, *this);
  72. if (m_page && is_main_frame())
  73. m_page->client().page_did_change_title(m_document->title());
  74. }
  75. if (m_page)
  76. m_page->client().page_did_set_document_in_main_frame(m_document);
  77. }
  78. void Frame::set_viewport_rect(const Gfx::IntRect& rect)
  79. {
  80. bool did_change = false;
  81. if (m_size != rect.size()) {
  82. m_size = rect.size();
  83. if (m_document) {
  84. m_document->window().dispatch_event(DOM::Event::create(UIEvents::EventNames::resize));
  85. m_document->update_layout();
  86. }
  87. did_change = true;
  88. }
  89. if (m_viewport_scroll_offset != rect.location()) {
  90. m_viewport_scroll_offset = rect.location();
  91. did_change = true;
  92. }
  93. if (did_change) {
  94. for (auto* client : m_viewport_clients)
  95. client->frame_did_set_viewport_rect(rect);
  96. }
  97. }
  98. void Frame::set_size(const Gfx::IntSize& size)
  99. {
  100. if (m_size == size)
  101. return;
  102. m_size = size;
  103. if (m_document) {
  104. m_document->window().dispatch_event(DOM::Event::create(UIEvents::EventNames::resize));
  105. m_document->update_layout();
  106. }
  107. for (auto* client : m_viewport_clients)
  108. client->frame_did_set_viewport_rect(viewport_rect());
  109. }
  110. void Frame::set_viewport_scroll_offset(const Gfx::IntPoint& offset)
  111. {
  112. if (m_viewport_scroll_offset == offset)
  113. return;
  114. m_viewport_scroll_offset = offset;
  115. for (auto* client : m_viewport_clients)
  116. client->frame_did_set_viewport_rect(viewport_rect());
  117. }
  118. void Frame::set_needs_display(const Gfx::IntRect& rect)
  119. {
  120. if (!viewport_rect().intersects(rect))
  121. return;
  122. if (is_main_frame()) {
  123. if (m_page)
  124. m_page->client().page_did_invalidate(to_main_frame_rect(rect));
  125. return;
  126. }
  127. if (host_element() && host_element()->layout_node())
  128. host_element()->layout_node()->set_needs_display();
  129. }
  130. void Frame::scroll_to_anchor(const String& fragment)
  131. {
  132. if (!document())
  133. return;
  134. auto element = document()->get_element_by_id(fragment);
  135. if (!element) {
  136. auto candidates = document()->get_elements_by_name(fragment);
  137. for (auto& candidate : candidates->collect_matching_elements()) {
  138. if (is<HTML::HTMLAnchorElement>(*candidate)) {
  139. element = downcast<HTML::HTMLAnchorElement>(*candidate);
  140. break;
  141. }
  142. }
  143. }
  144. // FIXME: This is overly aggressive and should be something more like a "update_layout_if_needed()"
  145. document()->force_layout();
  146. if (!element || !element->layout_node())
  147. return;
  148. auto& layout_node = *element->layout_node();
  149. Gfx::FloatRect float_rect { layout_node.box_type_agnostic_position(), { (float)viewport_rect().width(), (float)viewport_rect().height() } };
  150. if (is<Layout::Box>(layout_node)) {
  151. auto& layout_box = downcast<Layout::Box>(layout_node);
  152. auto padding_box = layout_box.box_model().padding_box();
  153. float_rect.translate_by(-padding_box.left, -padding_box.top);
  154. }
  155. if (m_page)
  156. m_page->client().page_did_request_scroll_into_view(enclosing_int_rect(float_rect));
  157. }
  158. Gfx::IntRect Frame::to_main_frame_rect(const Gfx::IntRect& a_rect)
  159. {
  160. auto rect = a_rect;
  161. rect.set_location(to_main_frame_position(a_rect.location()));
  162. return rect;
  163. }
  164. Gfx::IntPoint Frame::to_main_frame_position(const Gfx::IntPoint& a_position)
  165. {
  166. auto position = a_position;
  167. for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
  168. if (ancestor->is_main_frame())
  169. break;
  170. if (!ancestor->host_element())
  171. return {};
  172. if (!ancestor->host_element()->layout_node())
  173. return {};
  174. position.translate_by(ancestor->host_element()->layout_node()->box_type_agnostic_position().to_type<int>());
  175. }
  176. return position;
  177. }
  178. void Frame::set_cursor_position(DOM::Position position)
  179. {
  180. if (m_cursor_position == position)
  181. return;
  182. if (m_cursor_position.node() && m_cursor_position.node()->layout_node())
  183. m_cursor_position.node()->layout_node()->set_needs_display();
  184. m_cursor_position = move(position);
  185. if (m_cursor_position.node() && m_cursor_position.node()->layout_node())
  186. m_cursor_position.node()->layout_node()->set_needs_display();
  187. reset_cursor_blink_cycle();
  188. }
  189. String Frame::selected_text() const
  190. {
  191. StringBuilder builder;
  192. if (!m_document)
  193. return {};
  194. auto* layout_root = m_document->layout_node();
  195. if (!layout_root)
  196. return {};
  197. if (!layout_root->selection().is_valid())
  198. return {};
  199. auto selection = layout_root->selection().normalized();
  200. if (selection.start().layout_node == selection.end().layout_node) {
  201. if (!is<Layout::TextNode>(*selection.start().layout_node))
  202. return "";
  203. return downcast<Layout::TextNode>(*selection.start().layout_node).text_for_rendering().substring(selection.start().index_in_node, selection.end().index_in_node - selection.start().index_in_node);
  204. }
  205. // Start node
  206. auto layout_node = selection.start().layout_node;
  207. if (is<Layout::TextNode>(*layout_node)) {
  208. auto& text = downcast<Layout::TextNode>(*layout_node).text_for_rendering();
  209. builder.append(text.substring(selection.start().index_in_node, text.length() - selection.start().index_in_node));
  210. }
  211. // Middle nodes
  212. layout_node = layout_node->next_in_pre_order();
  213. while (layout_node && layout_node != selection.end().layout_node) {
  214. if (is<Layout::TextNode>(*layout_node))
  215. builder.append(downcast<Layout::TextNode>(*layout_node).text_for_rendering());
  216. else if (is<Layout::BreakNode>(*layout_node) || is<Layout::BlockBox>(*layout_node))
  217. builder.append('\n');
  218. layout_node = layout_node->next_in_pre_order();
  219. }
  220. // End node
  221. VERIFY(layout_node == selection.end().layout_node);
  222. if (is<Layout::TextNode>(*layout_node)) {
  223. auto& text = downcast<Layout::TextNode>(*layout_node).text_for_rendering();
  224. builder.append(text.substring(0, selection.end().index_in_node));
  225. }
  226. return builder.to_string();
  227. }
  228. void Frame::register_viewport_client(ViewportClient& client)
  229. {
  230. auto result = m_viewport_clients.set(&client);
  231. VERIFY(result == AK::HashSetResult::InsertedNewEntry);
  232. }
  233. void Frame::unregister_viewport_client(ViewportClient& client)
  234. {
  235. bool was_removed = m_viewport_clients.remove(&client);
  236. VERIFY(was_removed);
  237. }
  238. void Frame::register_frame_nesting(URL const& url)
  239. {
  240. m_frame_nesting_levels.ensure(url)++;
  241. }
  242. bool Frame::is_frame_nesting_allowed(URL const& url) const
  243. {
  244. return m_frame_nesting_levels.get(url).value_or(0) < 3;
  245. }
  246. }