UNPKG

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