UNPKG

3.84 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 OldValue = require('./old-value');
10
11var utils = require('./utils');
12
13var vendor = require('postcss').vendor;
14
15var Value =
16/*#__PURE__*/
17function (_Prefixer) {
18 _inheritsLoose(Value, _Prefixer);
19
20 function Value() {
21 return _Prefixer.apply(this, arguments) || this;
22 }
23
24 /**
25 * Clone decl for each prefixed values
26 */
27 Value.save = function save(prefixes, decl) {
28 var _this = this;
29
30 var prop = decl.prop;
31 var result = [];
32
33 var _loop = function _loop(prefix) {
34 var value = decl._autoprefixerValues[prefix];
35
36 if (value === decl.value) {
37 return "continue";
38 }
39
40 var item = void 0;
41 var propPrefix = vendor.prefix(prop);
42
43 if (propPrefix === '-pie-') {
44 return "continue";
45 }
46
47 if (propPrefix === prefix) {
48 item = decl.value = value;
49 result.push(item);
50 return "continue";
51 }
52
53 var prefixed = prefixes.prefixed(prop, prefix);
54 var rule = decl.parent;
55
56 if (!rule.every(function (i) {
57 return i.prop !== prefixed;
58 })) {
59 result.push(item);
60 return "continue";
61 }
62
63 var trimmed = value.replace(/\s+/, ' ');
64 var already = rule.some(function (i) {
65 return i.prop === decl.prop && i.value.replace(/\s+/, ' ') === trimmed;
66 });
67
68 if (already) {
69 result.push(item);
70 return "continue";
71 }
72
73 var cloned = _this.clone(decl, {
74 value: value
75 });
76
77 item = decl.parent.insertBefore(decl, cloned);
78 result.push(item);
79 };
80
81 for (var prefix in decl._autoprefixerValues) {
82 var _ret = _loop(prefix);
83
84 if (_ret === "continue") continue;
85 }
86
87 return result;
88 }
89 /**
90 * Is declaration need to be prefixed
91 */
92 ;
93
94 var _proto = Value.prototype;
95
96 _proto.check = function check(decl) {
97 var value = decl.value;
98
99 if (value.indexOf(this.name) === -1) {
100 return false;
101 }
102
103 return !!value.match(this.regexp());
104 }
105 /**
106 * Lazy regexp loading
107 */
108 ;
109
110 _proto.regexp = function regexp() {
111 return this.regexpCache || (this.regexpCache = utils.regexp(this.name));
112 }
113 /**
114 * Add prefix to values in string
115 */
116 ;
117
118 _proto.replace = function replace(string, prefix) {
119 return string.replace(this.regexp(), "$1" + prefix + "$2");
120 }
121 /**
122 * Get value with comments if it was not changed
123 */
124 ;
125
126 _proto.value = function value(decl) {
127 if (decl.raws.value && decl.raws.value.value === decl.value) {
128 return decl.raws.value.raw;
129 } else {
130 return decl.value;
131 }
132 }
133 /**
134 * Save values with next prefixed token
135 */
136 ;
137
138 _proto.add = function add(decl, prefix) {
139 if (!decl._autoprefixerValues) {
140 decl._autoprefixerValues = {};
141 }
142
143 var value = decl._autoprefixerValues[prefix] || this.value(decl);
144 var before;
145
146 do {
147 before = value;
148 value = this.replace(value, prefix);
149 if (value === false) return;
150 } while (value !== before);
151
152 decl._autoprefixerValues[prefix] = value;
153 }
154 /**
155 * Return function to fast find prefixed value
156 */
157 ;
158
159 _proto.old = function old(prefix) {
160 return new OldValue(this.name, prefix + this.name);
161 };
162
163 return Value;
164}(Prefixer);
165
166module.exports = Value;
\No newline at end of file