UNPKG

@wix/css-property-parser

Version:

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

106 lines (105 loc) 3.47 kB
"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 length_1 = require('./length.cjs'); const percentage_1 = require('./percentage.cjs'); const types_1 = require('../types.cjs'); /** * 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' } * ``` */ 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-indent specific keywords (experimental) const keywordResult = (0, shared_utils_1.getValidKeyword)(trimmed.toLowerCase(), types_1.TEXT_INDENT_KEYWORDS); if (keywordResult) { return { type: 'keyword', keyword: keywordResult }; } // Parse percentage values first (to handle % correctly) const percentageResult = (0, percentage_1.parse)(trimmed); if (percentageResult) { return percentageResult; } // Parse length values (including negative values) const lengthResult = (0, length_1.parse)(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' * ``` */ 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 && '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 (0, length_1.toCSSValue)(parsed); } // Handle percentage values if ('value' in parsed && 'unit' in parsed && parsed.unit === '%') { return (0, percentage_1.toCSSValue)(parsed); } return null; }