StyleRules.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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/ComponentValue.h>
  8. #include <LibWeb/CSS/Parser/Declaration.h>
  9. #include <LibWeb/CSS/Parser/DeclarationOrAtRule.h>
  10. #include <LibWeb/CSS/Parser/StyleBlockRule.h>
  11. #include <LibWeb/CSS/Parser/StyleFunctionRule.h>
  12. #include <LibWeb/CSS/Parser/StyleRule.h>
  13. #include <LibWeb/CSS/Serialize.h>
  14. namespace Web::CSS {
  15. DeclarationOrAtRule::DeclarationOrAtRule(RefPtr<StyleRule> at)
  16. : m_type(DeclarationType::At)
  17. , m_at(move(at))
  18. {
  19. }
  20. DeclarationOrAtRule::DeclarationOrAtRule(Declaration declaration)
  21. : m_type(DeclarationType::Declaration)
  22. , m_declaration(move(declaration))
  23. {
  24. }
  25. DeclarationOrAtRule::~DeclarationOrAtRule() = default;
  26. StyleRule::StyleRule(StyleRule::Type type)
  27. : m_type(type)
  28. {
  29. }
  30. StyleRule::~StyleRule() = default;
  31. StyleBlockRule::StyleBlockRule() = default;
  32. StyleBlockRule::~StyleBlockRule() = default;
  33. Declaration::Declaration() = default;
  34. Declaration::~Declaration() = default;
  35. StyleFunctionRule::StyleFunctionRule(String name)
  36. : m_name(move(name))
  37. {
  38. }
  39. StyleFunctionRule::StyleFunctionRule(String name, Vector<Parser::ComponentValue>&& values)
  40. : m_name(move(name))
  41. , m_values(move(values))
  42. {
  43. }
  44. StyleFunctionRule::~StyleFunctionRule() = default;
  45. template<class SeparatorType, class CollectionType>
  46. void append_with_to_string(StringBuilder& builder, SeparatorType& separator, CollectionType& collection)
  47. {
  48. bool first = true;
  49. for (auto& item : collection) {
  50. if (first)
  51. first = false;
  52. else
  53. builder.append(separator);
  54. builder.append(item.to_string());
  55. }
  56. }
  57. String DeclarationOrAtRule::to_string() const
  58. {
  59. StringBuilder builder;
  60. switch (m_type) {
  61. default:
  62. case DeclarationType::At:
  63. builder.append(m_at->to_string());
  64. break;
  65. case DeclarationType::Declaration:
  66. builder.append(m_declaration.to_string());
  67. break;
  68. }
  69. return builder.to_string();
  70. }
  71. String StyleRule::to_string() const
  72. {
  73. StringBuilder builder;
  74. if (is_at_rule()) {
  75. builder.append("@");
  76. serialize_an_identifier(builder, m_at_rule_name);
  77. }
  78. append_with_to_string(builder, " ", m_prelude);
  79. if (m_block)
  80. builder.append(m_block->to_string());
  81. else
  82. builder.append(';');
  83. return builder.to_string();
  84. }
  85. String StyleBlockRule::to_string() const
  86. {
  87. StringBuilder builder;
  88. builder.append(m_token.bracket_string());
  89. append_with_to_string(builder, " ", m_values);
  90. builder.append(m_token.bracket_mirror_string());
  91. return builder.to_string();
  92. }
  93. String Declaration::to_string() const
  94. {
  95. StringBuilder builder;
  96. serialize_an_identifier(builder, m_name);
  97. builder.append(": ");
  98. append_with_to_string(builder, " ", m_values);
  99. if (m_important == Important::Yes)
  100. builder.append(" !important");
  101. return builder.to_string();
  102. }
  103. String StyleFunctionRule::to_string() const
  104. {
  105. StringBuilder builder;
  106. serialize_an_identifier(builder, m_name);
  107. builder.append("(");
  108. append_with_to_string(builder, " ", m_values);
  109. builder.append(")");
  110. return builder.to_string();
  111. }
  112. }