CSSImportRule.cpp 3.7 KB

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