CSSStyleRule.cpp 3.7 KB

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