You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP โ†’
Socket
Book a DemoInstallSign in
Socket

@wix/css-property-parser

Package Overview
Dependencies
Maintainers
0
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@wix/css-property-parser

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

1.21.0
latest
Source
npmnpm
Version published
Weekly downloads
513
2.81%
Maintainers
0
Weekly downloads
ย 
Created
Source

CSS Property Parser

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

๐ŸŒ Try the Interactive Demo โ†’

Test all CSS property parsers live in your browser with real-time parsing and examples.

๐Ÿš€ Features

  • 124 CSS Property Evaluators - Complete parsing support for layout, typography, colors, and more
  • Dual Package Support - Works with both ESM (import) and CommonJS (require)
  • MDN Compliant - Follows official CSS specifications exactly
  • TypeScript First - Full type safety with comprehensive type definitions for all CSS values
  • Round-Trip Stable - Parse โ†’ serialize โ†’ parse produces identical results
  • CSS Functions - Supports calc(), min(), max(), clamp(), gradients, and more
  • CSS Variables - Handles custom properties with fallback values
  • SPX Units - Supports Wix's SPX (Scalable-Pixel) units alongside standard CSS units
  • Performance Optimized - Fast parsing with minimal memory footprint
  • 8,428+ Tests - Comprehensive test coverage across 132 test suites with 100% pass rate
  • Shared Evaluator Pattern - 95% code reduction through intelligent code reuse

๐Ÿ“ฆ Installation

npm install @wix/css-property-parser

๐ŸŽฏ Usage

import { Width, Height, Margin, Background, Font } from '@wix/css-property-parser';

// Parse CSS values
const width = Width.parse('calc(100% - 20px)');
const background = Background.parse('linear-gradient(45deg, red, blue)');

// Serialize back to CSS
const widthCSS = Width.toCSSValue(width);     // "calc(100% - 20px)"
const bgCSS = Background.toCSSValue(background); // "linear-gradient(45deg, red, blue)"

CommonJS (Require)

const { Width, Height, Margin, Background, Font } = require('@wix/css-property-parser');

// Parse CSS values
const width = Width.parse('calc(100% - 20px)');
const background = Background.parse('linear-gradient(45deg, red, blue)');

// Serialize back to CSS
const widthCSS = Width.toCSSValue(width);     // "calc(100% - 20px)"
const bgCSS = Background.toCSSValue(background); // "linear-gradient(45deg, red, blue)"

TypeScript Types

import { 
  Width, Height, Background, Font, Margin, Color, Position,
  // Import types for parsed values
  WidthValue, HeightValue, BackgroundValue, FontValue, MarginValue,
  ColorValue, PositionValue, LengthPercentageValue,
  // Import atomic types
  CSSLengthValue, CSSPercentageValue, LengthValue, PercentageValue
} from '@wix/css-property-parser';

// Use types for variables
const width: WidthValue | null = Width.parse('100px');
const background: BackgroundValue | null = Background.parse('red');
const margin: MarginValue | null = Margin.parse('10px 20px');
const color: ColorValue | null = Color.parse('#ff0000');
const position: PositionValue | null = Position.parse('absolute');

// Type-safe functions
function processWidth(value: string): WidthValue | null {
  return Width.parse(value);
}

function serializeFont(font: FontValue): string {
  return Font.toCSSValue(font) || '';
}

// All types are available from the main package for convenience
// This includes both property value types and atomic CSS types

๐ŸŽฏ Advanced Examples

Note: All examples work with both ESM (import) and CommonJS (require). For TypeScript type imports, see Usage section above.

import { Width, Height, Margin, Background, Font } from '@wix/css-property-parser';

// Parse CSS values with advanced features
const width = Width.parse('calc(100% - 20px)');
const height = Height.parse('50vh');
const margin = Margin.parse('10spx 20spx');  // SPX (Scalable-Pixel) support
const background = Background.parse('linear-gradient(45deg, red, blue)');
const font = Font.parse('bold 16px/1.5 "Helvetica Neue", Arial, sans-serif');

// Serialize back to CSS
const widthCSS = Width.toCSSValue(width);     // "calc(100% - 20px)"
const heightCSS = Height.toCSSValue(height);  // "50vh"
const marginCSS = Margin.toCSSValue(margin);  // "10spx 20spx"
const bgCSS = Background.toCSSValue(background); // "linear-gradient(45deg, red, blue)"
const fontCSS = Font.toCSSValue(font);        // "bold 16px/1.5 "Helvetica Neue", Arial, sans-serif"

๐Ÿ“‹ API Reference

Core API Pattern

