utils.cjs 8.2 KB

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