🌐 AI搜索 & 代理 主页
Skip to content

Conversation

@mgol
Copy link
Member

@mgol mgol commented Feb 13, 2025

Summary

Changes:

  1. Fix measurements of <col span="2"> elements in Firefox.
  2. Fix measurements of all implicitly sized <col> elements in Safari.
  3. Don't try to show elements hidden via display: none before measuring their width or height. The styles we used to apply included position: absolute which could influence the styles anyway, so this was never reliable. backed out

Firefox always reports computed width as if span was 1. In Safari, computed
width for columns is always 0. Work around both issues by using offsetWidth.

In IE/Edge, <col> computed width is "auto" unless width is set explicitly
via CSS so measurements there remain incorrect. Because of the lack of a proper
workaround, we accept this limitation.

Fixes gh-5628

Size diff:

main @667321eb2d1c4328b993c25fbe2342a01ec4f87f
   raw     gz     br Filename
   -44    -30     -2 dist/jquery.min.js
   -39     -6    -14 dist/jquery.slim.min.js
   -44    -30    +45 dist-module/jquery.module.min.js
   -39     -8    +30 dist-module/jquery.slim.module.min.js
main @7c123dec4b96e7c3ce5f5a78e828c8aa335bea98
   raw     gz     br Filename
  +204    +51    +49 dist/jquery.min.js
  +209    +78   +134 dist/jquery.slim.min.js
  +204    +50    -20 dist-module/jquery.module.min.js
  +209    +76    +81 dist-module/jquery.slim.module.min.js

Checklist

@mgol mgol added CSS Needs review Discuss in Meeting Reserved for Issues and PRs that anyone would like to discuss in the weekly meeting. labels Feb 13, 2025
@mgol mgol self-assigned this Feb 13, 2025
@mgol mgol removed the Discuss in Meeting Reserved for Issues and PRs that anyone would like to discuss in the weekly meeting. label Feb 13, 2025
@mgol mgol marked this pull request as ready for review February 13, 2025 23:18
@mgol
Copy link
Member Author

mgol commented Feb 13, 2025

I recommend reviewing with whitespace changes disabled: https://github.com/jquery/jquery/pull/5630/files?w=1

@mgol
Copy link
Member Author

mgol commented Feb 13, 2025

A test run on BrowserStack for this PR was green: https://github.com/mgol/jquery/actions/runs/13318861255/job/37199296893

Comment on lines 151 to 156
// Support: IE 10 - 11+
// IE misreports `getComputedStyle` of table rows with width/height
// set in CSS while `offset*` properties report correct values.
// Support: Firefox 70+
// Firefox includes border widths
// in computed dimensions for table rows. (gh-4529)
Copy link
Member Author

Choose a reason for hiding this comment

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

We already have these comments next to the support test so we don't need to repeat them here.

swap( elem, cssShow, function() {
return getWidthOrHeight( elem, dimension, extra );
} ) :
getWidthOrHeight( elem, dimension, extra );
Copy link
Member Author

Choose a reason for hiding this comment

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

We could potentially inline this function here now but I wanted to avoid a big diff.

Copy link
Member

@gibson042 gibson042 left a comment

Choose a reason for hiding this comment

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

I'm in favor of fixing measurement of column-spanning elements in Firefox and table-related elements in general, but opposed to removing the style swap for display: none. Measuring such elements by swapping in position: absolute is not guaranteed to be accurate, but it is useful, particularly when preparing for visibility (e.g., at the start of an animation). This is one of those clunky-to-implement but valuable-to-have features that I think is worth preserving.

@mgol
Copy link
Member Author

mgol commented Feb 15, 2025

@gibson042 I'll admit this was added before my time so I may be lacking context. Is this logic useful mostly in cases where element dimensions are explicitly set via CSS (inline or selector-based)? Otherwise, it seems setting position: absolute is going to change the dimensions heavily.

@mgol
Copy link
Member Author

mgol commented Feb 16, 2025

@gibson042 I'm also asking because if width is set explicitly via CSS then getComputedStyle(elem).width does return it in all supported browsers even if the element has display: none. Only if sizing is implicit it gets turned into "auto".

EDIT: This also explains why removing this fallback didn't make our tests fail. We already have a test for a hidden div with an explicitly set width returns .width() as expected. We could add a test checking that for width set via a CSS class styles, not inline.

@mgol mgol force-pushed the col-width-fixes branch 2 times, most recently from 7c8dc21 to ebb1689 Compare February 18, 2025 22:26
@mgol mgol requested review from gibson042 and timmywil February 18, 2025 22:35
@mgol mgol added this to the 4.0.0 milestone Feb 18, 2025
@mgol
Copy link
Member Author

mgol commented Feb 18, 2025

@timmywil @gibson042 this is ready for another review.

I'm considering whether to backport this to 3.x-stable but I'd like this merged first anyway.

@mgol mgol added the Discuss in Meeting Reserved for Issues and PRs that anyone would like to discuss in the weekly meeting. label Feb 18, 2025
@mgol mgol changed the title CSS: Fix dimensions of col elems, don't measure display: none ones CSS: Fix dimensions of table <col> elements Feb 24, 2025
@timmywil timmywil removed the Discuss in Meeting Reserved for Issues and PRs that anyone would like to discuss in the weekly meeting. label Feb 24, 2025
Changes:
1. Fix measurements of `<col span="2">` elements in Firefox.
2. Fix measurements of all implicitly sized `<col>` elements in Safari.

Firefox always reports computed width as if `span` was 1. In Safari, computed
width for columns is always 0. Work around both issues by using `offsetWidth`.

In IE/Edge, `<col>` computed width is `"auto"` unless `width` is set explicitly
via CSS so measurements there remain incorrect. Because of the lack of a proper
workaround, we accept this limitation.

Fixes jquerygh-5628

CSS: Support dimensions for implicitly sized hidden elements again
@mgol mgol merged commit eca2a56 into jquery:main Feb 24, 2025
17 checks passed
@mgol mgol deleted the col-width-fixes branch February 24, 2025 17:44
@mgol mgol removed the Needs review label Feb 24, 2025
mgol added a commit that referenced this pull request Feb 24, 2025
Changes:
1. Fix measurements of `<col span="2">` elements in Firefox.
2. Fix measurements of all implicitly sized `<col>` elements in Safari.

Firefox always reports computed width as if `span` was 1. In Safari, computed
width for columns is always 0. Work around both issues by using `offsetWidth`.

In IE/Edge, `<col>` computed width is `"auto"` unless `width` is set explicitly
via CSS so measurements there remain incorrect. Because of the lack of a proper
workaround, we accept this limitation.

Fixes gh-5628
Closes gh-5634
Ref gh-5630
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 24, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Development

Successfully merging this pull request may close these issues.

Reconsider the width/height css hooks CSS swap behavior

3 participants