@wix/css-property-parser
Version:
A comprehensive TypeScript library for parsing and serializing CSS property values with full MDN specification compliance
61 lines (60 loc) • 1.88 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
import { isCssVariable, isGlobalKeyword } from '../utils/shared-utils.js';
import { parse as parseCSSVariable, toCSSValue as cssVariableToCSSValue } from './css-variable.js';
import { BLEND_MODE_KEYWORDS } from '../types.js';
// Use centralized blend mode keywords from types.ts
/**
* 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
*/
export 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 (isCssVariable(trimmed)) {
return parseCSSVariable(trimmed);
}
const lowerValue = trimmed.toLowerCase();
// Check for global keywords first
if (isGlobalKeyword(lowerValue)) {
return {
type: 'keyword',
keyword: lowerValue
};
}
// Check for blend mode values
if (BLEND_MODE_KEYWORDS.includes(lowerValue)) {
return {
type: 'keyword',
keyword: 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
*/
export function toCSSValue(parsed) {
if (!parsed) {
return null;
}
// Handle CSS variables
if ('CSSvariable' in parsed) {
return cssVariableToCSSValue(parsed);
}
// Handle keyword values (both global and blend-mode specific)
if ('keyword' in parsed) {
return parsed.keyword;
}
return null;
}