CSSStyleRule.cpp 3.2 KB

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