BrowsingContext.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. /*
  2. * Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/MainThreadVM.h>
  7. #include <LibWeb/Bindings/Wrapper.h>
  8. #include <LibWeb/DOM/Document.h>
  9. #include <LibWeb/DOM/HTMLCollection.h>
  10. #include <LibWeb/HTML/BrowsingContext.h>
  11. #include <LibWeb/HTML/BrowsingContextContainer.h>
  12. #include <LibWeb/HTML/CrossOrigin/CrossOriginOpenerPolicy.h>
  13. #include <LibWeb/HTML/EventLoop/EventLoop.h>
  14. #include <LibWeb/HTML/HTMLAnchorElement.h>
  15. #include <LibWeb/HTML/HTMLInputElement.h>
  16. #include <LibWeb/HTML/SandboxingFlagSet.h>
  17. #include <LibWeb/HTML/Scripting/WindowEnvironmentSettingsObject.h>
  18. #include <LibWeb/HTML/Window.h>
  19. #include <LibWeb/Layout/BreakNode.h>
  20. #include <LibWeb/Layout/InitialContainingBlock.h>
  21. #include <LibWeb/Layout/TextNode.h>
  22. #include <LibWeb/Page/Page.h>
  23. namespace Web::HTML {
  24. // https://html.spec.whatwg.org/multipage/urls-and-fetching.html#matches-about:blank
  25. static bool url_matches_about_blank(AK::URL const& url)
  26. {
  27. // A URL matches about:blank if its scheme is "about", its path contains a single string "blank", its username and password are the empty string, and its host is null.
  28. return url.scheme() == "about"sv
  29. && url.path() == "blank"sv
  30. && url.username().is_empty()
  31. && url.password().is_empty()
  32. && url.host().is_null();
  33. }
  34. // https://url.spec.whatwg.org/#concept-url-origin
  35. static HTML::Origin url_origin(AK::URL const& url)
  36. {
  37. // FIXME: Move this whole function somewhere better.
  38. if (url.scheme() == "blob"sv) {
  39. // FIXME: Implement
  40. return HTML::Origin {};
  41. }
  42. if (url.scheme().is_one_of("ftp"sv, "http"sv, "https"sv, "ws"sv, "wss"sv)) {
  43. // Return the tuple origin (url’s scheme, url’s host, url’s port, null).
  44. return HTML::Origin(url.scheme(), url.host(), url.port().value_or(0));
  45. }
  46. if (url.scheme() == "file"sv) {
  47. // Unfortunate as it is, this is left as an exercise to the reader. When in doubt, return a new opaque origin.
  48. return HTML::Origin {};
  49. }
  50. return HTML::Origin {};
  51. }
  52. // https://html.spec.whatwg.org/multipage/browsers.html#determining-the-origin
  53. static HTML::Origin determine_the_origin(BrowsingContext const& browsing_context, Optional<AK::URL> url, SandboxingFlagSet sandbox_flags, Optional<HTML::Origin> invocation_origin)
  54. {
  55. // 1. If sandboxFlags has its sandboxed origin browsing context flag set, then return a new opaque origin.
  56. if (sandbox_flags.flags & SandboxingFlagSet::SandboxedOrigin) {
  57. return HTML::Origin {};
  58. }
  59. // 2. If url is null, then return a new opaque origin.
  60. if (!url.has_value()) {
  61. return HTML::Origin {};
  62. }
  63. // 3. If invocationOrigin is non-null and url matches about:blank, then return invocationOrigin.
  64. if (invocation_origin.has_value() && url_matches_about_blank(*url)) {
  65. return invocation_origin.value();
  66. }
  67. // 4. If url is about:srcdoc, then return the origin of browsingContext's container document.
  68. if (url == AK::URL("about:srcdoc")) {
  69. VERIFY(browsing_context.container_document());
  70. return browsing_context.container_document()->origin();
  71. }
  72. // 5. Return url's origin.
  73. return url_origin(*url);
  74. }
  75. // https://html.spec.whatwg.org/multipage/browsers.html#creating-a-new-browsing-context
  76. NonnullRefPtr<BrowsingContext> BrowsingContext::create_a_new_browsing_context(Page& page, RefPtr<DOM::Document> creator, RefPtr<DOM::Element> embedder)
  77. {
  78. // 1. Let browsingContext be a new browsing context.
  79. BrowsingContextContainer* container = (embedder && is<BrowsingContextContainer>(*embedder)) ? static_cast<BrowsingContextContainer*>(embedder.ptr()) : nullptr;
  80. auto browsing_context = adopt_ref(*new BrowsingContext(page, container));
  81. // 2. Let unsafeContextCreationTime be the unsafe shared current time.
  82. [[maybe_unused]] auto unsafe_context_creation_time = HTML::main_thread_event_loop().unsafe_shared_current_time();
  83. // 3. If creator is non-null, then set browsingContext's creator origin to return creator's origin,
  84. // browsingContext's creator URL to return creator's URL,
  85. // browsingContext's creator base URL to return creator's base URL,
  86. // FIXME: and browsingContext's virtual browsing context group ID to creator's top-level browsing context's virtual browsing context group ID.
  87. if (creator) {
  88. browsing_context->m_creator_origin = creator->origin();
  89. browsing_context->m_creator_url = creator->url();
  90. browsing_context->m_creator_base_url = creator->base_url();
  91. }
  92. // FIXME: 4. Let sandboxFlags be the result of determining the creation sandboxing flags given browsingContext and embedded.
  93. SandboxingFlagSet sandbox_flags;
  94. // 5. Let origin be the result of determining the origin given browsingContext, about:blank, sandboxFlags, and browsingContext's creator origin.
  95. auto origin = determine_the_origin(browsing_context, AK::URL("about:blank"), sandbox_flags, browsing_context->m_creator_origin);
  96. // FIXME: 6. Let permissionsPolicy be the result of creating a permissions policy given browsingContext and origin. [PERMISSIONSPOLICY]
  97. // FIXME: 7. Let agent be the result of obtaining a similar-origin window agent given origin, group, and false.
  98. RefPtr<Window> window;
  99. // 8. Let realm execution context be the result of creating a new JavaScript realm given agent and the following customizations:
  100. auto realm_execution_context = Bindings::create_a_new_javascript_realm(
  101. Bindings::main_thread_vm(),
  102. [&](JS::Realm& realm) -> JS::Value {
  103. // - For the global object, create a new Window object.
  104. window = HTML::Window::create();
  105. auto* global_object = realm.heap().allocate_without_global_object<Bindings::WindowObject>(realm, *window);
  106. VERIFY(window->wrapper() == global_object);
  107. return global_object;
  108. },
  109. [](JS::Realm&) -> JS::Value {
  110. // FIXME: - For the global this binding, use browsingContext's WindowProxy object.
  111. return JS::js_undefined();
  112. });
  113. // 9. Let topLevelCreationURL be about:blank if embedder is null; otherwise embedder's relevant settings object's top-level creation URL.
  114. auto top_level_creation_url = !embedder ? AK::URL("about:blank") : relevant_settings_object(*embedder).top_level_creation_url;
  115. // 10. Let topLevelOrigin be origin if embedder is null; otherwise embedder's relevant settings object's top-level origin.
  116. auto top_level_origin = !embedder ? origin : relevant_settings_object(*embedder).origin();
  117. // 11. Set up a window environment settings object with about:blank, realm execution context, null, topLevelCreationURL, and topLevelOrigin.
  118. HTML::WindowEnvironmentSettingsObject::setup(
  119. AK::URL("about:blank"),
  120. move(realm_execution_context),
  121. {},
  122. top_level_creation_url,
  123. top_level_origin);
  124. // FIXME: 12. Let loadTimingInfo be a new document load timing info with its navigation start time set to the result of calling
  125. // coarsen time with unsafeContextCreationTime and the new environment settings object's cross-origin isolated capability.
  126. // 13. Let coop be a new cross-origin opener policy.
  127. auto coop = CrossOriginOpenerPolicy {};
  128. // 14. If creator is non-null and creator's origin is same origin with creator's relevant settings object's top-level origin,
  129. // then set coop to creator's browsing context's top-level browsing context's active document's cross-origin opener policy.
  130. if (creator && creator->origin().is_same_origin(relevant_settings_object(*creator).top_level_origin)) {
  131. VERIFY(creator->browsing_context());
  132. auto* top_level_document = creator->browsing_context()->top_level_browsing_context().active_document();
  133. VERIFY(top_level_document);
  134. coop = top_level_document->cross_origin_opener_policy();
  135. }
  136. // 15. Let document be a new Document, marked as an HTML document in quirks mode,
  137. // whose content type is "text/html",
  138. // origin is origin,
  139. // FIXME: active sandboxing flag set is sandboxFlags,
  140. // FIXME: permissions policy is permissionsPolicy,
  141. // cross-origin opener policy is coop,
  142. // FIXME: load timing info is loadTimingInfo,
  143. // FIXME: navigation id is null,
  144. // and which is ready for post-load tasks.
  145. auto document = DOM::Document::create();
  146. // Non-standard
  147. document->set_window({}, *window);
  148. window->set_associated_document(*document);
  149. document->set_quirks_mode(DOM::QuirksMode::Yes);
  150. document->set_content_type("text/html");
  151. document->set_origin(origin);
  152. document->set_url(AK::URL("about:blank"));
  153. document->set_cross_origin_opener_policy(coop);
  154. document->set_ready_for_post_load_tasks(true);
  155. // FIXME: 16. Assert: document's URL and document's relevant settings object's creation URL are about:blank.
  156. // 17. Set document's is initial about:blank to true.
  157. document->set_is_initial_about_blank(true);
  158. // 18. Ensure that document has a single child html node, which itself has two empty child nodes: a head element, and a body element.
  159. auto html_node = document->create_element(HTML::TagNames::html).release_value();
  160. html_node->append_child(document->create_element(HTML::TagNames::head).release_value());
  161. html_node->append_child(document->create_element(HTML::TagNames::body).release_value());
  162. document->append_child(html_node);
  163. // 19. Set the active document of browsingContext to document.
  164. browsing_context->m_active_document = document;
  165. // 20. If browsingContext's creator URL is non-null, then set document's referrer to the serialization of it.
  166. if (browsing_context->m_creator_url.has_value()) {
  167. document->set_referrer(browsing_context->m_creator_url->serialize());
  168. }
  169. // FIXME: 21. If creator is non-null, then set document's policy container to a clone of creator's policy container.
  170. // 22. Append a new session history entry to browsingContext's session history whose URL is about:blank and document is document.
  171. browsing_context->m_session_history.append(HTML::SessionHistoryEntry {
  172. .url = AK::URL("about:blank"),
  173. .document = document,
  174. .serialized_state = {},
  175. .policy_container = {},
  176. .scroll_restoration_mode = {},
  177. .browsing_context_name = {},
  178. });
  179. // Non-standard:
  180. document->attach_to_browsing_context({}, browsing_context);
  181. // 23. Completely finish loading document.
  182. document->completely_finish_loading();
  183. // 24. Return browsingContext.
  184. return browsing_context;
  185. }
  186. BrowsingContext::BrowsingContext(Page& page, HTML::BrowsingContextContainer* container)
  187. : m_page(page)
  188. , m_loader(*this)
  189. , m_event_handler({}, *this)
  190. , m_container(container)
  191. {
  192. m_cursor_blink_timer = Core::Timer::construct(500, [this] {
  193. if (!is_focused_context())
  194. return;
  195. if (m_cursor_position.node() && m_cursor_position.node()->layout_node()) {
  196. m_cursor_blink_state = !m_cursor_blink_state;
  197. m_cursor_position.node()->layout_node()->set_needs_display();
  198. }
  199. });
  200. }
  201. BrowsingContext::~BrowsingContext() = default;
  202. void BrowsingContext::did_edit(Badge<EditEventHandler>)
  203. {
  204. reset_cursor_blink_cycle();
  205. if (m_cursor_position.node() && is<DOM::Text>(*m_cursor_position.node())) {
  206. auto& text_node = static_cast<DOM::Text&>(*m_cursor_position.node());
  207. if (auto* input_element = text_node.owner_input_element())
  208. input_element->did_edit_text_node({});
  209. }
  210. }
  211. void BrowsingContext::reset_cursor_blink_cycle()
  212. {
  213. m_cursor_blink_state = true;
  214. m_cursor_blink_timer->restart();
  215. if (m_cursor_position.is_valid() && m_cursor_position.node()->layout_node())
  216. m_cursor_position.node()->layout_node()->set_needs_display();
  217. }
  218. // https://html.spec.whatwg.org/multipage/browsers.html#top-level-browsing-context
  219. bool BrowsingContext::is_top_level() const
  220. {
  221. // 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.
  222. return !parent();
  223. }
  224. bool BrowsingContext::is_focused_context() const
  225. {
  226. return m_page && &m_page->focused_context() == this;
  227. }
  228. void BrowsingContext::set_active_document(DOM::Document* document)
  229. {
  230. if (m_active_document == document)
  231. return;
  232. m_cursor_position = {};
  233. if (m_active_document)
  234. m_active_document->detach_from_browsing_context({}, *this);
  235. // https://html.spec.whatwg.org/multipage/browsing-the-web.html#resetBCName
  236. // FIXME: The rest of set_active_document does not follow the spec very closely, this just implements the
  237. // relevant steps for resetting the browsing context name and should be updated closer to the spec once
  238. // the other parts of history handling/navigating are implemented
  239. // 3. If newDocument's origin is not same origin with the current entry's document's origin, then:
  240. if (!document || !m_active_document || !document->origin().is_same_origin(m_active_document->origin())) {
  241. // 3. If the browsing context is a top-level browsing context, but not an auxiliary browsing context
  242. // whose disowned is false, then set the browsing context's name to the empty string.
  243. // FIXME: this is not checking the second part of the condition yet
  244. if (is_top_level())
  245. m_name = String::empty();
  246. }
  247. m_active_document = document;
  248. if (m_active_document) {
  249. m_active_document->attach_to_browsing_context({}, *this);
  250. if (m_page && is_top_level())
  251. m_page->client().page_did_change_title(m_active_document->title());
  252. }
  253. }
  254. void BrowsingContext::set_viewport_rect(Gfx::IntRect const& rect)
  255. {
  256. bool did_change = false;
  257. if (m_size != rect.size()) {
  258. m_size = rect.size();
  259. if (auto* document = active_document()) {
  260. // NOTE: Resizing the viewport changes the reference value for viewport-relative CSS lengths.
  261. document->invalidate_style();
  262. document->invalidate_layout();
  263. }
  264. did_change = true;
  265. }
  266. if (m_viewport_scroll_offset != rect.location()) {
  267. m_viewport_scroll_offset = rect.location();
  268. did_change = true;
  269. }
  270. if (did_change) {
  271. for (auto* client : m_viewport_clients)
  272. client->browsing_context_did_set_viewport_rect(rect);
  273. }
  274. // Schedule the HTML event loop to ensure that a `resize` event gets fired.
  275. HTML::main_thread_event_loop().schedule();
  276. }
  277. void BrowsingContext::set_size(Gfx::IntSize const& size)
  278. {
  279. if (m_size == size)
  280. return;
  281. m_size = size;
  282. if (auto* document = active_document()) {
  283. document->invalidate_style();
  284. document->invalidate_layout();
  285. }
  286. for (auto* client : m_viewport_clients)
  287. client->browsing_context_did_set_viewport_rect(viewport_rect());
  288. // Schedule the HTML event loop to ensure that a `resize` event gets fired.
  289. HTML::main_thread_event_loop().schedule();
  290. }
  291. void BrowsingContext::set_needs_display()
  292. {
  293. set_needs_display(viewport_rect());
  294. }
  295. void BrowsingContext::set_needs_display(Gfx::IntRect const& rect)
  296. {
  297. if (!viewport_rect().intersects(rect))
  298. return;
  299. if (is_top_level()) {
  300. if (m_page)
  301. m_page->client().page_did_invalidate(to_top_level_rect(rect));
  302. return;
  303. }
  304. if (container() && container()->layout_node())
  305. container()->layout_node()->set_needs_display();
  306. }
  307. void BrowsingContext::scroll_to(Gfx::IntPoint const& position)
  308. {
  309. if (active_document())
  310. active_document()->force_layout();
  311. if (m_page)
  312. m_page->client().page_did_request_scroll_to(position);
  313. }
  314. void BrowsingContext::scroll_to_anchor(String const& fragment)
  315. {
  316. if (!active_document())
  317. return;
  318. auto element = active_document()->get_element_by_id(fragment);
  319. if (!element) {
  320. auto candidates = active_document()->get_elements_by_name(fragment);
  321. for (auto& candidate : candidates->collect_matching_elements()) {
  322. if (is<HTML::HTMLAnchorElement>(*candidate)) {
  323. element = verify_cast<HTML::HTMLAnchorElement>(*candidate);
  324. break;
  325. }
  326. }
  327. }
  328. active_document()->force_layout();
  329. if (!element || !element->layout_node())
  330. return;
  331. auto& layout_node = *element->layout_node();
  332. Gfx::FloatRect float_rect { layout_node.box_type_agnostic_position(), { (float)viewport_rect().width(), (float)viewport_rect().height() } };
  333. if (is<Layout::Box>(layout_node)) {
  334. auto& layout_box = verify_cast<Layout::Box>(layout_node);
  335. auto padding_box = layout_box.box_model().padding_box();
  336. float_rect.translate_by(-padding_box.left, -padding_box.top);
  337. }
  338. if (m_page)
  339. m_page->client().page_did_request_scroll_into_view(enclosing_int_rect(float_rect));
  340. }
  341. Gfx::IntRect BrowsingContext::to_top_level_rect(Gfx::IntRect const& a_rect)
  342. {
  343. auto rect = a_rect;
  344. rect.set_location(to_top_level_position(a_rect.location()));
  345. return rect;
  346. }
  347. Gfx::IntPoint BrowsingContext::to_top_level_position(Gfx::IntPoint const& a_position)
  348. {
  349. auto position = a_position;
  350. for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
  351. if (ancestor->is_top_level())
  352. break;
  353. if (!ancestor->container())
  354. return {};
  355. if (!ancestor->container()->layout_node())
  356. return {};
  357. position.translate_by(ancestor->container()->layout_node()->box_type_agnostic_position().to_type<int>());
  358. }
  359. return position;
  360. }
  361. void BrowsingContext::set_cursor_position(DOM::Position position)
  362. {
  363. if (m_cursor_position == position)
  364. return;
  365. if (m_cursor_position.node() && m_cursor_position.node()->layout_node())
  366. m_cursor_position.node()->layout_node()->set_needs_display();
  367. m_cursor_position = move(position);
  368. if (m_cursor_position.node() && m_cursor_position.node()->layout_node())
  369. m_cursor_position.node()->layout_node()->set_needs_display();
  370. reset_cursor_blink_cycle();
  371. }
  372. String BrowsingContext::selected_text() const
  373. {
  374. StringBuilder builder;
  375. if (!active_document())
  376. return {};
  377. auto* layout_root = active_document()->layout_node();
  378. if (!layout_root)
  379. return {};
  380. if (!layout_root->selection().is_valid())
  381. return {};
  382. auto selection = layout_root->selection().normalized();
  383. if (selection.start().layout_node == selection.end().layout_node) {
  384. if (!is<Layout::TextNode>(*selection.start().layout_node))
  385. return "";
  386. 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);
  387. }
  388. // Start node
  389. auto layout_node = selection.start().layout_node;
  390. if (is<Layout::TextNode>(*layout_node)) {
  391. auto& text = verify_cast<Layout::TextNode>(*layout_node).text_for_rendering();
  392. builder.append(text.substring(selection.start().index_in_node, text.length() - selection.start().index_in_node));
  393. }
  394. // Middle nodes
  395. layout_node = layout_node->next_in_pre_order();
  396. while (layout_node && layout_node != selection.end().layout_node) {
  397. if (is<Layout::TextNode>(*layout_node))
  398. builder.append(verify_cast<Layout::TextNode>(*layout_node).text_for_rendering());
  399. else if (is<Layout::BreakNode>(*layout_node) || is<Layout::BlockContainer>(*layout_node))
  400. builder.append('\n');
  401. layout_node = layout_node->next_in_pre_order();
  402. }
  403. // End node
  404. VERIFY(layout_node == selection.end().layout_node);
  405. if (is<Layout::TextNode>(*layout_node)) {
  406. auto& text = verify_cast<Layout::TextNode>(*layout_node).text_for_rendering();
  407. builder.append(text.substring(0, selection.end().index_in_node));
  408. }
  409. return builder.to_string();
  410. }
  411. void BrowsingContext::select_all()
  412. {
  413. if (!active_document())
  414. return;
  415. auto* layout_root = active_document()->layout_node();
  416. if (!layout_root)
  417. return;
  418. Layout::Node const* first_layout_node = layout_root;
  419. for (;;) {
  420. auto* next = first_layout_node->next_in_pre_order();
  421. if (!next)
  422. break;
  423. first_layout_node = next;
  424. if (is<Layout::TextNode>(*first_layout_node))
  425. break;
  426. }
  427. Layout::Node const* last_layout_node = first_layout_node;
  428. for (Layout::Node const* layout_node = first_layout_node; layout_node; layout_node = layout_node->next_in_pre_order()) {
  429. if (is<Layout::TextNode>(*layout_node))
  430. last_layout_node = layout_node;
  431. }
  432. VERIFY(first_layout_node);
  433. VERIFY(last_layout_node);
  434. int last_layout_node_index_in_node = 0;
  435. if (is<Layout::TextNode>(*last_layout_node)) {
  436. auto const& text_for_rendering = verify_cast<Layout::TextNode>(*last_layout_node).text_for_rendering();
  437. if (!text_for_rendering.is_empty())
  438. last_layout_node_index_in_node = text_for_rendering.length() - 1;
  439. }
  440. layout_root->set_selection({ { first_layout_node, 0 }, { last_layout_node, last_layout_node_index_in_node } });
  441. }
  442. void BrowsingContext::register_viewport_client(ViewportClient& client)
  443. {
  444. auto result = m_viewport_clients.set(&client);
  445. VERIFY(result == AK::HashSetResult::InsertedNewEntry);
  446. }
  447. void BrowsingContext::unregister_viewport_client(ViewportClient& client)
  448. {
  449. bool was_removed = m_viewport_clients.remove(&client);
  450. VERIFY(was_removed);
  451. }
  452. void BrowsingContext::register_frame_nesting(AK::URL const& url)
  453. {
  454. m_frame_nesting_levels.ensure(url)++;
  455. }
  456. bool BrowsingContext::is_frame_nesting_allowed(AK::URL const& url) const
  457. {
  458. return m_frame_nesting_levels.get(url).value_or(0) < 3;
  459. }
  460. bool BrowsingContext::increment_cursor_position_offset()
  461. {
  462. if (!m_cursor_position.increment_offset())
  463. return false;
  464. reset_cursor_blink_cycle();
  465. return true;
  466. }
  467. bool BrowsingContext::decrement_cursor_position_offset()
  468. {
  469. if (!m_cursor_position.decrement_offset())
  470. return false;
  471. reset_cursor_blink_cycle();
  472. return true;
  473. }
  474. DOM::Document* BrowsingContext::container_document()
  475. {
  476. if (auto* container = this->container())
  477. return &container->document();
  478. return nullptr;
  479. }
  480. DOM::Document const* BrowsingContext::container_document() const
  481. {
  482. if (auto* container = this->container())
  483. return &container->document();
  484. return nullptr;
  485. }
  486. // https://html.spec.whatwg.org/#rendering-opportunity
  487. bool BrowsingContext::has_a_rendering_opportunity() const
  488. {
  489. // 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,
  490. // accounting for hardware refresh rate constraints and user agent throttling for performance reasons, but considering content presentable even if it's outside the viewport.
  491. // FIXME: We should at the very least say `false` here if we're an inactive browser tab.
  492. return true;
  493. }
  494. // https://html.spec.whatwg.org/multipage/interaction.html#currently-focused-area-of-a-top-level-browsing-context
  495. RefPtr<DOM::Node> BrowsingContext::currently_focused_area()
  496. {
  497. // 1. If topLevelBC does not have system focus, then return null.
  498. if (!is_focused_context())
  499. return nullptr;
  500. // 2. Let candidate be topLevelBC's active document.
  501. auto* candidate = active_document();
  502. // 3. While candidate's focused area is a browsing context container with a non-null nested browsing context:
  503. // set candidate to the active document of that browsing context container's nested browsing context.
  504. while (candidate->focused_element()
  505. && is<HTML::BrowsingContextContainer>(candidate->focused_element())
  506. && static_cast<HTML::BrowsingContextContainer&>(*candidate->focused_element()).nested_browsing_context()) {
  507. candidate = static_cast<HTML::BrowsingContextContainer&>(*candidate->focused_element()).nested_browsing_context()->active_document();
  508. }
  509. // 4. If candidate's focused area is non-null, set candidate to candidate's focused area.
  510. if (candidate->focused_element()) {
  511. // NOTE: We return right away here instead of assigning to candidate,
  512. // since that would require compromising type safety.
  513. return candidate->focused_element();
  514. }
  515. // 5. Return candidate.
  516. return candidate;
  517. }
  518. BrowsingContext* BrowsingContext::choose_a_browsing_context(StringView name, bool)
  519. {
  520. // The rules for choosing a browsing context, given a browsing context name
  521. // name, a browsing context current, and a boolean noopener are as follows:
  522. // 1. Let chosen be null.
  523. BrowsingContext* chosen = nullptr;
  524. // FIXME: 2. Let windowType be "existing or none".
  525. // FIXME: 3. Let sandboxingFlagSet be current's active document's active
  526. // sandboxing flag set.
  527. // 4. If name is the empty string or an ASCII case-insensitive match for "_self", then set chosen to current.
  528. if (name.is_empty() || name.equals_ignoring_case("_self"sv))
  529. chosen = this;
  530. // 5. Otherwise, if name is an ASCII case-insensitive match for "_parent",
  531. // set chosen to current's parent browsing context, if any, and current
  532. // otherwise.
  533. if (name.equals_ignoring_case("_parent"sv)) {
  534. if (auto* parent = this->parent())
  535. chosen = parent;
  536. else
  537. chosen = this;
  538. }
  539. // 6. Otherwise, if name is an ASCII case-insensitive match for "_top", set
  540. // chosen to current's top-level browsing context, if any, and current
  541. // otherwise.
  542. if (name.equals_ignoring_case("_top"sv)) {
  543. chosen = &top_level_browsing_context();
  544. }
  545. // FIXME: 7. Otherwise, if name is not an ASCII case-insensitive match for
  546. // "_blank", there exists a browsing context whose name is the same as name,
  547. // current is familiar with that browsing context, and the user agent
  548. // determines that the two browsing contexts are related enough that it is
  549. // ok if they reach each other, set chosen to that browsing context. If
  550. // there are multiple matching browsing contexts, the user agent should set
  551. // chosen to one in some arbitrary consistent manner, such as the most
  552. // recently opened, most recently focused, or more closely related.
  553. if (!name.equals_ignoring_case("_blank"sv)) {
  554. chosen = this;
  555. } else {
  556. // 8. Otherwise, a new browsing context is being requested, and what
  557. // happens depends on the user agent's configuration and abilities — it
  558. // is determined by the rules given for the first applicable option from
  559. // the following list:
  560. dbgln("FIXME: Create a new browsing context!");
  561. // --> If current's active window does not have transient activation and
  562. // the user agent has been configured to not show popups (i.e., the
  563. // user agent has a "popup blocker" enabled)
  564. //
  565. // The user agent may inform the user that a popup has been blocked.
  566. // --> If sandboxingFlagSet has the sandboxed auxiliary navigation
  567. // browsing context flag set
  568. //
  569. // The user agent may report to a developer console that a popup has
  570. // been blocked.
  571. // --> If the user agent has been configured such that in this instance
  572. // it will create a new browsing context
  573. //
  574. // 1. Set windowType to "new and unrestricted".
  575. // 2. If current's top-level browsing context's active document's
  576. // cross-origin opener policy's value is "same-origin" or
  577. // "same-origin-plus-COEP", then:
  578. // 2.1. Let currentDocument be current's active document.
  579. // 2.2. If currentDocument's origin is not same origin with
  580. // currentDocument's relevant settings object's top-level
  581. // origin, then set noopener to true, name to "_blank", and
  582. // windowType to "new with no opener".
  583. // 3. If noopener is true, then set chosen to the result of creating
  584. // a new top-level browsing context.
  585. // 4. Otherwise:
  586. // 4.1. Set chosen to the result of creating a new auxiliary
  587. // browsing context with current.
  588. // 4.2. If sandboxingFlagSet's sandboxed navigation browsing
  589. // context flag is set, then current must be set as chosen's one
  590. // permitted sandboxed navigator.
  591. // 5. If sandboxingFlagSet's sandbox propagates to auxiliary
  592. // browsing contexts flag is set, then all the flags that are set in
  593. // sandboxingFlagSet must be set in chosen's popup sandboxing flag
  594. // set.
  595. // 6. If name is not an ASCII case-insensitive match for "_blank",
  596. // then set chosen's name to name.
  597. // --> If the user agent has been configured such that in this instance
  598. // it will reuse current
  599. //
  600. // Set chosen to current.
  601. // --> If the user agent has been configured such that in this instance
  602. // it will not find a browsing context
  603. //
  604. // Do nothing.
  605. }
  606. // 9. Return chosen and windowType.
  607. return chosen;
  608. }
  609. // https://html.spec.whatwg.org/multipage/dom.html#still-on-its-initial-about:blank-document
  610. bool BrowsingContext::still_on_its_initial_about_blank_document() const
  611. {
  612. // A browsing context browsingContext is still on its initial about:blank Document
  613. // if browsingContext's session history's size is 1
  614. // and browsingContext's session history[0]'s document's is initial about:blank is true.
  615. return m_session_history.size() == 1
  616. && m_session_history[0].document
  617. && m_session_history[0].document->is_initial_about_blank();
  618. }
  619. }