UNPKG

@wix/css-property-parser

Version:

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

76 lines (75 loc) 2.58 kB
"use strict"; // Shared Border Width Property Parser // Provides shared parsing logic for individual border width properties // borderTopWidth, borderRightWidth, borderBottomWidth, borderLeftWidth Object.defineProperty(exports, "__esModule", { value: true }); exports.parseBorderWidthProperty = parseBorderWidthProperty; exports.borderWidthToCSSValue = borderWidthToCSSValue; const length_1 = require('./length.cjs'); const css_variable_1 = require('./css-variable.cjs'); const shared_utils_1 = require('../utils/shared-utils.cjs'); // Import centralized types const types_1 = require('../types.cjs'); /** * Check if value is a border width keyword */ function isBorderWidthKeyword(value) { return types_1.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 */ 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 ((0, shared_utils_1.isCssVariable)(trimmed)) { return (0, css_variable_1.parse)(trimmed); } // Global keywords if ((0, shared_utils_1.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 = (0, length_1.parse)(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 */ function borderWidthToCSSValue(parsed) { if (!parsed) { return null; } // Handle CSS variables if ('CSSvariable' in parsed) { return (0, css_variable_1.toCSSValue)(parsed); } // Handle keywords (global + border width keywords) if ('keyword' in parsed) { return parsed.keyword; } // Handle length values by delegating to length toCSSValue return (0, length_1.toCSSValue)(parsed); }