CSSImportRule.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (c) 2021, the SerenityOS developers.
  3. * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
  4. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <AK/Debug.h>
  9. #include <AK/URL.h>
  10. #include <LibWeb/CSS/CSSImportRule.h>
  11. #include <LibWeb/CSS/Parser/Parser.h>
  12. #include <LibWeb/DOM/Document.h>
  13. #include <LibWeb/HTML/Window.h>
  14. #include <LibWeb/Loader/ResourceLoader.h>
  15. namespace Web::CSS {
  16. CSSImportRule* CSSImportRule::create(AK::URL url, DOM::Document& document)
  17. {
  18. auto& window_object = document.window();
  19. return window_object.heap().allocate<CSSImportRule>(window_object.realm(), move(url), document);
  20. }
  21. CSSImportRule::CSSImportRule(AK::URL url, DOM::Document& document)
  22. : CSSRule(document.window())
  23. , m_url(move(url))
  24. , m_document(document)
  25. {
  26. set_prototype(&document.window().cached_web_prototype("CSSImportRule"));
  27. dbgln_if(CSS_LOADER_DEBUG, "CSSImportRule: Loading import URL: {}", m_url);
  28. auto request = LoadRequest::create_for_url_on_page(m_url, document.page());
  29. // NOTE: Mark this rule as delaying the document load event *before* calling set_resource()
  30. // as it may trigger a synchronous resource_did_load() callback.
  31. m_document_load_event_delayer.emplace(document);
  32. set_resource(ResourceLoader::the().load_resource(Resource::Type::Generic, request));
  33. }
  34. void CSSImportRule::visit_edges(Cell::Visitor& visitor)
  35. {
  36. Base::visit_edges(visitor);
  37. visitor.visit(m_document);
  38. visitor.visit(m_style_sheet);
  39. }
  40. // https://www.w3.org/TR/cssom/#serialize-a-css-rule
  41. String CSSImportRule::serialized() const
  42. {
  43. StringBuilder builder;
  44. // The result of concatenating the following:
  45. // 1. The string "@import" followed by a single SPACE (U+0020).
  46. builder.append("@import "sv);
  47. // 2. The result of performing serialize a URL on the rule’s location.
  48. // FIXME: Look into the correctness of this serialization
  49. builder.append("url("sv);
  50. builder.append(m_url.to_string());
  51. builder.append(')');
  52. // FIXME: 3. If the rule’s associated media list is not empty, a single SPACE (U+0020) followed by the result of performing serialize a media query list on the media list.
  53. // 4. The string ";", i.e., SEMICOLON (U+003B).
  54. builder.append(';');
  55. return builder.to_string();
  56. }
  57. void CSSImportRule::resource_did_fail()
  58. {
  59. dbgln_if(CSS_LOADER_DEBUG, "CSSImportRule: Resource did fail. URL: {}", resource()->url());
  60. m_document_load_event_delayer.clear();
  61. }
  62. void CSSImportRule::resource_did_load()
  63. {
  64. VERIFY(resource());
  65. if (!m_document)
  66. return;
  67. m_document_load_event_delayer.clear();
  68. if (!resource()->has_encoded_data()) {
  69. dbgln_if(CSS_LOADER_DEBUG, "CSSImportRule: Resource did load, no encoded data. URL: {}", resource()->url());
  70. } else {
  71. dbgln_if(CSS_LOADER_DEBUG, "CSSImportRule: Resource did load, has encoded data. URL: {}", resource()->url());
  72. }
  73. auto* sheet = parse_css_stylesheet(CSS::Parser::ParsingContext(*m_document, resource()->url()), resource()->encoded_data());
  74. if (!sheet) {
  75. dbgln_if(CSS_LOADER_DEBUG, "CSSImportRule: Failed to parse stylesheet: {}", resource()->url());
  76. return;
  77. }
  78. m_style_sheet = sheet;
  79. m_document->style_computer().invalidate_rule_cache();
  80. m_document->invalidate_style();
  81. }
  82. }