UnicodeRange.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. var isHexDigit = require('../../tokenizer').isHexDigit;
  2. var cmpChar = require('../../tokenizer').cmpChar;
  3. var TYPE = require('../../tokenizer').TYPE;
  4. var NAME = require('../../tokenizer').NAME;
  5. var CHARCODE = require('../../tokenizer').CHARCODE;
  6. var IDENT = TYPE.Ident;
  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 eatHexSequence(offset, allowDash) {
  14. for (var pos = this.scanner.tokenStart + offset, len = 0; pos < this.scanner.tokenEnd; pos++) {
  15. var code = this.scanner.source.charCodeAt(pos);
  16. if (code === HYPHENMINUS && allowDash && len !== 0) {
  17. if (eatHexSequence.call(this, offset + len + 1, false) === 0) {
  18. this.error();
  19. }
  20. return -1;
  21. }
  22. if (!isHexDigit(code)) {
  23. this.error(
  24. allowDash && len !== 0
  25. ? 'HyphenMinus' + (len < 6 ? ' or hex digit' : '') + ' is expected'
  26. : (len < 6 ? 'Hex digit is expected' : 'Unexpected input'),
  27. pos
  28. );
  29. }
  30. if (++len > 6) {
  31. this.error('Too many hex digits', pos);
  32. };
  33. }
  34. this.scanner.next();
  35. return len;
  36. }
  37. function eatQuestionMarkSequence(max) {
  38. var count = 0;
  39. while (this.scanner.isDelim(QUESTIONMARK)) {
  40. if (++count > max) {
  41. this.error('Too many question marks');
  42. }
  43. this.scanner.next();
  44. }
  45. }
  46. function startsWith(code) {
  47. if (this.scanner.source.charCodeAt(this.scanner.tokenStart) !== code) {
  48. this.error(NAME[code] + ' is expected');
  49. }
  50. }
  51. // https://drafts.csswg.org/css-syntax/#urange
  52. // Informally, the <urange> production has three forms:
  53. // U+0001
  54. // Defines a range consisting of a single code point, in this case the code point "1".
  55. // U+0001-00ff
  56. // Defines a range of codepoints between the first and the second value, in this case
  57. // the range between "1" and "ff" (255 in decimal) inclusive.
  58. // U+00??
  59. // Defines a range of codepoints where the "?" characters range over all hex digits,
  60. // in this case defining the same as the value U+0000-00ff.
  61. // In each form, a maximum of 6 digits is allowed for each hexadecimal number (if you treat "?" as a hexadecimal digit).
  62. //
  63. // <urange> =
  64. // u '+' <ident-token> '?'* |
  65. // u <dimension-token> '?'* |
  66. // u <number-token> '?'* |
  67. // u <number-token> <dimension-token> |
  68. // u <number-token> <number-token> |
  69. // u '+' '?'+
  70. function scanUnicodeRange() {
  71. var hexLength = 0;
  72. // u '+' <ident-token> '?'*
  73. // u '+' '?'+
  74. if (this.scanner.isDelim(PLUSSIGN)) {
  75. this.scanner.next();
  76. if (this.scanner.tokenType === IDENT) {
  77. hexLength = eatHexSequence.call(this, 0, true);
  78. if (hexLength > 0) {
  79. eatQuestionMarkSequence.call(this, 6 - hexLength);
  80. }
  81. return;
  82. }
  83. if (this.scanner.isDelim(QUESTIONMARK)) {
  84. this.scanner.next();
  85. eatQuestionMarkSequence.call(this, 5);
  86. return;
  87. }
  88. this.error('Hex digit or question mark is expected');
  89. return;
  90. }
  91. // u <number-token> '?'*
  92. // u <number-token> <dimension-token>
  93. // u <number-token> <number-token>
  94. if (this.scanner.tokenType === NUMBER) {
  95. startsWith.call(this, PLUSSIGN);
  96. hexLength = eatHexSequence.call(this, 1, true);
  97. if (this.scanner.isDelim(QUESTIONMARK)) {
  98. eatQuestionMarkSequence.call(this, 6 - hexLength);
  99. return;
  100. }
  101. if (this.scanner.tokenType === DIMENSION ||
  102. this.scanner.tokenType === NUMBER) {
  103. startsWith.call(this, HYPHENMINUS);
  104. eatHexSequence.call(this, 1, false);
  105. return;
  106. }
  107. return;
  108. }
  109. // u <dimension-token> '?'*
  110. if (this.scanner.tokenType === DIMENSION) {
  111. startsWith.call(this, PLUSSIGN);
  112. hexLength = eatHexSequence.call(this, 1, true);
  113. if (hexLength > 0) {
  114. eatQuestionMarkSequence.call(this, 6 - hexLength);
  115. }
  116. return;
  117. }
  118. this.error();
  119. }
  120. module.exports = {
  121. name: 'UnicodeRange',
  122. structure: {
  123. value: String
  124. },
  125. parse: function() {
  126. var start = this.scanner.tokenStart;
  127. // U or u
  128. if (!cmpChar(this.scanner.source, start, U)) {
  129. this.error('U is expected');
  130. }
  131. if (!cmpChar(this.scanner.source, start + 1, PLUSSIGN)) {
  132. this.error('Plus sign is expected');
  133. }
  134. this.scanner.next();
  135. scanUnicodeRange.call(this);
  136. return {
  137. type: 'UnicodeRange',
  138. loc: this.getLocation(start, this.scanner.tokenStart),
  139. value: this.scanner.substrToCursor(start)
  140. };
  141. },
  142. generate: function(node) {
  143. this.chunk(node.value);
  144. }
  145. };