@wix/css-property-parser
Version:
A comprehensive TypeScript library for parsing and serializing CSS property values with full MDN specification compliance
87 lines (86 loc) • 2.45 kB
JavaScript
;
// Blend Mode data type parser
// Handles parsing of CSS blend mode values according to MDN specification
// https://developer.mozilla.org/en-US/docs/Web/CSS/blend-mode
Object.defineProperty(exports, "__esModule", { value: true });
exports.parse = parse;
exports.toCSSValue = toCSSValue;
const shared_utils_1 = require('../utils/shared-utils.cjs');
const css_variable_1 = require('./css-variable.cjs');
// CSS blend mode values
const BLEND_MODE_VALUES = [
'normal',
'multiply',
'screen',
'overlay',
'darken',
'lighten',
'color-dodge',
'color-burn',
'hard-light',
'soft-light',
'difference',
'exclusion',
'hue',
'saturation',
'color',
'luminosity'
];
/**
* Parses a CSS blend mode value into structured components
* @param input - The CSS blend mode string
* @returns Parsed blend mode object or null if invalid
*/
function parse(input) {
if (!input || typeof input !== 'string') {
return null;
}
const trimmed = input.trim();
if (trimmed === '') {
return null;
}
// Check for CSS variables - parse them if valid
if ((0, shared_utils_1.isCssVariable)(trimmed)) {
return (0, css_variable_1.parse)(trimmed);
}
const lowerValue = trimmed.toLowerCase();
// Check for global keywords first
if ((0, shared_utils_1.isGlobalKeyword)(lowerValue)) {
return {
type: 'blend-mode',
mode: lowerValue
};
}
// Check for blend mode values
if (BLEND_MODE_VALUES.includes(lowerValue)) {
return {
type: 'blend-mode',
mode: lowerValue
};
}
return null;
}
/**
* Converts a parsed blend mode back to a CSS value string
* @param parsed - The parsed blend mode object
* @returns CSS value string or null if invalid
*/
function toCSSValue(parsed) {
if (!parsed) {
return null;
}
// Handle CSS variables
if ('CSSvariable' in parsed) {
if (parsed.defaultValue) {
return `var(${parsed.CSSvariable}, ${parsed.defaultValue})`;
}
return `var(${parsed.CSSvariable})`;
}
// Handle blend mode values - cast to CSSBlendModeValue after type guard
const blendModeValue = parsed;
// Validate the parsed blend mode
if (typeof blendModeValue.mode !== 'string' || blendModeValue.mode.trim() === '') {
return null;
}
return blendModeValue.mode;
}