UnicodeRange.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import {
  2. isHexDigit,
  3. Ident,
  4. Number,
  5. Dimension
  6. } from '../../tokenizer/index.js';
  7. const PLUSSIGN = 0x002B; // U+002B PLUS SIGN (+)
  8. const HYPHENMINUS = 0x002D; // U+002D HYPHEN-MINUS (-)
  9. const QUESTIONMARK = 0x003F; // U+003F QUESTION MARK (?)
  10. function eatHexSequence(offset, allowDash) {
  11. let len = 0;
  12. for (let pos = this.tokenStart + offset; pos < this.tokenEnd; pos++) {
  13. const code = this.charCodeAt(pos);
  14. if (code === HYPHENMINUS && allowDash && len !== 0) {
  15. eatHexSequence.call(this, offset + len + 1, false);
  16. return -1;
  17. }
  18. if (!isHexDigit(code)) {
  19. this.error(
  20. allowDash && len !== 0
  21. ? 'Hyphen minus' + (len < 6 ? ' or hex digit' : '') + ' is expected'
  22. : (len < 6 ? 'Hex digit is expected' : 'Unexpected input'),
  23. pos
  24. );
  25. }
  26. if (++len > 6) {
  27. this.error('Too many hex digits', pos);
  28. };
  29. }
  30. this.next();
  31. return len;
  32. }
  33. function eatQuestionMarkSequence(max) {
  34. let count = 0;
  35. while (this.isDelim(QUESTIONMARK)) {
  36. if (++count > max) {
  37. this.error('Too many question marks');
  38. }
  39. this.next();
  40. }
  41. }
  42. function startsWith(code) {
  43. if (this.charCodeAt(this.tokenStart) !== code) {
  44. this.error((code === PLUSSIGN ? 'Plus sign' : 'Hyphen minus') + ' is expected');
  45. }
  46. }
  47. // https://drafts.csswg.org/css-syntax/#urange
  48. // Informally, the <urange> production has three forms:
  49. // U+0001
  50. // Defines a range consisting of a single code point, in this case the code point "1".
  51. // U+0001-00ff
  52. // Defines a range of codepoints between the first and the second value, in this case
  53. // the range between "1" and "ff" (255 in decimal) inclusive.
  54. // U+00??
  55. // Defines a range of codepoints where the "?" characters range over all hex digits,
  56. // in this case defining the same as the value U+0000-00ff.
  57. // In each form, a maximum of 6 digits is allowed for each hexadecimal number (if you treat "?" as a hexadecimal digit).
  58. //
  59. // <urange> =
  60. // u '+' <ident-token> '?'* |
  61. // u <dimension-token> '?'* |
  62. // u <number-token> '?'* |
  63. // u <number-token> <dimension-token> |
  64. // u <number-token> <number-token> |
  65. // u '+' '?'+
  66. function scanUnicodeRange() {
  67. let hexLength = 0;
  68. switch (this.tokenType) {
  69. case Number:
  70. // u <number-token> '?'*
  71. // u <number-token> <dimension-token>
  72. // u <number-token> <number-token>
  73. hexLength = eatHexSequence.call(this, 1, true);
  74. if (this.isDelim(QUESTIONMARK)) {
  75. eatQuestionMarkSequence.call(this, 6 - hexLength);
  76. break;
  77. }
  78. if (this.tokenType === Dimension ||
  79. this.tokenType === Number) {
  80. startsWith.call(this, HYPHENMINUS);
  81. eatHexSequence.call(this, 1, false);
  82. break;
  83. }
  84. break;
  85. case Dimension:
  86. // u <dimension-token> '?'*
  87. hexLength = eatHexSequence.call(this, 1, true);
  88. if (hexLength > 0) {
  89. eatQuestionMarkSequence.call(this, 6 - hexLength);
  90. }
  91. break;
  92. default:
  93. // u '+' <ident-token> '?'*
  94. // u '+' '?'+
  95. this.eatDelim(PLUSSIGN);
  96. if (this.tokenType === Ident) {
  97. hexLength = eatHexSequence.call(this, 0, true);
  98. if (hexLength > 0) {
  99. eatQuestionMarkSequence.call(this, 6 - hexLength);
  100. }
  101. break;
  102. }
  103. if (this.isDelim(QUESTIONMARK)) {
  104. this.next();
  105. eatQuestionMarkSequence.call(this, 5);
  106. break;
  107. }
  108. this.error('Hex digit or question mark is expected');
  109. }
  110. }
  111. export const name = 'UnicodeRange';
  112. export const structure = {
  113. value: String
  114. };
  115. export function parse() {
  116. const start = this.tokenStart;
  117. // U or u
  118. this.eatIdent('u');
  119. scanUnicodeRange.call(this);
  120. return {
  121. type: 'UnicodeRange',
  122. loc: this.getLocation(start, this.tokenStart),
  123. value: this.substrToCursor(start)
  124. };
  125. }
  126. export function generate(node) {
  127. this.tokenize(node.value);
  128. }