StyleRules.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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/DeclarationOrAtRule.h>
  8. #include <LibWeb/CSS/Parser/StyleBlockRule.h>
  9. #include <LibWeb/CSS/Parser/StyleComponentValueRule.h>
  10. #include <LibWeb/CSS/Parser/StyleDeclarationRule.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(StyleDeclarationRule declaration)
  21. : m_type(DeclarationType::Declaration)
  22. , m_declaration(move(declaration))
  23. {
  24. }
  25. DeclarationOrAtRule::~DeclarationOrAtRule() { }
  26. StyleRule::StyleRule(StyleRule::Type type)
  27. : m_type(type)
  28. {
  29. }
  30. StyleRule::~StyleRule() { }
  31. StyleBlockRule::StyleBlockRule() { }
  32. StyleBlockRule::~StyleBlockRule() { }
  33. StyleComponentValueRule::StyleComponentValueRule(Token token)
  34. : m_type(StyleComponentValueRule::ComponentType::Token)
  35. , m_token(token)
  36. {
  37. }
  38. StyleComponentValueRule::StyleComponentValueRule(NonnullRefPtr<StyleFunctionRule> function)
  39. : m_type(StyleComponentValueRule::ComponentType::Function)
  40. , m_function(function)
  41. {
  42. }
  43. StyleComponentValueRule::StyleComponentValueRule(NonnullRefPtr<StyleBlockRule> block)
  44. : m_type(StyleComponentValueRule::ComponentType::Block)
  45. , m_block(block)
  46. {
  47. }
  48. StyleComponentValueRule::~StyleComponentValueRule() { }
  49. StyleDeclarationRule::StyleDeclarationRule() { }
  50. StyleDeclarationRule::~StyleDeclarationRule() { }
  51. StyleFunctionRule::StyleFunctionRule(String name)
  52. : m_name(move(name))
  53. {
  54. }
  55. StyleFunctionRule::StyleFunctionRule(String name, Vector<StyleComponentValueRule>&& values)
  56. : m_name(move(name))
  57. , m_values(move(values))
  58. {
  59. }
  60. StyleFunctionRule::~StyleFunctionRule() { }
  61. template<class SeparatorType, class CollectionType>
  62. void append_with_to_string(StringBuilder& builder, SeparatorType& separator, CollectionType& collection)
  63. {
  64. bool first = true;
  65. for (auto& item : collection) {
  66. if (first)
  67. first = false;
  68. else
  69. builder.append(separator);
  70. builder.append(item.to_string());
  71. }
  72. }
  73. String DeclarationOrAtRule::to_string() const
  74. {
  75. StringBuilder builder;
  76. switch (m_type) {
  77. default:
  78. case DeclarationType::At:
  79. builder.append(m_at->to_string());
  80. break;
  81. case DeclarationType::Declaration:
  82. builder.append(m_declaration.to_string());
  83. break;
  84. }
  85. return builder.to_string();
  86. }
  87. String StyleRule::to_string() const
  88. {
  89. StringBuilder builder;
  90. if (m_type == Type::At) {
  91. builder.append("@");
  92. serialize_an_identifier(builder, m_name);
  93. }
  94. append_with_to_string(builder, " ", m_prelude);
  95. if (m_block)
  96. builder.append(m_block->to_string());
  97. else
  98. builder.append(';');
  99. return builder.to_string();
  100. }
  101. String StyleBlockRule::to_string() const
  102. {
  103. StringBuilder builder;
  104. builder.append(m_token.bracket_string());
  105. append_with_to_string(builder, " ", m_values);
  106. builder.append(m_token.bracket_mirror_string());
  107. return builder.to_string();
  108. }
  109. String StyleComponentValueRule::to_string() const
  110. {
  111. switch (m_type) {
  112. case StyleComponentValueRule::ComponentType::Token:
  113. return m_token.to_string();
  114. case StyleComponentValueRule::ComponentType::Function:
  115. return m_function->to_string();
  116. case StyleComponentValueRule::ComponentType::Block:
  117. return m_block->to_string();
  118. default:
  119. VERIFY_NOT_REACHED();
  120. }
  121. }
  122. String StyleComponentValueRule::to_debug_string() const
  123. {
  124. StringBuilder builder;
  125. switch (m_type) {
  126. case ComponentType::Token:
  127. builder.append("Token: ");
  128. builder.append(m_token.to_debug_string());
  129. break;
  130. case ComponentType::Function:
  131. builder.append("Function: ");
  132. builder.append(m_function->to_string());
  133. break;
  134. case ComponentType::Block:
  135. builder.append("Block: ");
  136. builder.append(m_block->to_string());
  137. break;
  138. }
  139. return builder.to_string();
  140. }
  141. String StyleDeclarationRule::to_string() const
  142. {
  143. StringBuilder builder;
  144. serialize_an_identifier(builder, m_name);
  145. builder.append(": ");
  146. append_with_to_string(builder, " ", m_values);
  147. if (m_important)
  148. builder.append(" !important");
  149. return builder.to_string();
  150. }
  151. String StyleFunctionRule::to_string() const
  152. {
  153. StringBuilder builder;
  154. serialize_an_identifier(builder, m_name);
  155. builder.append("(");
  156. append_with_to_string(builder, " ", m_values);
  157. builder.append(")");
  158. return builder.to_string();
  159. }
  160. }