Skip to content

Add classList support to SVGElement. #391

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

Merged
merged 11 commits into from
Oct 14, 2020
Merged

Conversation

bicknellr
Copy link
Collaborator

@bicknellr bicknellr commented Oct 10, 2020

This adds support for classList to SVGElement. Thanks to @justinfagnani for pointing out this trick, which makes this super simple. Originally, I included polyfills for newer pieces of DOMTokenList, but it sounds like we might not need those for the next version of Lit, so I've split them out into a separate PR (#392).

@bicknellr bicknellr changed the title Add polyfills for classList on SVGElement and parts ofDOMTokenList. Add polyfills for classList on SVGElement and parts of DOMTokenList. Oct 10, 2020
@bicknellr bicknellr changed the title Add polyfills for classList on SVGElement and parts of DOMTokenList. Add classList support to SVGElement. Oct 12, 2020
@bicknellr bicknellr marked this pull request as ready for review October 12, 2020 22:20
@bicknellr bicknellr requested a review from aomarks as a code owner October 12, 2020 22:20
suite('SVGElement classList', function() {
const createSVGElement = () => document.createElementNS('http://www.w3.org/2000/svg', 'g');

test('length', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

The big remaining test we want here is to ensure that setting and removing the class attribute is correctly reflected in classList.

Copy link
Collaborator Author

@bicknellr bicknellr Oct 13, 2020

Choose a reason for hiding this comment

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

SVGElement overwrites the usual Element#className with an SVGAnimatedString and Chrome doesn't seem to return the correct result for .classList.contains(x) after setting .className.baseVal = x. (I filed a bug for this here.) Getting .classList.value after setting .className.baseVal seems to work, but I think avoiding .contains() kind of defeats the purpose.

Given that this is a browser-compat bug for something we don't want to repair / polyfill ourselves (classList itself or the SVGAnimatedString#baseVal setter), I'm not really sure where to go from here other than to just not use both className and classList on SVG elements at the same time and test using only classList.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

(following up from external chat) For some reason I read your comment as being about setting className but it was actually about setting the class attribute via setAttribute. I'll write a test for that specific case.

@bicknellr bicknellr force-pushed the svg-element-class-list branch from 919916c to 737cd2d Compare October 13, 2020 01:23
@bicknellr bicknellr merged commit 93f9e56 into master Oct 14, 2020
@bicknellr bicknellr deleted the svg-element-class-list branch October 14, 2020 22:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants