CSSStyleRule.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
  3. * Copyright (c) 2021-2024, Sam Atkins <sam@ladybird.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibWeb/Bindings/CSSStyleRulePrototype.h>
  8. #include <LibWeb/Bindings/Intrinsics.h>
  9. #include <LibWeb/CSS/CSSRuleList.h>
  10. #include <LibWeb/CSS/CSSStyleRule.h>
  11. #include <LibWeb/CSS/Parser/Parser.h>
  12. #include <LibWeb/CSS/StyleComputer.h>
  13. namespace Web::CSS {
  14. JS_DEFINE_ALLOCATOR(CSSStyleRule);
  15. JS::NonnullGCPtr<CSSStyleRule> CSSStyleRule::create(JS::Realm& realm, SelectorList&& selectors, PropertyOwningCSSStyleDeclaration& declaration, CSSRuleList& nested_rules)
  16. {
  17. return realm.create<CSSStyleRule>(realm, move(selectors), declaration, nested_rules);
  18. }
  19. CSSStyleRule::CSSStyleRule(JS::Realm& realm, SelectorList&& selectors, PropertyOwningCSSStyleDeclaration& declaration, CSSRuleList& nested_rules)
  20. : CSSGroupingRule(realm, nested_rules, Type::Style)
  21. , m_selectors(move(selectors))
  22. , m_declaration(declaration)
  23. {
  24. m_declaration->set_parent_rule(*this);
  25. }
  26. void CSSStyleRule::initialize(JS::Realm& realm)
  27. {
  28. Base::initialize(realm);
  29. WEB_SET_PROTOTYPE_FOR_INTERFACE(CSSStyleRule);
  30. }
  31. void CSSStyleRule::visit_edges(Cell::Visitor& visitor)
  32. {
  33. Base::visit_edges(visitor);
  34. visitor.visit(m_declaration);
  35. }
  36. // https://drafts.csswg.org/cssom-1/#dom-cssstylerule-style
  37. CSSStyleDeclaration* CSSStyleRule::style()
  38. {
  39. return m_declaration;
  40. }
  41. // https://drafts.csswg.org/cssom-1/#serialize-a-css-rule
  42. String CSSStyleRule::serialized() const
  43. {
  44. StringBuilder builder;
  45. // 1. Let s initially be the result of performing serialize a group of selectors on the rule’s associated selectors,
  46. // followed by the string " {", i.e., a single SPACE (U+0020), followed by LEFT CURLY BRACKET (U+007B).
  47. builder.append(serialize_a_group_of_selectors(selectors()));
  48. builder.append(" {"sv);
  49. // 2. Let decls be the result of performing serialize a CSS declaration block on the rule’s associated declarations,
  50. // or null if there are no such declarations.
  51. auto decls = declaration().length() > 0 ? declaration().serialized() : Optional<String>();
  52. // 3. Let rules be the result of performing serialize a CSS rule on each rule in the rule’s cssRules list,
  53. // or null if there are no such rules.
  54. Vector<String> rules;
  55. for (auto& rule : css_rules()) {
  56. rules.append(rule->serialized());
  57. }
  58. // 4. If decls and rules are both null, append " }" to s (i.e. a single SPACE (U+0020) followed by RIGHT CURLY BRACKET (U+007D)) and return s.
  59. if (!decls.has_value() && rules.is_empty()) {
  60. builder.append(" }"sv);
  61. return builder.to_string_without_validation();
  62. }
  63. // 5. If rules is null:
  64. if (rules.is_empty()) {
  65. // 1. Append a single SPACE (U+0020) to s
  66. builder.append(' ');
  67. // 2. Append decls to s
  68. builder.append(*decls);
  69. // 3. Append " }" to s (i.e. a single SPACE (U+0020) followed by RIGHT CURLY BRACKET (U+007D)).
  70. builder.append(" }"sv);
  71. // 4. Return s.
  72. return builder.to_string_without_validation();
  73. }
  74. // 6. Otherwise:
  75. else {
  76. // 1. If decls is not null, prepend it to rules.
  77. if (decls.has_value())
  78. rules.prepend(decls.value());
  79. // 2. For each rule in rules:
  80. for (auto& rule : rules) {
  81. // * If rule is the empty string, do nothing.
  82. if (rule.is_empty())
  83. continue;
  84. // * Otherwise:
  85. // 1. Append a newline followed by two spaces to s.
  86. // 2. Append rule to s.
  87. builder.appendff("\n {}", rule);
  88. }
  89. // 3. Append a newline followed by RIGHT CURLY BRACKET (U+007D) to s.
  90. builder.append("\n}"sv);
  91. // 4. Return s.
  92. return builder.to_string_without_validation();
  93. }
  94. }
  95. // https://drafts.csswg.org/cssom-1/#dom-cssstylerule-selectortext
  96. String CSSStyleRule::selector_text() const
  97. {
  98. // The selectorText attribute, on getting, must return the result of serializing the associated group of selectors.
  99. return serialize_a_group_of_selectors(selectors());
  100. }
  101. // https://drafts.csswg.org/cssom-1/#dom-cssstylerule-selectortext
  102. void CSSStyleRule::set_selector_text(StringView selector_text)
  103. {
  104. clear_caches();
  105. // 1. Run the parse a group of selectors algorithm on the given value.
  106. Optional<SelectorList> parsed_selectors;
  107. if (parent_style_rule()) {
  108. // AD-HOC: If we're a nested style rule, then we need to parse the selector as relative and then adapt it with implicit &s.
  109. parsed_selectors = parse_selector_for_nested_style_rule(Parser::ParsingContext { realm() }, selector_text);
  110. } else {
  111. parsed_selectors = parse_selector(Parser::ParsingContext { realm() }, selector_text);
  112. }
  113. // 2. If the algorithm returns a non-null value replace the associated group of selectors with the returned value.
  114. if (parsed_selectors.has_value()) {
  115. // NOTE: If we have a parent style rule, we need to update the selectors to add any implicit `&`s
  116. m_selectors = parsed_selectors.release_value();
  117. if (auto* sheet = parent_style_sheet()) {
  118. if (auto style_sheet_list = sheet->style_sheet_list()) {
  119. style_sheet_list->document().style_computer().invalidate_rule_cache();
  120. style_sheet_list->document_or_shadow_root().invalidate_style(DOM::StyleInvalidationReason::SetSelectorText);
  121. }
  122. }
  123. }
  124. // 3. Otherwise, if the algorithm returns a null value, do nothing.
  125. }
  126. SelectorList const& CSSStyleRule::absolutized_selectors() const
  127. {
  128. if (m_cached_absolutized_selectors.has_value())
  129. return m_cached_absolutized_selectors.value();
  130. // Replace all occurrences of `&` with the nearest ancestor style rule's selector list wrapped in `:is(...)`,
  131. // or if we have no such ancestor, with `:scope`.
  132. // If we don't have any nesting selectors, we can just use our selectors as they are.
  133. bool has_any_nesting = false;
  134. for (auto const& selector : selectors()) {
  135. if (selector->contains_the_nesting_selector()) {
  136. has_any_nesting = true;
  137. break;
  138. }
  139. }
  140. if (!has_any_nesting) {
  141. m_cached_absolutized_selectors = m_selectors;
  142. return m_cached_absolutized_selectors.value();
  143. }
  144. // Otherwise, build up a new list of selectors with the `&` replaced.
  145. // First, figure out what we should replace `&` with.
  146. // "When used in the selector of a nested style rule, the nesting selector represents the elements matched by the parent rule.
  147. // When used in any other context, it represents the same elements as :scope in that context (unless otherwise defined)."
  148. // https://drafts.csswg.org/css-nesting-1/#nest-selector
  149. if (auto const* parent_style_rule = this->parent_style_rule()) {
  150. // TODO: If there's only 1, we don't have to use `:is()` for it
  151. Selector::SimpleSelector parent_selector = {
  152. .type = Selector::SimpleSelector::Type::PseudoClass,
  153. .value = Selector::SimpleSelector::PseudoClassSelector {
  154. .type = PseudoClass::Is,
  155. .argument_selector_list = parent_style_rule->absolutized_selectors(),
  156. },
  157. };
  158. SelectorList absolutized_selectors;
  159. for (auto const& selector : selectors()) {
  160. if (auto absolutized = selector->absolutized(parent_selector))
  161. absolutized_selectors.append(absolutized.release_nonnull());
  162. }
  163. m_cached_absolutized_selectors = move(absolutized_selectors);
  164. } else {
  165. // NOTE: We can't actually replace & with :scope, because & has to have 0 specificity.
  166. // So we leave it, and treat & like :scope during matching.
  167. m_cached_absolutized_selectors = m_selectors;
  168. }
  169. return m_cached_absolutized_selectors.value();
  170. }
  171. void CSSStyleRule::clear_caches()
  172. {
  173. Base::clear_caches();
  174. m_cached_absolutized_selectors.clear();
  175. }
  176. CSSStyleRule const* CSSStyleRule::parent_style_rule() const
  177. {
  178. for (auto* parent = parent_rule(); parent; parent = parent->parent_rule()) {
  179. if (parent->type() == CSSStyleRule::Type::Style)
  180. return static_cast<CSSStyleRule const*>(parent);
  181. }
  182. return nullptr;
  183. }
  184. }