UNPKG

@wix/css-property-parser

Version:

A comprehensive TypeScript library for parsing and serializing CSS property values with full MDN specification compliance

102 lines (101 loc) 3.41 kB
import { isCssVariable, isGlobalKeyword, getValidKeyword } from '../utils/shared-utils.js'; import { parse as parseCSSVariable, toCSSValue as cssVariableToCSSValue } from './css-variable.js'; import { parse as parseLength, toCSSValue as lengthToCSSValue } from './length.js'; import { parse as parsePercentage, toCSSValue as percentageToCSSValue } from './percentage.js'; import { TEXT_INDENT_KEYWORDS } from '../types.js'; /** * Parses CSS text-indent property values * * Syntax: <length> | <percentage> | [ each-line || hanging ] | inherit | initial | unset | revert * * @param value - The CSS text-indent value to parse * @returns Parsed TextIndentValue or null if invalid * * @example * ```typescript * parse('2em') // { value: 2, unit: 'em' } * parse('50%') // { value: 50, unit: '%' } * parse('each-line') // { type: 'keyword', keyword: 'each-line' } * parse('hanging') // { type: 'keyword', keyword: 'hanging' } * parse('inherit') // { type: 'keyword', keyword: 'inherit' } * parse('var(--my-indent)') // { type: 'variable', variable: 'my-indent' } * ``` */ export 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 (isCssVariable(trimmed)) { return parseCSSVariable(trimmed); } // Handle global keywords if (isGlobalKeyword(trimmed)) { return { type: 'keyword', keyword: trimmed.toLowerCase() }; } // Handle text-indent specific keywords (experimental) const keywordResult = getValidKeyword(trimmed.toLowerCase(), TEXT_INDENT_KEYWORDS); if (keywordResult) { return { type: 'keyword', keyword: keywordResult }; } // Parse percentage values first (to handle % correctly) const percentageResult = parsePercentage(trimmed); if (percentageResult) { return percentageResult; } // Parse length values (including negative values) const lengthResult = parseLength(trimmed); if (lengthResult) { return lengthResult; } return null; } /** * Converts a parsed TextIndentValue back to a CSS string * * @param parsed - The parsed TextIndentValue to convert * @returns CSS string representation or null if invalid * * @example * ```typescript * toCSSValue({ value: 2, unit: 'em' }) // '2em' * toCSSValue({ value: 50, unit: '%' }) // '50%' * toCSSValue({ type: 'keyword', keyword: 'each-line' }) // 'each-line' * ``` */ export function toCSSValue(parsed) { if (!parsed) { return null; } // Handle CSS variables if ('variable' in parsed || 'CSSvariable' in parsed) { return cssVariableToCSSValue(parsed); } // Handle keywords if ('keyword' in parsed && 'type' in parsed) { return parsed.keyword; } // Handle CSS functions if ('function' in parsed && 'expression' in parsed) { return `${parsed.function}(${parsed.expression})`; } // Handle length values if ('value' in parsed && 'unit' in parsed && parsed.unit !== '%') { return lengthToCSSValue(parsed); } // Handle percentage values if ('value' in parsed && 'unit' in parsed && parsed.unit === '%') { return percentageToCSSValue(parsed); } return null; }