CSSStyleSheet.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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/Bindings/Intrinsics.h>
  8. #include <LibWeb/CSS/CSSStyleSheet.h>
  9. #include <LibWeb/CSS/Parser/Parser.h>
  10. #include <LibWeb/CSS/StyleSheetList.h>
  11. #include <LibWeb/DOM/Document.h>
  12. #include <LibWeb/WebIDL/ExceptionOr.h>
  13. namespace Web::CSS {
  14. WebIDL::ExceptionOr<JS::NonnullGCPtr<CSSStyleSheet>> CSSStyleSheet::create(JS::Realm& realm, CSSRuleList& rules, MediaList& media, Optional<AK::URL> location)
  15. {
  16. return MUST_OR_THROW_OOM(realm.heap().allocate<CSSStyleSheet>(realm, realm, rules, media, move(location)));
  17. }
  18. CSSStyleSheet::CSSStyleSheet(JS::Realm& realm, CSSRuleList& rules, MediaList& media, Optional<AK::URL> location)
  19. : StyleSheet(realm, media)
  20. , m_rules(&rules)
  21. {
  22. if (location.has_value())
  23. set_location(location->to_deprecated_string());
  24. for (auto& rule : *m_rules)
  25. rule->set_parent_style_sheet(this);
  26. }
  27. JS::ThrowCompletionOr<void> CSSStyleSheet::initialize(JS::Realm& realm)
  28. {
  29. MUST_OR_THROW_OOM(Base::initialize(realm));
  30. set_prototype(&Bindings::ensure_web_prototype<Bindings::CSSStyleSheetPrototype>(realm, "CSSStyleSheet"));
  31. return {};
  32. }
  33. void CSSStyleSheet::visit_edges(Cell::Visitor& visitor)
  34. {
  35. Base::visit_edges(visitor);
  36. visitor.visit(m_style_sheet_list.ptr());
  37. visitor.visit(m_rules);
  38. visitor.visit(m_owner_css_rule);
  39. }
  40. // https://www.w3.org/TR/cssom/#dom-cssstylesheet-insertrule
  41. WebIDL::ExceptionOr<unsigned> CSSStyleSheet::insert_rule(StringView rule, unsigned index)
  42. {
  43. // FIXME: 1. If the origin-clean flag is unset, throw a SecurityError exception.
  44. // FIXME: 2. If the disallow modification flag is set, throw a NotAllowedError DOMException.
  45. // 3. Let parsed rule be the return value of invoking parse a rule with rule.
  46. auto context = m_style_sheet_list ? CSS::Parser::ParsingContext { m_style_sheet_list->document() } : CSS::Parser::ParsingContext { realm() };
  47. auto parsed_rule = parse_css_rule(context, rule);
  48. // 4. If parsed rule is a syntax error, return parsed rule.
  49. if (!parsed_rule)
  50. return WebIDL::SyntaxError::create(realm(), "Unable to parse CSS rule.");
  51. // FIXME: 5. If parsed rule is an @import rule, and the constructed flag is set, throw a SyntaxError DOMException.
  52. // 6. Return the result of invoking insert a CSS rule rule in the CSS rules at index.
  53. auto result = m_rules->insert_a_css_rule(parsed_rule, index);
  54. if (!result.is_exception()) {
  55. // NOTE: The spec doesn't say where to set the parent style sheet, so we'll do it here.
  56. parsed_rule->set_parent_style_sheet(this);
  57. if (m_style_sheet_list) {
  58. m_style_sheet_list->document().style_computer().invalidate_rule_cache();
  59. m_style_sheet_list->document().invalidate_style();
  60. }
  61. }
  62. return result;
  63. }
  64. // https://www.w3.org/TR/cssom/#dom-cssstylesheet-deleterule
  65. WebIDL::ExceptionOr<void> CSSStyleSheet::delete_rule(unsigned index)
  66. {
  67. // FIXME: 1. If the origin-clean flag is unset, throw a SecurityError exception.
  68. // FIXME: 2. If the disallow modification flag is set, throw a NotAllowedError DOMException.
  69. // 3. Remove a CSS rule in the CSS rules at index.
  70. auto result = m_rules->remove_a_css_rule(index);
  71. if (!result.is_exception()) {
  72. if (m_style_sheet_list) {
  73. m_style_sheet_list->document().style_computer().invalidate_rule_cache();
  74. m_style_sheet_list->document().invalidate_style();
  75. }
  76. }
  77. return result;
  78. }
  79. // https://www.w3.org/TR/cssom/#dom-cssstylesheet-removerule
  80. WebIDL::ExceptionOr<void> CSSStyleSheet::remove_rule(unsigned index)
  81. {
  82. // The removeRule(index) method must run the same steps as deleteRule().
  83. return delete_rule(index);
  84. }
  85. void CSSStyleSheet::for_each_effective_style_rule(Function<void(CSSStyleRule const&)> const& callback) const
  86. {
  87. if (m_media->matches()) {
  88. m_rules->for_each_effective_style_rule(callback);
  89. }
  90. }
  91. bool CSSStyleSheet::evaluate_media_queries(HTML::Window const& window)
  92. {
  93. bool any_media_queries_changed_match_state = false;
  94. bool did_match = m_media->matches();
  95. bool now_matches = m_media->evaluate(window);
  96. if (did_match != now_matches)
  97. any_media_queries_changed_match_state = true;
  98. if (now_matches && m_rules->evaluate_media_queries(window))
  99. any_media_queries_changed_match_state = true;
  100. return any_media_queries_changed_match_state;
  101. }
  102. void CSSStyleSheet::set_style_sheet_list(Badge<StyleSheetList>, StyleSheetList* list)
  103. {
  104. m_style_sheet_list = list;
  105. }
  106. }