@wix/css-property-parser
Version:
A comprehensive TypeScript library for parsing and serializing CSS property values with full MDN specification compliance
73 lines (72 loc) • 2.24 kB
JavaScript
;
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 types_1 = require('../types.cjs');
/**
* Parses CSS text-overflow property values
*
* Syntax: clip | ellipsis | <string> | inherit | initial | unset | revert
*
* @param value - The CSS text-overflow value to parse
* @returns Parsed TextOverflowValue or null if invalid
*
* @example
* ```typescript
* parse('clip') // { type: 'keyword', keyword: 'clip' }
* parse('ellipsis') // { type: 'keyword', keyword: 'ellipsis' }
* parse('inherit') // { type: 'keyword', keyword: 'inherit' }
* parse('var(--my-overflow)') // { type: 'css-variable', variable: 'my-overflow' }
* ```
*/
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 text-overflow specific keywords
const keywordResult = (0, shared_utils_1.getValidKeyword)(trimmed.toLowerCase(), types_1.TEXT_OVERFLOW_KEYWORDS);
if (keywordResult) {
return {
type: 'keyword',
keyword: keywordResult
};
}
return null;
}
/**
* Converts a parsed TextOverflowValue back to its CSS string representation
*
* @param value - The parsed text-overflow value
* @returns CSS string representation or null if invalid
*/
function toCSSValue(value) {
if (!value) {
return null;
}
// Handle CSS variables
if (value.type === 'variable') {
return (0, css_variable_1.toCSSValue)(value);
}
// Handle keyword values (both text-overflow-specific and global)
if (value.type === 'keyword') {
return value.keyword;
}
return null;
}