CSSStyleSheet.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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(window_object, 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. void CSSStyleSheet::visit_edges(Cell::Visitor& visitor)
  28. {
  29. Base::visit_edges(visitor);
  30. visitor.visit(m_style_sheet_list.ptr());
  31. }
  32. // https://www.w3.org/TR/cssom/#dom-cssstylesheet-insertrule
  33. DOM::ExceptionOr<unsigned> CSSStyleSheet::insert_rule(StringView rule, unsigned index)
  34. {
  35. // FIXME: 1. If the origin-clean flag is unset, throw a SecurityError exception.
  36. // FIXME: 2. If the disallow modification flag is set, throw a NotAllowedError DOMException.
  37. // 3. Let parsed rule be the return value of invoking parse a rule with rule.
  38. auto parsed_rule = parse_css_rule(CSS::Parser::ParsingContext {}, rule);
  39. // 4. If parsed rule is a syntax error, return parsed rule.
  40. if (!parsed_rule)
  41. return DOM::SyntaxError::create("Unable to parse CSS rule.");
  42. // FIXME: 5. If parsed rule is an @import rule, and the constructed flag is set, throw a SyntaxError DOMException.
  43. // 6. Return the result of invoking insert a CSS rule rule in the CSS rules at index.
  44. auto parsed_rule_nonnull = parsed_rule.release_nonnull();
  45. auto result = m_rules->insert_a_css_rule(parsed_rule_nonnull, index);
  46. if (!result.is_exception()) {
  47. // NOTE: The spec doesn't say where to set the parent style sheet, so we'll do it here.
  48. parsed_rule_nonnull->set_parent_style_sheet(this);
  49. if (m_style_sheet_list) {
  50. m_style_sheet_list->document().style_computer().invalidate_rule_cache();
  51. m_style_sheet_list->document().invalidate_style();
  52. }
  53. }
  54. return result;
  55. }
  56. // https://www.w3.org/TR/cssom/#dom-cssstylesheet-deleterule
  57. DOM::ExceptionOr<void> CSSStyleSheet::delete_rule(unsigned index)
  58. {
  59. // FIXME: 1. If the origin-clean flag is unset, throw a SecurityError exception.
  60. // FIXME: 2. If the disallow modification flag is set, throw a NotAllowedError DOMException.
  61. // 3. Remove a CSS rule in the CSS rules at index.
  62. auto result = m_rules->remove_a_css_rule(index);
  63. if (!result.is_exception()) {
  64. if (m_style_sheet_list) {
  65. m_style_sheet_list->document().style_computer().invalidate_rule_cache();
  66. m_style_sheet_list->document().invalidate_style();
  67. }
  68. }
  69. return result;
  70. }
  71. // https://www.w3.org/TR/cssom/#dom-cssstylesheet-removerule
  72. DOM::ExceptionOr<void> CSSStyleSheet::remove_rule(unsigned index)
  73. {
  74. // The removeRule(index) method must run the same steps as deleteRule().
  75. return delete_rule(index);
  76. }
  77. void CSSStyleSheet::for_each_effective_style_rule(Function<void(CSSStyleRule const&)> const& callback) const
  78. {
  79. m_rules->for_each_effective_style_rule(callback);
  80. }
  81. bool CSSStyleSheet::evaluate_media_queries(HTML::Window const& window)
  82. {
  83. return m_rules->evaluate_media_queries(window);
  84. }
  85. void CSSStyleSheet::set_style_sheet_list(Badge<StyleSheetList>, StyleSheetList* list)
  86. {
  87. m_style_sheet_list = list;
  88. }
  89. }