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

classList property of elements is not populated #65

Closed
amb26 opened this issue May 28, 2021 · 3 comments
Closed

classList property of elements is not populated #65

amb26 opened this issue May 28, 2021 · 3 comments

Comments

@amb26
Copy link

amb26 commented May 28, 2021

I can see that there is a placeholder for this since the property is attached but it is always populated with a Set of 0 elements, regardless of whether there is a "class" attribute on the element or not.

Specified in the DOM API here: https://developer.mozilla.org/en-US/docs/Web/API/Element/classList

See attached image showing classList as Set(0) even though there is an class attribute holding value renderer-test-container
classList

@amb26
Copy link
Author

amb26 commented May 28, 2021

"By eye" I see (at least) two sources of leakage -
i) The classList Set is not updated when markup is cloned via node.cloneNode()
ii) The classList is not updated if an existing "class" attribute is updated - e.g. at https://github.com/WebReflection/linkedom/blob/main/cjs/interface/element.js#L270

The following workaround is necessary before any read of classList:

        var clazz = node.getAttribute("class");
        node.removeAttribute("class");
        node.setAttribute("class", clazz);  

Note that being implemented in terms of a Set, the classList does not perfectly conform to the DOM API describing a "DOMTokenList" - e.g. it is missing a "length" property. Given that the developer has said that the target is only for linkedom to be "close" to the DOM API this may not be terribly important, since I guess it is still usable in spread contexts and via a few of its advertised named methods, but I think it should be kept in sync with the value imputed by "class".

@WebReflection
Copy link
Owner

Why do you use getAttribute and setAttribute or removeAttribute for class, if I might ask? You have element.className accessor and the classList too, and the `classList is lazy. Do you have any use case for this? I don't find this particularly interesting and using either ways (accessor or classList) reflect already in the attribute. Making attribute reflect on classList adds complexity for I am not sure what gain.

@WebReflection
Copy link
Owner

@amb26 never mind ... I think I've found a decent compromise to make it work. Fixed, thanks.

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

No branches or pull requests

2 participants