StyleRules.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * Copyright (c) 2020-2021, the SerenityOS developers.
  3. * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibWeb/CSS/Parser/Block.h>
  8. #include <LibWeb/CSS/Parser/ComponentValue.h>
  9. #include <LibWeb/CSS/Parser/Declaration.h>
  10. #include <LibWeb/CSS/Parser/DeclarationOrAtRule.h>
  11. #include <LibWeb/CSS/Parser/Function.h>
  12. #include <LibWeb/CSS/Parser/StyleRule.h>
  13. #include <LibWeb/CSS/Serialize.h>
  14. namespace Web::CSS {
  15. StyleRule::StyleRule(StyleRule::Type type)
  16. : m_type(type)
  17. {
  18. }
  19. StyleRule::~StyleRule() = default;
  20. template<class SeparatorType, class CollectionType>
  21. void append_with_to_string(StringBuilder& builder, SeparatorType& separator, CollectionType& collection)
  22. {
  23. bool first = true;
  24. for (auto& item : collection) {
  25. if (first)
  26. first = false;
  27. else
  28. builder.append(separator);
  29. builder.append(item.to_string());
  30. }
  31. }
  32. String StyleRule::to_string() const
  33. {
  34. StringBuilder builder;
  35. if (is_at_rule()) {
  36. builder.append("@");
  37. serialize_an_identifier(builder, m_at_rule_name);
  38. }
  39. append_with_to_string(builder, " ", m_prelude);
  40. if (m_block)
  41. builder.append(m_block->to_string());
  42. else
  43. builder.append(';');
  44. return builder.to_string();
  45. }
  46. }