MediaFeature.js 2.0 KB

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