atrule.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var tslib_1 = require("tslib");
  4. var node_1 = tslib_1.__importDefault(require("./node"));
  5. var selector_1 = tslib_1.__importDefault(require("./selector"));
  6. var ruleset_1 = tslib_1.__importDefault(require("./ruleset"));
  7. var anonymous_1 = tslib_1.__importDefault(require("./anonymous"));
  8. var AtRule = function (name, value, rules, index, currentFileInfo, debugInfo, isRooted, visibilityInfo) {
  9. var i;
  10. this.name = name;
  11. this.value = (value instanceof node_1.default) ? value : (value ? new anonymous_1.default(value) : value);
  12. if (rules) {
  13. if (Array.isArray(rules)) {
  14. this.rules = rules;
  15. }
  16. else {
  17. this.rules = [rules];
  18. this.rules[0].selectors = (new selector_1.default([], null, null, index, currentFileInfo)).createEmptySelectors();
  19. }
  20. for (i = 0; i < this.rules.length; i++) {
  21. this.rules[i].allowImports = true;
  22. }
  23. this.setParent(this.rules, this);
  24. }
  25. this._index = index;
  26. this._fileInfo = currentFileInfo;
  27. this.debugInfo = debugInfo;
  28. this.isRooted = isRooted || false;
  29. this.copyVisibilityInfo(visibilityInfo);
  30. this.allowRoot = true;
  31. };
  32. AtRule.prototype = Object.assign(new node_1.default(), {
  33. type: 'AtRule',
  34. accept: function (visitor) {
  35. var value = this.value, rules = this.rules;
  36. if (rules) {
  37. this.rules = visitor.visitArray(rules);
  38. }
  39. if (value) {
  40. this.value = visitor.visit(value);
  41. }
  42. },
  43. isRulesetLike: function () {
  44. return this.rules || !this.isCharset();
  45. },
  46. isCharset: function () {
  47. return '@charset' === this.name;
  48. },
  49. genCSS: function (context, output) {
  50. var value = this.value, rules = this.rules;
  51. output.add(this.name, this.fileInfo(), this.getIndex());
  52. if (value) {
  53. output.add(' ');
  54. value.genCSS(context, output);
  55. }
  56. if (rules) {
  57. this.outputRuleset(context, output, rules);
  58. }
  59. else {
  60. output.add(';');
  61. }
  62. },
  63. eval: function (context) {
  64. var mediaPathBackup, mediaBlocksBackup, value = this.value, rules = this.rules;
  65. // media stored inside other atrule should not bubble over it
  66. // backpup media bubbling information
  67. mediaPathBackup = context.mediaPath;
  68. mediaBlocksBackup = context.mediaBlocks;
  69. // deleted media bubbling information
  70. context.mediaPath = [];
  71. context.mediaBlocks = [];
  72. if (value) {
  73. value = value.eval(context);
  74. }
  75. if (rules) {
  76. // assuming that there is only one rule at this point - that is how parser constructs the rule
  77. rules = [rules[0].eval(context)];
  78. rules[0].root = true;
  79. }
  80. // restore media bubbling information
  81. context.mediaPath = mediaPathBackup;
  82. context.mediaBlocks = mediaBlocksBackup;
  83. return new AtRule(this.name, value, rules, this.getIndex(), this.fileInfo(), this.debugInfo, this.isRooted, this.visibilityInfo());
  84. },
  85. variable: function (name) {
  86. if (this.rules) {
  87. // assuming that there is only one rule at this point - that is how parser constructs the rule
  88. return ruleset_1.default.prototype.variable.call(this.rules[0], name);
  89. }
  90. },
  91. find: function () {
  92. if (this.rules) {
  93. // assuming that there is only one rule at this point - that is how parser constructs the rule
  94. return ruleset_1.default.prototype.find.apply(this.rules[0], arguments);
  95. }
  96. },
  97. rulesets: function () {
  98. if (this.rules) {
  99. // assuming that there is only one rule at this point - that is how parser constructs the rule
  100. return ruleset_1.default.prototype.rulesets.apply(this.rules[0]);
  101. }
  102. },
  103. outputRuleset: function (context, output, rules) {
  104. var ruleCnt = rules.length;
  105. var i;
  106. context.tabLevel = (context.tabLevel | 0) + 1;
  107. // Compressed
  108. if (context.compress) {
  109. output.add('{');
  110. for (i = 0; i < ruleCnt; i++) {
  111. rules[i].genCSS(context, output);
  112. }
  113. output.add('}');
  114. context.tabLevel--;
  115. return;
  116. }
  117. // Non-compressed
  118. var tabSetStr = "\n" + Array(context.tabLevel).join(' '), tabRuleStr = tabSetStr + " ";
  119. if (!ruleCnt) {
  120. output.add(" {" + tabSetStr + "}");
  121. }
  122. else {
  123. output.add(" {" + tabRuleStr);
  124. rules[0].genCSS(context, output);
  125. for (i = 1; i < ruleCnt; i++) {
  126. output.add(tabRuleStr);
  127. rules[i].genCSS(context, output);
  128. }
  129. output.add(tabSetStr + "}");
  130. }
  131. context.tabLevel--;
  132. }
  133. });
  134. exports.default = AtRule;
  135. //# sourceMappingURL=atrule.js.map