.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / autoprefixer / lib / selector.js
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 _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
6
7 function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
8
9 function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : _defaults(subClass, superClass); }
10
11 var OldSelector = require('./old-selector');
12 var Prefixer = require('./prefixer');
13 var Browsers = require('./browsers');
14 var utils = require('./utils');
15
16 var Selector = function (_Prefixer) {
17     _inherits(Selector, _Prefixer);
18
19     function Selector(name, prefixes, all) {
20         _classCallCheck(this, Selector);
21
22         var _this = _possibleConstructorReturn(this, _Prefixer.call(this, name, prefixes, all));
23
24         _this.regexpCache = {};
25         return _this;
26     }
27
28     /**
29      * Is rule selectors need to be prefixed
30      */
31
32
33     Selector.prototype.check = function check(rule) {
34         if (rule.selector.indexOf(this.name) !== -1) {
35             return !!rule.selector.match(this.regexp());
36         }
37
38         return false;
39     };
40
41     /**
42      * Return prefixed version of selector
43      */
44
45
46     Selector.prototype.prefixed = function prefixed(prefix) {
47         return this.name.replace(/^([^\w]*)/, '$1' + prefix);
48     };
49
50     /**
51      * Lazy loadRegExp for name
52      */
53
54
55     Selector.prototype.regexp = function regexp(prefix) {
56         if (this.regexpCache[prefix]) {
57             return this.regexpCache[prefix];
58         }
59
60         var name = prefix ? this.prefixed(prefix) : this.name;
61         this.regexpCache[prefix] = new RegExp('(^|[^:"\'=])' + utils.escapeRegexp(name), 'gi');
62         return this.regexpCache[prefix];
63     };
64
65     /**
66      * All possible prefixes
67      */
68
69
70     Selector.prototype.possible = function possible() {
71         return Browsers.prefixes();
72     };
73
74     /**
75      * Return all possible selector prefixes
76      */
77
78
79     Selector.prototype.prefixeds = function prefixeds(rule) {
80         if (rule._autoprefixerPrefixeds) {
81             return rule._autoprefixerPrefixeds;
82         }
83
84         var prefixeds = {};
85         for (var _iterator = this.possible(), _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
86             var _ref;
87
88             if (_isArray) {
89                 if (_i >= _iterator.length) break;
90                 _ref = _iterator[_i++];
91             } else {
92                 _i = _iterator.next();
93                 if (_i.done) break;
94                 _ref = _i.value;
95             }
96
97             var prefix = _ref;
98
99             prefixeds[prefix] = this.replace(rule.selector, prefix);
100         }
101
102         rule._autoprefixerPrefixeds = prefixeds;
103         return rule._autoprefixerPrefixeds;
104     };
105
106     /**
107      * Is rule already prefixed before
108      */
109
110
111     Selector.prototype.already = function already(rule, prefixeds, prefix) {
112         var index = rule.parent.index(rule) - 1;
113
114         while (index >= 0) {
115             var before = rule.parent.nodes[index];
116
117             if (before.type !== 'rule') {
118                 return false;
119             }
120
121             var some = false;
122             for (var key in prefixeds) {
123                 var prefixed = prefixeds[key];
124                 if (before.selector === prefixed) {
125                     if (prefix === key) {
126                         return true;
127                     } else {
128                         some = true;
129                         break;
130                     }
131                 }
132             }
133             if (!some) {
134                 return false;
135             }
136
137             index -= 1;
138         }
139
140         return false;
141     };
142
143     /**
144      * Replace selectors by prefixed one
145      */
146
147
148     Selector.prototype.replace = function replace(selector, prefix) {
149         return selector.replace(this.regexp(), '$1' + this.prefixed(prefix));
150     };
151
152     /**
153      * Clone and add prefixes for at-rule
154      */
155
156
157     Selector.prototype.add = function add(rule, prefix) {
158         var prefixeds = this.prefixeds(rule);
159
160         if (this.already(rule, prefixeds, prefix)) {
161             return;
162         }
163
164         var cloned = this.clone(rule, { selector: prefixeds[prefix] });
165         rule.parent.insertBefore(rule, cloned);
166     };
167
168     /**
169      * Return function to fast find prefixed selector
170      */
171
172
173     Selector.prototype.old = function old(prefix) {
174         return new OldSelector(this, prefix);
175     };
176
177     return Selector;
178 }(Prefixer);
179
180 module.exports = Selector;