3 function _defaults(obj, defaults) { var keys = Object.getOwnPropertyNames(defaults); for (var i = 0; i < keys.length; i++) { var key = keys[i]; var value = Object.getOwnPropertyDescriptor(defaults, key); if (value && value.configurable && obj[key] === undefined) { Object.defineProperty(obj, key, value); } } return obj; }
5 function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
7 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; }
9 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) : _defaults(subClass, superClass); }
11 var Prefixer = require('./prefixer');
12 var Browsers = require('./browsers');
13 var utils = require('./utils');
15 var Declaration = function (_Prefixer) {
16 _inherits(Declaration, _Prefixer);
18 function Declaration() {
19 _classCallCheck(this, Declaration);
21 return _possibleConstructorReturn(this, _Prefixer.apply(this, arguments));
25 * Always true, because we already get prefixer by property name
27 Declaration.prototype.check = function check() /* decl */{
32 * Return prefixed version of property
36 Declaration.prototype.prefixed = function prefixed(prop, prefix) {
41 * Return unprefixed version of property
45 Declaration.prototype.normalize = function normalize(prop) {
50 * Check `value`, that it contain other prefixes, rather than `prefix`
54 Declaration.prototype.otherPrefixes = function otherPrefixes(value, prefix) {
55 for (var _iterator = Browsers.prefixes(), _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
59 if (_i >= _iterator.length) break;
60 _ref = _iterator[_i++];
62 _i = _iterator.next();
69 if (other === prefix) {
72 if (value.indexOf(other) !== -1) {
80 * Set prefix to declaration
84 Declaration.prototype.set = function set(decl, prefix) {
85 decl.prop = this.prefixed(decl.prop, prefix);
90 * Should we use visual cascade for prefixes
94 Declaration.prototype.needCascade = function needCascade(decl) {
95 if (!decl._autoprefixerCascade) {
96 decl._autoprefixerCascade = this.all.options.cascade !== false && decl.raw('before').indexOf('\n') !== -1;
98 return decl._autoprefixerCascade;
102 * Return maximum length of possible prefixed property
106 Declaration.prototype.maxPrefixed = function maxPrefixed(prefixes, decl) {
107 if (decl._autoprefixerMax) {
108 return decl._autoprefixerMax;
112 for (var _iterator2 = prefixes, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
116 if (_i2 >= _iterator2.length) break;
117 _ref2 = _iterator2[_i2++];
119 _i2 = _iterator2.next();
126 prefix = utils.removeNote(prefix);
127 if (prefix.length > max) {
131 decl._autoprefixerMax = max;
133 return decl._autoprefixerMax;
137 * Calculate indentation to create visual cascade
141 Declaration.prototype.calcBefore = function calcBefore(prefixes, decl) {
142 var prefix = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
144 var max = this.maxPrefixed(prefixes, decl);
145 var diff = max - utils.removeNote(prefix).length;
147 var before = decl.raw('before');
149 before += Array(diff).fill(' ').join('');
156 * Remove visual cascade
160 Declaration.prototype.restoreBefore = function restoreBefore(decl) {
161 var lines = decl.raw('before').split('\n');
162 var min = lines[lines.length - 1];
164 this.all.group(decl).up(function (prefixed) {
165 var array = prefixed.raw('before').split('\n');
166 var last = array[array.length - 1];
167 if (last.length < min.length) {
172 lines[lines.length - 1] = min;
173 decl.raws.before = lines.join('\n');
177 * Clone and insert new declaration
181 Declaration.prototype.insert = function insert(decl, prefix, prefixes) {
182 var cloned = this.set(this.clone(decl), prefix);
183 if (!cloned) return undefined;
185 var already = decl.parent.some(function (i) {
186 return i.prop === cloned.prop && i.value === cloned.value;
192 if (this.needCascade(decl)) {
193 cloned.raws.before = this.calcBefore(prefixes, decl, prefix);
195 return decl.parent.insertBefore(decl, cloned);
199 * Did this declaration has this prefix above
203 Declaration.prototype.isAlready = function isAlready(decl, prefixed) {
204 var already = this.all.group(decl).up(function (i) {
205 return i.prop === prefixed;
208 already = this.all.group(decl).down(function (i) {
209 return i.prop === prefixed;
216 * Clone and add prefixes for declaration
220 Declaration.prototype.add = function add(decl, prefix, prefixes, result) {
221 var prefixed = this.prefixed(decl.prop, prefix);
222 if (this.isAlready(decl, prefixed) || this.otherPrefixes(decl.value, prefix)) {
225 return this.insert(decl, prefix, prefixes, result);
229 * Add spaces for visual cascade
233 Declaration.prototype.process = function process(decl, result) {
234 if (!this.needCascade(decl)) {
235 _Prefixer.prototype.process.call(this, decl, result);
239 var prefixes = _Prefixer.prototype.process.call(this, decl, result);
241 if (!prefixes || !prefixes.length) {
245 this.restoreBefore(decl);
246 decl.raws.before = this.calcBefore(prefixes, decl);
250 * Return list of prefixed properties to clean old prefixes
254 Declaration.prototype.old = function old(prop, prefix) {
255 return [this.prefixed(prop, prefix)];
261 module.exports = Declaration;