HTMLLinkElement.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. /*
  2. * Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, the SerenityOS developers.
  4. * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
  5. * Copyright (c) 2023, Srikavin Ramkumar <me@srikavin.me>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #include <AK/ByteBuffer.h>
  10. #include <AK/Debug.h>
  11. #include <AK/URL.h>
  12. #include <LibTextCodec/Decoder.h>
  13. #include <LibWeb/CSS/Parser/Parser.h>
  14. #include <LibWeb/DOM/Document.h>
  15. #include <LibWeb/DOM/Event.h>
  16. #include <LibWeb/DOM/ShadowRoot.h>
  17. #include <LibWeb/Fetch/Fetching/Fetching.h>
  18. #include <LibWeb/Fetch/Infrastructure/FetchAlgorithms.h>
  19. #include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
  20. #include <LibWeb/Fetch/Infrastructure/HTTP/Responses.h>
  21. #include <LibWeb/HTML/EventNames.h>
  22. #include <LibWeb/HTML/HTMLLinkElement.h>
  23. #include <LibWeb/HTML/PotentialCORSRequest.h>
  24. #include <LibWeb/HTML/TraversableNavigable.h>
  25. #include <LibWeb/Infra/CharacterTypes.h>
  26. #include <LibWeb/Infra/Strings.h>
  27. #include <LibWeb/Loader/ResourceLoader.h>
  28. #include <LibWeb/Page/Page.h>
  29. #include <LibWeb/Platform/ImageCodecPlugin.h>
  30. namespace Web::HTML {
  31. JS_DEFINE_ALLOCATOR(HTMLLinkElement);
  32. HTMLLinkElement::HTMLLinkElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  33. : HTMLElement(document, move(qualified_name))
  34. {
  35. }
  36. HTMLLinkElement::~HTMLLinkElement() = default;
  37. void HTMLLinkElement::initialize(JS::Realm& realm)
  38. {
  39. Base::initialize(realm);
  40. WEB_SET_PROTOTYPE_FOR_INTERFACE(HTMLLinkElement);
  41. }
  42. void HTMLLinkElement::inserted()
  43. {
  44. HTMLElement::inserted();
  45. // FIXME: Handle alternate stylesheets properly
  46. if (m_relationship & Relationship::Stylesheet && !(m_relationship & Relationship::Alternate)) {
  47. // https://html.spec.whatwg.org/multipage/links.html#link-type-stylesheet:fetch-and-process-the-linked-resource
  48. // The appropriate times to fetch and process this type of link are:
  49. // - When the external resource link is created on a link element that is already browsing-context connected.
  50. // - When the external resource link's link element becomes browsing-context connected.
  51. fetch_and_process_linked_resource();
  52. }
  53. // FIXME: Follow spec for fetching and processing these attributes as well
  54. if (m_relationship & Relationship::Preload) {
  55. // FIXME: Respect the "as" attribute.
  56. LoadRequest request;
  57. request.set_url(document().parse_url(get_attribute_value(HTML::AttributeNames::href)));
  58. set_resource(ResourceLoader::the().load_resource(Resource::Type::Generic, request));
  59. } else if (m_relationship & Relationship::DNSPrefetch) {
  60. ResourceLoader::the().prefetch_dns(document().parse_url(get_attribute_value(HTML::AttributeNames::href)));
  61. } else if (m_relationship & Relationship::Preconnect) {
  62. ResourceLoader::the().preconnect(document().parse_url(get_attribute_value(HTML::AttributeNames::href)));
  63. } else if (m_relationship & Relationship::Icon) {
  64. auto favicon_url = document().parse_url(href());
  65. auto favicon_request = LoadRequest::create_for_url_on_page(favicon_url, &document().page());
  66. set_resource(ResourceLoader::the().load_resource(Resource::Type::Generic, favicon_request));
  67. }
  68. }
  69. bool HTMLLinkElement::has_loaded_icon() const
  70. {
  71. return m_relationship & Relationship::Icon && resource() && resource()->is_loaded() && resource()->has_encoded_data();
  72. }
  73. void HTMLLinkElement::attribute_changed(FlyString const& name, Optional<String> const& value)
  74. {
  75. HTMLElement::attribute_changed(name, value);
  76. // 4.6.7 Link types - https://html.spec.whatwg.org/multipage/links.html#linkTypes
  77. if (name == HTML::AttributeNames::rel) {
  78. m_relationship = 0;
  79. // Keywords are always ASCII case-insensitive, and must be compared as such.
  80. auto lowercased_value = MUST(Infra::to_ascii_lowercase(value.value_or(String {})));
  81. // To determine which link types apply to a link, a, area, or form element,
  82. // the element's rel attribute must be split on ASCII whitespace.
  83. // The resulting tokens are the keywords for the link types that apply to that element.
  84. auto parts = lowercased_value.bytes_as_string_view().split_view_if(Infra::is_ascii_whitespace);
  85. for (auto& part : parts) {
  86. if (part == "stylesheet"sv)
  87. m_relationship |= Relationship::Stylesheet;
  88. else if (part == "alternate"sv)
  89. m_relationship |= Relationship::Alternate;
  90. else if (part == "preload"sv)
  91. m_relationship |= Relationship::Preload;
  92. else if (part == "dns-prefetch"sv)
  93. m_relationship |= Relationship::DNSPrefetch;
  94. else if (part == "preconnect"sv)
  95. m_relationship |= Relationship::Preconnect;
  96. else if (part == "icon"sv)
  97. m_relationship |= Relationship::Icon;
  98. }
  99. }
  100. // FIXME: Handle alternate stylesheets properly
  101. if (m_relationship & Relationship::Stylesheet && !(m_relationship & Relationship::Alternate)) {
  102. if (name == HTML::AttributeNames::disabled && m_loaded_style_sheet)
  103. document_or_shadow_root_style_sheets().remove_sheet(*m_loaded_style_sheet);
  104. // https://html.spec.whatwg.org/multipage/links.html#link-type-stylesheet:fetch-and-process-the-linked-resource
  105. // The appropriate times to fetch and process this type of link are:
  106. if (
  107. // AD-HOC: When the rel attribute changes
  108. name == AttributeNames::rel ||
  109. // - When the href attribute of the link element of an external resource link that is already browsing-context connected is changed.
  110. name == AttributeNames::href ||
  111. // - When the disabled attribute of the link element of an external resource link that is already browsing-context connected is set, changed, or removed.
  112. name == AttributeNames::disabled ||
  113. // - When the crossorigin attribute of the link element of an external resource link that is already browsing-context connected is set, changed, or removed.
  114. name == AttributeNames::crossorigin
  115. // FIXME: - When the type attribute of the link element of an external resource link that is already browsing-context connected is set or changed to a value that does not or no longer matches the Content-Type metadata of the previous obtained external resource, if any.
  116. // FIXME: - When the type attribute of the link element of an external resource link that is already browsing-context connected, but was previously not obtained due to the type attribute specifying an unsupported type, is removed or changed.
  117. ) {
  118. fetch_and_process_linked_resource();
  119. }
  120. }
  121. }
  122. void HTMLLinkElement::resource_did_fail()
  123. {
  124. dbgln_if(CSS_LOADER_DEBUG, "HTMLLinkElement: Resource did fail. URL: {}", resource()->url());
  125. if (m_relationship & Relationship::Preload) {
  126. dispatch_event(*DOM::Event::create(realm(), HTML::EventNames::error));
  127. }
  128. }
  129. void HTMLLinkElement::resource_did_load()
  130. {
  131. VERIFY(resource());
  132. if (m_relationship & Relationship::Icon) {
  133. resource_did_load_favicon();
  134. m_document_load_event_delayer.clear();
  135. }
  136. if (m_relationship & Relationship::Preload) {
  137. dispatch_event(*DOM::Event::create(realm(), HTML::EventNames::load));
  138. }
  139. }
  140. // https://html.spec.whatwg.org/multipage/semantics.html#create-link-options-from-element
  141. HTMLLinkElement::LinkProcessingOptions HTMLLinkElement::create_link_options()
  142. {
  143. // 1. Let document be el's node document.
  144. auto& document = this->document();
  145. // 2. Let options be a new link processing options with
  146. LinkProcessingOptions options;
  147. // FIXME: destination the result of translating the state of el's as attribute
  148. // crossorigin the state of el's crossorigin content attribute
  149. options.crossorigin = cors_setting_attribute_from_keyword(get_attribute(AttributeNames::crossorigin));
  150. // FIXME: referrer policy the state of el's referrerpolicy content attribute
  151. // FIXME: source set el's source set
  152. // base URL document's URL
  153. options.base_url = document.url();
  154. // origin document's origin
  155. options.origin = document.origin();
  156. // environment document's relevant settings object
  157. options.environment = &document.relevant_settings_object();
  158. // policy container document's policy container
  159. options.policy_container = document.policy_container();
  160. // document document
  161. options.document = &document;
  162. // FIXME: cryptographic nonce metadata The current value of el's [[CryptographicNonce]] internal slot
  163. // 3. If el has an href attribute, then set options's href to the value of el's href attribute.
  164. if (auto maybe_href = get_attribute(AttributeNames::href); maybe_href.has_value())
  165. options.href = maybe_href.value();
  166. // 4. If el has an integrity attribute, then set options's integrity to the value of el's integrity content attribute.
  167. if (auto maybe_integrity = get_attribute(AttributeNames::integrity); maybe_integrity.has_value())
  168. options.integrity = maybe_integrity.value();
  169. // 5. If el has a type attribute, then set options's type to the value of el's type attribute.
  170. if (auto maybe_type = get_attribute(AttributeNames::type); maybe_type.has_value())
  171. options.type = maybe_type.value();
  172. // FIXME: 6. Assert: options's href is not the empty string, or options's source set is not null.
  173. // A link element with neither an href or an imagesrcset does not represent a link.
  174. // 7. Return options.
  175. return options;
  176. }
  177. // https://html.spec.whatwg.org/multipage/semantics.html#create-a-link-request
  178. JS::GCPtr<Fetch::Infrastructure::Request> HTMLLinkElement::create_link_request(HTMLLinkElement::LinkProcessingOptions const& options)
  179. {
  180. // 1. Assert: options's href is not the empty string.
  181. // FIXME: 2. If options's destination is not a destination, then return null.
  182. // 3. Parse a URL given options's href, relative to options's base URL. If that fails, then return null. Otherwise, let url be the resulting URL record.
  183. auto url = options.base_url.complete_url(options.href);
  184. if (!url.is_valid())
  185. return nullptr;
  186. // 4. Let request be the result of creating a potential-CORS request given url, options's destination, and options's crossorigin.
  187. auto request = create_potential_CORS_request(vm(), url, options.destination, options.crossorigin);
  188. // 5. Set request's policy container to options's policy container.
  189. request->set_policy_container(options.policy_container);
  190. // 6. Set request's integrity metadata to options's integrity.
  191. request->set_integrity_metadata(options.integrity);
  192. // 7. Set request's cryptographic nonce metadata to options's cryptographic nonce metadata.
  193. request->set_cryptographic_nonce_metadata(options.cryptographic_nonce_metadata);
  194. // 8. Set request's referrer policy to options's referrer policy.
  195. request->set_referrer_policy(options.referrer_policy);
  196. // 9. Set request's client to options's environment.
  197. request->set_client(options.environment);
  198. // 10. Return request.
  199. return request;
  200. }
  201. // https://html.spec.whatwg.org/multipage/semantics.html#fetch-and-process-the-linked-resource
  202. void HTMLLinkElement::fetch_and_process_linked_resource()
  203. {
  204. default_fetch_and_process_linked_resource();
  205. }
  206. // https://html.spec.whatwg.org/multipage/semantics.html#default-fetch-and-process-the-linked-resource
  207. void HTMLLinkElement::default_fetch_and_process_linked_resource()
  208. {
  209. // https://html.spec.whatwg.org/multipage/semantics.html#the-link-element:attr-link-href-4
  210. // If both the href and imagesrcset attributes are absent, then the element does not define a link.
  211. // FIXME: Support imagesrcset attribute
  212. if (!has_attribute(AttributeNames::href) || href().is_empty())
  213. return;
  214. // 1. Let options be the result of creating link options from el.
  215. auto options = create_link_options();
  216. // 2. Let request be the result of creating a link request given options.
  217. auto request = create_link_request(options);
  218. // 3. If request is null, then return.
  219. if (request == nullptr) {
  220. return;
  221. }
  222. // FIXME: 4. Set request's synchronous flag.
  223. // 5. Run the linked resource fetch setup steps, given el and request. If the result is false, then return.
  224. if (!linked_resource_fetch_setup_steps(*request))
  225. return;
  226. // 6. Set request's initiator type to "css" if el's rel attribute contains the keyword stylesheet; "link" otherwise.
  227. if (m_relationship & Relationship::Stylesheet) {
  228. request->set_initiator_type(Fetch::Infrastructure::Request::InitiatorType::CSS);
  229. } else {
  230. request->set_initiator_type(Fetch::Infrastructure::Request::InitiatorType::Link);
  231. }
  232. // 7. Fetch request with processResponseConsumeBody set to the following steps given response response and null, failure, or a byte sequence bodyBytes:
  233. Fetch::Infrastructure::FetchAlgorithms::Input fetch_algorithms_input {};
  234. fetch_algorithms_input.process_response_consume_body = [this, hr = options](auto response, auto body_bytes) {
  235. // FIXME: If the response is CORS cross-origin, we must use its internal response to query any of its data. See:
  236. // https://github.com/whatwg/html/issues/9355
  237. response = response->unsafe_response();
  238. // 1. Let success be true.
  239. bool success = true;
  240. // 2. If either of the following conditions are met:
  241. // - bodyBytes is null or failure; or
  242. // - response's status is not an ok status,
  243. if (body_bytes.template has<Empty>() || body_bytes.template has<Fetch::Infrastructure::FetchAlgorithms::ConsumeBodyFailureTag>() || !Fetch::Infrastructure::is_ok_status(response->status())) {
  244. // then set success to false.
  245. success = false;
  246. }
  247. // FIXME: 3. Otherwise, wait for the link resource's critical subresources to finish loading.
  248. // 4. Process the linked resource given el, success, response, and bodyBytes.
  249. process_linked_resource(success, response, body_bytes);
  250. };
  251. Fetch::Fetching::fetch(realm(), *request, Fetch::Infrastructure::FetchAlgorithms::create(vm(), move(fetch_algorithms_input))).release_value_but_fixme_should_propagate_errors();
  252. }
  253. // https://html.spec.whatwg.org/multipage/links.html#link-type-stylesheet:process-the-linked-resource
  254. void HTMLLinkElement::process_stylesheet_resource(bool success, Fetch::Infrastructure::Response const& response, Variant<Empty, Fetch::Infrastructure::FetchAlgorithms::ConsumeBodyFailureTag, ByteBuffer> body_bytes)
  255. {
  256. // 1. If the resource's Content-Type metadata is not text/css, then set success to false.
  257. auto extracted_mime_type = response.header_list()->extract_mime_type().release_value_but_fixme_should_propagate_errors();
  258. if (!extracted_mime_type.has_value() || extracted_mime_type->essence() != "text/css") {
  259. success = false;
  260. }
  261. // FIXME: 2. If el no longer creates an external resource link that contributes to the styling processing model,
  262. // or if, since the resource in question was fetched, it has become appropriate to fetch it again, then return.
  263. // 3. If el has an associated CSS style sheet, remove the CSS style sheet.
  264. if (m_loaded_style_sheet) {
  265. document_or_shadow_root_style_sheets().remove_sheet(*m_loaded_style_sheet);
  266. m_loaded_style_sheet = nullptr;
  267. }
  268. // 4. If success is true, then:
  269. if (success) {
  270. // 1. Create a CSS style sheet with the following properties:
  271. // type
  272. // text/css
  273. // location
  274. // The resulting URL string determined during the fetch and process the linked resource algorithm.
  275. // owner node
  276. // element
  277. // media
  278. // The media attribute of element.
  279. // title
  280. // The title attribute of element, if element is in a document tree, or the empty string otherwise.
  281. // alternate flag
  282. // Set if the link is an alternative style sheet and element's explicitly enabled is false; unset otherwise.
  283. // origin-clean flag
  284. // Set if the resource is CORS-same-origin; unset otherwise.
  285. // parent CSS style sheet
  286. // owner CSS rule
  287. // null
  288. // disabled flag
  289. // Left at its default value.
  290. // CSS rules
  291. // Left uninitialized.
  292. //
  293. // The CSS environment encoding is the result of running the following steps: [CSSSYNTAX]
  294. // 1. If the element has a charset attribute, get an encoding from that attribute's value. If that succeeds, return the resulting encoding. [ENCODING]
  295. // 2. Otherwise, return the document's character encoding. [DOM]
  296. Optional<String> encoding;
  297. if (auto charset = attribute(HTML::AttributeNames::charset); charset.has_value())
  298. encoding = charset.release_value();
  299. if (!encoding.has_value())
  300. encoding = document().encoding_or_default();
  301. auto decoder = TextCodec::decoder_for(*encoding);
  302. if (!decoder.has_value()) {
  303. // If we don't support the encoding yet, let's error out instead of trying to decode it as something it's most likely not.
  304. dbgln("FIXME: Style sheet encoding '{}' is not supported yet", encoding);
  305. dispatch_event(*DOM::Event::create(realm(), HTML::EventNames::error));
  306. } else {
  307. auto const& encoded_string = body_bytes.get<ByteBuffer>();
  308. auto maybe_decoded_string = TextCodec::convert_input_to_utf8_using_given_decoder_unless_there_is_a_byte_order_mark(*decoder, encoded_string);
  309. if (maybe_decoded_string.is_error()) {
  310. dbgln("Style sheet {} claimed to be '{}' but decoding failed", response.url().value_or(URL()), encoding);
  311. dispatch_event(*DOM::Event::create(realm(), HTML::EventNames::error));
  312. } else {
  313. auto const decoded_string = maybe_decoded_string.release_value();
  314. m_loaded_style_sheet = parse_css_stylesheet(CSS::Parser::ParsingContext(document(), *response.url()), decoded_string);
  315. if (m_loaded_style_sheet) {
  316. m_loaded_style_sheet->set_owner_node(this);
  317. m_loaded_style_sheet->set_media(attribute(HTML::AttributeNames::media).value_or({}));
  318. document().style_sheets().add_sheet(*m_loaded_style_sheet);
  319. } else {
  320. dbgln_if(CSS_LOADER_DEBUG, "HTMLLinkElement: Failed to parse stylesheet: {}", resource()->url());
  321. }
  322. // 2. Fire an event named load at el.
  323. dispatch_event(*DOM::Event::create(realm(), HTML::EventNames::load));
  324. }
  325. }
  326. }
  327. // 5. Otherwise, fire an event named error at el.
  328. else {
  329. dispatch_event(*DOM::Event::create(realm(), HTML::EventNames::error));
  330. }
  331. // FIXME: 6. If el contributes a script-blocking style sheet, then:
  332. // FIXME: 1. Assert: el's node document's script-blocking style sheet counter is greater than 0.
  333. // FIXME: 2. Decrement el's node document's script-blocking style sheet counter by 1.
  334. // 7. Unblock rendering on el.
  335. m_document_load_event_delayer.clear();
  336. }
  337. // https://html.spec.whatwg.org/multipage/semantics.html#process-the-linked-resource
  338. void HTMLLinkElement::process_linked_resource(bool success, Fetch::Infrastructure::Response const& response, Variant<Empty, Fetch::Infrastructure::FetchAlgorithms::ConsumeBodyFailureTag, ByteBuffer> body_bytes)
  339. {
  340. if (m_relationship & Relationship::Stylesheet)
  341. process_stylesheet_resource(success, response, body_bytes);
  342. }
  343. // https://html.spec.whatwg.org/multipage/semantics.html#linked-resource-fetch-setup-steps
  344. bool HTMLLinkElement::linked_resource_fetch_setup_steps(Fetch::Infrastructure::Request& request)
  345. {
  346. if (m_relationship & Relationship::Stylesheet)
  347. return stylesheet_linked_resource_fetch_setup_steps(request);
  348. return true;
  349. }
  350. // https://html.spec.whatwg.org/multipage/links.html#link-type-stylesheet:linked-resource-fetch-setup-steps
  351. bool HTMLLinkElement::stylesheet_linked_resource_fetch_setup_steps(Fetch::Infrastructure::Request& request)
  352. {
  353. // 1. If el's disabled attribute is set, then return false.
  354. if (has_attribute(AttributeNames::disabled))
  355. return false;
  356. // FIXME: 2. If el contributes a script-blocking style sheet, increment el's node document's script-blocking style sheet counter by 1.
  357. // 3. If el's media attribute's value matches the environment and el is potentially render-blocking, then block rendering on el.
  358. // FIXME: Check media attribute value.
  359. m_document_load_event_delayer.emplace(document());
  360. // 4. If el is currently render-blocking, then set request's render-blocking to true.
  361. // FIXME: Check if el is currently render-blocking.
  362. request.set_render_blocking(true);
  363. // 5. Return true.
  364. return true;
  365. }
  366. void HTMLLinkElement::resource_did_load_favicon()
  367. {
  368. VERIFY(m_relationship & (Relationship::Icon));
  369. if (!resource()->has_encoded_data()) {
  370. dbgln_if(SPAM_DEBUG, "Favicon downloaded, no encoded data");
  371. return;
  372. }
  373. dbgln_if(SPAM_DEBUG, "Favicon downloaded, {} bytes from {}", resource()->encoded_data().size(), resource()->url());
  374. document().check_favicon_after_loading_link_resource();
  375. }
  376. static bool decode_favicon(ReadonlyBytes favicon_data, URL const& favicon_url, JS::GCPtr<Navigable> navigable)
  377. {
  378. auto decoded_image = Platform::ImageCodecPlugin::the().decode_image(favicon_data);
  379. if (!decoded_image.has_value() || decoded_image->frames.is_empty()) {
  380. dbgln_if(IMAGE_DECODER_DEBUG, "Could not decode favicon {}", favicon_url);
  381. return false;
  382. }
  383. auto favicon_bitmap = decoded_image->frames[0].bitmap;
  384. dbgln_if(IMAGE_DECODER_DEBUG, "Decoded favicon, {}", favicon_bitmap->size());
  385. if (navigable && navigable->is_traversable())
  386. navigable->traversable_navigable()->page().client().page_did_change_favicon(*favicon_bitmap);
  387. return favicon_bitmap;
  388. }
  389. bool HTMLLinkElement::load_favicon_and_use_if_window_is_active()
  390. {
  391. if (!has_loaded_icon())
  392. return false;
  393. return decode_favicon(resource()->encoded_data(), resource()->url(), navigable());
  394. }
  395. // https://html.spec.whatwg.org/multipage/links.html#rel-icon:the-link-element-3
  396. WebIDL::ExceptionOr<void> HTMLLinkElement::load_fallback_favicon_if_needed(JS::NonnullGCPtr<DOM::Document> document)
  397. {
  398. auto& realm = document->realm();
  399. auto& vm = realm.vm();
  400. // In the absence of a link with the icon keyword, for Document objects whose URL's scheme is an HTTP(S) scheme,
  401. // user agents may instead run these steps in parallel:
  402. if (document->has_active_favicon())
  403. return {};
  404. if (!document->url().scheme().is_one_of("http"sv, "https"sv))
  405. return {};
  406. // 1. Let request be a new request whose URL is the URL record obtained by resolving the URL "/favicon.ico" against
  407. // the Document object's URL, client is the Document object's relevant settings object, destination is "image",
  408. // synchronous flag is set, credentials mode is "include", and whose use-URL-credentials flag is set.
  409. // NOTE: Fetch requests no longer have a synchronous flag, see https://github.com/whatwg/fetch/pull/1165
  410. auto request = Fetch::Infrastructure::Request::create(vm);
  411. request->set_url(document->parse_url("/favicon.ico"sv));
  412. request->set_client(&document->relevant_settings_object());
  413. request->set_destination(Fetch::Infrastructure::Request::Destination::Image);
  414. request->set_credentials_mode(Fetch::Infrastructure::Request::CredentialsMode::Include);
  415. request->set_use_url_credentials(true);
  416. // 2. Let response be the result of fetching request.
  417. Fetch::Infrastructure::FetchAlgorithms::Input fetch_algorithms_input {};
  418. fetch_algorithms_input.process_response = [document, request](JS::NonnullGCPtr<Fetch::Infrastructure::Response> response) {
  419. auto& realm = document->realm();
  420. auto global = JS::NonnullGCPtr { realm.global_object() };
  421. auto process_body = [document, request](auto body) {
  422. decode_favicon(body, request->url(), document->navigable());
  423. };
  424. auto process_body_error = [](auto) {
  425. };
  426. // 3. Use response's unsafe response as an icon as if it had been declared using the icon keyword.
  427. if (auto body = response->unsafe_response()->body())
  428. body->fully_read(realm, move(process_body), move(process_body_error), global).release_value_but_fixme_should_propagate_errors();
  429. };
  430. TRY(Fetch::Fetching::fetch(realm, request, Fetch::Infrastructure::FetchAlgorithms::create(vm, move(fetch_algorithms_input))));
  431. return {};
  432. }
  433. void HTMLLinkElement::visit_edges(Cell::Visitor& visitor)
  434. {
  435. Base::visit_edges(visitor);
  436. visitor.visit(m_loaded_style_sheet);
  437. }
  438. }