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

Guard against className being null/undefined #37

Merged
merged 3 commits into from
Apr 23, 2017
Merged

Conversation

developit
Copy link
Contributor

We're seeing this line throw when element.className is null.

@ausi
Copy link
Owner

ausi commented Apr 10, 2017

Do you have any information about how element.className can be null? Maybe a link to a website where this happens?

@developit
Copy link
Contributor Author

developit commented Apr 17, 2017

Can't seem to reproduce it outside the private case I was seeing, but className is undefined for Document, Text and Comment nodes. Also for any SVG element, it's actually an Object:

https://jsfiddle.net/developit/gsLnt0cn/

(for the SVG case you basically are forced to use getAttribute('class'), which seems like a reasonable compromise given it works on any element.

@ausi
Copy link
Owner

ausi commented Apr 19, 2017

How about using this version then:

(element.getAttribute('class') || '').split(/\s+/).forEach(function(className) {
	foundRules = foundRules.concat(rules['.' + className] || []);
});

Does this solve your issue?

@developit
Copy link
Contributor Author

developit commented Apr 21, 2017

Yup! Updated the PR.

I actually managed to catch this exception today and can confirm it's happening on SVG elements (a <g> in this case):
screen shot 2017-04-21 at 3 51 03 pm

@ausi
Copy link
Owner

ausi commented Apr 23, 2017

Thank you for the pull request!

I added some tests and more fixes for SVG elements.

@ausi ausi merged commit 78e1fc6 into ausi:master Apr 23, 2017
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.

2 participants