char-code-definitions.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. var TAB = 9;
  2. var N = 10;
  3. var F = 12;
  4. var R = 13;
  5. var SPACE = 32;
  6. var EOF = 0;
  7. // https://drafts.csswg.org/css-syntax-3/
  8. // § 4.2. Definitions
  9. // digit
  10. // A code point between U+0030 DIGIT ZERO (0) and U+0039 DIGIT NINE (9).
  11. function isDigit(code) {
  12. return code >= 0x0030 && code <= 0x0039;
  13. }
  14. // hex digit
  15. // A digit, or a code point between U+0041 LATIN CAPITAL LETTER A (A) and U+0046 LATIN CAPITAL LETTER F (F),
  16. // or a code point between U+0061 LATIN SMALL LETTER A (a) and U+0066 LATIN SMALL LETTER F (f).
  17. function isHexDigit(code) {
  18. return (
  19. isDigit(code) || // 0 .. 9
  20. (code >= 0x0041 && code <= 0x0046) || // A .. F
  21. (code >= 0x0061 && code <= 0x0066) // a .. f
  22. );
  23. }
  24. // uppercase letter
  25. // A code point between U+0041 LATIN CAPITAL LETTER A (A) and U+005A LATIN CAPITAL LETTER Z (Z).
  26. function isUppercaseLetter(code) {
  27. return code >= 0x0041 && code <= 0x005A;
  28. }
  29. // lowercase letter
  30. // A code point between U+0061 LATIN SMALL LETTER A (a) and U+007A LATIN SMALL LETTER Z (z).
  31. function isLowercaseLetter(code) {
  32. return code >= 0x0061 && code <= 0x007A;
  33. }
  34. // letter
  35. // An uppercase letter or a lowercase letter.
  36. function isLetter(code) {
  37. return isUppercaseLetter(code) || isLowercaseLetter(code);
  38. }
  39. // non-ASCII code point
  40. // A code point with a value equal to or greater than U+0080 <control>.
  41. function isNonAscii(code) {
  42. return code >= 0x0080;
  43. }
  44. // name-start code point
  45. // A letter, a non-ASCII code point, or U+005F LOW LINE (_).
  46. function isNameStart(code) {
  47. return isLetter(code) || isNonAscii(code) || code === 0x005F;
  48. }
  49. // name code point
  50. // A name-start code point, a digit, or U+002D HYPHEN-MINUS (-).
  51. function isName(code) {
  52. return isNameStart(code) || isDigit(code) || code === 0x002D;
  53. }
  54. // non-printable code point
  55. // A code point between U+0000 NULL and U+0008 BACKSPACE, or U+000B LINE TABULATION,
  56. // or a code point between U+000E SHIFT OUT and U+001F INFORMATION SEPARATOR ONE, or U+007F DELETE.
  57. function isNonPrintable(code) {
  58. return (
  59. (code >= 0x0000 && code <= 0x0008) ||
  60. (code === 0x000B) ||
  61. (code >= 0x000E && code <= 0x001F) ||
  62. (code === 0x007F)
  63. );
  64. }
  65. // newline
  66. // U+000A LINE FEED. Note that U+000D CARRIAGE RETURN and U+000C FORM FEED are not included in this definition,
  67. // as they are converted to U+000A LINE FEED during preprocessing.
  68. // TODO: we doesn't do a preprocessing, so check a code point for U+000D CARRIAGE RETURN and U+000C FORM FEED
  69. function isNewline(code) {
  70. return code === R || code === N || code === F;
  71. }
  72. // whitespace
  73. // A newline, U+0009 CHARACTER TABULATION, or U+0020 SPACE.
  74. function isWhiteSpace(code) {
  75. return isNewline(code) || code === SPACE || code === TAB;
  76. }
  77. // § 4.3.8. Check if two code points are a valid escape
  78. function isValidEscape(first, second) {
  79. // If the first code point is not U+005C REVERSE SOLIDUS (\), return false.
  80. if (first !== 0x005C) {
  81. return false;
  82. }
  83. // Otherwise, if the second code point is a newline or EOF, return false.
  84. if (isNewline(second) || second === EOF) {
  85. return false;
  86. }
  87. // Otherwise, return true.
  88. return true;
  89. }
  90. // § 4.3.9. Check if three code points would start an identifier
  91. function isIdentifierStart(first, second, third) {
  92. // Look at the first code point:
  93. // U+002D HYPHEN-MINUS
  94. if (first === 0x002D) {
  95. // If the second code point is a name-start code point or a U+002D HYPHEN-MINUS,
  96. // or the second and third code points are a valid escape, return true. Otherwise, return false.
  97. return (
  98. isNameStart(second) ||
  99. second === 0x002D ||
  100. isValidEscape(second, third)
  101. );
  102. }
  103. // name-start code point
  104. if (isNameStart(first)) {
  105. // Return true.
  106. return true;
  107. }
  108. // U+005C REVERSE SOLIDUS (\)
  109. if (first === 0x005C) {
  110. // If the first and second code points are a valid escape, return true. Otherwise, return false.
  111. return isValidEscape(first, second);
  112. }
  113. // anything else
  114. // Return false.
  115. return false;
  116. }
  117. // § 4.3.10. Check if three code points would start a number
  118. function isNumberStart(first, second, third) {
  119. // Look at the first code point:
  120. // U+002B PLUS SIGN (+)
  121. // U+002D HYPHEN-MINUS (-)
  122. if (first === 0x002B || first === 0x002D) {
  123. // If the second code point is a digit, return true.
  124. if (isDigit(second)) {
  125. return 2;
  126. }
  127. // Otherwise, if the second code point is a U+002E FULL STOP (.)
  128. // and the third code point is a digit, return true.
  129. // Otherwise, return false.
  130. return second === 0x002E && isDigit(third) ? 3 : 0;
  131. }
  132. // U+002E FULL STOP (.)
  133. if (first === 0x002E) {
  134. // If the second code point is a digit, return true. Otherwise, return false.
  135. return isDigit(second) ? 2 : 0;
  136. }
  137. // digit
  138. if (isDigit(first)) {
  139. // Return true.
  140. return 1;
  141. }
  142. // anything else
  143. // Return false.
  144. return 0;
  145. }
  146. module.exports = {
  147. isDigit: isDigit,
  148. isHexDigit: isHexDigit,
  149. isUppercaseLetter: isUppercaseLetter,
  150. isLowercaseLetter: isLowercaseLetter,
  151. isLetter: isLetter,
  152. isNonAscii: isNonAscii,
  153. isNameStart: isNameStart,
  154. isName: isName,
  155. isNonPrintable: isNonPrintable,
  156. isNewline: isNewline,
  157. isWhiteSpace: isWhiteSpace,
  158. isValidEscape: isValidEscape,
  159. isIdentifierStart: isIdentifierStart,
  160. isNumberStart: isNumberStart
  161. };