UNPKG

@wix/css-property-parser

Version:

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

85 lines (84 loc) 3.13 kB
// Shared padding property parser - Phase 2 simple properties // Handles parsing of all individual padding properties according to MDN specification // All padding properties share the same syntax: <length-percentage> | global-keywords // Note: Padding does NOT support 'auto' keyword and requires non-negative values import { parse as parseLength, toCSSValue as lengthToCSSValue } from '../evaluators/length.js'; import { parse as parsePercentage, toCSSValue as percentageToCSSValue } from '../evaluators/percentage.js'; import { parse as parseCSSVariable, toCSSValue as cssVariableToCSSValue } from '../evaluators/css-variable.js'; import { isCssVariable, isGlobalKeyword, isNonNegative } from '../utils/shared-utils.js'; // ======================================== // Shared Padding Evaluator // ======================================== /** * Shared parser for all individual padding properties * All padding properties (padding-top, padding-right, padding-bottom, padding-left) * have identical syntax: <length-percentage> | global-keywords * Note: Unlike margins, padding does NOT support 'auto' and requires non-negative values * * @param value - The CSS padding property string * @returns Parsed padding value or null if invalid */ export function parsePaddingProperty(value) { if (!value || typeof value !== 'string') { return null; } const trimmed = value.trim(); if (trimmed === '') { return null; } // CSS variables return parsed variable object if (isCssVariable(trimmed)) { return parseCSSVariable(trimmed); } // Global keywords only (no 'auto' for padding) if (isGlobalKeyword(trimmed)) { return { type: 'keyword', keyword: trimmed.toLowerCase() }; } // Try atomic types (ORDER MATTERS: percentage before length for % detection) const percentageResult = parsePercentage(trimmed); if (percentageResult) { // Padding values must be non-negative if (!isNonNegative(percentageResult)) { return null; } return percentageResult; } const lengthResult = parseLength(trimmed); if (lengthResult) { // Padding values must be non-negative if (!isNonNegative(lengthResult)) { return null; } return lengthResult; } return null; } /** * Shared toCSSValue function for all individual padding properties * @param parsed - The parsed padding value * @returns CSS value string or null if invalid */ export function paddingToCSSValue(parsed) { if (!parsed) { return null; } // Handle CSS variables if ('CSSvariable' in parsed) { return cssVariableToCSSValue(parsed); } // Handle keywords if ('keyword' in parsed) { return parsed.keyword; } // Handle length values const lengthResult = lengthToCSSValue(parsed); if (lengthResult !== null) { return lengthResult; } // Handle percentage values const percentageResult = percentageToCSSValue(parsed); if (percentageResult !== null) { return percentageResult; } return null; }