Feature.cjs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. 'use strict';
  2. const types = require('../../tokenizer/types.cjs');
  3. const SOLIDUS = 0x002F; // U+002F SOLIDUS (/)
  4. const name = 'Feature';
  5. const structure = {
  6. kind: String,
  7. name: String,
  8. value: ['Identifier', 'Number', 'Dimension', 'Ratio', 'Function', null]
  9. };
  10. function parse(kind) {
  11. const start = this.tokenStart;
  12. let name;
  13. let value = null;
  14. this.eat(types.LeftParenthesis);
  15. this.skipSC();
  16. name = this.consume(types.Ident);
  17. this.skipSC();
  18. if (this.tokenType !== types.RightParenthesis) {
  19. this.eat(types.Colon);
  20. this.skipSC();
  21. switch (this.tokenType) {
  22. case types.Number:
  23. if (this.lookupNonWSType(1) === types.Delim) {
  24. value = this.Ratio();
  25. } else {
  26. value = this.Number();
  27. }
  28. break;
  29. case types.Dimension:
  30. value = this.Dimension();
  31. break;
  32. case types.Ident:
  33. value = this.Identifier();
  34. break;
  35. case types.Function:
  36. value = 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. break;
  50. default:
  51. this.error('Number, dimension, ratio or identifier is expected');
  52. }
  53. this.skipSC();
  54. }
  55. if (!this.eof) {
  56. this.eat(types.RightParenthesis);
  57. }
  58. return {
  59. type: 'Feature',
  60. loc: this.getLocation(start, this.tokenStart),
  61. kind,
  62. name,
  63. value
  64. };
  65. }
  66. function generate(node) {
  67. this.token(types.LeftParenthesis, '(');
  68. this.token(types.Ident, node.name);
  69. if (node.value !== null) {
  70. this.token(types.Colon, ':');
  71. this.node(node.value);
  72. }
  73. this.token(types.RightParenthesis, ')');
  74. }
  75. exports.generate = generate;
  76. exports.name = name;
  77. exports.parse = parse;
  78. exports.structure = structure;