StyleRules.cpp 4.4 KB

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