strings.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // Forked from https://github.com/jsdom/jsdom/blob/main/lib/jsdom/living/helpers/strings.js
  2. "use strict";
  3. // https://infra.spec.whatwg.org/#ascii-whitespace
  4. const asciiWhitespaceRe = /^[\t\n\f\r ]$/;
  5. exports.asciiWhitespaceRe = asciiWhitespaceRe;
  6. // https://infra.spec.whatwg.org/#ascii-lowercase
  7. exports.asciiLowercase = (s) => {
  8. const len = s.length;
  9. const out = new Array(len);
  10. for (let i = 0; i < len; i++) {
  11. const code = s.charCodeAt(i);
  12. // If the character is between 'A' (65) and 'Z' (90), convert using bitwise OR with 32
  13. out[i] = code >= 65 && code <= 90 ? String.fromCharCode(code | 32) : s[i];
  14. }
  15. return out.join("");
  16. };
  17. // https://infra.spec.whatwg.org/#ascii-uppercase
  18. exports.asciiUppercase = (s) => {
  19. const len = s.length;
  20. const out = new Array(len);
  21. for (let i = 0; i < len; i++) {
  22. const code = s.charCodeAt(i);
  23. // If the character is between 'a' (97) and 'z' (122), convert using bitwise AND with ~32
  24. out[i] = code >= 97 && code <= 122 ? String.fromCharCode(code & ~32) : s[i];
  25. }
  26. return out.join("");
  27. };
  28. // https://infra.spec.whatwg.org/#strip-newlines
  29. exports.stripNewlines = (s) => {
  30. return s.replace(/[\n\r]+/g, "");
  31. };
  32. // https://infra.spec.whatwg.org/#strip-leading-and-trailing-ascii-whitespace
  33. exports.stripLeadingAndTrailingASCIIWhitespace = (s) => {
  34. return s.replace(/^[ \t\n\f\r]+/, "").replace(/[ \t\n\f\r]+$/, "");
  35. };
  36. // https://infra.spec.whatwg.org/#strip-and-collapse-ascii-whitespace
  37. exports.stripAndCollapseASCIIWhitespace = (s) => {
  38. return s
  39. .replace(/[ \t\n\f\r]+/g, " ")
  40. .replace(/^[ \t\n\f\r]+/, "")
  41. .replace(/[ \t\n\f\r]+$/, "");
  42. };
  43. // https://html.spec.whatwg.org/multipage/infrastructure.html#valid-simple-colour
  44. exports.isValidSimpleColor = (s) => {
  45. return /^#[a-fA-F\d]{6}$/.test(s);
  46. };
  47. // https://infra.spec.whatwg.org/#ascii-case-insensitive
  48. exports.asciiCaseInsensitiveMatch = (a, b) => {
  49. if (a.length !== b.length) {
  50. return false;
  51. }
  52. for (let i = 0; i < a.length; ++i) {
  53. if ((a.charCodeAt(i) | 32) !== (b.charCodeAt(i) | 32)) {
  54. return false;
  55. }
  56. }
  57. return true;
  58. };
  59. // https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#rules-for-parsing-integers
  60. // Error is represented as null.
  61. const parseInteger = (exports.parseInteger = (input) => {
  62. // The implementation here is slightly different from the spec's. We want to use parseInt(), but parseInt() trims
  63. // Unicode whitespace in addition to just ASCII ones, so we make sure that the trimmed prefix contains only ASCII
  64. // whitespace ourselves.
  65. const numWhitespace = input.length - input.trimStart().length;
  66. if (/[^\t\n\f\r ]/.test(input.slice(0, numWhitespace))) {
  67. return null;
  68. }
  69. // We don't allow hexadecimal numbers here.
  70. // eslint-disable-next-line radix
  71. const value = parseInt(input, 10);
  72. if (Number.isNaN(value)) {
  73. return null;
  74. }
  75. // parseInt() returns -0 for "-0". Normalize that here.
  76. return value === 0 ? 0 : value;
  77. });
  78. // https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#rules-for-parsing-non-negative-integers
  79. // Error is represented as null.
  80. exports.parseNonNegativeInteger = (input) => {
  81. const value = parseInteger(input);
  82. if (value === null) {
  83. return null;
  84. }
  85. if (value < 0) {
  86. return null;
  87. }
  88. return value;
  89. };
  90. // https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-floating-point-number
  91. const floatingPointNumRe = /^-?(?:\d+|\d*\.\d+)(?:[eE][-+]?\d+)?$/;
  92. exports.isValidFloatingPointNumber = (str) => floatingPointNumRe.test(str);
  93. // https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#rules-for-parsing-floating-point-number-values
  94. // Error is represented as null.
  95. exports.parseFloatingPointNumber = (str) => {
  96. // The implementation here is slightly different from the spec's. We need to use parseFloat() in order to retain
  97. // accuracy, but parseFloat() trims Unicode whitespace in addition to just ASCII ones, so we make sure that the
  98. // trimmed prefix contains only ASCII whitespace ourselves.
  99. const numWhitespace = str.length - str.trimStart().length;
  100. if (/[^\t\n\f\r ]/.test(str.slice(0, numWhitespace))) {
  101. return null;
  102. }
  103. const parsed = parseFloat(str);
  104. return isFinite(parsed) ? parsed : null;
  105. };
  106. // https://infra.spec.whatwg.org/#split-on-ascii-whitespace
  107. exports.splitOnASCIIWhitespace = (str) => {
  108. let position = 0;
  109. const tokens = [];
  110. while (position < str.length && asciiWhitespaceRe.test(str[position])) {
  111. position++;
  112. }
  113. if (position === str.length) {
  114. return tokens;
  115. }
  116. while (position < str.length) {
  117. const start = position;
  118. while (position < str.length && !asciiWhitespaceRe.test(str[position])) {
  119. position++;
  120. }
  121. tokens.push(str.slice(start, position));
  122. while (position < str.length && asciiWhitespaceRe.test(str[position])) {
  123. position++;
  124. }
  125. }
  126. return tokens;
  127. };
  128. // https://infra.spec.whatwg.org/#split-on-commas
  129. exports.splitOnCommas = (str) => {
  130. let position = 0;
  131. const tokens = [];
  132. while (position < str.length) {
  133. let start = position;
  134. while (position < str.length && str[position] !== ",") {
  135. position++;
  136. }
  137. let end = position;
  138. while (start < str.length && asciiWhitespaceRe.test(str[start])) {
  139. start++;
  140. }
  141. while (end > start && asciiWhitespaceRe.test(str[end - 1])) {
  142. end--;
  143. }
  144. tokens.push(str.slice(start, end));
  145. if (position < str.length) {
  146. position++;
  147. }
  148. }
  149. return tokens;
  150. };