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

Ignore nonsignificant whitespace in selectors #348

Merged
merged 1 commit into from
Apr 27, 2016

Conversation

aweary
Copy link
Collaborator

@aweary aweary commented Apr 26, 2016

Resolves #347

@@ -29,7 +29,7 @@ export function childrenOfNode(node) {

export function hasClassName(node, className) {
const classes = propsOfNode(node).className || '';
return ` ${classes} `.indexOf(` ${className} `) > -1;
return ` ${classes.trim()} `.indexOf(` ${className} `) > -1;
Copy link
Member

Choose a reason for hiding this comment

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

trim won't catch foo\nbar, will it?

I think you may instead want to .replace(/\s*/g, ' ')

Copy link
Collaborator Author

@aweary aweary Apr 26, 2016

Choose a reason for hiding this comment

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

I guess I'm unsure of what the expected behavior is supposed to be in that case. This change was meant to just address non-significiant (leading/trailing) whitespace. If we're coercing all whitespace characters to spaces I think that's a bit different. A user might have some (strange) reason to have a new-line there instead of a space?

Also:

'foo\nbar'.replace(/\s*/g, ' ')
// " f o o  b a r "

Even if we just do .replace(/\s/g, ' ') cases like foo\nbar\n would return a string ("foo bar ") that still needs to be trimmed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Also, DOM API's like document.querySelector will ignore leading/trailing whitespace but not significant whitespace, which is kind of what I was following here. If you have significant whitespace in your class names (which you probably shouldn't anyways) you should test explicitly for that.

What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

It's a class name - the browser will always collapse whitespace into a single space anyways, I thought.

I totally agree though that what document.querySelector does should be our guide here - but i was under the impression that the array of class names on an element was as if you'd done .split(/\s+/g) on it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

At least in Chrome the new line characters are perserved (if I check node.className on an element with the class foo\nbar it prints with the new line) but I'm not entirely sure how consistent that is.

But you're right, checking classList does still parse it as two classes as it should. For some reason I thought we were talking about parsing foo\nbar as a singular class name. I'll add update the PR to handle that case as well.

@aweary
Copy link
Collaborator Author

aweary commented Apr 26, 2016

@ljharb updated the PR to handle significant whitespace as well 👍

@aweary aweary force-pushed the selector-whitespace branch 2 times, most recently from 7838753 to 5adbced Compare April 26, 2016 17:57
@@ -51,7 +51,8 @@ export function instHasClassName(inst, className) {
if (!isDOMComponent(inst)) {
return false;
}
const classes = findDOMNode(inst).className || '';
let classes = findDOMNode(inst).className || '';
classes = classes.replace(/\s/g, ' ').trim();
Copy link
Member

Choose a reason for hiding this comment

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

shouldn't the trim now be unnecessary?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

You're right, removed 👍

@ljharb
Copy link
Member

ljharb commented Apr 26, 2016

LGTM

@ljharb
Copy link
Member

ljharb commented Apr 27, 2016

@aweary why did you close this?

@aweary
Copy link
Collaborator Author

aweary commented Apr 27, 2016

I...didn't, I have no idea why that happened.

@aweary aweary reopened this Apr 27, 2016
@lelandrichardson
Copy link
Collaborator

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants