lepu-test-platform-web/node_modules/postcss-merge-rules/dist/lib/ensureCompatibility.js

140 lines
4.7 KiB
JavaScript
Raw Normal View History

'use strict';
exports.__esModule = true;
exports.pseudoElements = undefined;
exports.default = ensureCompatibility;
var _caniuseApi = require('caniuse-api');
var _postcssSelectorParser = require('postcss-selector-parser');
var _postcssSelectorParser2 = _interopRequireDefault(_postcssSelectorParser);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var simpleSelectorRe = /^#?[-._a-z0-9 ]+$/i;
var cssSel2 = 'css-sel2';
var cssSel3 = 'css-sel3';
var cssGencontent = 'css-gencontent';
var cssFirstLetter = 'css-first-letter';
var cssFirstLine = 'css-first-line';
var cssInOutOfRange = 'css-in-out-of-range';
var pseudoElements = exports.pseudoElements = {
':active': cssSel2,
':after': cssGencontent,
':before': cssGencontent,
':checked': cssSel3,
':default': 'css-default-pseudo',
':dir': 'css-dir-pseudo',
':disabled': cssSel3,
':empty': cssSel3,
':enabled': cssSel3,
':first-child': cssSel2,
':first-letter': cssFirstLetter,
':first-line': cssFirstLine,
':first-of-type': cssSel3,
':focus': cssSel2,
':focus-within': 'css-focus-within',
':has': 'css-has',
':hover': cssSel2,
':in-range': cssInOutOfRange,
':indeterminate': 'css-indeterminate-pseudo',
':lang': cssSel2,
':last-child': cssSel3,
':last-of-type': cssSel3,
':matches': 'css-matches-pseudo',
':not': cssSel3,
':nth-child': cssSel3,
':nth-last-child': cssSel3,
':nth-last-of-type': cssSel3,
':nth-of-type': cssSel3,
':only-child': cssSel3,
':only-of-type': cssSel3,
':optional': 'css-optional-pseudo',
':out-of-range': cssInOutOfRange,
':placeholder-shown': 'css-placeholder-shown',
':root': cssSel3,
':target': cssSel3,
'::after': cssGencontent,
'::backdrop': 'dialog',
'::before': cssGencontent,
'::first-letter': cssFirstLetter,
'::first-line': cssFirstLine,
'::marker': 'css-marker-pseudo',
'::placeholder': 'css-placeholder',
'::selection': 'css-selection'
};
function isCssMixin(selector) {
return selector[selector.length - 1] === ':';
}
function ensureCompatibility(selectors, browsers, compatibilityCache) {
// Should not merge mixins
if (selectors.some(isCssMixin)) {
return false;
}
return selectors.every(function (selector) {
if (simpleSelectorRe.test(selector)) {
return true;
}
if (compatibilityCache && selector in compatibilityCache) {
return compatibilityCache[selector];
}
var compatible = true;
(0, _postcssSelectorParser2.default)(function (ast) {
ast.walk(function (node) {
var type = node.type,
value = node.value;
if (type === 'pseudo') {
var entry = pseudoElements[value];
if (entry && compatible) {
compatible = (0, _caniuseApi.isSupported)(entry, browsers);
}
}
if (type === 'combinator') {
if (~value.indexOf('~')) {
compatible = (0, _caniuseApi.isSupported)(cssSel3, browsers);
}
if (~value.indexOf('>') || ~value.indexOf('+')) {
compatible = (0, _caniuseApi.isSupported)(cssSel2, browsers);
}
}
if (type === 'attribute' && node.attribute) {
// [foo]
if (!node.operator) {
compatible = (0, _caniuseApi.isSupported)(cssSel2, browsers);
}
if (value) {
// [foo="bar"], [foo~="bar"], [foo|="bar"]
if (~['=', '~=', '|='].indexOf(node.operator)) {
compatible = (0, _caniuseApi.isSupported)(cssSel2, browsers);
}
// [foo^="bar"], [foo$="bar"], [foo*="bar"]
if (~['^=', '$=', '*='].indexOf(node.operator)) {
compatible = (0, _caniuseApi.isSupported)(cssSel3, browsers);
}
}
// [foo="bar" i]
if (node.insensitive) {
compatible = (0, _caniuseApi.isSupported)('css-case-insensitive', browsers);
}
}
if (!compatible) {
// If this node was not compatible,
// break out early from walking the rest
return false;
}
});
}).process(selector);
if (compatibilityCache) {
compatibilityCache[selector] = compatible;
}
return compatible;
});
}