StyleSheet.cjs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. 'use strict';
  2. const types = require('../../tokenizer/types.cjs');
  3. const EXCLAMATIONMARK = 0x0021; // U+0021 EXCLAMATION MARK (!)
  4. function consumeRaw() {
  5. return this.Raw(null, false);
  6. }
  7. const name = 'StyleSheet';
  8. const walkContext = 'stylesheet';
  9. const structure = {
  10. children: [[
  11. 'Comment',
  12. 'CDO',
  13. 'CDC',
  14. 'Atrule',
  15. 'Rule',
  16. 'Raw'
  17. ]]
  18. };
  19. function parse() {
  20. const start = this.tokenStart;
  21. const children = this.createList();
  22. let child;
  23. while (!this.eof) {
  24. switch (this.tokenType) {
  25. case types.WhiteSpace:
  26. this.next();
  27. continue;
  28. case types.Comment:
  29. // ignore comments except exclamation comments (i.e. /*! .. */) on top level
  30. if (this.charCodeAt(this.tokenStart + 2) !== EXCLAMATIONMARK) {
  31. this.next();
  32. continue;
  33. }
  34. child = this.Comment();
  35. break;
  36. case types.CDO: // <!--
  37. child = this.CDO();
  38. break;
  39. case types.CDC: // -->
  40. child = this.CDC();
  41. break;
  42. // CSS Syntax Module Level 3
  43. // §2.2 Error handling
  44. // At the "top level" of a stylesheet, an <at-keyword-token> starts an at-rule.
  45. case types.AtKeyword:
  46. child = this.parseWithFallback(this.Atrule, consumeRaw);
  47. break;
  48. // Anything else starts a qualified rule ...
  49. default:
  50. child = this.parseWithFallback(this.Rule, consumeRaw);
  51. }
  52. children.push(child);
  53. }
  54. return {
  55. type: 'StyleSheet',
  56. loc: this.getLocation(start, this.tokenStart),
  57. children
  58. };
  59. }
  60. function generate(node) {
  61. this.children(node);
  62. }
  63. exports.generate = generate;
  64. exports.name = name;
  65. exports.parse = parse;
  66. exports.structure = structure;
  67. exports.walkContext = walkContext;