utils.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. import {
  2. isDigit,
  3. isHexDigit,
  4. isUppercaseLetter,
  5. isName,
  6. isWhiteSpace,
  7. isValidEscape
  8. } from './char-code-definitions.js';
  9. function getCharCode(source, offset) {
  10. return offset < source.length ? source.charCodeAt(offset) : 0;
  11. }
  12. export function getNewlineLength(source, offset, code) {
  13. if (code === 13 /* \r */ && getCharCode(source, offset + 1) === 10 /* \n */) {
  14. return 2;
  15. }
  16. return 1;
  17. }
  18. export function cmpChar(testStr, offset, referenceCode) {
  19. let code = testStr.charCodeAt(offset);
  20. // code.toLowerCase() for A..Z
  21. if (isUppercaseLetter(code)) {
  22. code = code | 32;
  23. }
  24. return code === referenceCode;
  25. }
  26. export function cmpStr(testStr, start, end, referenceStr) {
  27. if (end - start !== referenceStr.length) {
  28. return false;
  29. }
  30. if (start < 0 || end > testStr.length) {
  31. return false;
  32. }
  33. for (let i = start; i < end; i++) {
  34. const referenceCode = referenceStr.charCodeAt(i - start);
  35. let testCode = testStr.charCodeAt(i);
  36. // testCode.toLowerCase() for A..Z
  37. if (isUppercaseLetter(testCode)) {
  38. testCode = testCode | 32;
  39. }
  40. if (testCode !== referenceCode) {
  41. return false;
  42. }
  43. }
  44. return true;
  45. }
  46. export function findWhiteSpaceStart(source, offset) {
  47. for (; offset >= 0; offset--) {
  48. if (!isWhiteSpace(source.charCodeAt(offset))) {
  49. break;
  50. }
  51. }
  52. return offset + 1;
  53. }
  54. export function findWhiteSpaceEnd(source, offset) {
  55. for (; offset < source.length; offset++) {
  56. if (!isWhiteSpace(source.charCodeAt(offset))) {
  57. break;
  58. }
  59. }
  60. return offset;
  61. }
  62. export function findDecimalNumberEnd(source, offset) {
  63. for (; offset < source.length; offset++) {
  64. if (!isDigit(source.charCodeAt(offset))) {
  65. break;
  66. }
  67. }
  68. return offset;
  69. }
  70. // § 4.3.7. Consume an escaped code point
  71. export function consumeEscaped(source, offset) {
  72. // It assumes that the U+005C REVERSE SOLIDUS (\) has already been consumed and
  73. // that the next input code point has already been verified to be part of a valid escape.
  74. offset += 2;
  75. // hex digit
  76. if (isHexDigit(getCharCode(source, offset - 1))) {
  77. // Consume as many hex digits as possible, but no more than 5.
  78. // Note that this means 1-6 hex digits have been consumed in total.
  79. for (const maxOffset = Math.min(source.length, offset + 5); offset < maxOffset; offset++) {
  80. if (!isHexDigit(getCharCode(source, offset))) {
  81. break;
  82. }
  83. }
  84. // If the next input code point is whitespace, consume it as well.
  85. const code = getCharCode(source, offset);
  86. if (isWhiteSpace(code)) {
  87. offset += getNewlineLength(source, offset, code);
  88. }
  89. }
  90. return offset;
  91. }
  92. // §4.3.11. Consume a name
  93. // Note: This algorithm does not do the verification of the first few code points that are necessary
  94. // to ensure the returned code points would constitute an <ident-token>. If that is the intended use,
  95. // ensure that the stream starts with an identifier before calling this algorithm.
  96. export function consumeName(source, offset) {
  97. // Let result initially be an empty string.
  98. // Repeatedly consume the next input code point from the stream:
  99. for (; offset < source.length; offset++) {
  100. const code = source.charCodeAt(offset);
  101. // name code point
  102. if (isName(code)) {
  103. // Append the code point to result.
  104. continue;
  105. }
  106. // the stream starts with a valid escape
  107. if (isValidEscape(code, getCharCode(source, offset + 1))) {
  108. // Consume an escaped code point. Append the returned code point to result.
  109. offset = consumeEscaped(source, offset) - 1;
  110. continue;
  111. }
  112. // anything else
  113. // Reconsume the current input code point. Return result.
  114. break;
  115. }
  116. return offset;
  117. }
  118. // §4.3.12. Consume a number
  119. export function consumeNumber(source, offset) {
  120. let code = source.charCodeAt(offset);
  121. // 2. If the next input code point is U+002B PLUS SIGN (+) or U+002D HYPHEN-MINUS (-),
  122. // consume it and append it to repr.
  123. if (code === 0x002B || code === 0x002D) {
  124. code = source.charCodeAt(offset += 1);
  125. }
  126. // 3. While the next input code point is a digit, consume it and append it to repr.
  127. if (isDigit(code)) {
  128. offset = findDecimalNumberEnd(source, offset + 1);
  129. code = source.charCodeAt(offset);
  130. }
  131. // 4. If the next 2 input code points are U+002E FULL STOP (.) followed by a digit, then:
  132. if (code === 0x002E && isDigit(source.charCodeAt(offset + 1))) {
  133. // 4.1 Consume them.
  134. // 4.2 Append them to repr.
  135. offset += 2;
  136. // 4.3 Set type to "number".
  137. // TODO
  138. // 4.4 While the next input code point is a digit, consume it and append it to repr.
  139. offset = findDecimalNumberEnd(source, offset);
  140. }
  141. // 5. If the next 2 or 3 input code points are U+0045 LATIN CAPITAL LETTER E (E)
  142. // or U+0065 LATIN SMALL LETTER E (e), ... , followed by a digit, then:
  143. if (cmpChar(source, offset, 101 /* e */)) {
  144. let sign = 0;
  145. code = source.charCodeAt(offset + 1);
  146. // ... optionally followed by U+002D HYPHEN-MINUS (-) or U+002B PLUS SIGN (+) ...
  147. if (code === 0x002D || code === 0x002B) {
  148. sign = 1;
  149. code = source.charCodeAt(offset + 2);
  150. }
  151. // ... followed by a digit
  152. if (isDigit(code)) {
  153. // 5.1 Consume them.
  154. // 5.2 Append them to repr.
  155. // 5.3 Set type to "number".
  156. // TODO
  157. // 5.4 While the next input code point is a digit, consume it and append it to repr.
  158. offset = findDecimalNumberEnd(source, offset + 1 + sign + 1);
  159. }
  160. }
  161. return offset;
  162. }
  163. // § 4.3.14. Consume the remnants of a bad url
  164. // ... its sole use is to consume enough of the input stream to reach a recovery point
  165. // where normal tokenizing can resume.
  166. export function consumeBadUrlRemnants(source, offset) {
  167. // Repeatedly consume the next input code point from the stream:
  168. for (; offset < source.length; offset++) {
  169. const code = source.charCodeAt(offset);
  170. // U+0029 RIGHT PARENTHESIS ())
  171. // EOF
  172. if (code === 0x0029) {
  173. // Return.
  174. offset++;
  175. break;
  176. }
  177. if (isValidEscape(code, getCharCode(source, offset + 1))) {
  178. // Consume an escaped code point.
  179. // Note: This allows an escaped right parenthesis ("\)") to be encountered
  180. // without ending the <bad-url-token>. This is otherwise identical to
  181. // the "anything else" clause.
  182. offset = consumeEscaped(source, offset);
  183. }
  184. }
  185. return offset;
  186. }
  187. // § 4.3.7. Consume an escaped code point
  188. // Note: This algorithm assumes that escaped is valid without leading U+005C REVERSE SOLIDUS (\)
  189. export function decodeEscaped(escaped) {
  190. // Single char escaped that's not a hex digit
  191. if (escaped.length === 1 && !isHexDigit(escaped.charCodeAt(0))) {
  192. return escaped[0];
  193. }
  194. // Interpret the hex digits as a hexadecimal number.
  195. let code = parseInt(escaped, 16);
  196. if (
  197. (code === 0) || // If this number is zero,
  198. (code >= 0xD800 && code <= 0xDFFF) || // or is for a surrogate,
  199. (code > 0x10FFFF) // or is greater than the maximum allowed code point
  200. ) {
  201. // ... return U+FFFD REPLACEMENT CHARACTER
  202. code = 0xFFFD;
  203. }
  204. // Otherwise, return the code point with that value.
  205. return String.fromCodePoint(code);
  206. }