MediaQuery.cjs 2.5 KB

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