UNPKG

6.23 kBJavaScriptView Raw
1"use strict";
2
3function _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; }
4
5function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _defaults(subClass, superClass); }
6
7var Prefixer = require('./prefixer');
8
9var Browsers = require('./browsers');
10
11var utils = require('./utils');
12
13var Declaration =
14/*#__PURE__*/
15function (_Prefixer) {
16 _inheritsLoose(Declaration, _Prefixer);
17
18 function Declaration() {
19 return _Prefixer.apply(this, arguments) || this;
20 }
21
22 var _proto = Declaration.prototype;
23
24 /**
25 * Always true, because we already get prefixer by property name
26 */
27 _proto.check = function check()
28 /* decl */
29 {
30 return true;
31 }
32 /**
33 * Return prefixed version of property
34 */
35 ;
36
37 _proto.prefixed = function prefixed(prop, prefix) {
38 return prefix + prop;
39 }
40 /**
41 * Return unprefixed version of property
42 */
43 ;
44
45 _proto.normalize = function normalize(prop) {
46 return prop;
47 }
48 /**
49 * Check `value`, that it contain other prefixes, rather than `prefix`
50 */
51 ;
52
53 _proto.otherPrefixes = function otherPrefixes(value, prefix) {
54 for (var _iterator = Browsers.prefixes(), _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
55 var _ref;
56
57 if (_isArray) {
58 if (_i >= _iterator.length) break;
59 _ref = _iterator[_i++];
60 } else {
61 _i = _iterator.next();
62 if (_i.done) break;
63 _ref = _i.value;
64 }
65
66 var other = _ref;
67
68 if (other === prefix) {
69 continue;
70 }
71
72 if (value.includes(other)) {
73 return true;
74 }
75 }
76
77 return false;
78 }
79 /**
80 * Set prefix to declaration
81 */
82 ;
83
84 _proto.set = function set(decl, prefix) {
85 decl.prop = this.prefixed(decl.prop, prefix);
86 return decl;
87 }
88 /**
89 * Should we use visual cascade for prefixes
90 */
91 ;
92
93 _proto.needCascade = function needCascade(decl) {
94 if (!decl._autoprefixerCascade) {
95 decl._autoprefixerCascade = this.all.options.cascade !== false && decl.raw('before').includes('\n');
96 }
97
98 return decl._autoprefixerCascade;
99 }
100 /**
101 * Return maximum length of possible prefixed property
102 */
103 ;
104
105 _proto.maxPrefixed = function maxPrefixed(prefixes, decl) {
106 if (decl._autoprefixerMax) {
107 return decl._autoprefixerMax;
108 }
109
110 var max = 0;
111
112 for (var _iterator2 = prefixes, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
113 var _ref2;
114
115 if (_isArray2) {
116 if (_i2 >= _iterator2.length) break;
117 _ref2 = _iterator2[_i2++];
118 } else {
119 _i2 = _iterator2.next();
120 if (_i2.done) break;
121 _ref2 = _i2.value;
122 }
123
124 var prefix = _ref2;
125 prefix = utils.removeNote(prefix);
126
127 if (prefix.length > max) {
128 max = prefix.length;
129 }
130 }
131
132 decl._autoprefixerMax = max;
133 return decl._autoprefixerMax;
134 }
135 /**
136 * Calculate indentation to create visual cascade
137 */
138 ;
139
140 _proto.calcBefore = function calcBefore(prefixes, decl, prefix) {
141 if (prefix === void 0) {
142 prefix = '';
143 }
144
145 var max = this.maxPrefixed(prefixes, decl);
146 var diff = max - utils.removeNote(prefix).length;
147 var before = decl.raw('before');
148
149 if (diff > 0) {
150 before += Array(diff).fill(' ').join('');
151 }
152
153 return before;
154 }
155 /**
156 * Remove visual cascade
157 */
158 ;
159
160 _proto.restoreBefore = function restoreBefore(decl) {
161 var lines = decl.raw('before').split('\n');
162 var min = lines[lines.length - 1];
163 this.all.group(decl).up(function (prefixed) {
164 var array = prefixed.raw('before').split('\n');
165 var last = array[array.length - 1];
166
167 if (last.length < min.length) {
168 min = last;
169 }
170 });
171 lines[lines.length - 1] = min;
172 decl.raws.before = lines.join('\n');
173 }
174 /**
175 * Clone and insert new declaration
176 */
177 ;
178
179 _proto.insert = function insert(decl, prefix, prefixes) {
180 var cloned = this.set(this.clone(decl), prefix);
181 if (!cloned) return undefined;
182 var already = decl.parent.some(function (i) {
183 return i.prop === cloned.prop && i.value === cloned.value;
184 });
185
186 if (already) {
187 return undefined;
188 }
189
190 if (this.needCascade(decl)) {
191 cloned.raws.before = this.calcBefore(prefixes, decl, prefix);
192 }
193
194 return decl.parent.insertBefore(decl, cloned);
195 }
196 /**
197 * Did this declaration has this prefix above
198 */
199 ;
200
201 _proto.isAlready = function isAlready(decl, prefixed) {
202 var already = this.all.group(decl).up(function (i) {
203 return i.prop === prefixed;
204 });
205
206 if (!already) {
207 already = this.all.group(decl).down(function (i) {
208 return i.prop === prefixed;
209 });
210 }
211
212 return already;
213 }
214 /**
215 * Clone and add prefixes for declaration
216 */
217 ;
218
219 _proto.add = function add(decl, prefix, prefixes, result) {
220 var prefixed = this.prefixed(decl.prop, prefix);
221
222 if (this.isAlready(decl, prefixed) || this.otherPrefixes(decl.value, prefix)) {
223 return undefined;
224 }
225
226 return this.insert(decl, prefix, prefixes, result);
227 }
228 /**
229 * Add spaces for visual cascade
230 */
231 ;
232
233 _proto.process = function process(decl, result) {
234 if (!this.needCascade(decl)) {
235 _Prefixer.prototype.process.call(this, decl, result);
236
237 return;
238 }
239
240 var prefixes = _Prefixer.prototype.process.call(this, decl, result);
241
242 if (!prefixes || !prefixes.length) {
243 return;
244 }
245
246 this.restoreBefore(decl);
247 decl.raws.before = this.calcBefore(prefixes, decl);
248 }
249 /**
250 * Return list of prefixed properties to clean old prefixes
251 */
252 ;
253
254 _proto.old = function old(prop, prefix) {
255 return [this.prefixed(prop, prefix)];
256 };
257
258 return Declaration;
259}(Prefixer);
260
261module.exports = Declaration;
\No newline at end of file