CSSStyleSheet.cpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright (c) 2019-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/CSS/CSSImportRule.h>
  7. #include <LibWeb/CSS/CSSStyleSheet.h>
  8. #include <LibWeb/CSS/Parser/Parser.h>
  9. #include <LibWeb/DOM/ExceptionOr.h>
  10. namespace Web::CSS {
  11. CSSStyleSheet::CSSStyleSheet(NonnullRefPtrVector<CSSRule> rules)
  12. : m_rules(CSSRuleList::create(move(rules)))
  13. {
  14. }
  15. CSSStyleSheet::~CSSStyleSheet()
  16. {
  17. }
  18. // https://drafts.csswg.org/cssom/#dom-cssstylesheet-insertrule
  19. DOM::ExceptionOr<unsigned> CSSStyleSheet::insert_rule(StringView rule, unsigned index)
  20. {
  21. // FIXME: 1. If the origin-clean flag is unset, throw a SecurityError exception.
  22. // FIXME: 2. If the disallow modification flag is set, throw a NotAllowedError DOMException.
  23. // 3. Let parsed rule be the return value of invoking parse a rule with rule.
  24. auto parsed_rule = parse_css_rule(CSS::ParsingContext {}, rule);
  25. // 4. If parsed rule is a syntax error, return parsed rule.
  26. if (!parsed_rule)
  27. return DOM::SyntaxError::create("Unable to parse CSS rule.");
  28. // FIXME: 5. If parsed rule is an @import rule, and the constructed flag is set, throw a SyntaxError DOMException.
  29. // 6. Return the result of invoking insert a CSS rule rule in the CSS rules at index.
  30. return m_rules->insert_a_css_rule(parsed_rule.release_nonnull(), index);
  31. }
  32. // https://drafts.csswg.org/cssom/#dom-cssstylesheet-deleterule
  33. DOM::ExceptionOr<void> CSSStyleSheet::delete_rule(unsigned index)
  34. {
  35. // FIXME: 1. If the origin-clean flag is unset, throw a SecurityError exception.
  36. // FIXME: 2. If the disallow modification flag is set, throw a NotAllowedError DOMException.
  37. // 3. Remove a CSS rule in the CSS rules at index.
  38. return m_rules->remove_a_css_rule(index);
  39. }
  40. // https://drafts.csswg.org/cssom/#dom-cssstylesheet-removerule
  41. DOM::ExceptionOr<void> CSSStyleSheet::remove_rule(unsigned index)
  42. {
  43. // The removeRule(index) method must run the same steps as deleteRule().
  44. return delete_rule(index);
  45. }
  46. void CSSStyleSheet::for_each_effective_style_rule(Function<void(CSSStyleRule const&)> const& callback) const
  47. {
  48. for (auto& rule : *m_rules)
  49. if (rule.type() == CSSRule::Type::Style) {
  50. callback(verify_cast<CSSStyleRule>(rule));
  51. } else if (rule.type() == CSSRule::Type::Import) {
  52. const auto& import_rule = verify_cast<CSSImportRule>(rule);
  53. if (import_rule.has_import_result())
  54. import_rule.loaded_style_sheet()->for_each_effective_style_rule(callback);
  55. }
  56. }
  57. bool CSSStyleSheet::for_first_not_loaded_import_rule(Function<void(CSSImportRule&)> const& callback)
  58. {
  59. for (auto& rule : *m_rules)
  60. if (rule.type() == CSSRule::Type::Import) {
  61. auto& import_rule = verify_cast<CSSImportRule>(rule);
  62. if (!import_rule.has_import_result()) {
  63. callback(import_rule);
  64. return true;
  65. }
  66. if (import_rule.loaded_style_sheet()->for_first_not_loaded_import_rule(callback)) {
  67. return true;
  68. }
  69. }
  70. return false;
  71. }
  72. }