StyleRules.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright (c) 2020-2021, the SerenityOS developers.
  3. * Copyright (c) 2021, Sam Atkins <atkinssj@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibWeb/CSS/Parser/AtStyleRule.h>
  8. #include <LibWeb/CSS/Parser/DeclarationOrAtRule.h>
  9. #include <LibWeb/CSS/Parser/QualifiedStyleRule.h>
  10. #include <LibWeb/CSS/Parser/StyleBlockRule.h>
  11. #include <LibWeb/CSS/Parser/StyleComponentValueRule.h>
  12. #include <LibWeb/CSS/Parser/StyleDeclarationRule.h>
  13. #include <LibWeb/CSS/Parser/StyleFunctionRule.h>
  14. namespace Web::CSS {
  15. AtStyleRule::AtStyleRule() { }
  16. AtStyleRule::~AtStyleRule() { }
  17. DeclarationOrAtRule::DeclarationOrAtRule(RefPtr<AtStyleRule> at)
  18. : m_type(DeclarationType::At)
  19. , m_at(move(at))
  20. {
  21. }
  22. DeclarationOrAtRule::DeclarationOrAtRule(StyleDeclarationRule declaration)
  23. : m_type(DeclarationType::Declaration)
  24. , m_declaration(move(declaration))
  25. {
  26. }
  27. DeclarationOrAtRule::~DeclarationOrAtRule() { }
  28. QualifiedStyleRule::QualifiedStyleRule() { }
  29. QualifiedStyleRule::~QualifiedStyleRule() { }
  30. StyleBlockRule::StyleBlockRule() { }
  31. StyleBlockRule::~StyleBlockRule() { }
  32. StyleComponentValueRule::StyleComponentValueRule(ComponentType type)
  33. : m_type(type)
  34. {
  35. }
  36. StyleComponentValueRule::~StyleComponentValueRule() { }
  37. StyleDeclarationRule::StyleDeclarationRule() { }
  38. StyleDeclarationRule::~StyleDeclarationRule() { }
  39. StyleFunctionRule::StyleFunctionRule(String name)
  40. : m_name(name)
  41. {
  42. }
  43. StyleFunctionRule::~StyleFunctionRule() { }
  44. template<class SeparatorType, class CollectionType>
  45. void append_with_to_string(StringBuilder& builder, SeparatorType& separator, CollectionType& collection)
  46. {
  47. bool first = true;
  48. for (auto& item : collection) {
  49. if (first)
  50. first = false;
  51. else
  52. builder.append(separator);
  53. builder.append(item.to_string());
  54. }
  55. }
  56. template<class SeparatorType, class CollectionType>
  57. void append_raw(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);
  66. }
  67. }
  68. String AtStyleRule::to_string() const
  69. {
  70. StringBuilder builder;
  71. builder.append("@");
  72. builder.append(m_name);
  73. builder.append(QualifiedStyleRule::to_string());
  74. return builder.to_string();
  75. }
  76. String DeclarationOrAtRule::to_string() const
  77. {
  78. StringBuilder builder;
  79. switch (m_type) {
  80. default:
  81. case DeclarationType::At:
  82. builder.append(m_at->to_string());
  83. break;
  84. case DeclarationType::Declaration:
  85. builder.append(m_declaration.to_string());
  86. break;
  87. }
  88. return builder.to_string();
  89. }
  90. String QualifiedStyleRule::to_string() const
  91. {
  92. StringBuilder builder;
  93. append_with_to_string(builder, " ", m_prelude);
  94. builder.append(m_block->to_string());
  95. return builder.to_string();
  96. }
  97. String StyleBlockRule::to_string() const
  98. {
  99. StringBuilder builder;
  100. builder.append(m_token.bracket_string());
  101. append_with_to_string(builder, ", ", m_values);
  102. builder.append(m_token.bracket_mirror_string());
  103. return builder.to_string();
  104. }
  105. String StyleComponentValueRule::to_string() const
  106. {
  107. StringBuilder builder;
  108. switch (m_type) {
  109. case ComponentType::Token:
  110. builder.append(m_token.to_string());
  111. break;
  112. case ComponentType::Function:
  113. builder.append(m_function->to_string());
  114. break;
  115. case ComponentType::Block:
  116. builder.append(m_block->to_string());
  117. break;
  118. }
  119. return builder.to_string();
  120. }
  121. String StyleDeclarationRule::to_string() const
  122. {
  123. StringBuilder builder;
  124. builder.append(m_name);
  125. builder.append(": ");
  126. append_with_to_string(builder, " ", m_values);
  127. if (m_important)
  128. builder.append(" !important");
  129. return builder.to_string();
  130. }
  131. String StyleFunctionRule::to_string() const
  132. {
  133. StringBuilder builder;
  134. builder.append(m_name);
  135. builder.append("(");
  136. append_raw(builder, ", ", m_values);
  137. builder.append(")");
  138. return builder.to_string();
  139. }
  140. }