CSSLoader.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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/Loader/CSSLoader.h>
  32. #include <LibWeb/Loader/ResourceLoader.h>
  33. namespace Web {
  34. CSSLoader::CSSLoader(DOM::Document& document)
  35. : m_document(&document)
  36. {
  37. }
  38. void CSSLoader::load_from_text(const String& text)
  39. {
  40. m_style_sheet = parse_css(CSS::ParsingContext(*m_document), text);
  41. if (!m_style_sheet)
  42. m_style_sheet = CSS::CSSStyleSheet::create({});
  43. load_next_import_if_needed();
  44. }
  45. void CSSLoader::load_from_url(const URL& url)
  46. {
  47. m_style_sheet = CSS::CSSStyleSheet::create({});
  48. LoadRequest request;
  49. request.set_url(url);
  50. set_resource(ResourceLoader::the().load_resource(Resource::Type::Generic, request));
  51. }
  52. void CSSLoader::resource_did_load()
  53. {
  54. VERIFY(resource());
  55. if (!resource()->has_encoded_data()) {
  56. dbgln_if(CSS_LOADER_DEBUG, "CSSLoader: Resource did load, no encoded data. URL: {}", resource()->url());
  57. } else {
  58. dbgln_if(CSS_LOADER_DEBUG, "CSSLoader: Resource did load, has encoded data. URL: {}", resource()->url());
  59. }
  60. auto sheet = parse_css(CSS::ParsingContext(*m_document), resource()->encoded_data());
  61. if (!sheet) {
  62. dbgln_if(CSS_LOADER_DEBUG, "CSSLoader: Failed to parse stylesheet: {}", resource()->url());
  63. return;
  64. }
  65. bool was_imported = m_style_sheet->for_first_not_loaded_import_rule([&](auto& rule) {
  66. rule.set_style_sheet(sheet);
  67. });
  68. // Transfer the rules from the successfully parsed sheet into the sheet we've already inserted.
  69. if (!was_imported) {
  70. m_style_sheet->rules() = sheet->rules();
  71. }
  72. if (on_load)
  73. on_load();
  74. load_next_import_if_needed();
  75. }
  76. void CSSLoader::resource_did_fail()
  77. {
  78. dbgln_if(CSS_LOADER_DEBUG, "CSSLoader: Resource did fail. URL: {}", resource()->url());
  79. load_next_import_if_needed();
  80. }
  81. void CSSLoader::load_next_import_if_needed()
  82. {
  83. // Create load request for the first import which isn't loaded.
  84. // TODO: We need to somehow handle infinite cycles in imports.
  85. m_style_sheet->for_first_not_loaded_import_rule([&](auto& rule) {
  86. dbgln_if(CSS_LOADER_DEBUG, "CSSLoader: Loading @import {}", rule.url());
  87. LoadRequest request;
  88. request.set_url(rule.url());
  89. set_resource(ResourceLoader::the().load_resource(Resource::Type::Generic, request));
  90. });
  91. }
  92. }