feat(cssom): implement CSS nth-child() and nth-of-type() selector support #285
+483
−32
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Adds complete support for CSS
:nth-child()and:nth-of-type()pseudo-class selectors, including both parser and matching functionality.Features Added
Parser Support
odd,even(e.g.,:nth-child(odd)):nth-child(3))an+bsyntax support (e.g.,:nth-child(3n+2),:nth-child(-n+3)):nth-child()and:nth-of-type()selectorsMatching Engine
an+bformula evaluation for all edge cases2n+1) and backward sequences (-n+3)Examples
Implementation Details
Modified Files:
src/client/cssom/selectors/css_selector_parser.hpp- Added nth-child data fields and methodssrc/client/cssom/selectors/css_selector_parser.cpp- Implemented parsing logic foran+bformulassrc/client/cssom/selectors/matching.cpp- Added position-based matching algorithmstests/client/css_selector_parser_tests.cpp- Comprehensive test coverage for all syntax variationsdocs/internals/CSS_SELECTOR_PARSER_IMPL.md- Updated documentation with usage examplesKey Technical Changes:
Componentclass withnthA_andnthB_fields to storean+bcoefficientsparseNthFormula()method to handle all nth-child syntax variationsisNthChild()andisNthOfType()helper functions for accurate position matchingTesting
The implementation follows the CSS specification exactly and integrates seamlessly with the existing selector infrastructure without breaking any existing functionality.
Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
registry.npmmirror.comnpm ci(dns block)If you need me to access, download, or install something from one of these locations, you can either:
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.