StyleRules.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright (c) 2020-2021, SerenityOS developers
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibWeb/CSS/Parser/AtStyleRule.h>
  27. #include <LibWeb/CSS/Parser/DeclarationOrAtRule.h>
  28. #include <LibWeb/CSS/Parser/QualifiedStyleRule.h>
  29. #include <LibWeb/CSS/Parser/StyleBlockRule.h>
  30. #include <LibWeb/CSS/Parser/StyleComponentValueRule.h>
  31. #include <LibWeb/CSS/Parser/StyleDeclarationRule.h>
  32. #include <LibWeb/CSS/Parser/StyleFunctionRule.h>
  33. namespace Web::CSS {
  34. AtStyleRule::AtStyleRule() { }
  35. AtStyleRule::~AtStyleRule() { }
  36. DeclarationOrAtRule::DeclarationOrAtRule(AtStyleRule at)
  37. : m_type(DeclarationType::At)
  38. , m_at(move(at))
  39. {
  40. }
  41. DeclarationOrAtRule::DeclarationOrAtRule(StyleDeclarationRule declaration)
  42. : m_type(DeclarationType::Declaration)
  43. , m_declaration(move(declaration))
  44. {
  45. }
  46. DeclarationOrAtRule::~DeclarationOrAtRule() { }
  47. QualifiedStyleRule::QualifiedStyleRule() { }
  48. QualifiedStyleRule::~QualifiedStyleRule() { }
  49. StyleBlockRule::StyleBlockRule() { }
  50. StyleBlockRule::~StyleBlockRule() { }
  51. StyleComponentValueRule::StyleComponentValueRule(ComponentType type)
  52. : m_type(type)
  53. {
  54. }
  55. StyleComponentValueRule::~StyleComponentValueRule() { }
  56. StyleDeclarationRule::StyleDeclarationRule() { }
  57. StyleDeclarationRule::~StyleDeclarationRule() { }
  58. StyleFunctionRule::StyleFunctionRule() { }
  59. StyleFunctionRule::~StyleFunctionRule() { }
  60. template<class SeparatorType, class CollectionType>
  61. void append_with_to_string(StringBuilder& builder, SeparatorType& separator, CollectionType& collection)
  62. {
  63. bool first = true;
  64. for (auto& item : collection) {
  65. if (first)
  66. first = false;
  67. else
  68. builder.append(separator);
  69. builder.append(item.to_string());
  70. }
  71. }
  72. template<class SeparatorType, class CollectionType>
  73. void append_raw(StringBuilder& builder, SeparatorType& separator, CollectionType& collection)
  74. {
  75. bool first = true;
  76. for (auto& item : collection) {
  77. if (first)
  78. first = false;
  79. else
  80. builder.append(separator);
  81. builder.append(item);
  82. }
  83. }
  84. String AtStyleRule::to_string() const
  85. {
  86. StringBuilder builder;
  87. builder.append("@");
  88. builder.append(m_name);
  89. builder.append(QualifiedStyleRule::to_string());
  90. return builder.to_string();
  91. }
  92. String DeclarationOrAtRule::to_string() const
  93. {
  94. StringBuilder builder;
  95. switch (m_type) {
  96. default:
  97. case DeclarationType::At:
  98. builder.append(m_at.to_string());
  99. break;
  100. case DeclarationType::Declaration:
  101. builder.append(m_declaration.to_string());
  102. break;
  103. }
  104. return builder.to_string();
  105. }
  106. String QualifiedStyleRule::to_string() const
  107. {
  108. StringBuilder builder;
  109. append_raw(builder, " ", m_prelude);
  110. builder.append(m_block.to_string());
  111. return builder.to_string();
  112. }
  113. String StyleBlockRule::to_string() const
  114. {
  115. StringBuilder builder;
  116. builder.append(m_token.bracket_string());
  117. append_raw(builder, ", ", m_values);
  118. builder.append(m_token.bracket_mirror_string());
  119. return builder.to_string();
  120. }
  121. String StyleComponentValueRule::to_string() const
  122. {
  123. StringBuilder builder;
  124. switch (m_type) {
  125. case ComponentType::Token:
  126. builder.append(m_token.to_string());
  127. break;
  128. case ComponentType::Function:
  129. builder.append(m_function.to_string());
  130. break;
  131. case ComponentType::Block:
  132. builder.append(m_block.to_string());
  133. break;
  134. }
  135. return builder.to_string();
  136. }
  137. String StyleDeclarationRule::to_string() const
  138. {
  139. StringBuilder builder;
  140. builder.append(m_name);
  141. builder.append(": ");
  142. append_with_to_string(builder, " ", m_values);
  143. if (m_important)
  144. builder.append(" !important");
  145. return builder.to_string();
  146. }
  147. String StyleFunctionRule::to_string() const
  148. {
  149. StringBuilder builder;
  150. builder.append(m_name);
  151. builder.append("(");
  152. append_raw(builder, ", ", m_values);
  153. builder.append(");");
  154. return builder.to_string();
  155. }
  156. }