token-before.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import {
  2. WhiteSpace,
  3. Delim,
  4. Ident,
  5. Function as FunctionToken,
  6. Url,
  7. BadUrl,
  8. AtKeyword,
  9. Hash,
  10. Percentage,
  11. Dimension,
  12. Number as NumberToken,
  13. String as StringToken,
  14. Colon,
  15. LeftParenthesis,
  16. RightParenthesis,
  17. CDC
  18. } from '../tokenizer/index.js';
  19. const PLUSSIGN = 0x002B; // U+002B PLUS SIGN (+)
  20. const HYPHENMINUS = 0x002D; // U+002D HYPHEN-MINUS (-)
  21. const code = (type, value) => {
  22. if (type === Delim) {
  23. type = value;
  24. }
  25. if (typeof type === 'string') {
  26. const charCode = type.charCodeAt(0);
  27. return charCode > 0x7F ? 0x8000 : charCode << 8;
  28. }
  29. return type;
  30. };
  31. // https://www.w3.org/TR/css-syntax-3/#serialization
  32. // The only requirement for serialization is that it must "round-trip" with parsing,
  33. // that is, parsing the stylesheet must produce the same data structures as parsing,
  34. // serializing, and parsing again, except for consecutive <whitespace-token>s,
  35. // which may be collapsed into a single token.
  36. const specPairs = [
  37. [Ident, Ident],
  38. [Ident, FunctionToken],
  39. [Ident, Url],
  40. [Ident, BadUrl],
  41. [Ident, '-'],
  42. [Ident, NumberToken],
  43. [Ident, Percentage],
  44. [Ident, Dimension],
  45. [Ident, CDC],
  46. [Ident, LeftParenthesis],
  47. [AtKeyword, Ident],
  48. [AtKeyword, FunctionToken],
  49. [AtKeyword, Url],
  50. [AtKeyword, BadUrl],
  51. [AtKeyword, '-'],
  52. [AtKeyword, NumberToken],
  53. [AtKeyword, Percentage],
  54. [AtKeyword, Dimension],
  55. [AtKeyword, CDC],
  56. [Hash, Ident],
  57. [Hash, FunctionToken],
  58. [Hash, Url],
  59. [Hash, BadUrl],
  60. [Hash, '-'],
  61. [Hash, NumberToken],
  62. [Hash, Percentage],
  63. [Hash, Dimension],
  64. [Hash, CDC],
  65. [Dimension, Ident],
  66. [Dimension, FunctionToken],
  67. [Dimension, Url],
  68. [Dimension, BadUrl],
  69. [Dimension, '-'],
  70. [Dimension, NumberToken],
  71. [Dimension, Percentage],
  72. [Dimension, Dimension],
  73. [Dimension, CDC],
  74. ['#', Ident],
  75. ['#', FunctionToken],
  76. ['#', Url],
  77. ['#', BadUrl],
  78. ['#', '-'],
  79. ['#', NumberToken],
  80. ['#', Percentage],
  81. ['#', Dimension],
  82. ['#', CDC], // https://github.com/w3c/csswg-drafts/pull/6874
  83. ['-', Ident],
  84. ['-', FunctionToken],
  85. ['-', Url],
  86. ['-', BadUrl],
  87. ['-', '-'],
  88. ['-', NumberToken],
  89. ['-', Percentage],
  90. ['-', Dimension],
  91. ['-', CDC], // https://github.com/w3c/csswg-drafts/pull/6874
  92. [NumberToken, Ident],
  93. [NumberToken, FunctionToken],
  94. [NumberToken, Url],
  95. [NumberToken, BadUrl],
  96. [NumberToken, NumberToken],
  97. [NumberToken, Percentage],
  98. [NumberToken, Dimension],
  99. [NumberToken, '%'],
  100. [NumberToken, CDC], // https://github.com/w3c/csswg-drafts/pull/6874
  101. ['@', Ident],
  102. ['@', FunctionToken],
  103. ['@', Url],
  104. ['@', BadUrl],
  105. ['@', '-'],
  106. ['@', CDC], // https://github.com/w3c/csswg-drafts/pull/6874
  107. ['.', NumberToken],
  108. ['.', Percentage],
  109. ['.', Dimension],
  110. ['+', NumberToken],
  111. ['+', Percentage],
  112. ['+', Dimension],
  113. ['/', '*']
  114. ];
  115. // validate with scripts/generate-safe
  116. const safePairs = specPairs.concat([
  117. [Ident, Hash],
  118. [Dimension, Hash],
  119. [Hash, Hash],
  120. [AtKeyword, LeftParenthesis],
  121. [AtKeyword, StringToken],
  122. [AtKeyword, Colon],
  123. [Percentage, Percentage],
  124. [Percentage, Dimension],
  125. [Percentage, FunctionToken],
  126. [Percentage, '-'],
  127. [RightParenthesis, Ident],
  128. [RightParenthesis, FunctionToken],
  129. [RightParenthesis, Percentage],
  130. [RightParenthesis, Dimension],
  131. [RightParenthesis, Hash],
  132. [RightParenthesis, '-']
  133. ]);
  134. function createMap(pairs) {
  135. const isWhiteSpaceRequired = new Set(
  136. pairs.map(([prev, next]) => (code(prev) << 16 | code(next)))
  137. );
  138. return function(prevCode, type, value) {
  139. const nextCode = code(type, value);
  140. const nextCharCode = value.charCodeAt(0);
  141. const emitWs =
  142. (nextCharCode === HYPHENMINUS &&
  143. type !== Ident &&
  144. type !== FunctionToken &&
  145. type !== CDC) ||
  146. (nextCharCode === PLUSSIGN)
  147. ? isWhiteSpaceRequired.has(prevCode << 16 | nextCharCode << 8)
  148. : isWhiteSpaceRequired.has(prevCode << 16 | nextCode);
  149. if (emitWs) {
  150. this.emit(' ', WhiteSpace, true);
  151. }
  152. return nextCode;
  153. };
  154. }
  155. export const spec = createMap(specPairs);
  156. export const safe = createMap(safePairs);