CSSStyleSheet.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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/DOM/ExceptionOr.h>
  9. namespace Web::CSS {
  10. CSSStyleSheet::CSSStyleSheet(NonnullRefPtrVector<CSSRule> rules)
  11. : m_rules(CSSRuleList::create(move(rules)))
  12. {
  13. }
  14. CSSStyleSheet::~CSSStyleSheet()
  15. {
  16. }
  17. // https://www.w3.org/TR/cssom/#dom-cssstylesheet-insertrule
  18. DOM::ExceptionOr<unsigned> CSSStyleSheet::insert_rule(StringView rule, unsigned index)
  19. {
  20. // FIXME: 1. If the origin-clean flag is unset, throw a SecurityError exception.
  21. // FIXME: 2. If the disallow modification flag is set, throw a NotAllowedError DOMException.
  22. // 3. Let parsed rule be the return value of invoking parse a rule with rule.
  23. auto parsed_rule = parse_css_rule(CSS::ParsingContext {}, rule);
  24. // 4. If parsed rule is a syntax error, return parsed rule.
  25. if (!parsed_rule)
  26. return DOM::SyntaxError::create("Unable to parse CSS rule.");
  27. // FIXME: 5. If parsed rule is an @import rule, and the constructed flag is set, throw a SyntaxError DOMException.
  28. // 6. Return the result of invoking insert a CSS rule rule in the CSS rules at index.
  29. return m_rules->insert_a_css_rule(parsed_rule.release_nonnull(), index);
  30. }
  31. // https://www.w3.org/TR/cssom/#dom-cssstylesheet-deleterule
  32. DOM::ExceptionOr<void> CSSStyleSheet::delete_rule(unsigned index)
  33. {
  34. // FIXME: 1. If the origin-clean flag is unset, throw a SecurityError exception.
  35. // FIXME: 2. If the disallow modification flag is set, throw a NotAllowedError DOMException.
  36. // 3. Remove a CSS rule in the CSS rules at index.
  37. return m_rules->remove_a_css_rule(index);
  38. }
  39. // https://www.w3.org/TR/cssom/#dom-cssstylesheet-removerule
  40. DOM::ExceptionOr<void> CSSStyleSheet::remove_rule(unsigned index)
  41. {
  42. // The removeRule(index) method must run the same steps as deleteRule().
  43. return delete_rule(index);
  44. }
  45. void CSSStyleSheet::for_each_effective_style_rule(Function<void(CSSStyleRule const&)> const& callback) const
  46. {
  47. m_rules->for_each_effective_style_rule(callback);
  48. }
  49. void CSSStyleSheet::evaluate_media_queries(DOM::Window const& window)
  50. {
  51. m_rules->evaluate_media_queries(window);
  52. }
  53. }