generic-urange.js 4.6 KB

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