@wix/css-property-parser
Version:
A comprehensive TypeScript library for parsing and serializing CSS property values with full MDN specification compliance
83 lines (82 loc) • 2.56 kB
JavaScript
"use strict";
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 overflow-wrap property values
*
* Syntax: normal | anywhere | break-word | inherit | initial | unset | revert
*
* @param value - The CSS overflow-wrap value to parse
* @returns Parsed OverflowWrapValue or null if invalid
*
* @example
* ```typescript
* parse('normal') // { type: 'keyword', keyword: 'normal' }
* parse('anywhere') // { type: 'keyword', keyword: 'anywhere' }
* parse('break-word') // { type: 'keyword', keyword: 'break-word' }
* parse('inherit') // { type: 'keyword', keyword: 'inherit' }
* parse('var(--wrap)') // { type: 'variable', variable: 'wrap' }
* parse('invalid') // null
* ```
*/
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 overflow-wrap specific keywords
const keyword = (0, shared_utils_1.getValidKeyword)(trimmed, types_1.OVERFLOW_WRAP_KEYWORDS);
if (keyword) {
return {
type: 'keyword',
keyword: keyword
};
}
return null;
}
/**
* Converts a parsed OverflowWrapValue back to a CSS string
*
* @param parsed - The parsed OverflowWrapValue to convert
* @returns CSS string representation or null if invalid
*
* @example
* ```typescript
* toCSSValue({ type: 'keyword', keyword: 'normal' }) // 'normal'
* toCSSValue({ type: 'keyword', keyword: 'anywhere' }) // 'anywhere'
* toCSSValue({ type: 'keyword', keyword: 'break-word' }) // 'break-word'
* toCSSValue({ type: 'keyword', keyword: 'inherit' }) // 'inherit'
* ```
*/
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;
}