CSSImportRule.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 <LibURL/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(URL::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(URL::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. WEB_SET_PROTOTYPE_FOR_INTERFACE(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. String 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. serialize_a_url(builder, MUST(m_url.to_string()));
  57. // 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.
  58. // 4. The string ";", i.e., SEMICOLON (U+003B).
  59. builder.append(';');
  60. return MUST(builder.to_string());
  61. }
  62. void CSSImportRule::resource_did_fail()
  63. {
  64. dbgln_if(CSS_LOADER_DEBUG, "CSSImportRule: Resource did fail. URL: {}", resource()->url());
  65. m_document_load_event_delayer.clear();
  66. }
  67. void CSSImportRule::resource_did_load()
  68. {
  69. VERIFY(resource());
  70. if (!m_document)
  71. return;
  72. m_document_load_event_delayer.clear();
  73. if (!resource()->has_encoded_data()) {
  74. dbgln_if(CSS_LOADER_DEBUG, "CSSImportRule: Resource did load, no encoded data. URL: {}", resource()->url());
  75. } else {
  76. dbgln_if(CSS_LOADER_DEBUG, "CSSImportRule: Resource did load, has encoded data. URL: {}", resource()->url());
  77. }
  78. auto* sheet = parse_css_stylesheet(CSS::Parser::ParsingContext(*m_document, resource()->url()), resource()->encoded_data());
  79. if (!sheet) {
  80. dbgln_if(CSS_LOADER_DEBUG, "CSSImportRule: Failed to parse stylesheet: {}", resource()->url());
  81. return;
  82. }
  83. m_style_sheet = sheet;
  84. m_document->style_computer().invalidate_rule_cache();
  85. m_document->style_computer().load_fonts_from_sheet(*m_style_sheet);
  86. m_document->invalidate_style();
  87. }
  88. }