source: trip-planner-front/node_modules/postcss-merge-rules/dist/lib/ensureCompatibility.js@ 188ee53

Last change on this file since 188ee53 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 6.0 KB
Line 
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.filterPrefixes = filterPrefixes;
7exports.sameVendor = sameVendor;
8exports.noVendor = noVendor;
9exports.ensureCompatibility = ensureCompatibility;
10exports.pseudoElements = void 0;
11
12var _caniuseApi = require("caniuse-api");
13
14var _postcssSelectorParser = _interopRequireDefault(require("postcss-selector-parser"));
15
16var _vendors = _interopRequireDefault(require("vendors"));
17
18function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
19
20const simpleSelectorRe = /^#?[-._a-z0-9 ]+$/i;
21const cssSel2 = 'css-sel2';
22const cssSel3 = 'css-sel3';
23const cssGencontent = 'css-gencontent';
24const cssFirstLetter = 'css-first-letter';
25const cssFirstLine = 'css-first-line';
26const cssInOutOfRange = 'css-in-out-of-range';
27const formValidation = 'form-validation';
28/** @type {string[]} */
29
30const prefixes = _vendors.default.map(v => `-${v}-`);
31/**
32 * @param {string} selector
33 * @return {string[]}
34 */
35
36
37function filterPrefixes(selector) {
38 return prefixes.filter(prefix => selector.indexOf(prefix) !== -1);
39} // Internet Explorer use :-ms-input-placeholder.
40// Microsoft Edge use ::-ms-input-placeholder.
41
42
43const findMsInputPlaceholder = selector => ~selector.search(/-ms-input-placeholder/i);
44
45function sameVendor(selectorsA, selectorsB) {
46 let same = selectors => selectors.map(filterPrefixes).join();
47
48 let findMsVendor = selectors => selectors.find(findMsInputPlaceholder);
49
50 return same(selectorsA) === same(selectorsB) && !(findMsVendor(selectorsA) && findMsVendor(selectorsB));
51}
52/**
53 * @param {string} selector
54 * @return {boolean}
55 */
56
57
58function noVendor(selector) {
59 return !filterPrefixes(selector).length;
60}
61
62const pseudoElements = {
63 ':active': cssSel2,
64 ':after': cssGencontent,
65 ':any-link': 'css-any-link',
66 ':before': cssGencontent,
67 ':checked': cssSel3,
68 ':default': 'css-default-pseudo',
69 ':dir': 'css-dir-pseudo',
70 ':disabled': cssSel3,
71 ':empty': cssSel3,
72 ':enabled': cssSel3,
73 ':first-child': cssSel2,
74 ':first-letter': cssFirstLetter,
75 ':first-line': cssFirstLine,
76 ':first-of-type': cssSel3,
77 ':focus': cssSel2,
78 ':focus-within': 'css-focus-within',
79 ':focus-visible': 'css-focus-visible',
80 ':has': 'css-has',
81 ':hover': cssSel2,
82 ':in-range': cssInOutOfRange,
83 ':indeterminate': 'css-indeterminate-pseudo',
84 ':invalid': formValidation,
85 ':is': 'css-matches-pseudo',
86 ':lang': cssSel2,
87 ':last-child': cssSel3,
88 ':last-of-type': cssSel3,
89 ':link': cssSel2,
90 ':matches': 'css-matches-pseudo',
91 ':not': cssSel3,
92 ':nth-child': cssSel3,
93 ':nth-last-child': cssSel3,
94 ':nth-last-of-type': cssSel3,
95 ':nth-of-type': cssSel3,
96 ':only-child': cssSel3,
97 ':only-of-type': cssSel3,
98 ':optional': 'css-optional-pseudo',
99 ':out-of-range': cssInOutOfRange,
100 ':placeholder-shown': 'css-placeholder-shown',
101 ':required': formValidation,
102 ':root': cssSel3,
103 ':target': cssSel3,
104 '::after': cssGencontent,
105 '::backdrop': 'dialog',
106 '::before': cssGencontent,
107 '::first-letter': cssFirstLetter,
108 '::first-line': cssFirstLine,
109 '::marker': 'css-marker-pseudo',
110 '::placeholder': 'css-placeholder',
111 '::selection': 'css-selection',
112 ':valid': formValidation,
113 ':visited': cssSel2
114};
115exports.pseudoElements = pseudoElements;
116
117function isCssMixin(selector) {
118 return selector[selector.length - 1] === ':';
119}
120
121function isHostPseudoClass(selector) {
122 return selector.includes(':host');
123}
124
125const isSupportedCache = {}; // Move to util in future
126
127function isSupportedCached(feature, browsers) {
128 const key = JSON.stringify({
129 feature,
130 browsers
131 });
132 let result = isSupportedCache[key];
133
134 if (!result) {
135 result = (0, _caniuseApi.isSupported)(feature, browsers);
136 isSupportedCache[key] = result;
137 }
138
139 return result;
140}
141
142function ensureCompatibility(selectors, browsers, compatibilityCache) {
143 // Should not merge mixins
144 if (selectors.some(isCssMixin)) {
145 return false;
146 } // Should not merge :host selector https://github.com/angular/angular-cli/issues/18672
147
148
149 if (selectors.some(isHostPseudoClass)) {
150 return false;
151 }
152
153 return selectors.every(selector => {
154 if (simpleSelectorRe.test(selector)) {
155 return true;
156 }
157
158 if (compatibilityCache && selector in compatibilityCache) {
159 return compatibilityCache[selector];
160 }
161
162 let compatible = true;
163 (0, _postcssSelectorParser.default)(ast => {
164 ast.walk(node => {
165 const {
166 type,
167 value
168 } = node;
169
170 if (type === 'pseudo') {
171 const entry = pseudoElements[value];
172
173 if (!entry && noVendor(value)) {
174 compatible = false;
175 }
176
177 if (entry && compatible) {
178 compatible = isSupportedCached(entry, browsers);
179 }
180 }
181
182 if (type === 'combinator') {
183 if (~value.indexOf('~')) {
184 compatible = isSupportedCached(cssSel3, browsers);
185 }
186
187 if (~value.indexOf('>') || ~value.indexOf('+')) {
188 compatible = isSupportedCached(cssSel2, browsers);
189 }
190 }
191
192 if (type === 'attribute' && node.attribute) {
193 // [foo]
194 if (!node.operator) {
195 compatible = isSupportedCached(cssSel2, browsers);
196 }
197
198 if (value) {
199 // [foo="bar"], [foo~="bar"], [foo|="bar"]
200 if (~['=', '~=', '|='].indexOf(node.operator)) {
201 compatible = isSupportedCached(cssSel2, browsers);
202 } // [foo^="bar"], [foo$="bar"], [foo*="bar"]
203
204
205 if (~['^=', '$=', '*='].indexOf(node.operator)) {
206 compatible = isSupportedCached(cssSel3, browsers);
207 }
208 } // [foo="bar" i]
209
210
211 if (node.insensitive) {
212 compatible = isSupportedCached('css-case-insensitive', browsers);
213 }
214 }
215
216 if (!compatible) {
217 // If this node was not compatible,
218 // break out early from walking the rest
219 return false;
220 }
221 });
222 }).processSync(selector);
223
224 if (compatibilityCache) {
225 compatibilityCache[selector] = compatible;
226 }
227
228 return compatible;
229 });
230}
Note: See TracBrowser for help on using the repository browser.