UNPKG

@wix/css-property-parser

Version:

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

103 lines (102 loc) 3.6 kB
// CSS z-index property evaluator // Handles CSS z-index property values according to MDN specification // https://developer.mozilla.org/en-US/docs/Web/CSS/z-index import { isCssVariable, isGlobalKeyword, getValidKeyword } from '../utils/shared-utils.js'; import { parse as parseCSSVariable, toCSSValue as cssVariableToCSSValue } from './css-variable.js'; import { parse as parseNumber, toCSSValue as numberToCSSValue } from './number.js'; import { parseCalcFunction } from '../utils/css-function-parser.js'; import { Z_INDEX_KEYWORDS } from '../types.js'; /** * Check if a string represents a valid integer for z-index * We need to validate the string form to reject decimal representations like "10.0" */ function isValidZIndexInteger(value) { // Must match integer pattern exactly (no decimals, no scientific notation) const integerPattern = /^[+-]?\d+$/; return integerPattern.test(value); } /** * Check if a number value is a valid integer for z-index */ function isValidZIndexNumber(numberValue) { // CSS variables can't be validated at parse time if ('CSSvariable' in numberValue) { return true; } if ('expression' in numberValue) { // Allow calc expressions (can't validate at parse time) return true; } if ('keyword' in numberValue) { // Allow keywords (inherit, initial, etc.) return true; } // Check if it's an integer (z-index doesn't accept decimals) return Number.isInteger(numberValue.value); } /** * Parses a CSS z-index property value according to MDN specification * * Supports: * - auto: Stack level equals parent element (default) * - Integer values: Positive, zero, or negative integers * - Global keywords: inherit, initial, unset, revert, revert-layer * - CSS variables: var(--custom-z-index) * * @param value - The CSS value to parse * @returns Parsed ZIndexValue or null if invalid */ export 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 (isCssVariable(trimmed)) { return parseCSSVariable(trimmed); } // Global keywords if (isGlobalKeyword(trimmed)) { return { type: 'keyword', keyword: trimmed.toLowerCase() }; } // Z-index-specific keywords (auto) const zIndexKeyword = getValidKeyword(trimmed, Z_INDEX_KEYWORDS); if (zIndexKeyword) { return { type: 'keyword', keyword: zIndexKeyword }; } // Handle calc() expressions (they can contain any expression, can't validate at parse time) const calcResult = parseCalcFunction(trimmed); if (calcResult) { return calcResult; } // Try parsing as number (integers only) // First validate that the string represents a valid integer if (isValidZIndexInteger(trimmed)) { const numberResult = parseNumber(trimmed); if (numberResult && isValidZIndexNumber(numberResult)) { return numberResult; } } return null; } /** * Converts a parsed ZIndexValue back to CSS string representation * * @param parsed - The parsed z-index value * @returns CSS string or null if invalid */ export function toCSSValue(parsed) { if (!parsed) return null; if (parsed.type === 'variable') { return cssVariableToCSSValue(parsed); } if (parsed.type === 'keyword') { return parsed.keyword; } if (parsed.type === 'number') { return numberToCSSValue(parsed); } return null; }