@wix/css-property-parser
Version:
A comprehensive TypeScript library for parsing and serializing CSS property values with full MDN specification compliance
85 lines (84 loc) • 3.17 kB
JavaScript
;
// CSS logical border radius shared evaluator
// Eliminates code duplication across all logical border radius properties
// Maps to physical border radius properties based on writing mode and direction
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseLogicalBorderRadiusProperty = parseLogicalBorderRadiusProperty;
exports.logicalBorderRadiusToCSSValue = logicalBorderRadiusToCSSValue;
const length_1 = require('./length.cjs');
const percentage_1 = require('./percentage.cjs');
const css_variable_1 = require('./css-variable.cjs');
const shared_utils_1 = require('../utils/shared-utils.cjs');
/**
* Shared parser for all logical border radius properties
* Handles length, percentage, CSS functions, keywords, and variables with non-negative validation
*
* @param value - The CSS value to parse
* @returns Parsed value or null if invalid
*
* @example
* parseLogicalBorderRadiusProperty('10px') // { type: 'length', value: 10, unit: 'px' }
* parseLogicalBorderRadiusProperty('50%') // { type: 'percentage', value: 50, unit: '%' }
* parseLogicalBorderRadiusProperty('inherit') // { type: 'keyword', keyword: 'inherit' }
*/
function parseLogicalBorderRadiusProperty(value) {
if (!value || typeof value !== 'string') {
return null;
}
const trimmed = value.trim();
if (!trimmed) {
return null;
}
// CSS variables
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() };
}
// Try percentage first (to handle cases like '50%')
const percentageResult = (0, percentage_1.parse)(trimmed);
if (percentageResult && (0, shared_utils_1.isNonNegative)(percentageResult)) {
return percentageResult;
}
// Try length
const lengthResult = (0, length_1.parse)(trimmed);
if (lengthResult && (0, shared_utils_1.isNonNegative)(lengthResult)) {
return lengthResult;
}
return null;
}
/**
* Shared toCSSValue function for all logical border radius properties
* Converts parsed values back to CSS string representation
*
* @param parsed - The parsed value to convert
* @returns CSS string or null if invalid
*
* @example
* logicalBorderRadiusToCSSValue({ type: 'length', value: 10, unit: 'px' }) // '10px'
* logicalBorderRadiusToCSSValue({ type: 'percentage', value: 50, unit: '%' }) // '50%'
* logicalBorderRadiusToCSSValue({ type: 'keyword', keyword: 'inherit' }) // 'inherit'
*/
function logicalBorderRadiusToCSSValue(parsed) {
if (!parsed) {
return null;
}
if ('CSSvariable' in parsed) {
return (0, css_variable_1.toCSSValue)(parsed);
}
if ('keyword' in parsed) {
return parsed.keyword;
}
if (parsed.type === 'length') {
return `${parsed.value}${parsed.unit}`;
}
if (parsed.type === 'percentage') {
return `${parsed.value}%`;
}
if (parsed.type === 'function') {
return `${parsed.function}(${parsed.expression})`;
}
return null;
}