-
Notifications
You must be signed in to change notification settings - Fork 1k
[lit-labs/ssr-dom-shim] shim Element.prototype.toggleAttribute method #4493
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
Conversation
🦋 Changeset detectedLatest commit: c88c1f5 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
📊 Tachometer Benchmark ResultsSummarynop-update
render
update
update-reflect
Resultsthis-change
render
update
update-reflect
this-change, tip-of-tree, previous-release
render
update
nop-update
this-change, tip-of-tree, previous-release
render
update
this-change, tip-of-tree, previous-release
render
update
update-reflect
|
|
The size of lit-html.js and lit-core.min.js are as expected. |
augustjk
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent!
| if (force === undefined) { | ||
| force = !this.hasAttribute(name); | ||
| } | ||
| if (force) { | ||
| this.setAttribute(name, ''); | ||
| } else { | ||
| this.removeAttribute(name); | ||
| } | ||
| return force; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't quite correct, since if an attribute is already set and force is true the attribute should be left unmodified: https://dom.spec.whatwg.org/#dom-element-toggleattribute This code sets it to ''.
I think this should work, but it needs a test for that case:
| if (force === undefined) { | |
| force = !this.hasAttribute(name); | |
| } | |
| if (force) { | |
| this.setAttribute(name, ''); | |
| } else { | |
| this.removeAttribute(name); | |
| } | |
| return force; | |
| if (this.hasAttribute(name) { | |
| // Step 5 | |
| if (force === undefined || !force) { | |
| this.removeAttribute(name); | |
| return false; | |
| } | |
| } else { | |
| // Step 4 | |
| if (force === undefined || force) { | |
| // Step 4.1 | |
| this.setAttribute(name, ''); | |
| return true; | |
| } else { | |
| // Step 4.2 | |
| return false; | |
| } | |
| } | |
| // Step 6 | |
| return true; |
I think this handles boolean conversion correctly. ie, undefined is the real missing value and null treated as falsey.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exceptional catch! Thank you! Fixed in #4494
Fixes: #4468
This adds
Element.prototype.toggleAttribute(name, force?)to the SSR ElementShim.Test plan
Unit tested in the node environment using
uvu. Note, the package originally didn't have unit tests, so I've also set up a newtestrule.Assumptions about casing
Documenting this as I was going to implement lower-casing of attribute names until I spoke with Augustine.
Ignoring casing is by design in the shim because these shimmed APIs can be used on SVG elements where casing is preserved. I believe the justification is we preserve casing during SSR, and then the browser will lowercase the attributes if in the HTML namespace. E.g. if we SSR
<div cLaSs="example">, it'll be parsed into<div class="example">. But<svg viewBox=...>will be preserved as<svg viewBox=...>.