CSSImportRule.cpp 2.8 KB

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