MediaCondition.cjs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. 'use strict';
  2. const types = require('../../tokenizer/types.cjs');
  3. const MediaFeatureToken = new Set([types.Colon, types.RightParenthesis, types.EOF]);
  4. const name = 'MediaCondition';
  5. const structure = {
  6. children: [[
  7. 'Identifier',
  8. 'MediaFeature',
  9. 'MediaFeatureRange'
  10. ]]
  11. };
  12. function parse() {
  13. const children = this.createList();
  14. scan: while (!this.eof) {
  15. switch (this.tokenType) {
  16. case types.Comment:
  17. case types.WhiteSpace:
  18. this.next();
  19. continue;
  20. case types.Ident:
  21. children.push(this.Identifier());
  22. break;
  23. case types.LeftParenthesis:
  24. if (this.lookupTypeNonSC(1) === types.Ident && MediaFeatureToken.has(this.lookupTypeNonSC(2))) {
  25. children.push(this.MediaFeature());
  26. } else if (this.lookupTypeNonSC(1) === types.LeftParenthesis || this.lookupTypeNonSC(2) === types.LeftParenthesis) {
  27. this.next();
  28. children.push(this.MediaCondition());
  29. this.eat(types.RightParenthesis);
  30. } else {
  31. children.push(this.MediaFeatureRange());
  32. }
  33. break;
  34. default:
  35. break scan;
  36. }
  37. }
  38. return {
  39. type: 'MediaCondition',
  40. loc: this.getLocationFromList(children),
  41. children
  42. };
  43. }
  44. function generate(node) {
  45. node.children.forEach(child => {
  46. if (child.type === 'MediaCondition') {
  47. this.token(types.LeftParenthesis, '(');
  48. this.node(child);
  49. this.token(types.RightParenthesis, ')');
  50. } else {
  51. this.node(child);
  52. }
  53. });
  54. }
  55. exports.generate = generate;
  56. exports.name = name;
  57. exports.parse = parse;
  58. exports.structure = structure;