HTMLLinkElement.cpp 8.0 KB

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