You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add support for CSS if() inline conditional function parsing (#253)
## Summary
This PR adds comprehensive support for parsing CSS `if()` inline
conditional functions (CSS Values Level 5 spec). The parser now
recognizes `if()` functions and creates a dedicated `IF_BRANCH` node
type to represent each condition-value pair within the function.
## Key Changes
- **New `IF_BRANCH` node type**: Introduced a new AST node type to
represent individual branches within an `if()` function, with properties
for:
- `condition`: The condition text (e.g., `"style(--active: 1)"` or
`"else"`)
- `value`: The value text between the colon and semicolon (or `null` if
empty)
- `is_else`: Boolean flag indicating if this is the `else` branch
- `children`: Parsed condition node followed by parsed value nodes
- **Dedicated `if()` parser**: Implemented `parse_if_function_node()` in
`ValueParser` that:
- Recognizes `if()` functions and dispatches to specialized parsing
logic
- Parses multiple branches separated by semicolons
- Handles condition nodes (functions like `style()`, `supports()`,
`media()`, or `else` identifier)
- Parses value nodes of various types (dimensions, colors, functions,
etc.)
- Supports nested `if()` functions recursively
- Properly tracks location information (start, end, line, column)
- **Token handling**: Extended operator parsing to include colons and
semicolons as structural separators within `if()` branches
- **Type definitions**: Added `IfBranch` type to the public API with
proper TypeScript support and type guards
- **Comprehensive test coverage**: Added 30+ test cases covering:
- Basic structure and node properties
- Various condition types (`style()`, `supports()`, `media()`, `else`)
- Multiple branches and nested `if()` functions
- Different value node types (dimensions, colors, functions)
- Empty values and trailing semicolons
- Location tracking accuracy
- Real-world spec examples
## Implementation Details
The parser treats `if()` as a special function that creates a `FUNCTION`
node with `IF_BRANCH` children instead of generic value nodes. Each
branch's condition and value are tracked separately through arena fields
(`contentStartDelta`/`contentLength` for condition,
`valueStartDelta`/`valueLength` for value), allowing efficient text
extraction without reparsing. The implementation properly handles
whitespace, malformed input, and maintains accurate source location
information for all nodes.
https://claude.ai/code/session_01UmLv7na8e3eUyPAZbjMf3U
---------
Co-authored-by: Claude <noreply@anthropic.com>
0 commit comments