CSSStyleSheet.cpp 3.0 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/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, Optional<AK::URL> location)
  13. : m_rules(CSSRuleList::create(move(rules)))
  14. {
  15. if (location.has_value())
  16. set_location(location->to_string());
  17. }
  18. // https://www.w3.org/TR/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::Parser::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. auto result = m_rules->insert_a_css_rule(parsed_rule.release_nonnull(), index);
  31. if (!result.is_exception()) {
  32. if (m_style_sheet_list) {
  33. m_style_sheet_list->document().style_computer().invalidate_rule_cache();
  34. m_style_sheet_list->document().invalidate_style();
  35. }
  36. }
  37. return result;
  38. }
  39. // https://www.w3.org/TR/cssom/#dom-cssstylesheet-deleterule
  40. DOM::ExceptionOr<void> CSSStyleSheet::delete_rule(unsigned index)
  41. {
  42. // FIXME: 1. If the origin-clean flag is unset, throw a SecurityError exception.
  43. // FIXME: 2. If the disallow modification flag is set, throw a NotAllowedError DOMException.
  44. // 3. Remove a CSS rule in the CSS rules at index.
  45. auto result = m_rules->remove_a_css_rule(index);
  46. if (!result.is_exception()) {
  47. if (m_style_sheet_list) {
  48. m_style_sheet_list->document().style_computer().invalidate_rule_cache();
  49. m_style_sheet_list->document().invalidate_style();
  50. }
  51. }
  52. return result;
  53. }
  54. // https://www.w3.org/TR/cssom/#dom-cssstylesheet-removerule
  55. DOM::ExceptionOr<void> CSSStyleSheet::remove_rule(unsigned index)
  56. {
  57. // The removeRule(index) method must run the same steps as deleteRule().
  58. return delete_rule(index);
  59. }
  60. void CSSStyleSheet::for_each_effective_style_rule(Function<void(CSSStyleRule const&)> const& callback) const
  61. {
  62. m_rules->for_each_effective_style_rule(callback);
  63. }
  64. bool CSSStyleSheet::evaluate_media_queries(HTML::Window const& window)
  65. {
  66. return m_rules->evaluate_media_queries(window);
  67. }
  68. void CSSStyleSheet::set_style_sheet_list(Badge<StyleSheetList>, StyleSheetList* list)
  69. {
  70. m_style_sheet_list = list;
  71. }
  72. }