BrowsingContext.cpp 10 KB

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