rule.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. 'use strict';
  2. exports.__esModule = true;
  3. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  4. var _container = require('./container');
  5. var _container2 = _interopRequireDefault(_container);
  6. var _warnOnce = require('./warn-once');
  7. var _warnOnce2 = _interopRequireDefault(_warnOnce);
  8. var _list = require('./list');
  9. var _list2 = _interopRequireDefault(_list);
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  12. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  13. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  14. /**
  15. * Represents a CSS rule: a selector followed by a declaration block.
  16. *
  17. * @extends Container
  18. *
  19. * @example
  20. * const root = postcss.parse('a{}');
  21. * const rule = root.first;
  22. * rule.type //=> 'rule'
  23. * rule.toString() //=> 'a{}'
  24. */
  25. var Rule = function (_Container) {
  26. _inherits(Rule, _Container);
  27. function Rule(defaults) {
  28. _classCallCheck(this, Rule);
  29. var _this = _possibleConstructorReturn(this, _Container.call(this, defaults));
  30. _this.type = 'rule';
  31. if (!_this.nodes) _this.nodes = [];
  32. return _this;
  33. }
  34. /**
  35. * An array containing the rule’s individual selectors.
  36. * Groups of selectors are split at commas.
  37. *
  38. * @type {string[]}
  39. *
  40. * @example
  41. * const root = postcss.parse('a, b { }');
  42. * const rule = root.first;
  43. *
  44. * rule.selector //=> 'a, b'
  45. * rule.selectors //=> ['a', 'b']
  46. *
  47. * rule.selectors = ['a', 'strong'];
  48. * rule.selector //=> 'a, strong'
  49. */
  50. _createClass(Rule, [{
  51. key: 'selectors',
  52. get: function get() {
  53. return _list2.default.comma(this.selector);
  54. },
  55. set: function set(values) {
  56. var match = this.selector ? this.selector.match(/,\s*/) : null;
  57. var sep = match ? match[0] : ',' + this.raw('between', 'beforeOpen');
  58. this.selector = values.join(sep);
  59. }
  60. }, {
  61. key: '_selector',
  62. get: function get() {
  63. (0, _warnOnce2.default)('Rule#_selector is deprecated. Use Rule#raws.selector');
  64. return this.raws.selector;
  65. },
  66. set: function set(val) {
  67. (0, _warnOnce2.default)('Rule#_selector is deprecated. Use Rule#raws.selector');
  68. this.raws.selector = val;
  69. }
  70. /**
  71. * @memberof Rule#
  72. * @member {string} selector - the rule’s full selector represented
  73. * as a string
  74. *
  75. * @example
  76. * const root = postcss.parse('a, b { }');
  77. * const rule = root.first;
  78. * rule.selector //=> 'a, b'
  79. */
  80. /**
  81. * @memberof Rule#
  82. * @member {object} raws - Information to generate byte-to-byte equal
  83. * node string as it was in the origin input.
  84. *
  85. * Every parser saves its own properties,
  86. * but the default CSS parser uses:
  87. *
  88. * * `before`: the space symbols before the node. It also stores `*`
  89. * and `_` symbols before the declaration (IE hack).
  90. * * `after`: the space symbols after the last child of the node
  91. * to the end of the node.
  92. * * `between`: the symbols between the property and value
  93. * for declarations, selector and `{` for rules, or last parameter
  94. * and `{` for at-rules.
  95. * * `semicolon`: contains true if the last child has
  96. * an (optional) semicolon.
  97. *
  98. * PostCSS cleans selectors from comments and extra spaces,
  99. * but it stores origin content in raws properties.
  100. * As such, if you don’t change a declaration’s value,
  101. * PostCSS will use the raw value with comments.
  102. *
  103. * @example
  104. * const root = postcss.parse('a {\n color:black\n}')
  105. * root.first.first.raws //=> { before: '', between: ' ', after: '\n' }
  106. */
  107. }]);
  108. return Rule;
  109. }(_container2.default);
  110. exports.default = Rule;
  111. module.exports = exports['default'];
  112. //# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInJ1bGUuZXM2Il0sIm5hbWVzIjpbIlJ1bGUiLCJkZWZhdWx0cyIsInR5cGUiLCJub2RlcyIsImNvbW1hIiwic2VsZWN0b3IiLCJ2YWx1ZXMiLCJtYXRjaCIsInNlcCIsInJhdyIsImpvaW4iLCJyYXdzIiwidmFsIl0sIm1hcHBpbmdzIjoiOzs7Ozs7QUFBQTs7OztBQUNBOzs7O0FBQ0E7Ozs7Ozs7Ozs7OztBQUVBOzs7Ozs7Ozs7OztJQVdNQSxJOzs7QUFFRixrQkFBWUMsUUFBWixFQUFzQjtBQUFBOztBQUFBLHFEQUNsQixzQkFBTUEsUUFBTixDQURrQjs7QUFFbEIsY0FBS0MsSUFBTCxHQUFZLE1BQVo7QUFDQSxZQUFLLENBQUMsTUFBS0MsS0FBWCxFQUFtQixNQUFLQSxLQUFMLEdBQWEsRUFBYjtBQUhEO0FBSXJCOztBQUVEOzs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs0QkFnQmdCO0FBQ1osbUJBQU8sZUFBS0MsS0FBTCxDQUFXLEtBQUtDLFFBQWhCLENBQVA7QUFDSCxTOzBCQUVhQyxNLEVBQVE7QUFDbEIsZ0JBQUlDLFFBQVEsS0FBS0YsUUFBTCxHQUFnQixLQUFLQSxRQUFMLENBQWNFLEtBQWQsQ0FBb0IsTUFBcEIsQ0FBaEIsR0FBOEMsSUFBMUQ7QUFDQSxnQkFBSUMsTU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