UNPKG

@wix/css-property-parser

Version:

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

107 lines (106 loc) 3.73 kB
"use strict"; // 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 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 number_1 = require('./number.cjs'); const css_function_parser_1 = require('../utils/css-function-parser.cjs'); const types_1 = require('../types.cjs'); /** * 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 */ 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); } // Global keywords if ((0, shared_utils_1.isGlobalKeyword)(trimmed)) { return { type: 'keyword', keyword: trimmed.toLowerCase() }; } // Z-index-specific keywords (auto) const zIndexKeyword = (0, shared_utils_1.getValidKeyword)(trimmed, types_1.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 = (0, css_function_parser_1.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 = (0, number_1.parse)(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 */ function toCSSValue(parsed) { if (!parsed) return null; if (parsed.type === 'variable') { return (0, css_variable_1.toCSSValue)(parsed); } if (parsed.type === 'keyword') { return parsed.keyword; } if (parsed.type === 'number') { return (0, number_1.toCSSValue)(parsed); } return null; }