HTMLLinkElement.cpp 29 KB

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