'use strict';
const { isSupported } = require('caniuse-api');
const selectorParser = require('postcss-selector-parser');
const simpleSelectorRe = /^#?[-._a-z0-9 ]+$/i;
const cssSel2 = 'css-sel2';
const cssSel3 = 'css-sel3';
const cssGencontent = 'css-gencontent';
const cssFirstLetter = 'css-first-letter';
const cssFirstLine = 'css-first-line';
const cssInOutOfRange = 'css-in-out-of-range';
const formValidation = 'form-validation';
const vendorPrefix =
/-(ah|apple|atsc|epub|hp|khtml|moz|ms|o|rim|ro|tc|wap|webkit|xv)-/;
const level2Sel = new Set(['=', '~=', '|=']);
const level3Sel = new Set(['^=', '$=', '*=']);
function filterPrefixes(selector) {
return selector.match(vendorPrefix);
}
const findMsInputPlaceholder = (selector) =>
~selector.search(/-ms-input-placeholder/i);
function sameVendor(selectorsA, selectorsB) {
let same = (selectors) => selectors.map(filterPrefixes).join();
let findMsVendor = (selectors) => selectors.find(findMsInputPlaceholder);
return (
same(selectorsA) === same(selectorsB) &&
!(findMsVendor(selectorsA) && findMsVendor(selectorsB))
);
}
function noVendor(selector) {
return !vendorPrefix.test(selector);
}
const pseudoElements = {
':active': cssSel2,
':after': cssGencontent,
':any-link': 'css-any-link',
':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',
':focus-visible': 'css-focus-visible',
':has': 'css-has',
':hover': cssSel2,
':in-range': cssInOutOfRange,
':indeterminate': 'css-indeterminate-pseudo',
':invalid': formValidation,
':is': 'css-matches-pseudo',
':lang': cssSel2,
':last-child': cssSel3,
':last-of-type': cssSel3,
':link': cssSel2,
':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',
':required': formValidation,
':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',
':valid': formValidation,
':visited': cssSel2,
};
function isCssMixin(selector) {
return selector[selector.length - 1] === ':';
}
function isHostPseudoClass(selector) {
return selector.includes(':host');
}
const isSupportedCache = new Map();
function isSupportedCached(feature, browsers) {
const key = JSON.stringify({ feature, browsers });
let result = isSupportedCache.get(key);
if (!result) {
result = isSupported(feature, (browsers));
isSupportedCache.set(key, result);
}
return result;
}
function ensureCompatibility(selectors, browsers, compatibilityCache) {
if (selectors.some(isCssMixin)) {
return false;
}
if (selectors.some(isHostPseudoClass)) {
return false;
}
return selectors.every((selector) => {
if (simpleSelectorRe.test(selector)) {
return true;
}
if (compatibilityCache && compatibilityCache.has(selector)) {
return compatibilityCache.get(selector);
}
let compatible = true;
selectorParser((ast) => {
ast.walk((node) => {
const { type, value } = node;
if (type === 'pseudo') {
const entry =
pseudoElements[ (value)];
if (!entry && noVendor(value)) {
compatible = false;
}
if (entry && compatible) {
compatible = isSupportedCached(entry, browsers);
}
}
if (type === 'combinator') {
if (value.includes('~')) {
compatible = isSupportedCached(cssSel3, browsers);
}
if (value.includes('>') || value.includes('+')) {
compatible = isSupportedCached(cssSel2, browsers);
}
}
if (type === 'attribute' && node.attribute) {
if (!node.operator) {
compatible = isSupportedCached(cssSel2, browsers);
}
if (value) {
if (level2Sel.has( (node.operator))) {
compatible = isSupportedCached(cssSel2, browsers);
}
if (level3Sel.has( (node.operator))) {
compatible = isSupportedCached(cssSel3, browsers);
}
}
if (node.insensitive) {
compatible = isSupportedCached('css-case-insensitive', browsers);
}
}
if (!compatible) {
return false;
}
});
}).processSync(selector);
if (compatibilityCache) {
compatibilityCache.set(selector, compatible);
}
return compatible;
});
}
module.exports = { sameVendor, noVendor, pseudoElements, ensureCompatibility };