UNPKG

@wix/css-property-parser

Version:

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

77 lines (76 loc) 2.61 kB
"use strict"; // Letter Spacing property parser // Handles parsing of CSS letter-spacing property values // https://developer.mozilla.org/en-US/docs/Web/CSS/letter-spacing 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'); // Import centralized types const types_1 = require('../types.cjs'); /** * Parse a CSS letter-spacing property string */ 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 letter-spacing keywords const letterSpacingKeyword = (0, shared_utils_1.getValidKeyword)(trimmed, types_1.LETTER_SPACING_KEYWORDS); if (letterSpacingKeyword) { return { type: 'keyword', keyword: letterSpacingKeyword }; } // Length values (can be negative for letter-spacing) // Note: letter-spacing doesn't support percentages per CSS spec const lengthResult = (0, length_1.parse)(trimmed); if (lengthResult) { // Reject percentage values - letter-spacing doesn't support them if ('unit' in lengthResult && lengthResult.unit === '%') { return null; } // Reject calc expressions that contain percentages if ('expression' in lengthResult && lengthResult.expression.includes('%')) { return null; } return lengthResult; } return null; } /** * Convert LetterSpacingValue back to CSS string */ function toCSSValue(parsed) { if (!parsed) { return null; } // Handle CSS variables if ('CSSvariable' in parsed) { return (0, css_variable_1.toCSSValue)(parsed); } if ('keyword' in parsed) { return parsed.keyword; } // Handle length values if ('unit' in parsed && parsed.type === 'length') { return (0, length_1.toCSSValue)(parsed); } // Handle function expressions (calc, etc.) if ('expression' in parsed && parsed.type === 'function') { return `${parsed.function}(${parsed.expression})`; } return null; }