CSSImportRule.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. CSSImportRule* CSSImportRule::create(AK::URL url, DOM::Document& document)
  19. {
  20. auto& realm = document.realm();
  21. return 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. set_prototype(&Bindings::ensure_web_prototype<Bindings::CSSImportRulePrototype>(document.realm(), "CSSImportRule"));
  29. dbgln_if(CSS_LOADER_DEBUG, "CSSImportRule: Loading import URL: {}", m_url);
  30. auto request = LoadRequest::create_for_url_on_page(m_url, document.page());
  31. // NOTE: Mark this rule 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. }
  36. void CSSImportRule::visit_edges(Cell::Visitor& visitor)
  37. {
  38. Base::visit_edges(visitor);
  39. visitor.visit(m_document);
  40. visitor.visit(m_style_sheet);
  41. }
  42. // https://www.w3.org/TR/cssom/#serialize-a-css-rule
  43. DeprecatedString CSSImportRule::serialized() const
  44. {
  45. StringBuilder builder;
  46. // The result of concatenating the following:
  47. // 1. The string "@import" followed by a single SPACE (U+0020).
  48. builder.append("@import "sv);
  49. // 2. The result of performing serialize a URL on the rule’s location.
  50. // FIXME: Look into the correctness of this serialization
  51. builder.append("url("sv);
  52. builder.append(m_url.to_deprecated_string());
  53. builder.append(')');
  54. // 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.
  55. // 4. The string ";", i.e., SEMICOLON (U+003B).
  56. builder.append(';');
  57. return builder.to_deprecated_string();
  58. }
  59. void CSSImportRule::resource_did_fail()
  60. {
  61. dbgln_if(CSS_LOADER_DEBUG, "CSSImportRule: Resource did fail. URL: {}", resource()->url());
  62. m_document_load_event_delayer.clear();
  63. }
  64. void CSSImportRule::resource_did_load()
  65. {
  66. VERIFY(resource());
  67. if (!m_document)
  68. return;
  69. m_document_load_event_delayer.clear();
  70. if (!resource()->has_encoded_data()) {
  71. dbgln_if(CSS_LOADER_DEBUG, "CSSImportRule: Resource did load, no encoded data. URL: {}", resource()->url());
  72. } else {
  73. dbgln_if(CSS_LOADER_DEBUG, "CSSImportRule: Resource did load, has encoded data. URL: {}", resource()->url());
  74. }
  75. auto* sheet = parse_css_stylesheet(CSS::Parser::ParsingContext(*m_document, resource()->url()), resource()->encoded_data());
  76. if (!sheet) {
  77. dbgln_if(CSS_LOADER_DEBUG, "CSSImportRule: Failed to parse stylesheet: {}", resource()->url());
  78. return;
  79. }
  80. m_style_sheet = sheet;
  81. m_document->style_computer().invalidate_rule_cache();
  82. m_document->invalidate_style();
  83. }
  84. }