CSSMediaRule.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/CSS/CSSMediaRule.h>
  7. namespace Web::CSS {
  8. CSSMediaRule::CSSMediaRule(NonnullRefPtr<MediaList>&& media, NonnullRefPtrVector<CSSRule>&& rules)
  9. : CSSConditionRule(move(rules))
  10. , m_media(move(media))
  11. {
  12. }
  13. CSSMediaRule::~CSSMediaRule()
  14. {
  15. }
  16. String CSSMediaRule::condition_text() const
  17. {
  18. return m_media->media_text();
  19. }
  20. void CSSMediaRule::set_condition_text(String text)
  21. {
  22. m_media->set_media_text(text);
  23. }
  24. // https://www.w3.org/TR/cssom-1/#serialize-a-css-rule
  25. String CSSMediaRule::serialized() const
  26. {
  27. // The result of concatenating the following:
  28. StringBuilder builder;
  29. // 1. The string "@media", followed by a single SPACE (U+0020).
  30. builder.append("@media ");
  31. // 2. The result of performing serialize a media query list on rule’s media query list.
  32. builder.append(condition_text());
  33. // 3. A single SPACE (U+0020), followed by the string "{", i.e., LEFT CURLY BRACKET (U+007B), followed by a newline.
  34. builder.append(" {\n");
  35. // 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.
  36. for (size_t i = 0; i < css_rules().length(); i++) {
  37. auto rule = css_rules().item(i);
  38. if (i != 0)
  39. builder.append("\n");
  40. builder.append(" ");
  41. builder.append(rule->css_text());
  42. }
  43. // 5. A newline, followed by the string "}", i.e., RIGHT CURLY BRACKET (U+007D)
  44. builder.append("\n}");
  45. return builder.to_string();
  46. }
  47. }