BrowsingContext.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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/BrowsingContext.h>
  16. #include <LibWeb/UIEvents/EventNames.h>
  17. namespace Web {
  18. BrowsingContext::BrowsingContext(DOM::Element& host_element, BrowsingContext& top_level_browsing_context)
  19. : m_page(*top_level_browsing_context.page())
  20. , m_top_level_browsing_context(top_level_browsing_context)
  21. , m_loader(*this)
  22. , m_event_handler({}, *this)
  23. , m_host_element(host_element)
  24. {
  25. setup();
  26. }
  27. BrowsingContext::BrowsingContext(Page& page)
  28. : m_page(page)
  29. , m_top_level_browsing_context(*this)
  30. , m_loader(*this)
  31. , m_event_handler({}, *this)
  32. {
  33. setup();
  34. }
  35. BrowsingContext::~BrowsingContext()
  36. {
  37. }
  38. void BrowsingContext::setup()
  39. {
  40. m_cursor_blink_timer = Core::Timer::construct(500, [this] {
  41. if (!is_focused_context())
  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 BrowsingContext::did_edit(Badge<EditEventHandler>)
  50. {
  51. reset_cursor_blink_cycle();
  52. }
  53. void BrowsingContext::reset_cursor_blink_cycle()
  54. {
  55. m_cursor_blink_state = true;
  56. m_cursor_blink_timer->restart();
  57. m_cursor_position.node()->layout_node()->set_needs_display();
  58. }
  59. bool BrowsingContext::is_focused_context() const
  60. {
  61. return m_page && &m_page->focused_context() == this;
  62. }
  63. void BrowsingContext::set_document(DOM::Document* document)
  64. {
  65. if (m_document == document)
  66. return;
  67. m_cursor_position = {};
  68. if (m_document)
  69. m_document->detach_from_browsing_context({}, *this);
  70. m_document = document;
  71. if (m_document) {
  72. m_document->attach_to_browsing_context({}, *this);
  73. if (m_page && is_top_level())
  74. m_page->client().page_did_change_title(m_document->title());
  75. }
  76. if (m_page)
  77. m_page->client().page_did_set_document_in_top_level_browsing_context(m_document);
  78. }
  79. void BrowsingContext::set_viewport_rect(const Gfx::IntRect& rect)
  80. {
  81. bool did_change = false;
  82. if (m_size != rect.size()) {
  83. m_size = rect.size();
  84. if (m_document) {
  85. m_document->window().dispatch_event(DOM::Event::create(UIEvents::EventNames::resize));
  86. m_document->update_layout();
  87. }
  88. did_change = true;
  89. }
  90. if (m_viewport_scroll_offset != rect.location()) {
  91. m_viewport_scroll_offset = rect.location();
  92. did_change = true;
  93. }
  94. if (did_change) {
  95. for (auto* client : m_viewport_clients)
  96. client->frame_did_set_viewport_rect(rect);
  97. }
  98. }
  99. void BrowsingContext::set_size(const Gfx::IntSize& size)
  100. {
  101. if (m_size == size)
  102. return;
  103. m_size = size;
  104. if (m_document) {
  105. m_document->window().dispatch_event(DOM::Event::create(UIEvents::EventNames::resize));
  106. m_document->update_layout();
  107. }
  108. for (auto* client : m_viewport_clients)
  109. client->frame_did_set_viewport_rect(viewport_rect());
  110. }
  111. void BrowsingContext::set_viewport_scroll_offset(const Gfx::IntPoint& offset)
  112. {
  113. if (m_viewport_scroll_offset == offset)
  114. return;
  115. m_viewport_scroll_offset = offset;
  116. for (auto* client : m_viewport_clients)
  117. client->frame_did_set_viewport_rect(viewport_rect());
  118. }
  119. void BrowsingContext::set_needs_display(const Gfx::IntRect& rect)
  120. {
  121. if (!viewport_rect().intersects(rect))
  122. return;
  123. if (is_top_level()) {
  124. if (m_page)
  125. m_page->client().page_did_invalidate(to_top_level_rect(rect));
  126. return;
  127. }
  128. if (host_element() && host_element()->layout_node())
  129. host_element()->layout_node()->set_needs_display();
  130. }
  131. void BrowsingContext::scroll_to_anchor(const String& fragment)
  132. {
  133. if (!document())
  134. return;
  135. auto element = document()->get_element_by_id(fragment);
  136. if (!element) {
  137. auto candidates = document()->get_elements_by_name(fragment);
  138. for (auto& candidate : candidates->collect_matching_elements()) {
  139. if (is<HTML::HTMLAnchorElement>(*candidate)) {
  140. element = verify_cast<HTML::HTMLAnchorElement>(*candidate);
  141. break;
  142. }
  143. }
  144. }
  145. // FIXME: This is overly aggressive and should be something more like a "update_layout_if_needed()"
  146. document()->force_layout();
  147. if (!element || !element->layout_node())
  148. return;
  149. auto& layout_node = *element->layout_node();
  150. Gfx::FloatRect float_rect { layout_node.box_type_agnostic_position(), { (float)viewport_rect().width(), (float)viewport_rect().height() } };
  151. if (is<Layout::Box>(layout_node)) {
  152. auto& layout_box = verify_cast<Layout::Box>(layout_node);
  153. auto padding_box = layout_box.box_model().padding_box();
  154. float_rect.translate_by(-padding_box.left, -padding_box.top);
  155. }
  156. if (m_page)
  157. m_page->client().page_did_request_scroll_into_view(enclosing_int_rect(float_rect));
  158. }
  159. Gfx::IntRect BrowsingContext::to_top_level_rect(const Gfx::IntRect& a_rect)
  160. {
  161. auto rect = a_rect;
  162. rect.set_location(to_top_level_position(a_rect.location()));
  163. return rect;
  164. }
  165. Gfx::IntPoint BrowsingContext::to_top_level_position(const Gfx::IntPoint& a_position)
  166. {
  167. auto position = a_position;
  168. for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
  169. if (ancestor->is_top_level())
  170. break;
  171. if (!ancestor->host_element())
  172. return {};
  173. if (!ancestor->host_element()->layout_node())
  174. return {};
  175. position.translate_by(ancestor->host_element()->layout_node()->box_type_agnostic_position().to_type<int>());
  176. }
  177. return position;
  178. }
  179. void BrowsingContext::set_cursor_position(DOM::Position position)
  180. {
  181. if (m_cursor_position == position)
  182. return;
  183. if (m_cursor_position.node() && m_cursor_position.node()->layout_node())
  184. m_cursor_position.node()->layout_node()->set_needs_display();
  185. m_cursor_position = move(position);
  186. if (m_cursor_position.node() && m_cursor_position.node()->layout_node())
  187. m_cursor_position.node()->layout_node()->set_needs_display();
  188. reset_cursor_blink_cycle();
  189. }
  190. String BrowsingContext::selected_text() const
  191. {
  192. StringBuilder builder;
  193. if (!m_document)
  194. return {};
  195. auto* layout_root = m_document->layout_node();
  196. if (!layout_root)
  197. return {};
  198. if (!layout_root->selection().is_valid())
  199. return {};
  200. auto selection = layout_root->selection().normalized();
  201. if (selection.start().layout_node == selection.end().layout_node) {
  202. if (!is<Layout::TextNode>(*selection.start().layout_node))
  203. return "";
  204. return verify_cast<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);
  205. }
  206. // Start node
  207. auto layout_node = selection.start().layout_node;
  208. if (is<Layout::TextNode>(*layout_node)) {
  209. auto& text = verify_cast<Layout::TextNode>(*layout_node).text_for_rendering();
  210. builder.append(text.substring(selection.start().index_in_node, text.length() - selection.start().index_in_node));
  211. }
  212. // Middle nodes
  213. layout_node = layout_node->next_in_pre_order();
  214. while (layout_node && layout_node != selection.end().layout_node) {
  215. if (is<Layout::TextNode>(*layout_node))
  216. builder.append(verify_cast<Layout::TextNode>(*layout_node).text_for_rendering());
  217. else if (is<Layout::BreakNode>(*layout_node) || is<Layout::BlockBox>(*layout_node))
  218. builder.append('\n');
  219. layout_node = layout_node->next_in_pre_order();
  220. }
  221. // End node
  222. VERIFY(layout_node == selection.end().layout_node);
  223. if (is<Layout::TextNode>(*layout_node)) {
  224. auto& text = verify_cast<Layout::TextNode>(*layout_node).text_for_rendering();
  225. builder.append(text.substring(0, selection.end().index_in_node));
  226. }
  227. return builder.to_string();
  228. }
  229. void BrowsingContext::select_all()
  230. {
  231. if (!m_document)
  232. return;
  233. auto* layout_root = m_document->layout_node();
  234. if (!layout_root)
  235. return;
  236. const Layout::Node* first_layout_node = layout_root;
  237. for (;;) {
  238. auto* next = first_layout_node->next_in_pre_order();
  239. if (!next)
  240. break;
  241. first_layout_node = next;
  242. if (is<Layout::TextNode>(*first_layout_node))
  243. break;
  244. }
  245. const Layout::Node* last_layout_node = first_layout_node;
  246. for (const Layout::Node* layout_node = first_layout_node; layout_node; layout_node = layout_node->next_in_pre_order()) {
  247. if (is<Layout::TextNode>(*layout_node))
  248. last_layout_node = layout_node;
  249. }
  250. VERIFY(first_layout_node);
  251. VERIFY(last_layout_node);
  252. int last_layout_node_index_in_node = 0;
  253. if (is<Layout::TextNode>(*last_layout_node)) {
  254. auto const& text_for_rendering = verify_cast<Layout::TextNode>(*last_layout_node).text_for_rendering();
  255. if (!text_for_rendering.is_empty())
  256. last_layout_node_index_in_node = text_for_rendering.length() - 1;
  257. }
  258. layout_root->set_selection({ { first_layout_node, 0 }, { last_layout_node, last_layout_node_index_in_node } });
  259. }
  260. void BrowsingContext::register_viewport_client(ViewportClient& client)
  261. {
  262. auto result = m_viewport_clients.set(&client);
  263. VERIFY(result == AK::HashSetResult::InsertedNewEntry);
  264. }
  265. void BrowsingContext::unregister_viewport_client(ViewportClient& client)
  266. {
  267. bool was_removed = m_viewport_clients.remove(&client);
  268. VERIFY(was_removed);
  269. }
  270. void BrowsingContext::register_frame_nesting(URL const& url)
  271. {
  272. m_frame_nesting_levels.ensure(url)++;
  273. }
  274. bool BrowsingContext::is_frame_nesting_allowed(URL const& url) const
  275. {
  276. return m_frame_nesting_levels.get(url).value_or(0) < 3;
  277. }
  278. bool BrowsingContext::increment_cursor_position_offset()
  279. {
  280. if (!m_cursor_position.increment_offset())
  281. return false;
  282. reset_cursor_blink_cycle();
  283. return true;
  284. }
  285. bool BrowsingContext::decrement_cursor_position_offset()
  286. {
  287. if (!m_cursor_position.decrement_offset())
  288. return false;
  289. reset_cursor_blink_cycle();
  290. return true;
  291. }
  292. }