BrowsingContext.cpp 14 KB

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