CSSStyleRule.cpp 3.8 KB

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