CSSStyleSheet.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Copyright (c) 2019-2021, Andreas Kling <kling@serenityos.org>
  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. #pragma once
  27. #include <AK/NonnullRefPtrVector.h>
  28. #include <AK/TypeCasts.h>
  29. #include <LibWeb/CSS/CSSImportRule.h>
  30. #include <LibWeb/CSS/CSSRule.h>
  31. #include <LibWeb/CSS/StyleSheet.h>
  32. #include <LibWeb/Loader/Resource.h>
  33. namespace Web::CSS {
  34. class CSSStyleSheet final : public StyleSheet {
  35. public:
  36. static NonnullRefPtr<CSSStyleSheet> create(NonnullRefPtrVector<CSSRule> rules)
  37. {
  38. return adopt(*new CSSStyleSheet(move(rules)));
  39. }
  40. virtual ~CSSStyleSheet();
  41. const NonnullRefPtrVector<CSSRule>& rules() const { return m_rules; }
  42. NonnullRefPtrVector<CSSRule>& rules() { return m_rules; }
  43. template<typename Callback>
  44. void for_each_effective_style_rule(Callback callback) const
  45. {
  46. for (auto& rule : m_rules)
  47. if (rule.type() == CSSRule::Type::Style) {
  48. callback(downcast<CSSStyleRule>(rule));
  49. } else if (rule.type() == CSSRule::Type::Import) {
  50. const auto& import_rule = downcast<CSSImportRule>(rule);
  51. if (import_rule.has_import_result())
  52. import_rule.loaded_style_sheet()->for_each_effective_style_rule(callback);
  53. }
  54. }
  55. template<typename Callback>
  56. bool for_first_not_loaded_import_rule(Callback callback)
  57. {
  58. for (auto& rule : m_rules)
  59. if (rule.type() == CSSRule::Type::Import) {
  60. auto& import_rule = downcast<CSSImportRule>(rule);
  61. if (!import_rule.has_import_result()) {
  62. callback(import_rule);
  63. return true;
  64. }
  65. if (import_rule.loaded_style_sheet()->for_first_not_loaded_import_rule(callback)) {
  66. return true;
  67. }
  68. }
  69. return false;
  70. }
  71. private:
  72. explicit CSSStyleSheet(NonnullRefPtrVector<CSSRule>);
  73. NonnullRefPtrVector<CSSRule> m_rules;
  74. };
  75. }