CSSImportRule.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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/Bindings/CSSImportRulePrototype.h>
  11. #include <LibWeb/Bindings/Intrinsics.h>
  12. #include <LibWeb/CSS/CSSImportRule.h>
  13. #include <LibWeb/CSS/Parser/Parser.h>
  14. #include <LibWeb/DOM/Document.h>
  15. #include <LibWeb/HTML/Window.h>
  16. #include <LibWeb/Loader/ResourceLoader.h>
  17. namespace Web::CSS {
  18. WebIDL::ExceptionOr<JS::NonnullGCPtr<CSSImportRule>> CSSImportRule::create(AK::URL url, DOM::Document& document)
  19. {
  20. auto& realm = document.realm();
  21. return MUST_OR_THROW_OOM(realm.heap().allocate<CSSImportRule>(realm, move(url), document));
  22. }
  23. CSSImportRule::CSSImportRule(AK::URL url, DOM::Document& document)
  24. : CSSRule(document.realm())
  25. , m_url(move(url))
  26. , m_document(document)
  27. {
  28. dbgln_if(CSS_LOADER_DEBUG, "CSSImportRule: Loading import URL: {}", m_url);
  29. auto request = LoadRequest::create_for_url_on_page(m_url, document.page());
  30. // NOTE: Mark this rule as delaying the document load event *before* calling set_resource()
  31. // as it may trigger a synchronous resource_did_load() callback.
  32. m_document_load_event_delayer.emplace(document);
  33. set_resource(ResourceLoader::the().load_resource(Resource::Type::Generic, request));
  34. }
  35. JS::ThrowCompletionOr<void> CSSImportRule::initialize(JS::Realm& realm)
  36. {
  37. MUST_OR_THROW_OOM(Base::initialize(realm));
  38. set_prototype(&Bindings::ensure_web_prototype<Bindings::CSSImportRulePrototype>(realm, "CSSImportRule"));
  39. return {};
  40. }
  41. void CSSImportRule::visit_edges(Cell::Visitor& visitor)
  42. {
  43. Base::visit_edges(visitor);
  44. visitor.visit(m_document);
  45. visitor.visit(m_style_sheet);
  46. }
  47. // https://www.w3.org/TR/cssom/#serialize-a-css-rule
  48. DeprecatedString CSSImportRule::serialized() const
  49. {
  50. StringBuilder builder;
  51. // The result of concatenating the following:
  52. // 1. The string "@import" followed by a single SPACE (U+0020).
  53. builder.append("@import "sv);
  54. // 2. The result of performing serialize a URL on the rule’s location.
  55. // FIXME: Look into the correctness of this serialization
  56. builder.append("url("sv);
  57. builder.append(m_url.to_deprecated_string());
  58. builder.append(')');
  59. // 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.
  60. // 4. The string ";", i.e., SEMICOLON (U+003B).
  61. builder.append(';');
  62. return builder.to_deprecated_string();
  63. }
  64. void CSSImportRule::resource_did_fail()
  65. {
  66. dbgln_if(CSS_LOADER_DEBUG, "CSSImportRule: Resource did fail. URL: {}", resource()->url());
  67. m_document_load_event_delayer.clear();
  68. }
  69. void CSSImportRule::resource_did_load()
  70. {
  71. VERIFY(resource());
  72. if (!m_document)
  73. return;
  74. m_document_load_event_delayer.clear();
  75. if (!resource()->has_encoded_data()) {
  76. dbgln_if(CSS_LOADER_DEBUG, "CSSImportRule: Resource did load, no encoded data. URL: {}", resource()->url());
  77. } else {
  78. dbgln_if(CSS_LOADER_DEBUG, "CSSImportRule: Resource did load, has encoded data. URL: {}", resource()->url());
  79. }
  80. auto* sheet = parse_css_stylesheet(CSS::Parser::ParsingContext(*m_document, resource()->url()), resource()->encoded_data());
  81. if (!sheet) {
  82. dbgln_if(CSS_LOADER_DEBUG, "CSSImportRule: Failed to parse stylesheet: {}", resource()->url());
  83. return;
  84. }
  85. m_style_sheet = sheet;
  86. m_document->style_computer().invalidate_rule_cache();
  87. m_document->invalidate_style();
  88. }
  89. }