BrowsingContext.cpp 13 KB

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