Every property evaluator follows the same consistent API:

interface PropertyEvaluator<T> {
  parse(value: string): T | null;
  toCSSValue(parsed: T | null): string | null;
}

Property Evaluators

Layout Properties

import { Width, Height, MinWidth, MaxWidth, MinHeight, MaxHeight } from '@wix/css-property-parser';

// Sizing properties support lengths, percentages, and keywords
const width = Width.parse('50%');        // { value: 50, unit: '%' }
const height = Height.parse('auto');     // { keyword: 'auto' }
const minWidth = MinWidth.parse('0');    // { value: 0, unit: 'px' }

Spacing Properties

import { Margin, Padding, Gap, ColumnGap, RowGap } from '@wix/css-property-parser';

// Shorthand properties expand to constituent parts
const margin = Margin.parse('10px 20px');    // Expands to top/right/bottom/left
const padding = Padding.parse('1em');        // Expands to all sides
const gap = Gap.parse('10px 20px');         // Row and column gaps

Typography Properties

import { FontSize, FontWeight, FontFamily, LineHeight, LetterSpacing } from '@wix/css-property-parser';

// Font properties with comprehensive keyword support
const fontSize = FontSize.parse('1.2em');           // { type: 'length', value: 1.2, unit: 'em' }
const fontWeight = FontWeight.parse('bold');        // { type: 'keyword', keyword: 'bold' }
const fontFamily = FontFamily.parse('Arial, sans-serif'); // { type: 'font-list', families: ['Arial', 'sans-serif'] }
const lineHeight = LineHeight.parse('1.5');         // { type: 'number', value: 1.5 } (unitless)

Complex Properties

import { Background, Border, Font, TextShadow } from '@wix/css-property-parser';

// Multi-value and shorthand properties
const background = Background.parse('url(bg.jpg) no-repeat center / cover');
const border = Border.parse('2px solid red');
const font = Font.parse('bold 16px/1.4 "Helvetica Neue", Arial, sans-serif');
const textShadow = TextShadow.parse('2px 2px 4px rgba(0,0,0,0.5)');

Type Evaluators

For parsing individual CSS data types:

import { 
  LengthEvaluator, 
  PercentageEvaluator, 
  Color, 
  AngleEvaluator,
  NumberEvaluator 
} from '@wix/css-property-parser';

// Parse individual CSS types
const length = LengthEvaluator.parse('10px');        // { value: 10, unit: 'px' }
const percentage = PercentageEvaluator.parse('50%');  // { value: 50, unit: '%' }
const color = Color.parse('#ff0000');                // { type: 'hex', value: 'ff0000' }
const angle = AngleEvaluator.parse('45deg');         // { value: 45, unit: 'deg' }

๐Ÿ”ง Advanced Usage

CSS Variables

import { Width, CssVariable } from '@wix/css-property-parser';

// CSS variables are parsed but not resolved
const width = Width.parse('var(--main-width)');
// Returns: { CSSvariable: '--main-width' }

const widthWithFallback = Width.parse('var(--main-width, 100px)');
// Returns: { CSSvariable: '--main-width', defaultValue: '100px' }

CSS Functions

import { Width, FontSize } from '@wix/css-property-parser';

// Calc expressions
const width = Width.parse('calc(100% - 20px)');
// Returns: { expression: '100% - 20px', function: 'calc' }

// Min/max/clamp functions
const fontSize = FontSize.parse('clamp(1rem, 2.5vw, 2rem)');
// Returns: { expression: '1rem, 2.5vw, 2rem', function: 'clamp' }

Round-Trip Stability

import { Background } from '@wix/css-property-parser';

const originalValue = 'linear-gradient(45deg, red 0%, blue 100%)';
const parsed = Background.parse(originalValue);
const serialized = Background.toCSSValue(parsed);

console.log(serialized === originalValue); // true

๐ŸŽจ Use Cases

Design Systems

import { FontSize, Color, Margin } from '@wix/css-property-parser';

// Validate design tokens
function validateDesignToken(property: string, value: string): boolean {
  switch (property) {
    case 'font-size':
      return FontSize.parse(value) !== null;
    case 'color':
      return Color.parse(value) !== null;
    case 'margin':
      return Margin.parse(value) !== null;
    default:
      return false;
  }
}

CSS-in-JS Libraries

import { Width, Height, Background } from '@wix/css-property-parser';

// Parse and transform CSS values
function processStyles(styles: Record<string, string>) {
  const processed: Record<string, any> = {};
  
  for (const [property, value] of Object.entries(styles)) {
    switch (property) {
      case 'width':
        processed.width = Width.parse(value);
        break;
      case 'height':
        processed.height = Height.parse(value);
        break;
      case 'background':
        processed.background = Background.parse(value);
        break;
    }
  }
  
  return processed;
}

