-
Notifications
You must be signed in to change notification settings - Fork 3
Replace Rust CSS selectors with native C++ implementation and add comprehensive pseudo-class support #241
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Co-authored-by: yorkie <1935767+yorkie@users.noreply.github.com>
Co-authored-by: yorkie <1935767+yorkie@users.noreply.github.com>
| * Alternative implementation of CSSStyleRule that uses native C++ CSS selector parsing | ||
| * instead of the Rust-based parser. This addresses the TODO in the original CSSStyleRule. | ||
| */ | ||
| class CSSStyleRuleNative final : public CSSGroupingRule |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot Just use this to replace, not creating another class.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replaced CSSStyleRuleNative with direct modification to existing CSSStyleRule class. The separate native class has been removed and CSSStyleRule now uses C++ selectors directly instead of Rust selectors. Changes committed in 0c47774.
| * Adapter class to make our native C++ SelectorList compatible | ||
| * with the existing crates::css2::selectors::SelectorList interface | ||
| */ | ||
| class SelectorListAdapter |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot No need to introduce an adapter, you should use the new C++ SelectorList to replace the code that uses the old crates::css2::selectors::SelectorList.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed the adapter classes completely. CSSStyleRule now uses the native C++ SelectorList directly instead of crates::css2::selectors::SelectorList. All adapter files (css_selector_adapter.hpp/cpp) have been deleted and matching system simplified to only use C++ implementation. Changes committed in 0c47774.
…nd separate classes Co-authored-by: yorkie <1935767+yorkie@users.noreply.github.com>
src/client/dom/document.cpp
Outdated
|
|
||
| shared_ptr<Element> Document::querySelector(const string &selectors) | ||
| { | ||
| // TODO(yorkie): use C++ selectors? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot Complete this TODO, use the C/C++ css selector parser instead of crates::css2::parsing::parseSelectors here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Completed the TODO by replacing crates::css2::parsing::parseSelectors with client_cssom::selectors::CSSelectorParser::parseSelectors in both querySelector and querySelectorAll methods. The C++ CSS selector parser now handles all document query operations. Changes committed in ae92b07.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot Revert the changes in this file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reverted the package.json changes by restoring eslint from ^9.34.0 to ^9.27.0 and typescript from ^5.9.2 to ^5.1.6. Changes committed in ae92b07.
…ementation Co-authored-by: yorkie <1935767+yorkie@users.noreply.github.com>
|
Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details. 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:
If you need me to access, download, or install something from one of these locations, you can either:
|
yorkie
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot The following selectors doesn't work, please fix:
a > b { .. }
a b { ... }
…matching Co-authored-by: yorkie <1935767+yorkie@users.noreply.github.com>
... Fixed the child ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot Please make sure the descendant parsing work correctly, such as "div p" would be parsed into 3 components: div, descendant combinator and p, but the current implementation only returns 2 components.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed the descendant parsing issue. The CSS selector parser now correctly parses "div p" into 3 components: div, descendant combinator, and p. Also fixed parsing of other combinators with whitespace like "div > p", "div + p", and "div ~ p". The issue was in the whitespace handling logic in both parseSingleSelector and parseCombinator functions. Changes committed in 2d7a881.
…binators with whitespace Co-authored-by: yorkie <1935767+yorkie@users.noreply.github.com>
yorkie
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Support the following:
:first-child:first-of-type:last-child:last-of-type
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot Move to docs/internals/CSS_SELECTOR_PARSER_IMPL.md
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved the documentation from docs/css-selector-parser.md to docs/internals/CSS_SELECTOR_PARSER_IMPL.md as requested. Changes committed in 55c2aab.
…ype pseudo-classes and move documentation Co-authored-by: yorkie <1935767+yorkie@users.noreply.github.com>
Added support for all four requested pseudo-classes. The CSS selector parser now correctly handles 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:
If you need me to access, download, or install something from one of these locations, you can either:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR completely replaces the Rust-based CSS selector parsing and matching system with a native C++ implementation, while adding comprehensive support for structural pseudo-classes. The implementation includes fixes for CSS selector parsing issues (especially combinator handling) and introduces proper right-to-left CSS selector matching.
Key changes:
- Native C++ CSS parser: Replaced Rust
crates::css2::selectorswithclient_cssom::selectors::CSSelectorParser - Enhanced pseudo-class support: Added
:first-child,:last-child,:first-of-type,:last-of-typesupport - Fixed selector parsing: Corrected combinator detection and parsing for descendant selectors like "div p"
Reviewed Changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
tests/client/css_selector_parser_tests.cpp |
Comprehensive test suite for the new C++ CSS selector parser covering all selector types and combinations |
src/client/dom/document.cpp |
Updated querySelector and querySelectorAll to use C++ parser instead of Rust implementation |
src/client/cssom/selectors/matching.hpp |
Modified function signatures to use C++ selector types and added new matching functions |
src/client/cssom/selectors/matching.cpp |
Implemented structural pseudo-class helpers and right-to-left selector matching algorithm |
src/client/cssom/selectors/css_selector_parser.hpp |
Complete C++ selector parser class definitions with comprehensive enum types and data structures |
src/client/cssom/selectors/css_selector_parser.cpp |
Full implementation of CSS selector parsing logic with proper combinator handling |
src/client/cssom/rules/css_style_rule.hpp |
Updated to use native C++ selectors and added element matching capability |
fixtures/html/simple.html |
Test file updates to demonstrate structural pseudo-class functionality |
docs/internals/CSS_SELECTOR_PARSER_IMPL.md |
Comprehensive documentation for the new CSS parser implementation |
This PR completely replaces the Rust-based CSS selector parsing and matching system with a native C++ implementation in JSAR Runtime, while adding comprehensive support for structural pseudo-classes.
Implementation Details
Complete Rust Removal: The existing
CSSStyleRuleclass now uses native C++SelectorListdirectly instead ofcrates::css2::selectors::SelectorList, eliminating all Rust selector dependencies.Document Query Integration: Both
querySelectorandquerySelectorAllmethods indocument.cppnow use the C++ CSS selector parser (client_cssom::selectors::CSSelectorParser::parseSelectors) instead of the Rust implementation (crates::css2::parsing::parseSelectors), completing the migration.Simplified Architecture:
CSSStyleRuleNativeclass - modified existingCSSStyleRuledirectlyFixed CSS Selector Matching: Implemented proper right-to-left CSS selector matching algorithm to fix child and descendant combinators:
a > b) now correctly matches direct parent-child relationshipsa b) now correctly matches ancestor-descendant relationshipsmatchesSelectorFromEnd()function that processes components from right to left as per CSS specificationFixed CSS Selector Parsing: Resolved critical parsing issues where descendant selectors like "div p" were incorrectly parsed into only 2 components instead of the expected 3 components (div, descendant combinator, p):
>,+,~)Comprehensive Pseudo-class Support: Added complete support for structural pseudo-classes:
:first-child- matches elements that are the first child of their parent:last-child- matches elements that are the last child of their parent:first-of-type- matches elements that are the first of their type among siblings:last-of-type- matches elements that are the last of their type among siblingsCSS Selector Support: The native C++ parser handles all standard CSS selectors:
div,.class,#id:hover,:focus,:active,:first-child,:last-child,:first-of-type,:last-of-type,:root::before,::after>), descendant (space), sibling (+,~)div.class > p:hover,body, .foo > div#barBenefits
Example Usage
The implementation maintains full API compatibility while providing a cleaner, more efficient CSS selector system throughout the entire codebase with correctly functioning combinators, accurate component parsing, and comprehensive pseudo-class support.
Fixes #240.
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.