1 | "use strict";
|
---|
2 |
|
---|
3 | function _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 |
|
---|
5 | function _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 |
|
---|
7 | function _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 |
|
---|
9 | function _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 |
|
---|
11 | function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _defaults(subClass, superClass); }
|
---|
12 |
|
---|
13 | var _require = require('postcss'),
|
---|
14 | list = _require.list;
|
---|
15 |
|
---|
16 | var OldSelector = require('./old-selector');
|
---|
17 |
|
---|
18 | var Prefixer = require('./prefixer');
|
---|
19 |
|
---|
20 | var Browsers = require('./browsers');
|
---|
21 |
|
---|
22 | var utils = require('./utils');
|
---|
23 |
|
---|
24 | var Selector = /*#__PURE__*/function (_Prefixer) {
|
---|
25 | _inheritsLoose(Selector, _Prefixer);
|
---|
26 |
|
---|
27 | function Selector(name, prefixes, all) {
|
---|
28 | var _this;
|
---|
29 |
|
---|
30 | _this = _Prefixer.call(this, name, prefixes, all) || this;
|
---|
31 | _this.regexpCache = {};
|
---|
32 | return _this;
|
---|
33 | }
|
---|
34 | /**
|
---|
35 | * Is rule selectors need to be prefixed
|
---|
36 | */
|
---|
37 |
|
---|
38 |
|
---|
39 | var _proto = Selector.prototype;
|
---|
40 |
|
---|
41 | _proto.check = function check(rule) {
|
---|
42 | if (rule.selector.includes(this.name)) {
|
---|
43 | return !!rule.selector.match(this.regexp());
|
---|
44 | }
|
---|
45 |
|
---|
46 | return false;
|
---|
47 | }
|
---|
48 | /**
|
---|
49 | * Return prefixed version of selector
|
---|
50 | */
|
---|
51 | ;
|
---|
52 |
|
---|
53 | _proto.prefixed = function prefixed(prefix) {
|
---|
54 | return this.name.replace(/^(\W*)/, "$1" + prefix);
|
---|
55 | }
|
---|
56 | /**
|
---|
57 | * Lazy loadRegExp for name
|
---|
58 | */
|
---|
59 | ;
|
---|
60 |
|
---|
61 | _proto.regexp = function regexp(prefix) {
|
---|
62 | if (this.regexpCache[prefix]) {
|
---|
63 | return this.regexpCache[prefix];
|
---|
64 | }
|
---|
65 |
|
---|
66 | var name = prefix ? this.prefixed(prefix) : this.name;
|
---|
67 | this.regexpCache[prefix] = new RegExp("(^|[^:\"'=])" + utils.escapeRegexp(name), 'gi');
|
---|
68 | return this.regexpCache[prefix];
|
---|
69 | }
|
---|
70 | /**
|
---|
71 | * All possible prefixes
|
---|
72 | */
|
---|
73 | ;
|
---|
74 |
|
---|
75 | _proto.possible = function possible() {
|
---|
76 | return Browsers.prefixes();
|
---|
77 | }
|
---|
78 | /**
|
---|
79 | * Return all possible selector prefixes
|
---|
80 | */
|
---|
81 | ;
|
---|
82 |
|
---|
83 | _proto.prefixeds = function prefixeds(rule) {
|
---|
84 | var _this2 = this;
|
---|
85 |
|
---|
86 | if (rule._autoprefixerPrefixeds) {
|
---|
87 | if (rule._autoprefixerPrefixeds[this.name]) {
|
---|
88 | return rule._autoprefixerPrefixeds;
|
---|
89 | }
|
---|
90 | } else {
|
---|
91 | rule._autoprefixerPrefixeds = {};
|
---|
92 | }
|
---|
93 |
|
---|
94 | var prefixeds = {};
|
---|
95 |
|
---|
96 | if (rule.selector.includes(',')) {
|
---|
97 | var ruleParts = list.comma(rule.selector);
|
---|
98 | var toProcess = ruleParts.filter(function (el) {
|
---|
99 | return el.includes(_this2.name);
|
---|
100 | });
|
---|
101 |
|
---|
102 | var _loop = function _loop() {
|
---|
103 | var prefix = _step.value;
|
---|
104 | prefixeds[prefix] = toProcess.map(function (el) {
|
---|
105 | return _this2.replace(el, prefix);
|
---|
106 | }).join(', ');
|
---|
107 | };
|
---|
108 |
|
---|
109 | for (var _iterator = _createForOfIteratorHelperLoose(this.possible()), _step; !(_step = _iterator()).done;) {
|
---|
110 | _loop();
|
---|
111 | }
|
---|
112 | } else {
|
---|
113 | for (var _iterator2 = _createForOfIteratorHelperLoose(this.possible()), _step2; !(_step2 = _iterator2()).done;) {
|
---|
114 | var prefix = _step2.value;
|
---|
115 | prefixeds[prefix] = this.replace(rule.selector, prefix);
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | rule._autoprefixerPrefixeds[this.name] = prefixeds;
|
---|
120 | return rule._autoprefixerPrefixeds;
|
---|
121 | }
|
---|
122 | /**
|
---|
123 | * Is rule already prefixed before
|
---|
124 | */
|
---|
125 | ;
|
---|
126 |
|
---|
127 | _proto.already = function already(rule, prefixeds, prefix) {
|
---|
128 | var index = rule.parent.index(rule) - 1;
|
---|
129 |
|
---|
130 | while (index >= 0) {
|
---|
131 | var before = rule.parent.nodes[index];
|
---|
132 |
|
---|
133 | if (before.type !== 'rule') {
|
---|
134 | return false;
|
---|
135 | }
|
---|
136 |
|
---|
137 | var some = false;
|
---|
138 |
|
---|
139 | for (var key in prefixeds[this.name]) {
|
---|
140 | var prefixed = prefixeds[this.name][key];
|
---|
141 |
|
---|
142 | if (before.selector === prefixed) {
|
---|
143 | if (prefix === key) {
|
---|
144 | return true;
|
---|
145 | } else {
|
---|
146 | some = true;
|
---|
147 | break;
|
---|
148 | }
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 | if (!some) {
|
---|
153 | return false;
|
---|
154 | }
|
---|
155 |
|
---|
156 | index -= 1;
|
---|
157 | }
|
---|
158 |
|
---|
159 | return false;
|
---|
160 | }
|
---|
161 | /**
|
---|
162 | * Replace selectors by prefixed one
|
---|
163 | */
|
---|
164 | ;
|
---|
165 |
|
---|
166 | _proto.replace = function replace(selector, prefix) {
|
---|
167 | return selector.replace(this.regexp(), "$1" + this.prefixed(prefix));
|
---|
168 | }
|
---|
169 | /**
|
---|
170 | * Clone and add prefixes for at-rule
|
---|
171 | */
|
---|
172 | ;
|
---|
173 |
|
---|
174 | _proto.add = function add(rule, prefix) {
|
---|
175 | var prefixeds = this.prefixeds(rule);
|
---|
176 |
|
---|
177 | if (this.already(rule, prefixeds, prefix)) {
|
---|
178 | return;
|
---|
179 | }
|
---|
180 |
|
---|
181 | var cloned = this.clone(rule, {
|
---|
182 | selector: prefixeds[this.name][prefix]
|
---|
183 | });
|
---|
184 | rule.parent.insertBefore(rule, cloned);
|
---|
185 | }
|
---|
186 | /**
|
---|
187 | * Return function to fast find prefixed selector
|
---|
188 | */
|
---|
189 | ;
|
---|
190 |
|
---|
191 | _proto.old = function old(prefix) {
|
---|
192 | return new OldSelector(this, prefix);
|
---|
193 | };
|
---|
194 |
|
---|
195 | return Selector;
|
---|
196 | }(Prefixer);
|
---|
197 |
|
---|
198 | module.exports = Selector; |
---|