You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

173 lines
4.9 KiB

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