CSSLoader.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright (c) 2021, the SerenityOS developers.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Debug.h>
  27. #include <AK/URL.h>
  28. #include <LibWeb/CSS/CSSImportRule.h>
  29. #include <LibWeb/CSS/Parser/CSSParser.h>
  30. #include <LibWeb/CSS/StyleSheet.h>
  31. #include <LibWeb/DOM/Element.h>
  32. #include <LibWeb/Loader/CSSLoader.h>
  33. #include <LibWeb/Loader/ResourceLoader.h>
  34. namespace Web {
  35. CSSLoader::CSSLoader(DOM::Element& owner_element)
  36. : m_owner_element(owner_element)
  37. {
  38. }
  39. void CSSLoader::load_from_text(const String& text)
  40. {
  41. m_style_sheet = parse_css(CSS::ParsingContext(m_owner_element.document()), text);
  42. if (!m_style_sheet) {
  43. m_style_sheet = CSS::CSSStyleSheet::create({});
  44. m_style_sheet->set_owner_node(&m_owner_element);
  45. }
  46. load_next_import_if_needed();
  47. }
  48. void CSSLoader::load_from_url(const URL& url)
  49. {
  50. m_style_sheet = CSS::CSSStyleSheet::create({});
  51. m_style_sheet->set_owner_node(&m_owner_element);
  52. LoadRequest request;
  53. request.set_url(url);
  54. set_resource(ResourceLoader::the().load_resource(Resource::Type::Generic, request));
  55. }
  56. void CSSLoader::resource_did_load()
  57. {
  58. VERIFY(resource());
  59. if (!resource()->has_encoded_data()) {
  60. dbgln_if(CSS_LOADER_DEBUG, "CSSLoader: Resource did load, no encoded data. URL: {}", resource()->url());
  61. } else {
  62. dbgln_if(CSS_LOADER_DEBUG, "CSSLoader: Resource did load, has encoded data. URL: {}", resource()->url());
  63. }
  64. auto sheet = parse_css(CSS::ParsingContext(m_owner_element.document()), resource()->encoded_data());
  65. if (!sheet) {
  66. dbgln_if(CSS_LOADER_DEBUG, "CSSLoader: Failed to parse stylesheet: {}", resource()->url());
  67. return;
  68. }
  69. bool was_imported = m_style_sheet->for_first_not_loaded_import_rule([&](auto& rule) {
  70. rule.set_style_sheet(sheet);
  71. });
  72. // Transfer the rules from the successfully parsed sheet into the sheet we've already inserted.
  73. if (!was_imported) {
  74. m_style_sheet->rules() = sheet->rules();
  75. }
  76. if (on_load)
  77. on_load();
  78. load_next_import_if_needed();
  79. }
  80. void CSSLoader::resource_did_fail()
  81. {
  82. dbgln_if(CSS_LOADER_DEBUG, "CSSLoader: Resource did fail. URL: {}", resource()->url());
  83. load_next_import_if_needed();
  84. }
  85. void CSSLoader::load_next_import_if_needed()
  86. {
  87. // Create load request for the first import which isn't loaded.
  88. // TODO: We need to somehow handle infinite cycles in imports.
  89. m_style_sheet->for_first_not_loaded_import_rule([&](auto& rule) {
  90. dbgln_if(CSS_LOADER_DEBUG, "CSSLoader: Loading @import {}", rule.url());
  91. LoadRequest request;
  92. request.set_url(rule.url());
  93. set_resource(ResourceLoader::the().load_resource(Resource::Type::Generic, request));
  94. });
  95. }
  96. }