CSSImportRule.cpp 2.8 KB

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