@wix/css-property-parser
Version:
A comprehensive TypeScript library for parsing and serializing CSS property values with full MDN specification compliance
89 lines (88 loc) • 3.26 kB
JavaScript
"use strict";
// 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
Object.defineProperty(exports, "__esModule", { value: true });
exports.parsePaddingProperty = parsePaddingProperty;
exports.paddingToCSSValue = paddingToCSSValue;
const length_1 = require('../evaluators/length.cjs');
const percentage_1 = require('../evaluators/percentage.cjs');
const css_variable_1 = require('../evaluators/css-variable.cjs');
const shared_utils_1 = require('../utils/shared-utils.cjs');
// ========================================
// 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
*/
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 ((0, shared_utils_1.isCssVariable)(trimmed)) {
return (0, css_variable_1.parse)(trimmed);
}
// Global keywords only (no 'auto' for padding)
if ((0, shared_utils_1.isGlobalKeyword)(trimmed)) {
return { type: 'keyword', keyword: trimmed.toLowerCase() };
}
// Try atomic types (ORDER MATTERS: percentage before length for % detection)
const percentageResult = (0, percentage_1.parse)(trimmed);
if (percentageResult) {
// Padding values must be non-negative
if (!(0, shared_utils_1.isNonNegative)(percentageResult)) {
return null;
}
return percentageResult;
}
const lengthResult = (0, length_1.parse)(trimmed);
if (lengthResult) {
// Padding values must be non-negative
if (!(0, shared_utils_1.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
*/
function paddingToCSSValue(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
const lengthResult = (0, length_1.toCSSValue)(parsed);
if (lengthResult !== null) {
return lengthResult;
}
// Handle percentage values
const percentageResult = (0, percentage_1.toCSSValue)(parsed);
if (percentageResult !== null) {
return percentageResult;
}
return null;
}