UNPKG

@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.73 kB
"use strict"; // Text-align property parser // Handles parsing of CSS text-align property according to MDN specification // https://developer.mozilla.org/en-US/docs/Web/CSS/text-align Object.defineProperty(exports, "__esModule", { value: true }); exports.parse = parse; exports.toCSSValue = toCSSValue; const string_1 = require('./string.cjs'); const css_variable_1 = require('./css-variable.cjs'); const shared_utils_1 = require('../utils/shared-utils.cjs'); const types_1 = require('../types.cjs'); /** * Checks if a string is a valid text-align keyword */ function isTextAlignKeyword(value) { return (0, shared_utils_1.isKeywordInArray)(value, types_1.TEXT_ALIGN_KEYWORDS); } /** * Parses a CSS text-align property string into structured components * Follows MDN specification: https://developer.mozilla.org/en-US/docs/Web/CSS/text-align */ function parse(value) { if (!value || typeof value !== 'string') { return null; } const trimmed = value.trim(); if (trimmed === '') { return null; } // CSS variables can be parsed 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-align specific keywords if (isTextAlignKeyword(trimmed)) { return { type: 'keyword', keyword: trimmed.toLowerCase() }; } // Handle string values (for custom alignment characters in table cells) const stringResult = (0, string_1.parse)(trimmed); if (stringResult) { // Handle CSS variables from string parser if ('CSSvariable' in stringResult) { return stringResult; } return stringResult; } return null; } /** * Converts a parsed text-align value back to a CSS string */ function toCSSValue(parsed) { if (!parsed) { return null; } // Handle CSS variables if ('CSSvariable' in parsed) { return (0, css_variable_1.toCSSValue)(parsed); } // Handle keywords if ('keyword' in parsed) { // Runtime validation: ensure keyword is actually valid for text-align if ((0, shared_utils_1.isGlobalKeyword)(parsed.keyword) || (0, shared_utils_1.isKeywordInArray)(parsed.keyword, types_1.TEXT_ALIGN_KEYWORDS)) { return parsed.keyword; } // Invalid keyword - return null instead of invalid CSS return null; } // Handle string values (delegate to string evaluator) if ('value' in parsed && 'quote' in parsed) { return (0, string_1.toCSSValue)(parsed); } return null; }