FeatureRange.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import {
  2. Ident,
  3. Number,
  4. Dimension,
  5. Function as FunctionToken,
  6. LeftParenthesis,
  7. RightParenthesis
  8. } from '../../tokenizer/index.js';
  9. const SOLIDUS = 0x002F; // U+002F SOLIDUS (/)
  10. const LESSTHANSIGN = 0x003C; // U+003C LESS-THAN SIGN (<)
  11. const EQUALSSIGN = 0x003D; // U+003D EQUALS SIGN (=)
  12. const GREATERTHANSIGN = 0x003E; // U+003E GREATER-THAN SIGN (>)
  13. export const name = 'FeatureRange';
  14. export const structure = {
  15. kind: String,
  16. left: ['Identifier', 'Number', 'Dimension', 'Ratio', 'Function'],
  17. leftComparison: String,
  18. middle: ['Identifier', 'Number', 'Dimension', 'Ratio', 'Function'],
  19. rightComparison: [String, null],
  20. right: ['Identifier', 'Number', 'Dimension', 'Ratio', 'Function', null]
  21. };
  22. function readTerm() {
  23. this.skipSC();
  24. switch (this.tokenType) {
  25. case Number:
  26. if (this.isDelim(SOLIDUS, this.lookupOffsetNonSC(1))) {
  27. return this.Ratio();
  28. } else {
  29. return this.Number();
  30. }
  31. case Dimension:
  32. return this.Dimension();
  33. case Ident:
  34. return this.Identifier();
  35. case FunctionToken:
  36. return this.parseWithFallback(
  37. () => {
  38. const res = this.Function(this.readSequence, this.scope.Value);
  39. this.skipSC();
  40. if (this.isDelim(SOLIDUS)) {
  41. this.error();
  42. }
  43. return res;
  44. },
  45. () => {
  46. return this.Ratio();
  47. }
  48. );
  49. default:
  50. this.error('Number, dimension, ratio or identifier is expected');
  51. }
  52. }
  53. function readComparison(expectColon) {
  54. this.skipSC();
  55. if (this.isDelim(LESSTHANSIGN) ||
  56. this.isDelim(GREATERTHANSIGN)) {
  57. const value = this.source[this.tokenStart];
  58. this.next();
  59. if (this.isDelim(EQUALSSIGN)) {
  60. this.next();
  61. return value + '=';
  62. }
  63. return value;
  64. }
  65. if (this.isDelim(EQUALSSIGN)) {
  66. return '=';
  67. }
  68. this.error(`Expected ${expectColon ? '":", ' : ''}"<", ">", "=" or ")"`);
  69. }
  70. export function parse(kind = 'unknown') {
  71. const start = this.tokenStart;
  72. this.skipSC();
  73. this.eat(LeftParenthesis);
  74. const left = readTerm.call(this);
  75. const leftComparison = readComparison.call(this, left.type === 'Identifier');
  76. const middle = readTerm.call(this);
  77. let rightComparison = null;
  78. let right = null;
  79. if (this.lookupNonWSType(0) !== RightParenthesis) {
  80. rightComparison = readComparison.call(this);
  81. right = readTerm.call(this);
  82. }
  83. this.skipSC();
  84. this.eat(RightParenthesis);
  85. return {
  86. type: 'FeatureRange',
  87. loc: this.getLocation(start, this.tokenStart),
  88. kind,
  89. left,
  90. leftComparison,
  91. middle,
  92. rightComparison,
  93. right
  94. };
  95. }
  96. export function generate(node) {
  97. this.token(LeftParenthesis, '(');
  98. this.node(node.left);
  99. this.tokenize(node.leftComparison);
  100. this.node(node.middle);
  101. if (node.right) {
  102. this.tokenize(node.rightComparison);
  103. this.node(node.right);
  104. }
  105. this.token(RightParenthesis, ')');
  106. }