CSSStyleSheet.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (c) 2019-2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/CSSStyleSheetPrototype.h>
  7. #include <LibWeb/CSS/CSSStyleSheet.h>
  8. #include <LibWeb/CSS/Parser/Parser.h>
  9. #include <LibWeb/CSS/StyleSheetList.h>
  10. #include <LibWeb/DOM/Document.h>
  11. #include <LibWeb/DOM/ExceptionOr.h>
  12. namespace Web::CSS {
  13. CSSStyleSheet* CSSStyleSheet::create(Bindings::WindowObject& window_object, NonnullRefPtrVector<CSSRule> rules, Optional<AK::URL> location)
  14. {
  15. return window_object.heap().allocate<CSSStyleSheet>(window_object.realm(), window_object, move(rules), move(location));
  16. }
  17. CSSStyleSheet::CSSStyleSheet(Bindings::WindowObject& window_object, NonnullRefPtrVector<CSSRule> rules, Optional<AK::URL> location)
  18. : StyleSheet(window_object)
  19. , m_rules(CSSRuleList::create(move(rules)))
  20. {
  21. set_prototype(&window_object.ensure_web_prototype<Bindings::CSSStyleSheetPrototype>("CSSStyleSheet"));
  22. if (location.has_value())
  23. set_location(location->to_string());
  24. for (auto& rule : *m_rules)
  25. rule.set_parent_style_sheet(this);
  26. }
  27. // https://www.w3.org/TR/cssom/#dom-cssstylesheet-insertrule
  28. DOM::ExceptionOr<unsigned> CSSStyleSheet::insert_rule(StringView rule, unsigned index)
  29. {
  30. // FIXME: 1. If the origin-clean flag is unset, throw a SecurityError exception.
  31. // FIXME: 2. If the disallow modification flag is set, throw a NotAllowedError DOMException.
  32. // 3. Let parsed rule be the return value of invoking parse a rule with rule.
  33. auto parsed_rule = parse_css_rule(CSS::Parser::ParsingContext {}, rule);
  34. // 4. If parsed rule is a syntax error, return parsed rule.
  35. if (!parsed_rule)
  36. return DOM::SyntaxError::create("Unable to parse CSS rule.");
  37. // FIXME: 5. If parsed rule is an @import rule, and the constructed flag is set, throw a SyntaxError DOMException.
  38. // 6. Return the result of invoking insert a CSS rule rule in the CSS rules at index.
  39. auto parsed_rule_nonnull = parsed_rule.release_nonnull();
  40. auto result = m_rules->insert_a_css_rule(parsed_rule_nonnull, index);
  41. if (!result.is_exception()) {
  42. // NOTE: The spec doesn't say where to set the parent style sheet, so we'll do it here.
  43. parsed_rule_nonnull->set_parent_style_sheet(this);
  44. if (m_style_sheet_list) {
  45. m_style_sheet_list->document().style_computer().invalidate_rule_cache();
  46. m_style_sheet_list->document().invalidate_style();
  47. }
  48. }
  49. return result;
  50. }
  51. // https://www.w3.org/TR/cssom/#dom-cssstylesheet-deleterule
  52. DOM::ExceptionOr<void> CSSStyleSheet::delete_rule(unsigned index)
  53. {
  54. // FIXME: 1. If the origin-clean flag is unset, throw a SecurityError exception.
  55. // FIXME: 2. If the disallow modification flag is set, throw a NotAllowedError DOMException.
  56. // 3. Remove a CSS rule in the CSS rules at index.
  57. auto result = m_rules->remove_a_css_rule(index);
  58. if (!result.is_exception()) {
  59. if (m_style_sheet_list) {
  60. m_style_sheet_list->document().style_computer().invalidate_rule_cache();
  61. m_style_sheet_list->document().invalidate_style();
  62. }
  63. }
  64. return result;
  65. }
  66. // https://www.w3.org/TR/cssom/#dom-cssstylesheet-removerule
  67. DOM::ExceptionOr<void> CSSStyleSheet::remove_rule(unsigned index)
  68. {
  69. // The removeRule(index) method must run the same steps as deleteRule().
  70. return delete_rule(index);
  71. }
  72. void CSSStyleSheet::for_each_effective_style_rule(Function<void(CSSStyleRule const&)> const& callback) const
  73. {
  74. m_rules->for_each_effective_style_rule(callback);
  75. }
  76. bool CSSStyleSheet::evaluate_media_queries(HTML::Window const& window)
  77. {
  78. return m_rules->evaluate_media_queries(window);
  79. }
  80. void CSSStyleSheet::set_style_sheet_list(Badge<StyleSheetList>, StyleSheetList* list)
  81. {
  82. m_style_sheet_list = list;
  83. }
  84. }