fix(parser): match exact class name in selectorMatches (#8838)#11044
Open
xianjianlf2 wants to merge 1 commit into
Open
fix(parser): match exact class name in selectorMatches (#8838)#11044xianjianlf2 wants to merge 1 commit into
xianjianlf2 wants to merge 1 commit into
Conversation
The selector matcher used a negative lookahead `(?![a-zA-Z\-]+)` to ensure a class/id name was not merely a prefix of a longer name. It excluded letters and hyphens but not digits or underscores, so `.st1` matched inside `.st12` (the trailing `2` slipped past the lookahead), leaving a leftover `2` and causing the `.st12` rule to be dropped. Exclude every valid CSS identifier continuation character (`[\w\-]`) so numeric/underscore-suffixed class and id names match correctly. close fabricjs#8838
Review or Edit in CodeSandboxOpen the branch in Web Editor • VS Code • Insiders |
Contributor
|
Build Stats
|
Member
|
The PR looks good. i have to check recent changes to those regex because some were simplifications at the cost of small edge cases to avoid security issues in node. Thanks for the PR meanwhile |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
Closes #8838.
When an SVG element carries multiple classes such as
class="st12 st1", the.st12style rule was dropped and only.st1applied.The selector matcher in
src/parser/selectorMatches.tsused a negative lookahead(?![a-zA-Z\-]+)to make sure a class/id name was not merely a prefix of a longer name. That lookahead excluded letters and hyphens, but not digits or underscores, so.st1matched inside.st12: the trailing2slipped past the lookahead, leaving a leftover token and causing the.st12rule to be discarded.The fix replaces the lookahead with
(?![\w\-]), which excludes every valid CSS identifier continuation character (letters, digits, underscore and hyphen), so numeric- and underscore-suffixed class and id names are matched exactly. This matches the exact-class-name approach @asturur prescribed in the issue.Added
src/parser/selectorMatches.spec.tsverifying that.st1no longer matches an element whose class list containsst12, while genuine matches still succeed. Existing parser specs remain green.In Action
Before: an element with
class="st12 st1"lost its.st12styling because.st1matched insidest12.After:
.st12and.st1are matched independently and both rules apply.