SupportsFeature.cjs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. 'use strict';
  2. const types = require('../../tokenizer/types.cjs');
  3. const name = 'SupportsDeclaration';
  4. const structure = {
  5. feature: String,
  6. value: 'Declaration'
  7. };
  8. function parse() {
  9. const start = this.tokenStart;
  10. let featureName = 'declaration';
  11. let valueParser = this.Declaration;
  12. if (this.tokenType === types.Function) {
  13. featureName = this.consumeFunctionName();
  14. valueParser = this.supportsFeature[featureName.toLowerCase()];
  15. if (!valueParser) {
  16. this.error(`Unknown supports feature ${featureName.toLowerCase()}()`);
  17. }
  18. } else {
  19. this.eat(types.LeftParenthesis);
  20. }
  21. this.skipSC();
  22. const value = this.parseWithFallback(
  23. () => {
  24. const startValueToken = this.tokenIndex;
  25. const value = valueParser.call(this);
  26. if (this.eof === false &&
  27. this.isBalanceEdge(startValueToken) === false) {
  28. this.error();
  29. }
  30. return value;
  31. },
  32. (startToken) => this.Raw(startToken, null, false)
  33. );
  34. if (!this.eof) {
  35. this.eat(types.RightParenthesis);
  36. }
  37. return {
  38. type: 'SupportsDeclaration',
  39. loc: this.getLocation(start, this.tokenStart),
  40. feature: featureName,
  41. value
  42. };
  43. }
  44. function generate(node) {
  45. if (node.feature !== 'declaration') {
  46. this.token(types.Function, node.feature + '(');
  47. } else {
  48. this.token(types.LeftParenthesis, '(');
  49. }
  50. this.node(node.value);
  51. this.token(types.RightParenthesis, ')');
  52. }
  53. exports.generate = generate;
  54. exports.name = name;
  55. exports.parse = parse;
  56. exports.structure = structure;