Serialize.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/StringBuilder.h>
  7. #include <AK/Utf8View.h>
  8. #include <LibWeb/CSS/Serialize.h>
  9. namespace Web::CSS {
  10. // https://www.w3.org/TR/cssom-1/#escape-a-character
  11. void escape_a_character(StringBuilder& builder, u32 character)
  12. {
  13. builder.append('\\');
  14. builder.append_code_point(character);
  15. }
  16. // https://www.w3.org/TR/cssom-1/#escape-a-character-as-code-point
  17. void escape_a_character_as_code_point(StringBuilder& builder, u32 character)
  18. {
  19. builder.appendff("\\{:x} ", character);
  20. }
  21. // https://www.w3.org/TR/cssom-1/#serialize-an-identifier
  22. void serialize_an_identifier(StringBuilder& builder, StringView ident)
  23. {
  24. Utf8View characters { ident };
  25. auto first_character = characters.is_empty() ? 0 : *characters.begin();
  26. // To serialize an identifier means to create a string represented by the concatenation of,
  27. // for each character of the identifier:
  28. for (auto character : characters) {
  29. // If the character is NULL (U+0000), then the REPLACEMENT CHARACTER (U+FFFD).
  30. if (character == 0) {
  31. builder.append_code_point(0xFFFD);
  32. continue;
  33. }
  34. // If the character is in the range [\1-\1f] (U+0001 to U+001F) or is U+007F,
  35. // then the character escaped as code point.
  36. if ((character >= 0x0001 && character <= 0x001F) || (character == 0x007F)) {
  37. escape_a_character_as_code_point(builder, character);
  38. continue;
  39. }
  40. // If the character is the first character and is in the range [0-9] (U+0030 to U+0039),
  41. // then the character escaped as code point.
  42. if (builder.is_empty() && character >= '0' && character <= '9') {
  43. escape_a_character_as_code_point(builder, character);
  44. continue;
  45. }
  46. // If the character is the second character and is in the range [0-9] (U+0030 to U+0039)
  47. // and the first character is a "-" (U+002D), then the character escaped as code point.
  48. if (builder.length() == 1 && first_character == '-' && character >= '0' && character <= '9') {
  49. escape_a_character_as_code_point(builder, character);
  50. continue;
  51. }
  52. // If the character is the first character and is a "-" (U+002D), and there is no second
  53. // character, then the escaped character.
  54. if (builder.is_empty() && character == '-' && characters.length() == 1) {
  55. escape_a_character(builder, character);
  56. continue;
  57. }
  58. // If the character is not handled by one of the above rules and is greater than or equal to U+0080, is "-" (U+002D) or "_" (U+005F), or is in one of the ranges [0-9] (U+0030 to U+0039), [A-Z] (U+0041 to U+005A), or \[a-z] (U+0061 to U+007A), then the character itself.
  59. if ((character >= 0x0080)
  60. || (character == '-') || (character == '_')
  61. || (character >= '0' && character <= '9')
  62. || (character >= 'A' && character <= 'Z')
  63. || (character >= 'a' && character <= 'z')) {
  64. builder.append_code_point(character);
  65. continue;
  66. }
  67. // Otherwise, the escaped character.
  68. escape_a_character(builder, character);
  69. }
  70. }
  71. // https://www.w3.org/TR/cssom-1/#serialize-a-string
  72. void serialize_a_string(StringBuilder& builder, StringView string)
  73. {
  74. Utf8View characters { string };
  75. // To serialize a string means to create a string represented by '"' (U+0022), followed by the result
  76. // of applying the rules below to each character of the given string, followed by '"' (U+0022):
  77. builder.append('"');
  78. for (auto character : characters) {
  79. // If the character is NULL (U+0000), then the REPLACEMENT CHARACTER (U+FFFD).
  80. if (character == 0) {
  81. builder.append_code_point(0xFFFD);
  82. continue;
  83. }
  84. // If the character is in the range [\1-\1f] (U+0001 to U+001F) or is U+007F, the character escaped as code point.
  85. if ((character >= 0x0001 && character <= 0x001F) || (character == 0x007F)) {
  86. escape_a_character_as_code_point(builder, character);
  87. continue;
  88. }
  89. // If the character is '"' (U+0022) or "\" (U+005C), the escaped character.
  90. if (character == 0x0022 || character == 0x005C) {
  91. escape_a_character(builder, character);
  92. continue;
  93. }
  94. // Otherwise, the character itself.
  95. builder.append_code_point(character);
  96. }
  97. builder.append('"');
  98. }
  99. // https://www.w3.org/TR/cssom-1/#serialize-a-url
  100. void serialize_a_url(StringBuilder& builder, StringView url)
  101. {
  102. // To serialize a URL means to create a string represented by "url(",
  103. // followed by the serialization of the URL as a string, followed by ")".
  104. builder.append("url("sv);
  105. serialize_a_string(builder, url.to_string());
  106. builder.append(')');
  107. }
  108. // https://www.w3.org/TR/css-color-4/#serializing-sRGB-values
  109. void serialize_a_srgb_value(StringBuilder& builder, Color color)
  110. {
  111. // The serialized form is derived from the computed value and thus, uses either the rgb() or rgba() form
  112. // (depending on whether the alpha is exactly 1, or not), with lowercase letters for the function name.
  113. // NOTE: Since we use Gfx::Color, having an "alpha of 1" means its value is 255.
  114. if (color.alpha() == 255)
  115. builder.appendff("rgb({}, {}, {})"sv, color.red(), color.green(), color.blue());
  116. else
  117. builder.appendff("rgba({}, {}, {}, {})"sv, color.red(), color.green(), color.blue(), (float)(color.alpha()) / 255.0f);
  118. }
  119. String escape_a_character(u32 character)
  120. {
  121. StringBuilder builder;
  122. escape_a_character(builder, character);
  123. return builder.to_string();
  124. }
  125. String escape_a_character_as_code_point(u32 character)
  126. {
  127. StringBuilder builder;
  128. escape_a_character_as_code_point(builder, character);
  129. return builder.to_string();
  130. }
  131. String serialize_an_identifier(StringView ident)
  132. {
  133. StringBuilder builder;
  134. serialize_an_identifier(builder, ident);
  135. return builder.to_string();
  136. }
  137. String serialize_a_string(StringView string)
  138. {
  139. StringBuilder builder;
  140. serialize_a_string(builder, string);
  141. return builder.to_string();
  142. }
  143. String serialize_a_url(StringView url)
  144. {
  145. StringBuilder builder;
  146. serialize_a_url(builder, url);
  147. return builder.to_string();
  148. }
  149. String serialize_a_srgb_value(Color color)
  150. {
  151. StringBuilder builder;
  152. serialize_a_srgb_value(builder, color);
  153. return builder.to_string();
  154. }
  155. }