BrowsingContext.cpp 11 KB

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