StyleSheet.js 2.2 KB

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