const.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. const charCodeDef = require('./char-code-definitions');
  2. function fillCodeTypes(array, check, type) {
  3. for (var i = 0; i < array.length; i++) {
  4. if (check(i)) {
  5. array[i] = type;
  6. }
  7. }
  8. }
  9. // CSS Syntax Module Level 3
  10. // https://www.w3.org/TR/css-syntax-3/
  11. var TYPE = {
  12. Ident: 1, // <ident-token>
  13. Function: 2, // <function-token>
  14. AtKeyword: 3, // <at-keyword-token>
  15. Hash: 4, // <hash-token>
  16. String: 5, // <string-token>
  17. BadString: 6, // <bad-string-token>
  18. Url: 7, // <url-token>
  19. BadUrl: 8, // <bad-url-token>
  20. Delim: 9, // <delim-token>
  21. Number: 10, // <number-token>
  22. Percentage: 11, // <percentage-token>
  23. Dimension: 12, // <dimension-token>
  24. WhiteSpace: 13, // <whitespace-token>
  25. CDO: 14, // <CDO-token>
  26. CDC: 15, // <CDC-token>
  27. Colon: 16, // <colon-token> :
  28. Semicolon: 17, // <semicolon-token> ;
  29. Comma: 18, // <comma-token> ,
  30. LeftSquareBracket: 19, // <[-token>
  31. RightSquareBracket: 20, // <]-token>
  32. LeftParenthesis: 21, // <(-token>
  33. RightParenthesis: 22, // <)-token>
  34. LeftCurlyBracket: 23, // <{-token>
  35. RightCurlyBracket: 24, // <}-token>
  36. Comment: 25
  37. };
  38. var NAME = Object.keys(TYPE).reduce(function(result, key) {
  39. result[TYPE[key]] = key;
  40. return result;
  41. }, {});
  42. var CHARCODE = {
  43. Tab: 9, // \t
  44. LineFeed: 10, // \n
  45. FormFeed: 12, // \f
  46. CarriageReturn: 13, // \r
  47. Space: 32,
  48. ExclamationMark: 33, // !
  49. QuotationMark: 34, // "
  50. NumberSign: 35, // #
  51. DollarSign: 36, // $
  52. PercentSign: 37, // %
  53. Ampersand: 38, // &
  54. Apostrophe: 39, // '
  55. LeftParenthesis: 40, // (
  56. RightParenthesis: 41, // )
  57. Asterisk: 42, // *
  58. PlusSign: 43, // +
  59. Comma: 44, // ,
  60. HyphenMinus: 45, // -
  61. FullStop: 46, // .
  62. Solidus: 47, // /
  63. Colon: 58, // :
  64. Semicolon: 59, // ;
  65. LessThanSign: 60, // <
  66. EqualsSign: 61, // =
  67. GreaterThanSign: 62, // >
  68. QuestionMark: 63, // ?
  69. CommercialAt: 64, // @
  70. LeftSquareBracket: 91, // [
  71. Backslash: 92, // \
  72. RightSquareBracket: 93, // ]
  73. CircumflexAccent: 94, // ^
  74. LowLine: 95, // _
  75. GraveAccent: 96, // `
  76. LeftCurlyBracket: 123, // {
  77. VerticalLine: 124, // |
  78. RightCurlyBracket: 125, // }
  79. Tilde: 126 // ~
  80. };
  81. var INPUT_STREAM_CODE_TYPE = {
  82. Eof: 0x80,
  83. Delim: 0x81,
  84. WhiteSpace: 0x82,
  85. Digit: 0x83,
  86. NameStart: 0x84,
  87. NonPrintable: 0x85,
  88. Newline: 0x86
  89. };
  90. // https://drafts.csswg.org/css-syntax/#tokenizer-definitions
  91. // > non-ASCII code point
  92. // > A code point with a value equal to or greater than U+0080 <control>
  93. // > name-start code point
  94. // > A letter, a non-ASCII code point, or U+005F LOW LINE (_).
  95. // > name code point
  96. // > A name-start code point, a digit, or U+002D HYPHEN-MINUS (-)
  97. // That means only ASCII code points has a special meaning and we define a maps for 0..127 codes only
  98. var SafeUint32Array = typeof Uint32Array !== 'undefined' ? Uint32Array : Array; // fallback on Array when TypedArray is not supported
  99. var INPUT_STREAM_CODE = new SafeUint32Array(0x80);
  100. var INPUT_STREAM_CODE_STRING = new SafeUint32Array(0x80);
  101. var INPUT_STREAM_CODE_URL = new SafeUint32Array(0x80);
  102. for (var i = 0; i < INPUT_STREAM_CODE.length; i++) {
  103. INPUT_STREAM_CODE_STRING[i] =
  104. INPUT_STREAM_CODE_URL[i] =
  105. INPUT_STREAM_CODE[i] = i || INPUT_STREAM_CODE_TYPE.Eof;
  106. }
  107. fillCodeTypes(INPUT_STREAM_CODE, charCodeDef.isWhiteSpace, INPUT_STREAM_CODE_TYPE.WhiteSpace);
  108. fillCodeTypes(INPUT_STREAM_CODE, charCodeDef.isDigit, INPUT_STREAM_CODE_TYPE.Digit);
  109. fillCodeTypes(INPUT_STREAM_CODE, charCodeDef.isNameStart, INPUT_STREAM_CODE_TYPE.NameStart);
  110. fillCodeTypes(INPUT_STREAM_CODE_STRING, charCodeDef.isNewline, INPUT_STREAM_CODE_TYPE.Newline);
  111. fillCodeTypes(INPUT_STREAM_CODE_URL, charCodeDef.isWhiteSpace, INPUT_STREAM_CODE_TYPE.WhiteSpace);
  112. fillCodeTypes(INPUT_STREAM_CODE_URL, charCodeDef.isNonPrintable, INPUT_STREAM_CODE_TYPE.NonPrintable);
  113. module.exports = {
  114. TYPE: TYPE,
  115. NAME: NAME,
  116. CHARCODE: CHARCODE,
  117. INPUT_STREAM_CODE: INPUT_STREAM_CODE,
  118. INPUT_STREAM_CODE_STRING: INPUT_STREAM_CODE_STRING,
  119. INPUT_STREAM_CODE_URL: INPUT_STREAM_CODE_URL,
  120. INPUT_STREAM_CODE_TYPE: INPUT_STREAM_CODE_TYPE
  121. };