CSSMediaRule.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. String CSSMediaRule::condition_text() const
  14. {
  15. return m_media->media_text();
  16. }
  17. void CSSMediaRule::set_condition_text(String text)
  18. {
  19. m_media->set_media_text(text);
  20. }
  21. // https://www.w3.org/TR/cssom-1/#serialize-a-css-rule
  22. String CSSMediaRule::serialized() const
  23. {
  24. // The result of concatenating the following:
  25. StringBuilder builder;
  26. // 1. The string "@media", followed by a single SPACE (U+0020).
  27. builder.append("@media ");
  28. // 2. The result of performing serialize a media query list on rule’s media query list.
  29. builder.append(condition_text());
  30. // 3. A single SPACE (U+0020), followed by the string "{", i.e., LEFT CURLY BRACKET (U+007B), followed by a newline.
  31. builder.append(" {\n");
  32. // 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.
  33. for (size_t i = 0; i < css_rules().length(); i++) {
  34. auto rule = css_rules().item(i);
  35. if (i != 0)
  36. builder.append("\n");
  37. builder.append(" ");
  38. builder.append(rule->css_text());
  39. }
  40. // 5. A newline, followed by the string "}", i.e., RIGHT CURLY BRACKET (U+007D)
  41. builder.append("\n}");
  42. return builder.to_string();
  43. }
  44. }