UNPKG

3.93 kBJavaScriptView Raw
1"use strict";
2
3var vendor = require('postcss').vendor;
4
5var Browsers = require('./browsers');
6
7var utils = require('./utils');
8/**
9 * Recursively clone objects
10 */
11
12
13function _clone(obj, parent) {
14 var cloned = new obj.constructor();
15
16 for (var _i = 0, _Object$keys = Object.keys(obj || {}); _i < _Object$keys.length; _i++) {
17 var i = _Object$keys[_i];
18 var value = obj[i];
19
20 if (i === 'parent' && typeof value === 'object') {
21 if (parent) {
22 cloned[i] = parent;
23 }
24 } else if (i === 'source' || i === null) {
25 cloned[i] = value;
26 } else if (Array.isArray(value)) {
27 cloned[i] = value.map(function (x) {
28 return _clone(x, cloned);
29 });
30 } else if (i !== '_autoprefixerPrefix' && i !== '_autoprefixerValues') {
31 if (typeof value === 'object' && value !== null) {
32 value = _clone(value, cloned);
33 }
34
35 cloned[i] = value;
36 }
37 }
38
39 return cloned;
40}
41
42var Prefixer =
43/*#__PURE__*/
44function () {
45 /**
46 * Add hack to selected names
47 */
48 Prefixer.hack = function hack(klass) {
49 var _this = this;
50
51 if (!this.hacks) {
52 this.hacks = {};
53 }
54
55 return klass.names.map(function (name) {
56 _this.hacks[name] = klass;
57 return _this.hacks[name];
58 });
59 }
60 /**
61 * Load hacks for some names
62 */
63 ;
64
65 Prefixer.load = function load(name, prefixes, all) {
66 var Klass = this.hacks && this.hacks[name];
67
68 if (Klass) {
69 return new Klass(name, prefixes, all);
70 } else {
71 return new this(name, prefixes, all);
72 }
73 }
74 /**
75 * Clone node and clean autprefixer custom caches
76 */
77 ;
78
79 Prefixer.clone = function clone(node, overrides) {
80 var cloned = _clone(node);
81
82 for (var name in overrides) {
83 cloned[name] = overrides[name];
84 }
85
86 return cloned;
87 };
88
89 function Prefixer(name, prefixes, all) {
90 this.prefixes = prefixes;
91 this.name = name;
92 this.all = all;
93 }
94 /**
95 * Find prefix in node parents
96 */
97
98
99 var _proto = Prefixer.prototype;
100
101 _proto.parentPrefix = function parentPrefix(node) {
102 var prefix;
103
104 if (typeof node._autoprefixerPrefix !== 'undefined') {
105 prefix = node._autoprefixerPrefix;
106 } else if (node.type === 'decl' && node.prop[0] === '-') {
107 prefix = vendor.prefix(node.prop);
108 } else if (node.type === 'root') {
109 prefix = false;
110 } else if (node.type === 'rule' && node.selector.includes(':-') && /:(-\w+-)/.test(node.selector)) {
111 prefix = node.selector.match(/:(-\w+-)/)[1];
112 } else if (node.type === 'atrule' && node.name[0] === '-') {
113 prefix = vendor.prefix(node.name);
114 } else {
115 prefix = this.parentPrefix(node.parent);
116 }
117
118 if (!Browsers.prefixes().includes(prefix)) {
119 prefix = false;
120 }
121
122 node._autoprefixerPrefix = prefix;
123 return node._autoprefixerPrefix;
124 }
125 /**
126 * Clone node with prefixes
127 */
128 ;
129
130 _proto.process = function process(node, result) {
131 if (!this.check(node)) {
132 return undefined;
133 }
134
135 var parent = this.parentPrefix(node);
136 var prefixes = this.prefixes.filter(function (prefix) {
137 return !parent || parent === utils.removeNote(prefix);
138 });
139 var added = [];
140
141 for (var _iterator = prefixes, _isArray = Array.isArray(_iterator), _i2 = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
142 var _ref;
143
144 if (_isArray) {
145 if (_i2 >= _iterator.length) break;
146 _ref = _iterator[_i2++];
147 } else {
148 _i2 = _iterator.next();
149 if (_i2.done) break;
150 _ref = _i2.value;
151 }
152
153 var prefix = _ref;
154
155 if (this.add(node, prefix, added.concat([prefix]), result)) {
156 added.push(prefix);
157 }
158 }
159
160 return added;
161 }
162 /**
163 * Shortcut for Prefixer.clone
164 */
165 ;
166
167 _proto.clone = function clone(node, overrides) {
168 return Prefixer.clone(node, overrides);
169 };
170
171 return Prefixer;
172}();
173
174module.exports = Prefixer;
\No newline at end of file