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 _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _defaults(subClass, superClass); }
|
6 |
|
7 | var OldSelector = require('./old-selector');
|
8 |
|
9 | var Prefixer = require('./prefixer');
|
10 |
|
11 | var Browsers = require('./browsers');
|
12 |
|
13 | var utils = require('./utils');
|
14 |
|
15 | var Selector =
|
16 |
|
17 | function (_Prefixer) {
|
18 | _inheritsLoose(Selector, _Prefixer);
|
19 |
|
20 | function Selector(name, prefixes, all) {
|
21 | var _this;
|
22 |
|
23 | _this = _Prefixer.call(this, name, prefixes, all) || this;
|
24 | _this.regexpCache = {};
|
25 | return _this;
|
26 | }
|
27 | |
28 |
|
29 |
|
30 |
|
31 |
|
32 | var _proto = Selector.prototype;
|
33 |
|
34 | _proto.check = function check(rule) {
|
35 | if (rule.selector.indexOf(this.name) !== -1) {
|
36 | return !!rule.selector.match(this.regexp());
|
37 | }
|
38 |
|
39 | return false;
|
40 | }
|
41 | |
42 |
|
43 |
|
44 | ;
|
45 |
|
46 | _proto.prefixed = function prefixed(prefix) {
|
47 | return this.name.replace(/^([^\w]*)/, "$1" + prefix);
|
48 | }
|
49 | |
50 |
|
51 |
|
52 | ;
|
53 |
|
54 | _proto.regexp = function regexp(prefix) {
|
55 | if (this.regexpCache[prefix]) {
|
56 | return this.regexpCache[prefix];
|
57 | }
|
58 |
|
59 | var name = prefix ? this.prefixed(prefix) : this.name;
|
60 | this.regexpCache[prefix] = new RegExp("(^|[^:\"'=])" + utils.escapeRegexp(name), 'gi');
|
61 | return this.regexpCache[prefix];
|
62 | }
|
63 | |
64 |
|
65 |
|
66 | ;
|
67 |
|
68 | _proto.possible = function possible() {
|
69 | return Browsers.prefixes();
|
70 | }
|
71 | |
72 |
|
73 |
|
74 | ;
|
75 |
|
76 | _proto.prefixeds = function prefixeds(rule) {
|
77 | if (rule._autoprefixerPrefixeds) {
|
78 | return rule._autoprefixerPrefixeds;
|
79 | }
|
80 |
|
81 | var prefixeds = {};
|
82 |
|
83 | for (var _iterator = this.possible(), _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
|
84 | var _ref;
|
85 |
|
86 | if (_isArray) {
|
87 | if (_i >= _iterator.length) break;
|
88 | _ref = _iterator[_i++];
|
89 | } else {
|
90 | _i = _iterator.next();
|
91 | if (_i.done) break;
|
92 | _ref = _i.value;
|
93 | }
|
94 |
|
95 | var prefix = _ref;
|
96 | prefixeds[prefix] = this.replace(rule.selector, prefix);
|
97 | }
|
98 |
|
99 | rule._autoprefixerPrefixeds = prefixeds;
|
100 | return rule._autoprefixerPrefixeds;
|
101 | }
|
102 | |
103 |
|
104 |
|
105 | ;
|
106 |
|
107 | _proto.already = function already(rule, prefixeds, prefix) {
|
108 | var index = rule.parent.index(rule) - 1;
|
109 |
|
110 | while (index >= 0) {
|
111 | var before = rule.parent.nodes[index];
|
112 |
|
113 | if (before.type !== 'rule') {
|
114 | return false;
|
115 | }
|
116 |
|
117 | var some = false;
|
118 |
|
119 | for (var key in prefixeds) {
|
120 | var prefixed = prefixeds[key];
|
121 |
|
122 | if (before.selector === prefixed) {
|
123 | if (prefix === key) {
|
124 | return true;
|
125 | } else {
|
126 | some = true;
|
127 | break;
|
128 | }
|
129 | }
|
130 | }
|
131 |
|
132 | if (!some) {
|
133 | return false;
|
134 | }
|
135 |
|
136 | index -= 1;
|
137 | }
|
138 |
|
139 | return false;
|
140 | }
|
141 | |
142 |
|
143 |
|
144 | ;
|
145 |
|
146 | _proto.replace = function replace(selector, prefix) {
|
147 | return selector.replace(this.regexp(), "$1" + this.prefixed(prefix));
|
148 | }
|
149 | |
150 |
|
151 |
|
152 | ;
|
153 |
|
154 | _proto.add = function add(rule, prefix) {
|
155 | var prefixeds = this.prefixeds(rule);
|
156 |
|
157 | if (this.already(rule, prefixeds, prefix)) {
|
158 | return;
|
159 | }
|
160 |
|
161 | var cloned = this.clone(rule, {
|
162 | selector: prefixeds[prefix]
|
163 | });
|
164 | rule.parent.insertBefore(rule, cloned);
|
165 | }
|
166 | |
167 |
|
168 |
|
169 | ;
|
170 |
|
171 | _proto.old = function old(prefix) {
|
172 | return new OldSelector(this, prefix);
|
173 | };
|
174 |
|
175 | return Selector;
|
176 | }(Prefixer);
|
177 |
|
178 | module.exports = Selector; |
\ | No newline at end of file |