HTMLLinkElement.cpp 6.9 KB

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