CSSMediaRule.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
  3. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibWeb/Bindings/CSSMediaRulePrototype.h>
  8. #include <LibWeb/Bindings/WindowObject.h>
  9. #include <LibWeb/CSS/CSSMediaRule.h>
  10. namespace Web::CSS {
  11. CSSMediaRule* CSSMediaRule::create(Bindings::WindowObject& window_object, MediaList& media_queries, CSSRuleList& rules)
  12. {
  13. return window_object.heap().allocate<CSSMediaRule>(window_object.realm(), window_object, media_queries, rules);
  14. }
  15. CSSMediaRule::CSSMediaRule(Bindings::WindowObject& window_object, MediaList& media, CSSRuleList& rules)
  16. : CSSConditionRule(window_object, rules)
  17. , m_media(media)
  18. {
  19. set_prototype(&window_object.ensure_web_prototype<Bindings::CSSMediaRulePrototype>("CSSMediaRule"));
  20. }
  21. void CSSMediaRule::visit_edges(Cell::Visitor& visitor)
  22. {
  23. Base::visit_edges(visitor);
  24. visitor.visit(&m_media);
  25. }
  26. String CSSMediaRule::condition_text() const
  27. {
  28. return m_media.media_text();
  29. }
  30. void CSSMediaRule::set_condition_text(String text)
  31. {
  32. m_media.set_media_text(text);
  33. }
  34. // https://www.w3.org/TR/cssom-1/#serialize-a-css-rule
  35. String CSSMediaRule::serialized() const
  36. {
  37. // The result of concatenating the following:
  38. StringBuilder builder;
  39. // 1. The string "@media", followed by a single SPACE (U+0020).
  40. builder.append("@media "sv);
  41. // 2. The result of performing serialize a media query list on rule’s media query list.
  42. builder.append(condition_text());
  43. // 3. A single SPACE (U+0020), followed by the string "{", i.e., LEFT CURLY BRACKET (U+007B), followed by a newline.
  44. builder.append(" {\n"sv);
  45. // 4. The result of performing serialize a CSS rule on each rule in the rule’s cssRules list, separated by a newline and indented by two spaces.
  46. for (size_t i = 0; i < css_rules().length(); i++) {
  47. auto rule = css_rules().item(i);
  48. if (i != 0)
  49. builder.append("\n"sv);
  50. builder.append(" "sv);
  51. builder.append(rule->css_text());
  52. }
  53. // 5. A newline, followed by the string "}", i.e., RIGHT CURLY BRACKET (U+007D)
  54. builder.append("\n}"sv);
  55. return builder.to_string();
  56. }
  57. }