Serialize.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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(");
  105. serialize_a_string(builder, url.to_string());
  106. builder.append(')');
  107. }
  108. String escape_a_character(u32 character)
  109. {
  110. StringBuilder builder;
  111. escape_a_character(builder, character);
  112. return builder.to_string();
  113. }
  114. String escape_a_character_as_code_point(u32 character)
  115. {
  116. StringBuilder builder;
  117. escape_a_character_as_code_point(builder, character);
  118. return builder.to_string();
  119. }
  120. String serialize_an_identifier(StringView ident)
  121. {
  122. StringBuilder builder;
  123. serialize_an_identifier(builder, ident);
  124. return builder.to_string();
  125. }
  126. String serialize_a_string(StringView string)
  127. {
  128. StringBuilder builder;
  129. serialize_a_string(builder, string);
  130. return builder.to_string();
  131. }
  132. String serialize_a_url(StringView url)
  133. {
  134. StringBuilder builder;
  135. serialize_a_url(builder, url);
  136. return builder.to_string();
  137. }
  138. }