CSS Optimization Tools

import { Color, Background } from '@wix/css-property-parser';

// Optimize CSS values
function optimizeColor(value: string): string {
  const parsed = Color.parse(value);
  if (parsed && parsed.type === 'hex') {
    // Convert to shortest hex format
    return Color.toCSSValue(parsed);
  }
  return value;
}

๐Ÿ“š Documentation

๐Ÿงช Testing

# Run all tests
npm test

# Run tests for specific property
npm test -- width.test.ts

# Run tests with coverage
npm test:coverage

# Watch mode for development
npm test:watch

๐Ÿ—๏ธ Development

# Install dependencies
npm install

# Build TypeScript
npm run build

# Build with watch mode
npm run build:watch

# Run development server
npm run dev

๐Ÿ“Š Supported Properties

Total: 124 CSS Property Evaluators - All evaluators include full MDN compliance, comprehensive test coverage, and TypeScript type definitions.

๐Ÿ’ก Quick Reference: Use {PropertyName}.parse(value) to parse and {PropertyName}.toCSSValue(parsed) to serialize any property below.

๐Ÿ”ง Data Types & Base Types (10 evaluators)

  • Primitive Types: angle, color, length, number, percentage, string, time
  • Composite Types: length-percentage, css-variable, blend-mode

๐Ÿ“ Sizing Properties (12 evaluators)

  • Standard Box Model: width, height, min-width, max-width, min-height, max-height
  • CSS Logical Properties: block-size, inline-size, min-block-size, max-block-size, min-inline-size, max-inline-size

๐Ÿ“ Spacing Properties (17 evaluators)

  • Gap Properties (3): gap, column-gap, row-gap
  • Margin Properties (7): margin (shorthand), margin-top, margin-right, margin-bottom, margin-left, margin-inline-start, margin-inline-end
  • Padding Properties (7): padding (shorthand), padding-top, padding-right, padding-bottom, padding-left, padding-inline-start, padding-inline-end

๐Ÿ”ฒ Border Properties (43 evaluators)

  • Border Shorthands (4): border, border-color, border-style, border-width
  • Border Radius (5): border-radius, border-start-start-radius, border-start-end-radius, border-end-start-radius, border-end-end-radius
  • Physical Borders (16):
    • Shorthands: border-top, border-right, border-bottom, border-left
    • Colors: border-top-color, border-right-color, border-bottom-color, border-left-color
    • Styles: border-top-style, border-right-style, border-bottom-style, border-left-style
    • Widths: border-top-width, border-right-width, border-bottom-width, border-left-width
  • CSS Logical Block Borders (9):
    • Shorthands: border-block, border-block-start, border-block-end
    • Colors: border-block-start-color, border-block-end-color
    • Styles: border-block-start-style, border-block-end-style
    • Widths: border-block-start-width, border-block-end-width
  • CSS Logical Inline Borders (9):
    • Shorthands: border-inline, border-inline-start, border-inline-end
    • Colors: border-inline-start-color, border-inline-end-color
    • Styles: border-inline-start-style, border-inline-end-style
    • Widths: border-inline-start-width, border-inline-end-width

๐Ÿ”ค Typography Properties (18 evaluators)

  • Font Properties (7): font (shorthand), font-family, font-size, font-weight, font-style, font-variant, font-stretch
  • Text Properties (6): text-align, text-transform, text-decoration, text-shadow, text-overflow, text-indent
  • Text Spacing (2): line-height, letter-spacing
  • Text Flow (4): white-space, word-break, overflow-wrap, writing-mode

๐Ÿ—๏ธ Layout & Grid Properties (12 evaluators)

  • CSS Grid Template (3): grid-template (shorthand), grid-template-columns, grid-template-rows
  • CSS Grid Areas (3): grid-area, grid-column, grid-row
  • CSS Grid Gaps (3): grid-gap (shorthand), grid-column-gap, grid-row-gap
  • Layout Control (3): display, overflow, visibility

๐ŸŽจ Visual Properties (8 evaluators)

  • Colors & Effects (4): background, color, opacity, box-shadow
  • Object Properties (2): object-fit, object-position
  • Positioning (2): position, z-index

โšก Layout Alignment (4 evaluators)

  • Flexbox & Grid Alignment (4): align-items, align-self, justify-content, flex-direction

