UNPKG

@wix/css-property-parser

Version:

A comprehensive TypeScript library for parsing and serializing CSS property values with full MDN specification compliance

72 lines (71 loc) 2.44 kB
// Shared Border Width Property Parser // Provides shared parsing logic for individual border width properties // borderTopWidth, borderRightWidth, borderBottomWidth, borderLeftWidth import { parse as parseLength, toCSSValue as lengthToCSSValue } from './length.js'; import { parse as parseCSSVariable, toCSSValue as cssVariableToCSSValue } from './css-variable.js'; import { isCssVariable, isGlobalKeyword } from '../utils/shared-utils.js'; // Import centralized types import { BORDER_WIDTH_KEYWORDS } from '../types.js'; /** * Check if value is a border width keyword */ function isBorderWidthKeyword(value) { return BORDER_WIDTH_KEYWORDS.includes(value.toLowerCase()); } /** * Parse a border width property value (length or keyword) * Accepts non-negative lengths and border width keywords * * @param value - CSS border width value string * @returns Parsed border width value or null if invalid */ export function parseBorderWidthProperty(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 width keywords (thin, medium, thick) if (isBorderWidthKeyword(trimmed)) { return { type: 'keyword', keyword: trimmed.toLowerCase() }; } // Length values (non-negative only) const lengthResult = parseLength(trimmed); if (lengthResult && (('value' in lengthResult && lengthResult.value >= 0) || ('expression' in lengthResult))) { return lengthResult; } return null; } /** * Convert border width value back to CSS string * * @param parsed - Parsed border width value * @returns CSS string representation or null if invalid */ export function borderWidthToCSSValue(parsed) { if (!parsed) { return null; } // Handle CSS variables if ('CSSvariable' in parsed) { return cssVariableToCSSValue(parsed); } // Handle keywords (global + border width keywords) if ('keyword' in parsed) { return parsed.keyword; } // Handle length values by delegating to length toCSSValue return lengthToCSSValue(parsed); }