generic-urange.js 4.5 KB

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