CSSStyleRule.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/CSS/CSSStyleRule.h>
  7. #include <LibWeb/CSS/Parser/Parser.h>
  8. namespace Web::CSS {
  9. CSSStyleRule::CSSStyleRule(NonnullRefPtrVector<Selector>&& selectors, NonnullRefPtr<CSSStyleDeclaration>&& declaration)
  10. : m_selectors(move(selectors))
  11. , m_declaration(move(declaration))
  12. {
  13. }
  14. CSSStyleRule::~CSSStyleRule()
  15. {
  16. }
  17. // https://www.w3.org/TR/cssom/#dom-cssstylerule-style
  18. CSSStyleDeclaration* CSSStyleRule::style()
  19. {
  20. return m_declaration;
  21. }
  22. // https://www.w3.org/TR/cssom/#serialize-a-css-rule
  23. String CSSStyleRule::serialized() const
  24. {
  25. StringBuilder builder;
  26. // 1. Let s initially be the result of performing serialize a group of selectors on the rule’s associated selectors,
  27. // followed by the string " {", i.e., a single SPACE (U+0020), followed by LEFT CURLY BRACKET (U+007B).
  28. builder.append(serialize_a_group_of_selectors(selectors()));
  29. builder.append(" {"sv);
  30. // 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.
  31. auto decls = declaration().serialized();
  32. // 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.
  33. String rules;
  34. // 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.
  35. if (decls.is_null() && rules.is_null()) {
  36. builder.append(" }"sv);
  37. return builder.to_string();
  38. }
  39. // 5. If rules is null:
  40. if (rules.is_null()) {
  41. // 1. Append a single SPACE (U+0020) to s
  42. builder.append(' ');
  43. // 2. Append decls to s
  44. builder.append(decls);
  45. // 3. Append " }" to s (i.e. a single SPACE (U+0020) followed by RIGHT CURLY BRACKET (U+007D)).
  46. builder.append(" }"sv);
  47. // 4. Return s.
  48. return builder.to_string();
  49. }
  50. // FIXME: 6. Otherwise:
  51. // FIXME: 1. If decls is not null, prepend it to rules.
  52. // FIXME: 2. For each rule in rules:
  53. // FIXME: 1. Append a newline followed by two spaces to s.
  54. // FIXME: 2. Append rule to s.
  55. // FIXME: 3. Append a newline followed by RIGHT CURLY BRACKET (U+007D) to s.
  56. // FIXME: 4. Return s.
  57. TODO();
  58. }
  59. // https://www.w3.org/TR/cssom/#dom-cssstylerule-selectortext
  60. String CSSStyleRule::selector_text() const
  61. {
  62. // The selectorText attribute, on getting, must return the result of serializing the associated group of selectors.
  63. return serialize_a_group_of_selectors(selectors());
  64. }
  65. // https://www.w3.org/TR/cssom/#dom-cssstylerule-selectortext
  66. void CSSStyleRule::set_selector_text(StringView selector_text)
  67. {
  68. // 1. Run the parse a group of selectors algorithm on the given value.
  69. auto parsed_selectors = parse_selector({}, selector_text);
  70. // 2. If the algorithm returns a non-null value replace the associated group of selectors with the returned value.
  71. if (parsed_selectors.has_value())
  72. m_selectors = parsed_selectors.release_value();
  73. // 3. Otherwise, if the algorithm returns a null value, do nothing.
  74. }
  75. }