@wix/css-property-parser
Version:
A comprehensive TypeScript library for parsing and serializing CSS property values with full MDN specification compliance
63 lines (62 loc) • 2.05 kB
JavaScript
// Shared border style evaluator
// Provides parsing and serialization for individual border style properties
// Used by border-top-style, border-right-style, border-bottom-style, border-left-style
import { isCssVariable, isGlobalKeyword } from '../utils/shared-utils.js';
import { parse as parseCSSVariable, toCSSValue as cssVariableToCSSValue } from './css-variable.js';
// Import centralized types
import { BORDER_STYLE_KEYWORDS } from '../types.js';
/**
* Check if value is a border style keyword
*/
function isBorderStyleKeyword(value) {
return BORDER_STYLE_KEYWORDS.includes(value.toLowerCase());
}
/**
* Parse a border style property value (keyword only)
* Accepts border style keywords: none, hidden, dotted, dashed, solid, double, groove, ridge, inset, outset
*
* @param value - CSS border style value string
* @returns Parsed border style value or null if invalid
*/
export function parseBorderStyleProperty(value) {
if (!value || typeof value !== 'string') {
return null;
}
const trimmed = value.trim();
if (trimmed === '') {
return null;
}
// CSS variables - return proper CSSVariable object
if (isCssVariable(trimmed)) {
return parseCSSVariable(trimmed);
}
// Global keywords
if (isGlobalKeyword(trimmed)) {
return { type: 'keyword', keyword: trimmed.toLowerCase() };
}
// Border style keywords
if (isBorderStyleKeyword(trimmed)) {
return { type: 'keyword', keyword: trimmed.toLowerCase() };
}
return null;
}
/**
* Convert a border style property value back to CSS string
*
* @param parsed - Parsed border style value
* @returns CSS string representation or null if invalid
*/
export function borderStyleToCSSValue(parsed) {
if (!parsed) {
return null;
}
// Handle CSS variables
if ('CSSvariable' in parsed) {
return cssVariableToCSSValue(parsed);
}
// All border style values are keywords
if ('keyword' in parsed) {
return parsed.keyword;
}
return null;
}