๐Ÿ”„ Advanced Features

  • Shared Evaluator Pattern (8 shared): 95% code reduction through intelligent reuse

    • Sizing properties: width/height variants
    • Spacing properties: gap, margin, padding variants
    • Border width properties: all border-width variants
    • Border style properties: all border-style variants
    • Border color properties: all border-color variants
    • Border individual properties: border shorthand variants
    • Logical border properties: block/inline border variants
    • Logical border radius properties: corner radius variants
  • CSS Functions: Full support for calc(), min(), max(), clamp(), var(), gradients

  • SPX Units: Wix Scalable-Pixel units for responsive design

  • Round-Trip Stability: Parse โ†’ serialize โ†’ parse produces identical results

  • Performance: Optimized parsing with minimal memory footprint

๐Ÿค Contributing

  • Fork the repository
  • Create a feature branch
  • Add comprehensive tests (20+ tests minimum)
  • Ensure all tests pass
  • Follow the Evaluator Guide
  • Submit a pull request

๐Ÿ“„ License

ISC License - see LICENSE file for details.

๐Ÿ™ Acknowledgments

  • Built with MDN CSS specifications as the authoritative reference
  • Inspired by the need for robust CSS parsing in modern web applications
  • Comprehensive testing ensures production-ready reliability
  • Enhanced and documented with assistance from Claude (Anthropic AI)

๐Ÿ“‹ Complete Evaluator Exports Reference

All evaluators are available as named exports from the main package. Use import { EvaluatorName } from '@wix/css-property-parser' or const { EvaluatorName } = require('@wix/css-property-parser').

๐Ÿ”ง Type & Utility Evaluators

Export NameCSS Property/TypeDescription
AngleEvaluator<angle>CSS angle values (deg, rad, turn, grad)
BlendModemix-blend-mode, background-blend-modeCSS blend mode keywords
Color<color>CSS color values (hex, rgb, hsl, named, etc.)
CssVariablevar()CSS custom properties and variables
LengthEvaluator<length>CSS length values (px, em, rem, vh, etc.)
LengthPercentage<length-percentage>Combined length and percentage values
NumberEvaluator<number>CSS numeric values
PercentageEvaluator<percentage>CSS percentage values
PositionpositionCSS position keywords (static, relative, absolute, fixed, sticky)
StringEvaluator<string>CSS string values
TimeEvaluator<time>CSS time values (s, ms)

๐Ÿ“ Sizing Properties

Export NameCSS PropertyDescription
BlockSizeblock-sizeLogical block dimension
HeightheightElement height
InlineSizeinline-sizeLogical inline dimension
MaxBlockSizemax-block-sizeMaximum logical block size
MaxHeightmax-heightMaximum element height
MaxInlineSizemax-inline-sizeMaximum logical inline size
MaxWidthmax-widthMaximum element width
MinBlockSizemin-block-sizeMinimum logical block size
MinHeightmin-heightMinimum element height
MinInlineSizemin-inline-sizeMinimum logical inline size
MinWidthmin-widthMinimum element width
WidthwidthElement width

๐Ÿ“ Spacing Properties

Export NameCSS PropertyDescription
ColumnGapcolumn-gapGap between columns
GapgapGap shorthand (row and column)
MarginmarginMargin shorthand (all sides)
MarginBottommargin-bottomBottom margin
MarginInlineEndmargin-inline-endLogical inline end margin
MarginInlineStartmargin-inline-startLogical inline start margin
MarginLeftmargin-leftLeft margin
MarginRightmargin-rightRight margin
MarginTopmargin-topTop margin
PaddingpaddingPadding shorthand (all sides)
PaddingBottompadding-bottomBottom padding
PaddingInlineEndpadding-inline-endLogical inline end padding
PaddingInlineStartpadding-inline-startLogical inline start padding
PaddingLeftpadding-leftLeft padding
PaddingRightpadding-rightRight padding
PaddingToppadding-topTop padding
RowGaprow-gapGap between rows

๐Ÿ”ฒ Border Properties

