UNPKG

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