-
-
Notifications
You must be signed in to change notification settings - Fork 24
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
Incorrect selector splitting & broken styles #45
Comments
Thank you for the detailed report, I'll look into how this behavior should be improved. @all-contributors please add @eddyw for reporting a bug! |
I've put up a pull request to add @eddyw! 🎉 |
I've been working on this with moderate success. I would appreciate if you could help in eliminating false matches by modifying the linked regular expression. References for the values accepted by an attribute selector: |
@kripod I'm not quite sure how to achieve this with a single RegExp 🤔 From CSS syntax, these 👇 are known to create groups, so anything within a group shouldn't be split (regardless if it's valid or not) + escaped const MATCHES = [
/\"([^\"]*)\"?/g, // R_STR_DBL
/\'([^\']*)\'?/g, // R_STR_SGL
/\[([^\]]*)\]?/g, // R_SQUARE
/\(([^\)]*)\)?/g, // R_PARENT
/\{([^\}]*)\}?/g, // R_CURLY
/\\,/, // R_ESCAPED_COMMA
];
function toPlaceholder(m) {
return "_".repeat(m.length);
}
function getSelectors(rawInput) {
let input = rawInput;
MATCHES.forEach((r) => (input = input.replace(r, toPlaceholder)));
const selectors = input.split(',')
for (let i = 0, lastIndex = 0; i < selectors.length; i++) {
const current = selectors[i]
selectors[i] = rawInput.substr(lastIndex, current.length).trim()
// Here you can validate selectors[i][0] === '&' or not
lastIndex = lastIndex + current.length + 1;
}
return selectors
} I know it's ugly lol but it works and maybe it could be improved somehow 🤷♂️ If a selectors parser and validator is within the scope of otion to implement, then I'd suggest to have a look at:
Those are the only 2 projects I know that are up to date and seem to follow w3c specs (CSS syntax tokenizer & parser). However, I don't know of any CSS-in-JS lib .. or any library that actually does validation of CSS selectors 🤷♀️ . All assume you know what you're doing 😅 |
Thank you for these well-detailed insights! At first, I was thinking about providing a fully-featured solution for this. |
Interesting work about recursive regular expressions in JS has been done by Steven Levithan. The article might help with implementing an efficient solution. |
Parsing is out of this project's scope, so we should consider this to be mostly resolved with RegExp. I've expanded the docs, linking to this issue as a reference. |
Description
When a selector (in
selectors
css prop) contains a,
(COMMA), it may be split incorrectly.Reproduction & Expected behavior
For instance:
It generates:
It generates:
Expected behavior
A selector group should be split correctly. Escaped values are valid in selectors, so a COMMA (,) may appear outside of a quoted value, e.g:
In:
Actual behavior
It is split as
rule.split(',')
which incorrectly splits valid group selectorsThe text was updated successfully, but these errors were encountered: