Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@AndrewJakubowicz
Copy link
Contributor

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 new test rule.

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=...>.

@changeset-bot
Copy link

changeset-bot bot commented Jan 12, 2024

🦋 Changeset detected

Latest commit: c88c1f5

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@lit-labs/ssr-dom-shim Minor

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

@github-actions
Copy link
Contributor

github-actions bot commented Jan 12, 2024

📊 Tachometer Benchmark Results

Summary

nop-update

  • this-change, tip-of-tree, previous-release: unsure 🔍 -7% - +2% (-0.80ms - +0.24ms)
    this-change vs tip-of-tree

render

  • this-change: 47.34ms - 49.97ms
  • this-change, tip-of-tree, previous-release: unsure 🔍 -5% - +4% (-0.85ms - +0.66ms)
    this-change vs tip-of-tree
  • this-change, tip-of-tree, previous-release: unsure 🔍 -2% - +2% (-0.58ms - +0.74ms)
    this-change vs tip-of-tree
  • this-change, tip-of-tree, previous-release: unsure 🔍 -2% - +2% (-0.55ms - +0.79ms)
    this-change vs tip-of-tree

update

  • this-change: 487.18ms - 494.49ms
  • this-change, tip-of-tree, previous-release: unsure 🔍 -9% - +3% (-3.76ms - +1.14ms)
    this-change vs tip-of-tree
  • this-change, tip-of-tree, previous-release: unsure 🔍 -1% - +3% (-0.36ms - +1.86ms)
    this-change vs tip-of-tree
  • this-change, tip-of-tree, previous-release: unsure 🔍 -1% - +2% (-5.42ms - +11.37ms)
    this-change vs tip-of-tree

update-reflect

  • this-change: 490.25ms - 494.55ms
  • this-change, tip-of-tree, previous-release: unsure 🔍 -2% - +1% (-9.19ms - +6.72ms)
    this-change vs tip-of-tree

Results

this-change

render

VersionAvg timevs
47.34ms - 49.97ms-

update

VersionAvg timevs
487.18ms - 494.49ms-

update-reflect

VersionAvg timevs
490.25ms - 494.55ms-
this-change, tip-of-tree, previous-release

render

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
18.10ms - 19.18ms-unsure 🔍
-5% - +4%
-0.85ms - +0.66ms
unsure 🔍
-4% - +5%
-0.69ms - +0.83ms
tip-of-tree
tip-of-tree
18.21ms - 19.27msunsure 🔍
-4% - +5%
-0.66ms - +0.85ms
-unsure 🔍
-3% - +5%
-0.58ms - +0.92ms
previous-release
previous-release
18.03ms - 19.10msunsure 🔍
-4% - +4%
-0.83ms - +0.69ms
unsure 🔍
-5% - +3%
-0.92ms - +0.58ms
-

update

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
37.08ms - 40.42ms-unsure 🔍
-9% - +3%
-3.76ms - +1.14ms
unsure 🔍
-6% - +6%
-2.50ms - +2.41ms
tip-of-tree
tip-of-tree
38.27ms - 41.85msunsure 🔍
-3% - +10%
-1.14ms - +3.76ms
-unsure 🔍
-3% - +10%
-1.28ms - +3.80ms
previous-release
previous-release
36.99ms - 40.60msunsure 🔍
-6% - +6%
-2.41ms - +2.50ms
unsure 🔍
-9% - +3%
-3.80ms - +1.28ms
-

nop-update

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
10.90ms - 11.55ms-unsure 🔍
-7% - +2%
-0.80ms - +0.24ms
unsure 🔍
-4% - +5%
-0.40ms - +0.59ms
tip-of-tree
tip-of-tree
11.10ms - 11.91msunsure 🔍
-2% - +7%
-0.24ms - +0.80ms
-unsure 🔍
-2% - +8%
-0.17ms - +0.93ms
previous-release
previous-release
10.76ms - 11.50msunsure 🔍
-5% - +4%
-0.59ms - +0.40ms
unsure 🔍
-8% - +1%
-0.93ms - +0.17ms
-
this-change, tip-of-tree, previous-release

