@wix/css-property-parser
Version:
A comprehensive TypeScript library for parsing and serializing CSS property values with full MDN specification compliance
41 lines (40 loc) • 1.45 kB
JavaScript
import { parse as parsePosition, toCSSValue as positionToCSSValue } from './position.js';
/**
* Parses CSS object-position property values
*
* Object-position uses the same syntax as background-position:
* - Keywords: left, right, top, bottom, center
* - Length/percentage values: 50px, 25%, etc.
* - Combinations: "left top", "50% 25%", "center", etc.
* - Offset syntax: "left 10px top 20px"
*
* Syntax: <position> | inherit | initial | unset | revert
*
* @param value - The CSS object-position value to parse
* @returns Parsed PositionValue or null if invalid
*
* @example
* ```typescript
* parse('center') // { type: 'position', x: 'center', y: 'center' }
* parse('left top') // { type: 'position', x: 'left', y: 'top' }
* parse('50% 25%') // { type: 'position', x: '50%', y: '25%' }
* parse('left 10px top 20px') // { type: 'position', x: 'left', y: 'top', xOffset: '10px', yOffset: '20px' }
* parse('var(--obj-pos)') // { type: 'variable', variable: 'obj-pos' }
* ```
*/
export function parse(value) {
// Handle non-string inputs gracefully
if (!value || typeof value !== 'string') {
return null;
}
return parsePosition(value);
}
/**
* Converts a parsed PositionValue back to its CSS string representation
*
* @param value - The parsed object-position value
* @returns CSS string representation or null if invalid
*/
export function toCSSValue(value) {
return positionToCSSValue(value);
}