CSSStyleSheet.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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/StyleComputer.h>
  11. #include <LibWeb/CSS/StyleSheetList.h>
  12. #include <LibWeb/DOM/Document.h>
  13. #include <LibWeb/WebIDL/ExceptionOr.h>
  14. namespace Web::CSS {
  15. JS::NonnullGCPtr<CSSStyleSheet> CSSStyleSheet::create(JS::Realm& realm, CSSRuleList& rules, MediaList& media, Optional<AK::URL> location)
  16. {
  17. return realm.heap().allocate<CSSStyleSheet>(realm, realm, rules, media, move(location));
  18. }
  19. CSSStyleSheet::CSSStyleSheet(JS::Realm& realm, CSSRuleList& rules, MediaList& media, Optional<AK::URL> location)
  20. : StyleSheet(realm, media)
  21. , m_rules(&rules)
  22. {
  23. if (location.has_value())
  24. set_location(MUST(location->to_string()));
  25. for (auto& rule : *m_rules)
  26. rule->set_parent_style_sheet(this);
  27. recalculate_namespaces();
  28. m_rules->on_change = [this]() {
  29. recalculate_namespaces();
  30. };
  31. }
  32. void CSSStyleSheet::initialize(JS::Realm& realm)
  33. {
  34. Base::initialize(realm);
  35. set_prototype(&Bindings::ensure_web_prototype<Bindings::CSSStyleSheetPrototype>(realm, "CSSStyleSheet"));
  36. }
  37. void CSSStyleSheet::visit_edges(Cell::Visitor& visitor)
  38. {
  39. Base::visit_edges(visitor);
  40. visitor.visit(m_style_sheet_list.ptr());
  41. visitor.visit(m_rules);
  42. visitor.visit(m_owner_css_rule);
  43. visitor.visit(m_default_namespace_rule);
  44. for (auto& [key, namespace_rule] : m_namespace_rules)
  45. visitor.visit(namespace_rule);
  46. }
  47. // https://www.w3.org/TR/cssom/#dom-cssstylesheet-insertrule
  48. WebIDL::ExceptionOr<unsigned> CSSStyleSheet::insert_rule(StringView rule, unsigned index)
  49. {
  50. // FIXME: 1. If the origin-clean flag is unset, throw a SecurityError exception.
  51. // FIXME: 2. If the disallow modification flag is set, throw a NotAllowedError DOMException.
  52. // 3. Let parsed rule be the return value of invoking parse a rule with rule.
  53. auto context = m_style_sheet_list ? CSS::Parser::ParsingContext { m_style_sheet_list->document() } : CSS::Parser::ParsingContext { realm() };
  54. auto parsed_rule = parse_css_rule(context, rule);
  55. // 4. If parsed rule is a syntax error, return parsed rule.
  56. if (!parsed_rule)
  57. return WebIDL::SyntaxError::create(realm(), "Unable to parse CSS rule."_fly_string);
  58. // FIXME: 5. If parsed rule is an @import rule, and the constructed flag is set, throw a SyntaxError DOMException.
  59. // 6. Return the result of invoking insert a CSS rule rule in the CSS rules at index.
  60. auto result = m_rules->insert_a_css_rule(parsed_rule, index);
  61. if (!result.is_exception()) {
  62. // NOTE: The spec doesn't say where to set the parent style sheet, so we'll do it here.
  63. parsed_rule->set_parent_style_sheet(this);
  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-deleterule
  72. WebIDL::ExceptionOr<void> CSSStyleSheet::delete_rule(unsigned index)
  73. {
  74. // FIXME: 1. If the origin-clean flag is unset, throw a SecurityError exception.
  75. // FIXME: 2. If the disallow modification flag is set, throw a NotAllowedError DOMException.
  76. // 3. Remove a CSS rule in the CSS rules at index.
  77. auto result = m_rules->remove_a_css_rule(index);
  78. if (!result.is_exception()) {
  79. if (m_style_sheet_list) {
  80. m_style_sheet_list->document().style_computer().invalidate_rule_cache();
  81. m_style_sheet_list->document().invalidate_style();
  82. }
  83. }
  84. return result;
  85. }
  86. // https://www.w3.org/TR/cssom/#dom-cssstylesheet-removerule
  87. WebIDL::ExceptionOr<void> CSSStyleSheet::remove_rule(unsigned index)
  88. {
  89. // The removeRule(index) method must run the same steps as deleteRule().
  90. return delete_rule(index);
  91. }
  92. void CSSStyleSheet::for_each_effective_style_rule(Function<void(CSSStyleRule const&)> const& callback) const
  93. {
  94. if (m_media->matches()) {
  95. m_rules->for_each_effective_style_rule(callback);
  96. }
  97. }
  98. void CSSStyleSheet::for_each_effective_keyframes_at_rule(Function<void(CSSKeyframesRule const&)> const& callback) const
  99. {
  100. if (m_media->matches())
  101. m_rules->for_each_effective_keyframes_at_rule(callback);
  102. }
  103. bool CSSStyleSheet::evaluate_media_queries(HTML::Window const& window)
  104. {
  105. bool any_media_queries_changed_match_state = false;
  106. bool did_match = m_media->matches();
  107. bool now_matches = m_media->evaluate(window);
  108. if (did_match != now_matches)
  109. any_media_queries_changed_match_state = true;
  110. if (now_matches && m_rules->evaluate_media_queries(window))
  111. any_media_queries_changed_match_state = true;
  112. return any_media_queries_changed_match_state;
  113. }
  114. void CSSStyleSheet::set_style_sheet_list(Badge<StyleSheetList>, StyleSheetList* list)
  115. {
  116. m_style_sheet_list = list;
  117. }
  118. Optional<StringView> CSSStyleSheet::default_namespace() const
  119. {
  120. if (m_default_namespace_rule)
  121. return m_default_namespace_rule->namespace_uri().view();
  122. return {};
  123. }
  124. Optional<StringView> CSSStyleSheet::namespace_uri(StringView namespace_prefix) const
  125. {
  126. return m_namespace_rules.get(namespace_prefix)
  127. .map([](JS::GCPtr<CSSNamespaceRule> namespace_) {
  128. return namespace_->namespace_uri().view();
  129. });
  130. }
  131. void CSSStyleSheet::recalculate_namespaces()
  132. {
  133. m_default_namespace_rule = nullptr;
  134. m_namespace_rules.clear();
  135. for (JS::NonnullGCPtr<CSSRule> rule : *m_rules) {
  136. // "Any @namespace rules must follow all @charset and @import rules and precede all other
  137. // non-ignored at-rules and style rules in a style sheet.
  138. // ...
  139. // A syntactically invalid @namespace rule (whether malformed or misplaced) must be ignored."
  140. // https://drafts.csswg.org/css-namespaces/#syntax
  141. switch (rule->type()) {
  142. case CSSRule::Type::Import:
  143. continue;
  144. case CSSRule::Type::Namespace:
  145. break;
  146. default:
  147. // Any other types mean that further @namespace rules are invalid, so we can stop here.
  148. return;
  149. }
  150. auto& namespace_rule = verify_cast<CSSNamespaceRule>(*rule);
  151. if (!namespace_rule.namespace_uri().is_empty() && namespace_rule.prefix().is_empty())
  152. m_default_namespace_rule = namespace_rule;
  153. m_namespace_rules.set(FlyString::from_deprecated_fly_string(namespace_rule.prefix()).release_value_but_fixme_should_propagate_errors(), namespace_rule);
  154. }
  155. }
  156. }