StyleRules.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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(name)
  53. {
  54. }
  55. StyleFunctionRule::~StyleFunctionRule() { }
  56. template<class SeparatorType, class CollectionType>
  57. void append_with_to_string(StringBuilder& builder, SeparatorType& separator, CollectionType& collection)
  58. {
  59. bool first = true;
  60. for (auto& item : collection) {
  61. if (first)
  62. first = false;
  63. else
  64. builder.append(separator);
  65. builder.append(item.to_string());
  66. }
  67. }
  68. String DeclarationOrAtRule::to_string() const
  69. {
  70. StringBuilder builder;
  71. switch (m_type) {
  72. default:
  73. case DeclarationType::At:
  74. builder.append(m_at->to_string());
  75. break;
  76. case DeclarationType::Declaration:
  77. builder.append(m_declaration.to_string());
  78. break;
  79. }
  80. return builder.to_string();
  81. }
  82. String StyleRule::to_string() const
  83. {
  84. StringBuilder builder;
  85. if (m_type == Type::At) {
  86. builder.append("@");
  87. serialize_an_identifier(builder, m_name);
  88. }
  89. append_with_to_string(builder, " ", m_prelude);
  90. if (m_block)
  91. builder.append(m_block->to_string());
  92. else
  93. builder.append(';');
  94. return builder.to_string();
  95. }
  96. String StyleBlockRule::to_string() const
  97. {
  98. StringBuilder builder;
  99. builder.append(m_token.bracket_string());
  100. append_with_to_string(builder, " ", m_values);
  101. builder.append(m_token.bracket_mirror_string());
  102. return builder.to_string();
  103. }
  104. String StyleComponentValueRule::to_string() const
  105. {
  106. switch (m_type) {
  107. case StyleComponentValueRule::ComponentType::Token:
  108. return m_token.to_string();
  109. case StyleComponentValueRule::ComponentType::Function:
  110. return m_function->to_string();
  111. case StyleComponentValueRule::ComponentType::Block:
  112. return m_block->to_string();
  113. default:
  114. VERIFY_NOT_REACHED();
  115. }
  116. }
  117. String StyleComponentValueRule::to_debug_string() const
  118. {
  119. StringBuilder builder;
  120. switch (m_type) {
  121. case ComponentType::Token:
  122. builder.append("Token: ");
  123. builder.append(m_token.to_debug_string());
  124. break;
  125. case ComponentType::Function:
  126. builder.append("Function: ");
  127. builder.append(m_function->to_string());
  128. break;
  129. case ComponentType::Block:
  130. builder.append("Block: ");
  131. builder.append(m_block->to_string());
  132. break;
  133. }
  134. return builder.to_string();
  135. }
  136. String StyleDeclarationRule::to_string() const
  137. {
  138. StringBuilder builder;
  139. serialize_an_identifier(builder, m_name);
  140. builder.append(": ");
  141. append_with_to_string(builder, " ", m_values);
  142. if (m_important)
  143. builder.append(" !important");
  144. return builder.to_string();
  145. }
  146. String StyleFunctionRule::to_string() const
  147. {
  148. StringBuilder builder;
  149. serialize_an_identifier(builder, m_name);
  150. builder.append("(");
  151. append_with_to_string(builder, " ", m_values);
  152. builder.append(")");
  153. return builder.to_string();
  154. }
  155. }