BrowsingContext.cpp 13 KB

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