HTMLLinkElement.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, the SerenityOS developers.
  4. * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <AK/ByteBuffer.h>
  9. #include <AK/Debug.h>
  10. #include <AK/URL.h>
  11. #include <LibWeb/CSS/Parser/Parser.h>
  12. #include <LibWeb/DOM/Document.h>
  13. #include <LibWeb/HTML/HTMLLinkElement.h>
  14. #include <LibWeb/Infra/CharacterTypes.h>
  15. #include <LibWeb/Loader/ResourceLoader.h>
  16. #include <LibWeb/Page/Page.h>
  17. #include <LibWeb/Platform/ImageCodecPlugin.h>
  18. namespace Web::HTML {
  19. HTMLLinkElement::HTMLLinkElement(DOM::Document& document, DOM::QualifiedName qualified_name)
  20. : HTMLElement(document, move(qualified_name))
  21. {
  22. set_prototype(&Bindings::cached_web_prototype(realm(), "HTMLLinkElement"));
  23. }
  24. HTMLLinkElement::~HTMLLinkElement() = default;
  25. void HTMLLinkElement::inserted()
  26. {
  27. HTMLElement::inserted();
  28. if (m_relationship & Relationship::Stylesheet && !(m_relationship & Relationship::Alternate)) {
  29. auto url = document().parse_url(href());
  30. dbgln_if(CSS_LOADER_DEBUG, "HTMLLinkElement: Loading import URL: {}", url);
  31. auto request = LoadRequest::create_for_url_on_page(url, document().page());
  32. // NOTE: Mark this element as delaying the document load event *before* calling set_resource()
  33. // as it may trigger a synchronous resource_did_load() callback.
  34. m_document_load_event_delayer.emplace(document());
  35. set_resource(ResourceLoader::the().load_resource(Resource::Type::Generic, request));
  36. // NOTE: If we ended up not loading a resource for whatever reason, don't delay the load event.
  37. if (!resource())
  38. m_document_load_event_delayer.clear();
  39. }
  40. if (m_relationship & Relationship::Preload) {
  41. // FIXME: Respect the "as" attribute.
  42. LoadRequest request;
  43. request.set_url(document().parse_url(attribute(HTML::AttributeNames::href)));
  44. m_preload_resource = ResourceLoader::the().load_resource(Resource::Type::Generic, request);
  45. } else if (m_relationship & Relationship::DNSPrefetch) {
  46. ResourceLoader::the().prefetch_dns(document().parse_url(attribute(HTML::AttributeNames::href)));
  47. } else if (m_relationship & Relationship::Preconnect) {
  48. ResourceLoader::the().preconnect(document().parse_url(attribute(HTML::AttributeNames::href)));
  49. } else if (m_relationship & Relationship::Icon) {
  50. auto favicon_url = document().parse_url(href());
  51. auto favicon_request = LoadRequest::create_for_url_on_page(favicon_url, document().page());
  52. set_resource(ResourceLoader::the().load_resource(Resource::Type::Generic, favicon_request));
  53. }
  54. }
  55. bool HTMLLinkElement::has_loaded_icon() const
  56. {
  57. return m_relationship & Relationship::Icon && resource() && resource()->is_loaded() && resource()->has_encoded_data();
  58. }
  59. void HTMLLinkElement::parse_attribute(FlyString const& name, String const& value)
  60. {
  61. // 4.6.7 Link types - https://html.spec.whatwg.org/multipage/links.html#linkTypes
  62. if (name == HTML::AttributeNames::rel) {
  63. m_relationship = 0;
  64. // Keywords are always ASCII case-insensitive, and must be compared as such.
  65. auto lowercased_value = value.to_lowercase();
  66. // To determine which link types apply to a link, a, area, or form element,
  67. // the element's rel attribute must be split on ASCII whitespace.
  68. // The resulting tokens are the keywords for the link types that apply to that element.
  69. auto parts = lowercased_value.split_view(Infra::is_ascii_whitespace);
  70. for (auto& part : parts) {
  71. if (part == "stylesheet"sv)
  72. m_relationship |= Relationship::Stylesheet;
  73. else if (part == "alternate"sv)
  74. m_relationship |= Relationship::Alternate;
  75. else if (part == "preload"sv)
  76. m_relationship |= Relationship::Preload;
  77. else if (part == "dns-prefetch"sv)
  78. m_relationship |= Relationship::DNSPrefetch;
  79. else if (part == "preconnect"sv)
  80. m_relationship |= Relationship::Preconnect;
  81. else if (part == "icon"sv)
  82. m_relationship |= Relationship::Icon;
  83. }
  84. }
  85. }
  86. void HTMLLinkElement::resource_did_fail()
  87. {
  88. dbgln_if(CSS_LOADER_DEBUG, "HTMLLinkElement: Resource did fail. URL: {}", resource()->url());
  89. m_document_load_event_delayer.clear();
  90. }
  91. void HTMLLinkElement::resource_did_load()
  92. {
  93. VERIFY(resource());
  94. VERIFY(m_relationship & (Relationship::Stylesheet | Relationship::Icon));
  95. if (m_relationship & Relationship::Stylesheet)
  96. resource_did_load_stylesheet();
  97. if (m_relationship & Relationship::Icon)
  98. resource_did_load_favicon();
  99. }
  100. void HTMLLinkElement::resource_did_load_stylesheet()
  101. {
  102. VERIFY(m_relationship & Relationship::Stylesheet);
  103. m_document_load_event_delayer.clear();
  104. if (!resource()->has_encoded_data()) {
  105. dbgln_if(CSS_LOADER_DEBUG, "HTMLLinkElement: Resource did load, no encoded data. URL: {}", resource()->url());
  106. } else {
  107. dbgln_if(CSS_LOADER_DEBUG, "HTMLLinkElement: Resource did load, has encoded data. URL: {}", resource()->url());
  108. if (resource()->mime_type() != "text/css"sv) {
  109. dbgln_if(CSS_LOADER_DEBUG, "HTMLLinkElement: Resource did load, but MIME type was {} instead of text/css. URL: {}", resource()->mime_type(), resource()->url());
  110. return;
  111. }
  112. }
  113. auto* sheet = parse_css_stylesheet(CSS::Parser::ParsingContext(document(), resource()->url()), resource()->encoded_data());
  114. if (!sheet) {
  115. dbgln_if(CSS_LOADER_DEBUG, "HTMLLinkElement: Failed to parse stylesheet: {}", resource()->url());
  116. return;
  117. }
  118. sheet->set_owner_node(this);
  119. document().style_sheets().add_sheet(*sheet);
  120. }
  121. void HTMLLinkElement::resource_did_load_favicon()
  122. {
  123. VERIFY(m_relationship & (Relationship::Icon));
  124. if (!resource()->has_encoded_data()) {
  125. dbgln_if(SPAM_DEBUG, "Favicon downloaded, no encoded data");
  126. return;
  127. }
  128. dbgln_if(SPAM_DEBUG, "Favicon downloaded, {} bytes from {}", resource()->encoded_data().size(), resource()->url());
  129. document().check_favicon_after_loading_link_resource();
  130. }
  131. bool HTMLLinkElement::load_favicon_and_use_if_window_is_active()
  132. {
  133. if (!has_loaded_icon())
  134. return false;
  135. RefPtr<Gfx::Bitmap> favicon_bitmap;
  136. auto decoded_image = Platform::ImageCodecPlugin::the().decode_image(resource()->encoded_data());
  137. if (!decoded_image.has_value() || decoded_image->frames.is_empty()) {
  138. dbgln("Could not decode favicon {}", resource()->url());
  139. return false;
  140. }
  141. favicon_bitmap = decoded_image->frames[0].bitmap;
  142. dbgln_if(IMAGE_DECODER_DEBUG, "Decoded favicon, {}", favicon_bitmap->size());
  143. auto* page = document().page();
  144. if (!page)
  145. return favicon_bitmap;
  146. if (document().browsing_context() == &page->top_level_browsing_context())
  147. if (favicon_bitmap) {
  148. page->client().page_did_change_favicon(*favicon_bitmap);
  149. return true;
  150. }
  151. return false;
  152. }
  153. }