Skip to content
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

fixes #590: change isCompoundSelector to not match prop selector #595

Merged
merged 1 commit into from
Oct 12, 2016

Conversation

joeduncan
Copy link
Contributor

No description provided.

@aweary
Copy link
Collaborator

aweary commented Sep 12, 2016

Thanks for this @joeduncan, all the extra tests are awesome. Since all of our existing tests look good as well, this LGTM.

I'd like to keep this open for review since I'm not 100% with regexes, so maybe someone like @ljharb can take a look as well.

@@ -189,7 +189,7 @@ export function selectorError(selector, type = '') {
);
}

export const isCompoundSelector = /([a-z]\.[a-z]|[a-z]\[.*\]|[a-z]#[a-z])/i;
export const isCompoundSelector = /^[\.#]?-?[_a-z]+[_a-z0-9-]*[\.\[#]/i;
Copy link
Member

@ljharb ljharb Sep 12, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i wish we had commented regexps for situations like this. Let's see:

/
  ^               # start
  [\.#]?          # a dot or a hash, 0 or 1
  -?              # a hyphen, 0 or 1
  [_a-z]+         # any letter or underscore, 1 or more
  [_a-z0-9-]*     # any letter, underscore, number, or hyphen, 0 or more
  [\.\[#]         # any dot, left bracket, or hash
/i

Even after that I'm not really sure how to reason out what it's matching against. I wonder if we could write this as multiple different regexes, instead of trying to combine it into one case?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even after that I'm not really sure how to reason out what it's matching against. I wonder if we could write this as multiple different regexes, instead of trying to combine it into one case?

Agreed, that's the approach I took with #591 because using a single regex was just too confusing. Breaking this into multiple regexes seems reasonable, but at a certain point it feels like we'd be implementing our own parser and I wonder if it would be better to just put the work into making #534.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That also seems reasonable.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[\.#]? # anything but a hash, 0 or 1
not sure it matters here but I think this one is actually '. or a #, 0 or 1'

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops, you're right. Will update my comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually you'll need a parser because CSS selectors are described using a LL(1) grammar and therefore cannot be parsed by a regular expression.

But you don't need to reinvent the wheel - use an existing selector engine like Sizzle for example and kill all the birds.. eh bugs with one stone.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could bring in xregexp if we wanted to make this regexp more readable with comments. I generally think that regexps should have comments, but I honestly don't find this one to be too bad.

Also, inside of brackets, I don't think you need to escape things like . and [, so you could remove a couple of backslashes to clean it up a bit.

Overall, this seems to be an improvement so I think we might as well merge it in. In the long run, if we want to support the full range of CSS selectors, I agree with @Jazen that we should bring in a parser.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lencioni we have an open PR for implementing a parser #458

@joeduncan
Copy link
Contributor Author

Is there anything I can do here?

@lencioni
Copy link
Contributor

@joeduncan Thanks for your contribution! When you have a moment, will you please rebase this onto the latest master branch?

@joeduncan
Copy link
Contributor Author

Rebased and ready for merge.

@lencioni lencioni merged commit 19d06e8 into enzymejs:master Oct 12, 2016
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.

6 participants