HTMLLinkElement.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. if (has_attribute(AttributeNames::disabled) && (m_relationship & Relationship::Stylesheet))
  28. return;
  29. HTMLElement::inserted();
  30. if (m_relationship & Relationship::Stylesheet && !(m_relationship & Relationship::Alternate)) {
  31. auto url = document().parse_url(href());
  32. dbgln_if(CSS_LOADER_DEBUG, "HTMLLinkElement: Loading import URL: {}", url);
  33. auto request = LoadRequest::create_for_url_on_page(url, document().page());
  34. // NOTE: Mark this element as delaying the document load event *before* calling set_resource()
  35. // as it may trigger a synchronous resource_did_load() callback.
  36. m_document_load_event_delayer.emplace(document());
  37. set_resource(ResourceLoader::the().load_resource(Resource::Type::Generic, request));
  38. // NOTE: If we ended up not loading a resource for whatever reason, don't delay the load event.
  39. if (!resource())
  40. m_document_load_event_delayer.clear();
  41. }
  42. if (m_relationship & Relationship::Preload) {
  43. // FIXME: Respect the "as" attribute.
  44. LoadRequest request;
  45. request.set_url(document().parse_url(attribute(HTML::AttributeNames::href)));
  46. m_preload_resource = ResourceLoader::the().load_resource(Resource::Type::Generic, request);
  47. } else if (m_relationship & Relationship::DNSPrefetch) {
  48. ResourceLoader::the().prefetch_dns(document().parse_url(attribute(HTML::AttributeNames::href)));
  49. } else if (m_relationship & Relationship::Preconnect) {
  50. ResourceLoader::the().preconnect(document().parse_url(attribute(HTML::AttributeNames::href)));
  51. } else if (m_relationship & Relationship::Icon) {
  52. auto favicon_url = document().parse_url(href());
  53. auto favicon_request = LoadRequest::create_for_url_on_page(favicon_url, document().page());
  54. set_resource(ResourceLoader::the().load_resource(Resource::Type::Generic, favicon_request));
  55. }
  56. }
  57. bool HTMLLinkElement::has_loaded_icon() const
  58. {
  59. return m_relationship & Relationship::Icon && resource() && resource()->is_loaded() && resource()->has_encoded_data();
  60. }
  61. void HTMLLinkElement::parse_attribute(FlyString const& name, DeprecatedString const& value)
  62. {
  63. // 4.6.7 Link types - https://html.spec.whatwg.org/multipage/links.html#linkTypes
  64. if (name == HTML::AttributeNames::rel) {
  65. m_relationship = 0;
  66. // Keywords are always ASCII case-insensitive, and must be compared as such.
  67. auto lowercased_value = value.to_lowercase();
  68. // To determine which link types apply to a link, a, area, or form element,
  69. // the element's rel attribute must be split on ASCII whitespace.
  70. // The resulting tokens are the keywords for the link types that apply to that element.
  71. auto parts = lowercased_value.split_view(Infra::is_ascii_whitespace);
  72. for (auto& part : parts) {
  73. if (part == "stylesheet"sv)
  74. m_relationship |= Relationship::Stylesheet;
  75. else if (part == "alternate"sv)
  76. m_relationship |= Relationship::Alternate;
  77. else if (part == "preload"sv)
  78. m_relationship |= Relationship::Preload;
  79. else if (part == "dns-prefetch"sv)
  80. m_relationship |= Relationship::DNSPrefetch;
  81. else if (part == "preconnect"sv)
  82. m_relationship |= Relationship::Preconnect;
  83. else if (part == "icon"sv)
  84. m_relationship |= Relationship::Icon;
  85. }
  86. }
  87. if (name == HTML::AttributeNames::disabled && (m_relationship & Relationship::Stylesheet) && m_loaded_style_sheet)
  88. document().style_sheets().remove_sheet(*m_loaded_style_sheet);
  89. }
  90. void HTMLLinkElement::resource_did_fail()
  91. {
  92. dbgln_if(CSS_LOADER_DEBUG, "HTMLLinkElement: Resource did fail. URL: {}", resource()->url());
  93. m_document_load_event_delayer.clear();
  94. }
  95. void HTMLLinkElement::resource_did_load()
  96. {
  97. VERIFY(resource());
  98. VERIFY(m_relationship & (Relationship::Stylesheet | Relationship::Icon));
  99. if (m_relationship & Relationship::Stylesheet)
  100. resource_did_load_stylesheet();
  101. if (m_relationship & Relationship::Icon)
  102. resource_did_load_favicon();
  103. }
  104. void HTMLLinkElement::did_remove_attribute(FlyString const& attr)
  105. {
  106. if (attr == HTML::AttributeNames::disabled && (m_relationship & Relationship::Stylesheet)) {
  107. if (!resource())
  108. inserted();
  109. else
  110. resource_did_load_stylesheet();
  111. }
  112. }
  113. void HTMLLinkElement::resource_did_load_stylesheet()
  114. {
  115. VERIFY(m_relationship & Relationship::Stylesheet);
  116. m_document_load_event_delayer.clear();
  117. if (!resource()->has_encoded_data()) {
  118. dbgln_if(CSS_LOADER_DEBUG, "HTMLLinkElement: Resource did load, no encoded data. URL: {}", resource()->url());
  119. } else {
  120. dbgln_if(CSS_LOADER_DEBUG, "HTMLLinkElement: Resource did load, has encoded data. URL: {}", resource()->url());
  121. if (resource()->mime_type() != "text/css"sv) {
  122. dbgln_if(CSS_LOADER_DEBUG, "HTMLLinkElement: Resource did load, but MIME type was {} instead of text/css. URL: {}", resource()->mime_type(), resource()->url());
  123. return;
  124. }
  125. }
  126. CSS::CSSStyleSheet* sheet = m_loaded_style_sheet;
  127. if (!sheet) {
  128. sheet = parse_css_stylesheet(CSS::Parser::ParsingContext(document(), resource()->url()), resource()->encoded_data());
  129. if (!sheet) {
  130. dbgln_if(CSS_LOADER_DEBUG, "HTMLLinkElement: Failed to parse stylesheet: {}", resource()->url());
  131. return;
  132. }
  133. m_loaded_style_sheet = sheet;
  134. }
  135. sheet->set_owner_node(this);
  136. document().style_sheets().add_sheet(*sheet);
  137. }
  138. void HTMLLinkElement::resource_did_load_favicon()
  139. {
  140. VERIFY(m_relationship & (Relationship::Icon));
  141. if (!resource()->has_encoded_data()) {
  142. dbgln_if(SPAM_DEBUG, "Favicon downloaded, no encoded data");
  143. return;
  144. }
  145. dbgln_if(SPAM_DEBUG, "Favicon downloaded, {} bytes from {}", resource()->encoded_data().size(), resource()->url());
  146. document().check_favicon_after_loading_link_resource();
  147. }
  148. bool HTMLLinkElement::load_favicon_and_use_if_window_is_active()
  149. {
  150. if (!has_loaded_icon())
  151. return false;
  152. RefPtr<Gfx::Bitmap> favicon_bitmap;
  153. auto decoded_image = Platform::ImageCodecPlugin::the().decode_image(resource()->encoded_data());
  154. if (!decoded_image.has_value() || decoded_image->frames.is_empty()) {
  155. dbgln("Could not decode favicon {}", resource()->url());
  156. return false;
  157. }
  158. favicon_bitmap = decoded_image->frames[0].bitmap;
  159. dbgln_if(IMAGE_DECODER_DEBUG, "Decoded favicon, {}", favicon_bitmap->size());
  160. auto* page = document().page();
  161. if (!page)
  162. return favicon_bitmap;
  163. if (document().browsing_context() == &page->top_level_browsing_context())
  164. if (favicon_bitmap) {
  165. page->client().page_did_change_favicon(*favicon_bitmap);
  166. return true;
  167. }
  168. return false;
  169. }
  170. void HTMLLinkElement::visit_edges(Cell::Visitor& visitor)
  171. {
  172. Base::visit_edges(visitor);
  173. visitor.visit(m_loaded_style_sheet);
  174. }
  175. }