@wix/css-property-parser
Version:
A comprehensive TypeScript library for parsing and serializing CSS property values with full MDN specification compliance
68 lines (67 loc) • 2.18 kB
JavaScript
;
// Font Stretch property parser
// Handles parsing of CSS font-stretch property values
// https://developer.mozilla.org/en-US/docs/Web/CSS/font-stretch
Object.defineProperty(exports, "__esModule", { value: true });
exports.parse = parse;
exports.toCSSValue = toCSSValue;
const shared_utils_1 = require('../utils/shared-utils.cjs');
const css_variable_1 = require('./css-variable.cjs');
const percentage_1 = require('./percentage.cjs');
// Import centralized types
const types_1 = require('../types.cjs');
/**
* Parse a CSS font-stretch property string
*/
function parse(value) {
if (!value || typeof value !== 'string') {
return null;
}
const trimmed = value.trim();
if (trimmed === '') {
return null;
}
// CSS variables - parse and return directly
if ((0, shared_utils_1.isCssVariable)(trimmed)) {
return (0, css_variable_1.parse)(trimmed);
}
// Handle global keywords
if ((0, shared_utils_1.isGlobalKeyword)(trimmed)) {
return { type: 'keyword', keyword: trimmed.toLowerCase() };
}
// Handle font-stretch keywords
const fontStretchKeyword = (0, shared_utils_1.getValidKeyword)(trimmed, types_1.FONT_STRETCH_KEYWORDS);
if (fontStretchKeyword) {
return { type: 'keyword', keyword: fontStretchKeyword };
}
// Try parsing as percentage (50% - 200% are valid)
const percentageResult = (0, percentage_1.parse)(trimmed);
if (percentageResult && 'value' in percentageResult) {
const val = percentageResult.value;
// CSS font-stretch allows 50% to 200%
if (val >= 50 && val <= 200) {
return percentageResult;
}
}
return null;
}
/**
* Convert FontStretchValue back to CSS string
*/
function toCSSValue(parsed) {
if (!parsed) {
return null;
}
// Handle CSS variables
if ('CSSvariable' in parsed) {
return (0, css_variable_1.toCSSValue)(parsed);
}
if ('keyword' in parsed) {
return parsed.keyword;
}
// Handle percentage values
if ('value' in parsed && 'unit' in parsed) {
return (0, percentage_1.toCSSValue)(parsed);
}
return null;
}