CSSStyleSheet.cpp 2.9 KB

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