CSSStyleRule.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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, Vector<NonnullRefPtr<Web::CSS::Selector>>&& selectors, PropertyOwningCSSStyleDeclaration& declaration, CSSRuleList& nested_rules)
  16. {
  17. return realm.heap().allocate<CSSStyleRule>(realm, realm, move(selectors), declaration, nested_rules);
  18. }
  19. CSSStyleRule::CSSStyleRule(JS::Realm& realm, Vector<NonnullRefPtr<Selector>>&& selectors, PropertyOwningCSSStyleDeclaration& declaration, CSSRuleList& nested_rules)
  20. : CSSGroupingRule(realm, nested_rules)
  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. // 1. Append a newline followed by two spaces to s.
  82. // 2. Append rule to s.
  83. builder.appendff("\n {}", rule);
  84. }
  85. // 3. Append a newline followed by RIGHT CURLY BRACKET (U+007D) to s.
  86. builder.append("\n}"sv);
  87. // 4. Return s.
  88. return builder.to_string_without_validation();
  89. }
  90. }
  91. // https://drafts.csswg.org/cssom-1/#dom-cssstylerule-selectortext
  92. String CSSStyleRule::selector_text() const
  93. {
  94. // The selectorText attribute, on getting, must return the result of serializing the associated group of selectors.
  95. return serialize_a_group_of_selectors(selectors());
  96. }
  97. // https://drafts.csswg.org/cssom-1/#dom-cssstylerule-selectortext
  98. void CSSStyleRule::set_selector_text(StringView selector_text)
  99. {
  100. // 1. Run the parse a group of selectors algorithm on the given value.
  101. auto parsed_selectors = parse_selector(Parser::ParsingContext { realm() }, selector_text);
  102. // 2. If the algorithm returns a non-null value replace the associated group of selectors with the returned value.
  103. if (parsed_selectors.has_value()) {
  104. m_selectors = parsed_selectors.release_value();
  105. if (auto* sheet = parent_style_sheet()) {
  106. if (auto style_sheet_list = sheet->style_sheet_list()) {
  107. style_sheet_list->document().style_computer().invalidate_rule_cache();
  108. style_sheet_list->document_or_shadow_root().invalidate_style(DOM::StyleInvalidationReason::SetSelectorText);
  109. }
  110. }
  111. }
  112. // 3. Otherwise, if the algorithm returns a null value, do nothing.
  113. }
  114. }