media.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. "use strict";
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = function (d, b) {
  4. extendStatics = Object.setPrototypeOf ||
  5. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  6. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. };
  9. return function (d, b) {
  10. extendStatics(d, b);
  11. function __() { this.constructor = d; }
  12. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  13. };
  14. })();
  15. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  16. if (k2 === undefined) k2 = k;
  17. Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
  18. }) : (function(o, m, k, k2) {
  19. if (k2 === undefined) k2 = k;
  20. o[k2] = m[k];
  21. }));
  22. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  23. Object.defineProperty(o, "default", { enumerable: true, value: v });
  24. }) : function(o, v) {
  25. o["default"] = v;
  26. });
  27. var __importStar = (this && this.__importStar) || function (mod) {
  28. if (mod && mod.__esModule) return mod;
  29. var result = {};
  30. if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  31. __setModuleDefault(result, mod);
  32. return result;
  33. };
  34. var __importDefault = (this && this.__importDefault) || function (mod) {
  35. return (mod && mod.__esModule) ? mod : { "default": mod };
  36. };
  37. Object.defineProperty(exports, "__esModule", { value: true });
  38. var ruleset_1 = __importDefault(require("./ruleset"));
  39. var value_1 = __importDefault(require("./value"));
  40. var selector_1 = __importDefault(require("./selector"));
  41. var anonymous_1 = __importDefault(require("./anonymous"));
  42. var expression_1 = __importDefault(require("./expression"));
  43. var atrule_1 = __importDefault(require("./atrule"));
  44. var utils = __importStar(require("../utils"));
  45. var Media = /** @class */ (function (_super) {
  46. __extends(Media, _super);
  47. function Media(value, features, index, currentFileInfo, visibilityInfo) {
  48. var _this = _super.call(this) || this;
  49. _this._index = index;
  50. _this._fileInfo = currentFileInfo;
  51. var selectors = (new selector_1.default([], null, null, _this._index, _this._fileInfo)).createEmptySelectors();
  52. _this.features = new value_1.default(features);
  53. _this.rules = [new ruleset_1.default(selectors, value)];
  54. _this.rules[0].allowImports = true;
  55. _this.copyVisibilityInfo(visibilityInfo);
  56. _this.allowRoot = true;
  57. _this.setParent(selectors, _this);
  58. _this.setParent(_this.features, _this);
  59. _this.setParent(_this.rules, _this);
  60. return _this;
  61. }
  62. Media.prototype.isRulesetLike = function () {
  63. return true;
  64. };
  65. Media.prototype.accept = function (visitor) {
  66. if (this.features) {
  67. this.features = visitor.visit(this.features);
  68. }
  69. if (this.rules) {
  70. this.rules = visitor.visitArray(this.rules);
  71. }
  72. };
  73. Media.prototype.genCSS = function (context, output) {
  74. output.add('@media ', this._fileInfo, this._index);
  75. this.features.genCSS(context, output);
  76. this.outputRuleset(context, output, this.rules);
  77. };
  78. Media.prototype.eval = function (context) {
  79. if (!context.mediaBlocks) {
  80. context.mediaBlocks = [];
  81. context.mediaPath = [];
  82. }
  83. var media = new Media(null, [], this._index, this._fileInfo, this.visibilityInfo());
  84. if (this.debugInfo) {
  85. this.rules[0].debugInfo = this.debugInfo;
  86. media.debugInfo = this.debugInfo;
  87. }
  88. media.features = this.features.eval(context);
  89. context.mediaPath.push(media);
  90. context.mediaBlocks.push(media);
  91. this.rules[0].functionRegistry = context.frames[0].functionRegistry.inherit();
  92. context.frames.unshift(this.rules[0]);
  93. media.rules = [this.rules[0].eval(context)];
  94. context.frames.shift();
  95. context.mediaPath.pop();
  96. return context.mediaPath.length === 0 ? media.evalTop(context) :
  97. media.evalNested(context);
  98. };
  99. Media.prototype.evalTop = function (context) {
  100. var result = this;
  101. // Render all dependent Media blocks.
  102. if (context.mediaBlocks.length > 1) {
  103. var selectors = (new selector_1.default([], null, null, this.getIndex(), this.fileInfo())).createEmptySelectors();
  104. result = new ruleset_1.default(selectors, context.mediaBlocks);
  105. result.multiMedia = true;
  106. result.copyVisibilityInfo(this.visibilityInfo());
  107. this.setParent(result, this);
  108. }
  109. delete context.mediaBlocks;
  110. delete context.mediaPath;
  111. return result;
  112. };
  113. Media.prototype.evalNested = function (context) {
  114. var i;
  115. var value;
  116. var path = context.mediaPath.concat([this]);
  117. // Extract the media-query conditions separated with `,` (OR).
  118. for (i = 0; i < path.length; i++) {
  119. value = path[i].features instanceof value_1.default ?
  120. path[i].features.value : path[i].features;
  121. path[i] = Array.isArray(value) ? value : [value];
  122. }
  123. // Trace all permutations to generate the resulting media-query.
  124. //
  125. // (a, b and c) with nested (d, e) ->
  126. // a and d
  127. // a and e
  128. // b and c and d
  129. // b and c and e
  130. this.features = new value_1.default(this.permute(path).map(function (path) {
  131. path = path.map(function (fragment) { return fragment.toCSS ? fragment : new anonymous_1.default(fragment); });
  132. for (i = path.length - 1; i > 0; i--) {
  133. path.splice(i, 0, new anonymous_1.default('and'));
  134. }
  135. return new expression_1.default(path);
  136. }));
  137. this.setParent(this.features, this);
  138. // Fake a tree-node that doesn't output anything.
  139. return new ruleset_1.default([], []);
  140. };
  141. Media.prototype.permute = function (arr) {
  142. if (arr.length === 0) {
  143. return [];
  144. }
  145. else if (arr.length === 1) {
  146. return arr[0];
  147. }
  148. else {
  149. var result = [];
  150. var rest = this.permute(arr.slice(1));
  151. for (var i = 0; i < rest.length; i++) {
  152. for (var j = 0; j < arr[0].length; j++) {
  153. result.push([arr[0][j]].concat(rest[i]));
  154. }
  155. }
  156. return result;
  157. }
  158. };
  159. Media.prototype.bubbleSelectors = function (selectors) {
  160. if (!selectors) {
  161. return;
  162. }
  163. this.rules = [new ruleset_1.default(utils.copyArray(selectors), [this.rules[0]])];
  164. this.setParent(this.rules, this);
  165. };
  166. return Media;
  167. }(atrule_1.default));
  168. Media.prototype.type = 'Media';
  169. exports.default = Media;
  170. //# sourceMappingURL=media.js.map