MediaQuery.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import {
  2. Comma,
  3. EOF,
  4. Ident,
  5. LeftCurlyBracket,
  6. LeftParenthesis,
  7. Function as FunctionToken,
  8. Semicolon
  9. } from '../../tokenizer/index.js';
  10. export const name = 'MediaQuery';
  11. export const structure = {
  12. modifier: [String, null],
  13. mediaType: [String, null],
  14. condition: ['Condition', null]
  15. };
  16. export function parse() {
  17. const start = this.tokenStart;
  18. let modifier = null;
  19. let mediaType = null;
  20. let condition = null;
  21. this.skipSC();
  22. if (this.tokenType === Ident && this.lookupTypeNonSC(1) !== LeftParenthesis) {
  23. // [ not | only ]? <media-type>
  24. const ident = this.consume(Ident);
  25. const identLowerCase = ident.toLowerCase();
  26. if (identLowerCase === 'not' || identLowerCase === 'only') {
  27. this.skipSC();
  28. modifier = identLowerCase;
  29. mediaType = this.consume(Ident);
  30. } else {
  31. mediaType = ident;
  32. }
  33. switch (this.lookupTypeNonSC(0)) {
  34. case Ident: {
  35. // and <media-condition-without-or>
  36. this.skipSC();
  37. this.eatIdent('and');
  38. condition = this.Condition('media');
  39. break;
  40. }
  41. case LeftCurlyBracket:
  42. case Semicolon:
  43. case Comma:
  44. case EOF:
  45. break;
  46. default:
  47. this.error('Identifier or parenthesis is expected');
  48. }
  49. } else {
  50. switch (this.tokenType) {
  51. case Ident:
  52. case LeftParenthesis:
  53. case FunctionToken: {
  54. // <media-condition>
  55. condition = this.Condition('media');
  56. break;
  57. }
  58. case LeftCurlyBracket:
  59. case Semicolon:
  60. case EOF:
  61. break;
  62. default:
  63. this.error('Identifier or parenthesis is expected');
  64. }
  65. }
  66. return {
  67. type: 'MediaQuery',
  68. loc: this.getLocation(start, this.tokenStart),
  69. modifier,
  70. mediaType,
  71. condition
  72. };
  73. }
  74. export function generate(node) {
  75. if (node.mediaType) {
  76. if (node.modifier) {
  77. this.token(Ident, node.modifier);
  78. }
  79. this.token(Ident, node.mediaType);
  80. if (node.condition) {
  81. this.token(Ident, 'and');
  82. this.node(node.condition);
  83. }
  84. } else if (node.condition) {
  85. this.node(node.condition);
  86. }
  87. }