CSSMediaRule.cpp 2.1 KB

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