CSSStyleSheet.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. // https://www.w3.org/TR/cssom/#dom-cssstylesheet-insertrule
  17. DOM::ExceptionOr<unsigned> CSSStyleSheet::insert_rule(StringView rule, unsigned index)
  18. {
  19. // FIXME: 1. If the origin-clean flag is unset, throw a SecurityError exception.
  20. // FIXME: 2. If the disallow modification flag is set, throw a NotAllowedError DOMException.
  21. // 3. Let parsed rule be the return value of invoking parse a rule with rule.
  22. auto parsed_rule = parse_css_rule(CSS::ParsingContext {}, rule);
  23. // 4. If parsed rule is a syntax error, return parsed rule.
  24. if (!parsed_rule)
  25. return DOM::SyntaxError::create("Unable to parse CSS rule.");
  26. // FIXME: 5. If parsed rule is an @import rule, and the constructed flag is set, throw a SyntaxError DOMException.
  27. // 6. Return the result of invoking insert a CSS rule rule in the CSS rules at index.
  28. auto result = m_rules->insert_a_css_rule(parsed_rule.release_nonnull(), index);
  29. if (!result.is_exception()) {
  30. if (m_style_sheet_list) {
  31. m_style_sheet_list->document().style_computer().invalidate_rule_cache();
  32. m_style_sheet_list->document().invalidate_style();
  33. }
  34. }
  35. return result;
  36. }
  37. // https://www.w3.org/TR/cssom/#dom-cssstylesheet-deleterule
  38. DOM::ExceptionOr<void> CSSStyleSheet::delete_rule(unsigned index)
  39. {
  40. // FIXME: 1. If the origin-clean flag is unset, throw a SecurityError exception.
  41. // FIXME: 2. If the disallow modification flag is set, throw a NotAllowedError DOMException.
  42. // 3. Remove a CSS rule in the CSS rules at index.
  43. auto result = m_rules->remove_a_css_rule(index);
  44. if (!result.is_exception()) {
  45. if (m_style_sheet_list) {
  46. m_style_sheet_list->document().style_computer().invalidate_rule_cache();
  47. m_style_sheet_list->document().invalidate_style();
  48. }
  49. }
  50. return result;
  51. }
  52. // https://www.w3.org/TR/cssom/#dom-cssstylesheet-removerule
  53. DOM::ExceptionOr<void> CSSStyleSheet::remove_rule(unsigned index)
  54. {
  55. // The removeRule(index) method must run the same steps as deleteRule().
  56. return delete_rule(index);
  57. }
  58. void CSSStyleSheet::for_each_effective_style_rule(Function<void(CSSStyleRule const&)> const& callback) const
  59. {
  60. m_rules->for_each_effective_style_rule(callback);
  61. }
  62. bool CSSStyleSheet::evaluate_media_queries(HTML::Window const& window)
  63. {
  64. return m_rules->evaluate_media_queries(window);
  65. }
  66. void CSSStyleSheet::set_style_sheet_list(Badge<StyleSheetList>, StyleSheetList* list)
  67. {
  68. m_style_sheet_list = list;
  69. }
  70. }