generic-urange.cjs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. 'use strict';
  2. const charCodeDefinitions = require('../tokenizer/char-code-definitions.cjs');
  3. const types = require('../tokenizer/types.cjs');
  4. const utils = require('../tokenizer/utils.cjs');
  5. const PLUSSIGN = 0x002B; // U+002B PLUS SIGN (+)
  6. const HYPHENMINUS = 0x002D; // U+002D HYPHEN-MINUS (-)
  7. const QUESTIONMARK = 0x003F; // U+003F QUESTION MARK (?)
  8. const U = 0x0075; // U+0075 LATIN SMALL LETTER U (u)
  9. function isDelim(token, code) {
  10. return token !== null && token.type === types.Delim && token.value.charCodeAt(0) === code;
  11. }
  12. function startsWith(token, code) {
  13. return token.value.charCodeAt(0) === code;
  14. }
  15. function hexSequence(token, offset, allowDash) {
  16. let hexlen = 0;
  17. for (let pos = offset; pos < token.value.length; pos++) {
  18. const code = token.value.charCodeAt(pos);
  19. if (code === HYPHENMINUS && allowDash && hexlen !== 0) {
  20. hexSequence(token, offset + hexlen + 1, false);
  21. return 6; // dissallow following question marks
  22. }
  23. if (!charCodeDefinitions.isHexDigit(code)) {
  24. return 0; // not a hex digit
  25. }
  26. if (++hexlen > 6) {
  27. return 0; // too many hex digits
  28. } }
  29. return hexlen;
  30. }
  31. function withQuestionMarkSequence(consumed, length, getNextToken) {
  32. if (!consumed) {
  33. return 0; // nothing consumed
  34. }
  35. while (isDelim(getNextToken(length), QUESTIONMARK)) {
  36. if (++consumed > 6) {
  37. return 0; // too many question marks
  38. }
  39. length++;
  40. }
  41. return length;
  42. }
  43. // https://drafts.csswg.org/css-syntax/#urange
  44. // Informally, the <urange> production has three forms:
  45. // U+0001
  46. // Defines a range consisting of a single code point, in this case the code point "1".
  47. // U+0001-00ff
  48. // Defines a range of codepoints between the first and the second value, in this case
  49. // the range between "1" and "ff" (255 in decimal) inclusive.
  50. // U+00??
  51. // Defines a range of codepoints where the "?" characters range over all hex digits,
  52. // in this case defining the same as the value U+0000-00ff.
  53. // In each form, a maximum of 6 digits is allowed for each hexadecimal number (if you treat "?" as a hexadecimal digit).
  54. //
  55. // <urange> =
  56. // u '+' <ident-token> '?'* |
  57. // u <dimension-token> '?'* |
  58. // u <number-token> '?'* |
  59. // u <number-token> <dimension-token> |
  60. // u <number-token> <number-token> |
  61. // u '+' '?'+
  62. function urange(token, getNextToken) {
  63. let length = 0;
  64. // should start with `u` or `U`
  65. if (token === null || token.type !== types.Ident || !utils.cmpChar(token.value, 0, U)) {
  66. return 0;
  67. }
  68. token = getNextToken(++length);
  69. if (token === null) {
  70. return 0;
  71. }
  72. // u '+' <ident-token> '?'*
  73. // u '+' '?'+
  74. if (isDelim(token, PLUSSIGN)) {
  75. token = getNextToken(++length);
  76. if (token === null) {
  77. return 0;
  78. }
  79. if (token.type === types.Ident) {
  80. // u '+' <ident-token> '?'*
  81. return withQuestionMarkSequence(hexSequence(token, 0, true), ++length, getNextToken);
  82. }
  83. if (isDelim(token, QUESTIONMARK)) {
  84. // u '+' '?'+
  85. return withQuestionMarkSequence(1, ++length, getNextToken);
  86. }
  87. // Hex digit or question mark is expected
  88. return 0;
  89. }
  90. // u <number-token> '?'*
  91. // u <number-token> <dimension-token>
  92. // u <number-token> <number-token>
  93. if (token.type === types.Number) {
  94. const consumedHexLength = hexSequence(token, 1, true);
  95. if (consumedHexLength === 0) {
  96. return 0;
  97. }
  98. token = getNextToken(++length);
  99. if (token === null) {
  100. // u <number-token> <eof>
  101. return length;
  102. }
  103. if (token.type === types.Dimension || token.type === types.Number) {
  104. // u <number-token> <dimension-token>
  105. // u <number-token> <number-token>
  106. if (!startsWith(token, HYPHENMINUS) || !hexSequence(token, 1, false)) {
  107. return 0;
  108. }
  109. return length + 1;
  110. }
  111. // u <number-token> '?'*
  112. return withQuestionMarkSequence(consumedHexLength, length, getNextToken);
  113. }
  114. // u <dimension-token> '?'*
  115. if (token.type === types.Dimension) {
  116. return withQuestionMarkSequence(hexSequence(token, 1, true), ++length, getNextToken);
  117. }
  118. return 0;
  119. }
  120. module.exports = urange;