@wix/css-property-parser
Version:
A comprehensive TypeScript library for parsing and serializing CSS property values with full MDN specification compliance
62 lines (61 loc) • 1.85 kB
JavaScript
// CSS flex-direction property parser
// Handles parsing of CSS flex-direction property according to MDN specification
// https://developer.mozilla.org/en-US/docs/Web/CSS/flex-direction
import { isCssVariable, isGlobalKeyword } from '../utils/shared-utils.js';
import { parse as parseCSSVariable, toCSSValue as cssVariableToCSSValue } from './css-variable.js';
import { FLEX_DIRECTION_KEYWORDS } from '../types.js';
/**
* Checks if a string is a valid flex-direction keyword
*/
function isFlexDirectionKeyword(value) {
return FLEX_DIRECTION_KEYWORDS.includes(value.toLowerCase());
}
/**
* Parses a CSS flex-direction property string into structured components
* Follows MDN specification: https://developer.mozilla.org/en-US/docs/Web/CSS/flex-direction
*/
export function parse(value) {
if (!value || typeof value !== 'string') {
return null;
}
const trimmed = value.trim();
if (trimmed === '') {
return null;
}
// CSS variables can be parsed directly
if (isCssVariable(trimmed)) {
return parseCSSVariable(trimmed);
}
// Handle global keywords
if (isGlobalKeyword(trimmed)) {
return {
type: 'keyword',
keyword: trimmed.toLowerCase()
};
}
// Handle flex-direction keywords
if (isFlexDirectionKeyword(trimmed)) {
return {
type: 'keyword',
keyword: trimmed.toLowerCase()
};
}
return null;
}
/**
* Converts a parsed flex-direction back to a CSS value string
*/
export function toCSSValue(parsed) {
if (!parsed) {
return null;
}
// Handle CSS variables
if ('CSSvariable' in parsed) {
return cssVariableToCSSValue(parsed);
}
// Handle keyword values
if ('keyword' in parsed) {
return parsed.keyword;
}
return null;
}