HTMLLinkElement.cpp 8.0 KB

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