caniemail
Version:
HTML and CSS Feature Support for Email Clients from caniemail.com
86 lines • 3.49 kB
JavaScript
/* eslint-disable func-names */
import { getFeatures } from './features.js';
import { fromTitleEntries, getTitleMatches } from './helpers.js';
const features = getFeatures().css;
const keys = [...features.keys()];
const propertyNameRegex = /^[a-z-]+$/;
export const atRuleTitles = fromTitleEntries(keys.map((title) => {
if (!title.startsWith('@'))
return void 0;
return { title, value: title.replace(/^@/, '') };
}));
export const functionTitles = fromTitleEntries(keys.map((title) => {
if (title.startsWith(':'))
return void 0;
if (title === 'CSS Variables (Custom Properties)') {
return {
title,
value: 'var'
};
}
if (!title.includes('()'))
return void 0;
const matches = /([a-z-]+)\(\)/.exec(title);
if (matches === null) {
throw new Error(`Could not determine function name in ${title}`);
}
return { title, value: matches[1] };
}));
export const keywordTitles = fromTitleEntries(keys.map((title) => {
if (!title.includes(' keyword'))
return void 0;
return {
title,
value: title.replace(/ keyword$/, '')
};
}));
export const propertyValuePairTitles = fromTitleEntries(keys.map((title) => {
const matches = /([a-z-]+):\s*([a-z-]+)/.exec(title);
if (matches === null)
return void 0;
return { title, value: [matches[1], matches[2]] };
}));
// Map of property titles to the properties they support
export const propertyTitles = fromTitleEntries(keys.map((title) => {
if (title === 'left, right, top, bottom') {
return {
title,
value: ['left', 'right', 'top', 'bottom']
};
}
const trimmedTitle = title
.trim()
.replace(/ shorthand$/, '')
.replace(/ property$/, '');
if (propertyNameRegex.test(trimmedTitle))
return { title, value: [trimmedTitle] };
return void 0;
}));
export const unitTitles = fromTitleEntries(keys.map((title) => {
if (!title.endsWith(' unit'))
return void 0;
return { title, value: title.replace(/ unit$/, '') };
}));
export const getMatchingPropertyValuePairTitles = ({ propertyName, propertyValue }) => getTitleMatches(propertyValuePairTitles, '', (value) => value[0] === propertyName && value[1] === propertyValue);
export const getMatchingAtRuleTitles = ({ atRules }) => getTitleMatches(atRuleTitles, atRules);
export const getMatchingFunctionTitles = ({ propertyValue }) => {
const matchingFunctionNames = [];
// Match `<function>(` (e.g. `max(`, `calc(`)
const valueFunctionNames = new Set([...propertyValue.matchAll(/([a-z-]+)\(/g)].map((match) => match[1]));
for (const [functionName, functionTitle] of Object.entries(functionTitles)) {
if (valueFunctionNames.has(functionName)) {
matchingFunctionNames.push(functionTitle);
}
valueFunctionNames.delete(functionName);
}
return matchingFunctionNames;
};
export const getMatchingKeywordTitles = ({ propertyValue }) => getTitleMatches(keywordTitles, propertyValue);
export const getMatchingPropertyTitles = ({ propertyName }) => getTitleMatches(propertyTitles, propertyName);
export const getMatchingUnitTitles = ({ propertyValue }) => getTitleMatches(unitTitles, propertyValue, (value) => {
if (value === 'initial') {
return /\binitial\b/.test(propertyValue);
}
return new RegExp(`\\d${value}`).test(propertyValue);
});
//# sourceMappingURL=css-titles.js.map