CSSStyleRule.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. namespace Web::CSS {
  11. CSSStyleRule* CSSStyleRule::create(JS::Realm& realm, NonnullRefPtrVector<Web::CSS::Selector>&& selectors, CSSStyleDeclaration& declaration)
  12. {
  13. return realm.heap().allocate<CSSStyleRule>(realm, realm, move(selectors), declaration);
  14. }
  15. CSSStyleRule::CSSStyleRule(JS::Realm& realm, NonnullRefPtrVector<Selector>&& selectors, CSSStyleDeclaration& declaration)
  16. : CSSRule(realm)
  17. , m_selectors(move(selectors))
  18. , m_declaration(declaration)
  19. {
  20. }
  21. void CSSStyleRule::initialize(JS::Realm& realm)
  22. {
  23. Base::initialize(realm);
  24. set_prototype(&Bindings::ensure_web_prototype<Bindings::CSSStyleRulePrototype>(realm, "CSSStyleRule"));
  25. }
  26. void CSSStyleRule::visit_edges(Cell::Visitor& visitor)
  27. {
  28. Base::visit_edges(visitor);
  29. visitor.visit(&m_declaration);
  30. }
  31. // https://www.w3.org/TR/cssom/#dom-cssstylerule-style
  32. CSSStyleDeclaration* CSSStyleRule::style()
  33. {
  34. return &m_declaration;
  35. }
  36. // https://www.w3.org/TR/cssom/#serialize-a-css-rule
  37. DeprecatedString CSSStyleRule::serialized() const
  38. {
  39. StringBuilder builder;
  40. // 1. Let s initially be the result of performing serialize a group of selectors on the rule’s associated selectors,
  41. // followed by the string " {", i.e., a single SPACE (U+0020), followed by LEFT CURLY BRACKET (U+007B).
  42. builder.append(serialize_a_group_of_selectors(selectors()));
  43. builder.append(" {"sv);
  44. // 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.
  45. auto decls = declaration().serialized();
  46. // 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.
  47. DeprecatedString rules;
  48. // 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.
  49. if (decls.is_null() && rules.is_null()) {
  50. builder.append(" }"sv);
  51. return builder.to_deprecated_string();
  52. }
  53. // 5. If rules is null:
  54. if (rules.is_null()) {
  55. // 1. Append a single SPACE (U+0020) to s
  56. builder.append(' ');
  57. // 2. Append decls to s
  58. builder.append(decls);
  59. // 3. Append " }" to s (i.e. a single SPACE (U+0020) followed by RIGHT CURLY BRACKET (U+007D)).
  60. builder.append(" }"sv);
  61. // 4. Return s.
  62. return builder.to_deprecated_string();
  63. }
  64. // FIXME: 6. Otherwise:
  65. // FIXME: 1. If decls is not null, prepend it to rules.
  66. // FIXME: 2. For each rule in rules:
  67. // FIXME: 1. Append a newline followed by two spaces to s.
  68. // FIXME: 2. Append rule to s.
  69. // FIXME: 3. Append a newline followed by RIGHT CURLY BRACKET (U+007D) to s.
  70. // FIXME: 4. Return s.
  71. TODO();
  72. }
  73. // https://www.w3.org/TR/cssom/#dom-cssstylerule-selectortext
  74. DeprecatedString CSSStyleRule::selector_text() const
  75. {
  76. // The selectorText attribute, on getting, must return the result of serializing the associated group of selectors.
  77. return serialize_a_group_of_selectors(selectors());
  78. }
  79. // https://www.w3.org/TR/cssom/#dom-cssstylerule-selectortext
  80. void CSSStyleRule::set_selector_text(StringView selector_text)
  81. {
  82. // 1. Run the parse a group of selectors algorithm on the given value.
  83. auto parsed_selectors = parse_selector({}, selector_text);
  84. // 2. If the algorithm returns a non-null value replace the associated group of selectors with the returned value.
  85. if (parsed_selectors.has_value())
  86. m_selectors = parsed_selectors.release_value();
  87. // 3. Otherwise, if the algorithm returns a null value, do nothing.
  88. }
  89. }