render

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
32.82ms - 33.71ms-unsure 🔍
-2% - +2%
-0.58ms - +0.74ms
unsure 🔍
-2% - +2%
-0.73ms - +0.65ms
tip-of-tree
tip-of-tree
32.70ms - 33.67msunsure 🔍
-2% - +2%
-0.74ms - +0.58ms
-unsure 🔍
-3% - +2%
-0.84ms - +0.59ms
previous-release
previous-release
32.78ms - 33.83msunsure 🔍
-2% - +2%
-0.65ms - +0.73ms
unsure 🔍
-2% - +3%
-0.59ms - +0.84ms
-

update

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
65.88ms - 67.91ms-unsure 🔍
-1% - +3%
-0.36ms - +1.86ms
unsure 🔍
-2% - +2%
-1.54ms - +1.11ms
tip-of-tree
tip-of-tree
65.69ms - 66.59msunsure 🔍
-3% - +1%
-1.86ms - +0.36ms
-faster ✔
0% - 3%
0.01ms - 1.93ms
previous-release
previous-release
66.26ms - 67.96msunsure 🔍
-2% - +2%
-1.11ms - +1.54ms
slower ❌
0% - 3%
0.01ms - 1.93ms
-
this-change, tip-of-tree, previous-release

render

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
33.39ms - 34.33ms-unsure 🔍
-2% - +2%
-0.55ms - +0.79ms
unsure 🔍
-2% - +2%
-0.66ms - +0.74ms
tip-of-tree
tip-of-tree
33.26ms - 34.22msunsure 🔍
-2% - +2%
-0.79ms - +0.55ms
-unsure 🔍
-2% - +2%
-0.79ms - +0.63ms
previous-release
previous-release
33.29ms - 34.34msunsure 🔍
-2% - +2%
-0.74ms - +0.66ms
unsure 🔍
-2% - +2%
-0.63ms - +0.79ms
-

update

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
507.73ms - 519.63ms-unsure 🔍
-1% - +2%
-5.42ms - +11.37ms
unsure 🔍
-2% - +1%
-10.33ms - +7.56ms
tip-of-tree
tip-of-tree
504.78ms - 516.62msunsure 🔍
-2% - +1%
-11.37ms - +5.42ms
-unsure 🔍
-3% - +1%
-13.29ms - +4.56ms
previous-release
previous-release
508.38ms - 521.74msunsure 🔍
-1% - +2%
-7.56ms - +10.33ms
unsure 🔍
-1% - +3%
-4.56ms - +13.29ms
-

update-reflect

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
511.96ms - 522.48ms-unsure 🔍
-2% - +1%
-9.19ms - +6.72ms
unsure 🔍
-2% - +1%
-11.60ms - +4.30ms
tip-of-tree
tip-of-tree
512.48ms - 524.42msunsure 🔍
-1% - +2%
-6.72ms - +9.19ms
-unsure 🔍
-2% - +1%
-10.86ms - +6.02ms
previous-release
previous-release
514.91ms - 526.83msunsure 🔍
-1% - +2%
-4.30ms - +11.60ms
unsure 🔍
-1% - +2%
-6.02ms - +10.86ms
-

tachometer-reporter-action v2 for Benchmarks

@github-actions
Copy link
Contributor

The size of lit-html.js and lit-core.min.js are as expected.

Copy link
Member

@augustjk augustjk left a comment

Choose a reason for hiding this comment

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

Excellent!

@AndrewJakubowicz AndrewJakubowicz merged commit e901c58 into main Jan 12, 2024
@AndrewJakubowicz AndrewJakubowicz deleted the shim-toggle-attribute branch January 12, 2024 22:24
Comment on lines +65 to +73
if (force === undefined) {
force = !this.hasAttribute(name);
}
if (force) {
this.setAttribute(name, '');
} else {
this.removeAttribute(name);
}
return force;
Copy link
Collaborator

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:

Suggested change
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.

Copy link
Contributor Author

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

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.

[labs/ssr-dom-shim] Implement toggleAttribute

4 participants