CSSImportRule.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. set_resource(ResourceLoader::the().load_resource(Resource::Type::Generic, request));
  21. m_document_load_event_delayer.emplace(document);
  22. }
  23. CSSImportRule::~CSSImportRule()
  24. {
  25. }
  26. // https://www.w3.org/TR/cssom/#serialize-a-css-rule
  27. String CSSImportRule::serialized() const
  28. {
  29. StringBuilder builder;
  30. // The result of concatenating the following:
  31. // 1. The string "@import" followed by a single SPACE (U+0020).
  32. builder.append("@import "sv);
  33. // 2. The result of performing serialize a URL on the rule’s location.
  34. // FIXME: Look into the correctness of this serialization
  35. builder.append("url("sv);
  36. builder.append(m_url.to_string());
  37. builder.append(')');
  38. // 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.
  39. // 4. The string ";", i.e., SEMICOLON (U+003B).
  40. builder.append(';');
  41. return builder.to_string();
  42. }
  43. void CSSImportRule::resource_did_fail()
  44. {
  45. dbgln_if(CSS_LOADER_DEBUG, "CSSImportRule: Resource did fail. URL: {}", resource()->url());
  46. m_document_load_event_delayer.clear();
  47. }
  48. void CSSImportRule::resource_did_load()
  49. {
  50. VERIFY(resource());
  51. if (!m_document)
  52. return;
  53. m_document_load_event_delayer.clear();
  54. if (!resource()->has_encoded_data()) {
  55. dbgln_if(CSS_LOADER_DEBUG, "CSSImportRule: Resource did load, no encoded data. URL: {}", resource()->url());
  56. } else {
  57. dbgln_if(CSS_LOADER_DEBUG, "CSSImportRule: Resource did load, has encoded data. URL: {}", resource()->url());
  58. }
  59. auto sheet = parse_css(CSS::ParsingContext(*m_document), resource()->encoded_data());
  60. if (!sheet) {
  61. dbgln_if(CSS_LOADER_DEBUG, "CSSImportRule: Failed to parse stylesheet: {}", resource()->url());
  62. return;
  63. }
  64. m_style_sheet = move(sheet);
  65. }
  66. }