@wix/css-property-parser
Version:
A comprehensive TypeScript library for parsing and serializing CSS property values with full MDN specification compliance
50 lines (49 loc) • 1.49 kB
JavaScript
// Font Style property parser
// Handles parsing of CSS font-style property values
// https://developer.mozilla.org/en-US/docs/Web/CSS/font-style
import { isCssVariable, isGlobalKeyword, getValidKeyword } from '../utils/shared-utils.js';
import { parse as parseCSSVariable, toCSSValue as cssVariableToCSSValue } from './css-variable.js';
// Import centralized types
import { FONT_STYLE_KEYWORDS } from '../types.js';
/**
* Parse a CSS font-style property string
*/
export function parse(value) {
if (!value || typeof value !== 'string') {
return null;
}
const trimmed = value.trim();
if (trimmed === '') {
return null;
}
// CSS variables - parse and return directly
if (isCssVariable(trimmed)) {
return parseCSSVariable(trimmed);
}
// Handle global keywords
if (isGlobalKeyword(trimmed)) {
return { type: 'keyword', keyword: trimmed.toLowerCase() };
}
// Handle font-style keywords
const fontStyleKeyword = getValidKeyword(trimmed, FONT_STYLE_KEYWORDS);
if (fontStyleKeyword) {
return { type: 'keyword', keyword: fontStyleKeyword };
}
return null;
}
/**
* Convert FontStyleValue back to CSS string
*/
export function toCSSValue(parsed) {
if (!parsed) {
return null;
}
// Handle CSS variables
if ('CSSvariable' in parsed) {
return cssVariableToCSSValue(parsed);
}
if ('keyword' in parsed) {
return parsed.keyword;
}
return null;
}