CSSStyleRule.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/CSSStyleRulePrototype.h>
  7. #include <LibWeb/Bindings/Intrinsics.h>
  8. #include <LibWeb/CSS/CSSStyleRule.h>
  9. #include <LibWeb/CSS/Parser/Parser.h>
  10. #include <LibWeb/CSS/StyleComputer.h>
  11. #include <LibWeb/WebIDL/ExceptionOr.h>
  12. namespace Web::CSS {
  13. JS_DEFINE_ALLOCATOR(CSSStyleRule);
  14. JS::NonnullGCPtr<CSSStyleRule> CSSStyleRule::create(JS::Realm& realm, Vector<NonnullRefPtr<Web::CSS::Selector>>&& selectors, PropertyOwningCSSStyleDeclaration& declaration)
  15. {
  16. return realm.heap().allocate<CSSStyleRule>(realm, realm, move(selectors), declaration);
  17. }
  18. CSSStyleRule::CSSStyleRule(JS::Realm& realm, Vector<NonnullRefPtr<Selector>>&& selectors, PropertyOwningCSSStyleDeclaration& declaration)
  19. : CSSRule(realm)
  20. , m_selectors(move(selectors))
  21. , m_declaration(declaration)
  22. {
  23. m_declaration->set_parent_rule(*this);
  24. }
  25. void CSSStyleRule::initialize(JS::Realm& realm)
  26. {
  27. Base::initialize(realm);
  28. WEB_SET_PROTOTYPE_FOR_INTERFACE(CSSStyleRule);
  29. }
  30. void CSSStyleRule::visit_edges(Cell::Visitor& visitor)
  31. {
  32. Base::visit_edges(visitor);
  33. visitor.visit(m_declaration);
  34. }
  35. // https://www.w3.org/TR/cssom/#dom-cssstylerule-style
  36. CSSStyleDeclaration* CSSStyleRule::style()
  37. {
  38. return m_declaration;
  39. }
  40. // https://www.w3.org/TR/cssom/#serialize-a-css-rule
  41. String CSSStyleRule::serialized() const
  42. {
  43. StringBuilder builder;
  44. // 1. Let s initially be the result of performing serialize a group of selectors on the rule’s associated selectors,
  45. // followed by the string " {", i.e., a single SPACE (U+0020), followed by LEFT CURLY BRACKET (U+007B).
  46. builder.append(serialize_a_group_of_selectors(selectors()));
  47. builder.append(" {"sv);
  48. // 2. Let decls be the result of performing serialize a CSS declaration block on the rule’s associated declarations, or null if there are no such declarations.
  49. auto decls = declaration().length() > 0 ? declaration().serialized() : Optional<String>();
  50. // FIXME: 3. Let rules be the result of performing serialize a CSS rule on each rule in the rule’s cssRules list, or null if there are no such rules.
  51. Optional<String> rules;
  52. // 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.
  53. if (!decls.has_value() && !rules.has_value()) {
  54. builder.append(" }"sv);
  55. return MUST(builder.to_string());
  56. }
  57. // 5. If rules is null:
  58. if (!rules.has_value()) {
  59. // 1. Append a single SPACE (U+0020) to s
  60. builder.append(' ');
  61. // 2. Append decls to s
  62. builder.append(*decls);
  63. // 3. Append " }" to s (i.e. a single SPACE (U+0020) followed by RIGHT CURLY BRACKET (U+007D)).
  64. builder.append(" }"sv);
  65. // 4. Return s.
  66. return MUST(builder.to_string());
  67. }
  68. // FIXME: 6. Otherwise:
  69. // FIXME: 1. If decls is not null, prepend it to rules.
  70. // FIXME: 2. For each rule in rules:
  71. // FIXME: 1. Append a newline followed by two spaces to s.
  72. // FIXME: 2. Append rule to s.
  73. // FIXME: 3. Append a newline followed by RIGHT CURLY BRACKET (U+007D) to s.
  74. // FIXME: 4. Return s.
  75. TODO();
  76. }
  77. // https://www.w3.org/TR/cssom/#dom-cssstylerule-selectortext
  78. String CSSStyleRule::selector_text() const
  79. {
  80. // The selectorText attribute, on getting, must return the result of serializing the associated group of selectors.
  81. return serialize_a_group_of_selectors(selectors());
  82. }
  83. // https://www.w3.org/TR/cssom/#dom-cssstylerule-selectortext
  84. void CSSStyleRule::set_selector_text(StringView selector_text)
  85. {
  86. // 1. Run the parse a group of selectors algorithm on the given value.
  87. auto parsed_selectors = parse_selector(Parser::ParsingContext { realm() }, selector_text);
  88. // 2. If the algorithm returns a non-null value replace the associated group of selectors with the returned value.
  89. if (parsed_selectors.has_value()) {
  90. m_selectors = parsed_selectors.release_value();
  91. if (auto* sheet = parent_style_sheet()) {
  92. if (auto style_sheet_list = sheet->style_sheet_list()) {
  93. style_sheet_list->document().style_computer().invalidate_rule_cache();
  94. style_sheet_list->document_or_shadow_root().invalidate_style(DOM::StyleInvalidationReason::SetSelectorText);
  95. }
  96. }
  97. }
  98. // 3. Otherwise, if the algorithm returns a null value, do nothing.
  99. }
  100. }