import.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import {
  2. String as StringToken,
  3. Ident,
  4. Url,
  5. Function as FunctionToken,
  6. LeftParenthesis,
  7. RightParenthesis
  8. } from '../../tokenizer/index.js';
  9. function parseWithFallback(parse, fallback) {
  10. return this.parseWithFallback(
  11. () => {
  12. try {
  13. return parse.call(this);
  14. } finally {
  15. this.skipSC();
  16. if (this.lookupNonWSType(0) !== RightParenthesis) {
  17. this.error();
  18. }
  19. }
  20. },
  21. fallback || (() => this.Raw(null, true))
  22. );
  23. }
  24. const parseFunctions = {
  25. layer() {
  26. this.skipSC();
  27. const children = this.createList();
  28. const node = parseWithFallback.call(this, this.Layer);
  29. if (node.type !== 'Raw' || node.value !== '') {
  30. children.push(node);
  31. }
  32. return children;
  33. },
  34. supports() {
  35. this.skipSC();
  36. const children = this.createList();
  37. const node = parseWithFallback.call(
  38. this,
  39. this.Declaration,
  40. () => parseWithFallback.call(this, () => this.Condition('supports'))
  41. );
  42. if (node.type !== 'Raw' || node.value !== '') {
  43. children.push(node);
  44. }
  45. return children;
  46. }
  47. };
  48. export default {
  49. parse: {
  50. prelude() {
  51. const children = this.createList();
  52. switch (this.tokenType) {
  53. case StringToken:
  54. children.push(this.String());
  55. break;
  56. case Url:
  57. case FunctionToken:
  58. children.push(this.Url());
  59. break;
  60. default:
  61. this.error('String or url() is expected');
  62. }
  63. this.skipSC();
  64. if (this.tokenType === Ident &&
  65. this.cmpStr(this.tokenStart, this.tokenEnd, 'layer')) {
  66. children.push(this.Identifier());
  67. } else if (
  68. this.tokenType === FunctionToken &&
  69. this.cmpStr(this.tokenStart, this.tokenEnd, 'layer(')
  70. ) {
  71. children.push(this.Function(null, parseFunctions));
  72. }
  73. this.skipSC();
  74. if (this.tokenType === FunctionToken &&
  75. this.cmpStr(this.tokenStart, this.tokenEnd, 'supports(')) {
  76. children.push(this.Function(null, parseFunctions));
  77. }
  78. if (this.lookupNonWSType(0) === Ident ||
  79. this.lookupNonWSType(0) === LeftParenthesis) {
  80. children.push(this.MediaQueryList());
  81. }
  82. return children;
  83. },
  84. block: null
  85. }
  86. };