UNPKG

@wix/css-property-parser

Version:

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

88 lines (87 loc) 2.96 kB
"use strict"; // Shared Gap Property Evaluator // Handles column-gap, row-gap // Root Type Pattern: CSSLengthValue | CSSPercentageValue | NormalKeyword Object.defineProperty(exports, "__esModule", { value: true }); exports.parseGapProperty = parseGapProperty; exports.gapToCSSValue = gapToCSSValue; const shared_utils_1 = require('../utils/shared-utils.cjs'); const length_1 = require('./length.cjs'); const percentage_1 = require('./percentage.cjs'); const css_variable_1 = require('./css-variable.cjs'); /** * Generic gap property parser * @param value - CSS value string to parse * @returns Parsed gap value or null if invalid */ function parseGapProperty(value) { if (!value || typeof value !== 'string') { return null; } const trimmed = value.trim(); if (trimmed === '') { return null; } // CSS variables can be parsed if ((0, shared_utils_1.isCssVariable)(trimmed)) { return (0, css_variable_1.parse)(trimmed); } // Handle global keywords (inherit, initial, unset, revert, revert-layer) if ((0, shared_utils_1.isGlobalKeyword)(trimmed)) { return { type: 'keyword', keyword: trimmed.toLowerCase() }; } // Handle gap-specific keyword: normal if (trimmed.toLowerCase() === 'normal') { return { type: 'keyword', keyword: 'normal' }; } // Try to parse as percentage first (before length, since length includes %) const percentageResult = (0, percentage_1.parse)(trimmed); if (percentageResult) { // Gap percentages must be non-negative if (!(0, shared_utils_1.isNonNegative)(percentageResult)) { return null; } return percentageResult; } // Try to parse as length const lengthResult = (0, length_1.parse)(trimmed); if (lengthResult) { // Gap lengths must be non-negative if (!(0, shared_utils_1.isNonNegative)(lengthResult)) { return null; } return lengthResult; } return null; } /** * Generic gap property toCSSValue converter * @param parsed - Parsed gap value * @returns CSS string or null if invalid */ function gapToCSSValue(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) { return parsed.keyword; } // Handle length values (including calc expressions) if ('unit' in parsed) { return (0, length_1.toCSSValue)(parsed); } // Handle percentage values (including calc expressions) if ('value' in parsed && !('unit' in parsed)) { return (0, percentage_1.toCSSValue)(parsed); } // Handle calc expressions that could be length or percentage if ('expression' in parsed) { return (0, length_1.toCSSValue)(parsed) || (0, percentage_1.toCSSValue)(parsed); } return null; }