CSSStyleRule.cpp 4.2 KB

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