@wix/css-property-parser
Version:
A comprehensive TypeScript library for parsing and serializing CSS property values with full MDN specification compliance
79 lines (78 loc) • 2.32 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 word-break property values
*
* Syntax: normal | break-all | keep-all | break-word | inherit | initial | unset | revert
*
* @param value - The CSS word-break value to parse
* @returns Parsed WordBreakValue or null if invalid
*
* @example
* ```typescript
* parse('normal') // { type: 'keyword', keyword: 'normal' }
* parse('break-all') // { type: 'keyword', keyword: 'break-all' }
* parse('keep-all') // { type: 'keyword', keyword: 'keep-all' }
* ```
*/
function parse(value) {
if (!value || typeof value !== 'string') {
return null;
}
const trimmed = value.trim();
if (trimmed === '') {
return null;
}
// Handle CSS variables
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 word-break specific keywords
const keyword = (0, shared_utils_1.getValidKeyword)(trimmed, types_1.WORD_BREAK_KEYWORDS);
if (keyword) {
return {
type: 'keyword',
keyword: keyword
};
}
return null;
}
/**
* Converts a parsed WordBreakValue back to a CSS string
*
* @param parsed - The parsed WordBreakValue to convert
* @returns CSS string representation or null if invalid
*
* @example
* ```typescript
* toCSSValue({ type: 'keyword', keyword: 'normal' }) // 'normal'
* toCSSValue({ type: 'keyword', keyword: 'break-all' }) // 'break-all'
* toCSSValue({ type: 'keyword', keyword: 'keep-all' }) // 'keep-all'
* ```
*/
function toCSSValue(parsed) {
if (!parsed) {
return null;
}
// Handle CSS variables
if ('variable' in parsed || 'CSSvariable' in parsed) {
return (0, css_variable_1.toCSSValue)(parsed);
}
// Handle keywords
if ('keyword' in parsed) {
return parsed.keyword;
}
return null;
}