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.

76 lines
1.9 KiB

4 years ago
  1. var TYPE = require('../../tokenizer').TYPE;
  2. var IDENT = TYPE.Ident;
  3. var NUMBER = TYPE.Number;
  4. var DIMENSION = TYPE.Dimension;
  5. var LEFTPARENTHESIS = TYPE.LeftParenthesis;
  6. var RIGHTPARENTHESIS = TYPE.RightParenthesis;
  7. var COLON = TYPE.Colon;
  8. var DELIM = TYPE.Delim;
  9. module.exports = {
  10. name: 'MediaFeature',
  11. structure: {
  12. name: String,
  13. value: ['Identifier', 'Number', 'Dimension', 'Ratio', null]
  14. },
  15. parse: function() {
  16. var start = this.scanner.tokenStart;
  17. var name;
  18. var value = null;
  19. this.eat(LEFTPARENTHESIS);
  20. this.scanner.skipSC();
  21. name = this.consume(IDENT);
  22. this.scanner.skipSC();
  23. if (this.scanner.tokenType !== RIGHTPARENTHESIS) {
  24. this.eat(COLON);
  25. this.scanner.skipSC();
  26. switch (this.scanner.tokenType) {
  27. case NUMBER:
  28. if (this.lookupNonWSType(1) === DELIM) {
  29. value = this.Ratio();
  30. } else {
  31. value = this.Number();
  32. }
  33. break;
  34. case DIMENSION:
  35. value = this.Dimension();
  36. break;
  37. case IDENT:
  38. value = this.Identifier();
  39. break;
  40. default:
  41. this.error('Number, dimension, ratio or identifier is expected');
  42. }
  43. this.scanner.skipSC();
  44. }
  45. this.eat(RIGHTPARENTHESIS);
  46. return {
  47. type: 'MediaFeature',
  48. loc: this.getLocation(start, this.scanner.tokenStart),
  49. name: name,
  50. value: value
  51. };
  52. },
  53. generate: function(node) {
  54. this.chunk('(');
  55. this.chunk(node.name);
  56. if (node.value !== null) {
  57. this.chunk(':');
  58. this.node(node.value);
  59. }
  60. this.chunk(')');
  61. }
  62. };