utils.js 8.3 KB

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