Skip to content

Support variable replacement in Lexer#matchProperty#321

Open
nzakas wants to merge 6 commits intocsstree:masterfrom
nzakas:custom-vars
Open

Support variable replacement in Lexer#matchProperty#321
nzakas wants to merge 6 commits intocsstree:masterfrom
nzakas:custom-vars

Conversation

@nzakas
Copy link
Contributor

@nzakas nzakas commented Jan 29, 2025

This pull request introduces enhancements to the Lexer class to support CSS variable (var()) token replacement and matching. The most important changes include adding a new function for variable token replacement, updating the matchSyntax function to handle variable tokens, and modifying the matchProperty and matchDeclaration methods to accept variable values.

The basic idea is to let consumers pass in strings or nodes that would be used in place of the variable reference. I opted to create an options object rather than variables object to leave space for further expansion.

There are a couple of failing tests that I couldn't figure out, but at this point, just looking for feedback on if this approach would be acceptable.

Fixes #317

Enhancements to CSS variable handling:

  • lib/lexer/Lexer.js: Added the replaceVarTokens function to replace var() tokens with their corresponding values from a provided variables object.
  • lib/lexer/Lexer.js: Updated the matchSyntax function to utilize replaceVarTokens for handling var() tokens and returning an error if the variable value is not defined. [1] [2]
  • lib/lexer/Lexer.js: Modified the matchProperty and matchDeclaration methods to accept an additional vars parameter for variable values. [1] [2]

Testing for new functionality:

@nzakas
Copy link
Contributor Author

nzakas commented Feb 14, 2025

@lahmatiy looking for your feedback here.

@coveralls
Copy link

coveralls commented Feb 27, 2025

Coverage Status

coverage: 98.867% (-0.01%) from 98.88%
when pulling 0eeca41 on nzakas:custom-vars
into b3fe3e0 on csstree:master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Lexer errors on custom variables in values

2 participants