DeclarationList.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import {
  2. WhiteSpace,
  3. Comment,
  4. Semicolon,
  5. AtKeyword
  6. } from '../../tokenizer/index.js';
  7. const AMPERSAND = 0x0026; // U+0026 AMPERSAND (&)
  8. function consumeRaw() {
  9. return this.Raw(this.consumeUntilSemicolonIncluded, true);
  10. }
  11. export const name = 'DeclarationList';
  12. export const structure = {
  13. children: [[
  14. 'Declaration',
  15. 'Atrule',
  16. 'Rule'
  17. ]]
  18. };
  19. export function parse() {
  20. const children = this.createList();
  21. scan:
  22. while (!this.eof) {
  23. switch (this.tokenType) {
  24. case WhiteSpace:
  25. case Comment:
  26. case Semicolon:
  27. this.next();
  28. break;
  29. case AtKeyword:
  30. children.push(this.parseWithFallback(this.Atrule.bind(this, true), consumeRaw));
  31. break;
  32. default:
  33. if (this.isDelim(AMPERSAND)) {
  34. children.push(this.parseWithFallback(this.Rule, consumeRaw));
  35. } else {
  36. children.push(this.parseWithFallback(this.Declaration, consumeRaw));
  37. }
  38. }
  39. }
  40. return {
  41. type: 'DeclarationList',
  42. loc: this.getLocationFromList(children),
  43. children
  44. };
  45. }
  46. export function generate(node) {
  47. this.children(node, prev => {
  48. if (prev.type === 'Declaration') {
  49. this.token(Semicolon, ';');
  50. }
  51. });
  52. }