Export NameCSS PropertyDescription
BorderborderBorder shorthand (all sides)
BorderBlockborder-blockLogical block borders
BorderBlockEndborder-block-endLogical block end border
BorderBlockEndColorborder-block-end-colorLogical block end border color
BorderBlockEndStyleborder-block-end-styleLogical block end border style
BorderBlockEndWidthborder-block-end-widthLogical block end border width
BorderBlockStartborder-block-startLogical block start border
BorderBlockStartColorborder-block-start-colorLogical block start border color
BorderBlockStartStyleborder-block-start-styleLogical block start border style
BorderBlockStartWidthborder-block-start-widthLogical block start border width
BorderBottomborder-bottomBottom border
BorderBottomColorborder-bottom-colorBottom border color
BorderBottomStyleborder-bottom-styleBottom border style
BorderBottomWidthborder-bottom-widthBottom border width
BorderColorborder-colorBorder color shorthand
BorderEndEndRadiusborder-end-end-radiusLogical end-end corner radius
BorderEndStartRadiusborder-end-start-radiusLogical end-start corner radius
BorderInlineborder-inlineLogical inline borders
BorderInlineEndborder-inline-endLogical inline end border
BorderInlineEndColorborder-inline-end-colorLogical inline end border color
BorderInlineEndStyleborder-inline-end-styleLogical inline end border style
BorderInlineEndWidthborder-inline-end-widthLogical inline end border width
BorderInlineStartborder-inline-startLogical inline start border
BorderInlineStartColorborder-inline-start-colorLogical inline start border color
BorderInlineStartStyleborder-inline-start-styleLogical inline start border style
BorderInlineStartWidthborder-inline-start-widthLogical inline start border width
BorderLeftborder-leftLeft border
BorderLeftColorborder-left-colorLeft border color
BorderLeftStyleborder-left-styleLeft border style
BorderLeftWidthborder-left-widthLeft border width
BorderRadiusborder-radiusBorder radius shorthand
BorderRightborder-rightRight border
BorderRightColorborder-right-colorRight border color
BorderRightStyleborder-right-styleRight border style
BorderRightWidthborder-right-widthRight border width
BorderStartEndRadiusborder-start-end-radiusLogical start-end corner radius
BorderStartStartRadiusborder-start-start-radiusLogical start-start corner radius
BorderStyleborder-styleBorder style shorthand
BorderTopborder-topTop border
BorderTopColorborder-top-colorTop border color
BorderTopStyleborder-top-styleTop border style
BorderTopWidthborder-top-widthTop border width
BorderWidthborder-widthBorder width shorthand

๐Ÿ”ค Typography Properties

Export NameCSS PropertyDescription
FontfontFont shorthand (family, size, weight, style, etc.)
FontFamilyfont-familyFont family stack
FontSizefont-sizeFont size
FontStretchfont-stretchFont stretch/width
FontStylefont-styleFont style (normal, italic, oblique)
FontVariantfont-variantFont variant settings
FontWeightfont-weightFont weight (normal, bold, 100-900)
LetterSpacingletter-spacingSpacing between characters
LineHeightline-heightLine height/leading
TextAligntext-alignText alignment
TextDecorationtext-decorationText decoration (underline, strikethrough, etc.)
TextIndenttext-indentFirst-line text indentation
TextOverflowtext-overflowText overflow behavior
TextShadowtext-shadowText shadow effects
TextTransformtext-transformText case transformation
WhiteSpacewhite-spaceWhitespace handling
WordBreakword-breakWord breaking behavior
WritingModewriting-modeText writing direction

๐Ÿ—๏ธ Layout & Grid Properties

Export NameCSS PropertyDescription
DisplaydisplayDisplay type (block, inline, flex, grid, etc.)
GridAreagrid-areaGrid area placement
GridColumngrid-columnGrid column placement
GridColumnGapgrid-column-gapGap between grid columns
GridGapgrid-gapGrid gap shorthand
GridRowgrid-rowGrid row placement
GridRowGapgrid-row-gapGap between grid rows
GridTemplategrid-templateGrid template shorthand
GridTemplateColumnsgrid-template-columnsGrid column track definitions
GridTemplateRowsgrid-template-rowsGrid row track definitions
OverflowoverflowContent overflow behavior
OverflowWrapoverflow-wrapLong word wrapping behavior

๐ŸŽจ Visual Properties

Export NameCSS PropertyDescription
BackgroundbackgroundBackground shorthand (image, color, position, etc.)
BoxShadowbox-shadowBox shadow effects
ObjectFitobject-fitObject fitting behavior
ObjectPositionobject-positionObject positioning
OpacityopacityElement opacity/transparency
VisibilityvisibilityElement visibility
ZIndexz-indexStacking order

โšก Layout Alignment

Export NameCSS PropertyDescription
AlignItemsalign-itemsCross-axis alignment
AlignSelfalign-selfIndividual item cross-axis alignment
FlexDirectionflex-directionFlex container direction
JustifyContentjustify-contentMain-axis alignment

Total: 124 Evaluator Exports - All evaluators follow the same API pattern: parse(value: string) and toCSSValue(parsed) with full TypeScript support.

Keywords

css

FAQs

Package last updated on 31 Jul 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts