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

Conversation

@timmywil
Copy link
Member

@timmywil timmywil commented Mar 6, 2017

Fixes gh-3193

Summary

  • I've removed our use of getBoundingClientRect in getWidthOrHeight. This is a stopgap that retrieves width/height ignoring transforms, without resorting to offsetWidth/Height, and without breaking existing tests.
  • @gibson042 suggested further refactoring and renaming, but I'm not sure all he has in mind. My main priority is fixing the bug with transforms. I think we can refactor further in future versions. Let me know if I missed something important.

Checklist

  • All authors have signed the CLA at https://contribute.jquery.com/CLA/
  • New tests have been added to show the fix or feature works
  • Grunt build and unit tests pass locally with these changes

@mention-bot
Copy link

@timmywil, thanks for your PR! By analyzing the history of the files in this pull request, we identified @markelog, @jeresig and @brandonaaron to be potential reviewers.

src/css.js Outdated
if ( elem.getClientRects().length ) {
val = elem.getBoundingClientRect()[ name ];
// Fall back to uncomputed css if necessary
if ( val < 0 || val == null ) {
Copy link
Member

Choose a reason for hiding this comment

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

This is dead code. curCSS will always get you a string so these conditions will always be false. curCSS will already get the inline style if the computed one is an empty string so this condition is already covered there.

Copy link
Member Author

Choose a reason for hiding this comment

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

Isn't that a change for another ticket? This is legacy code that I left alone. I just wonder why this has been here so long if it wasn't necessary.

Copy link
Member

Choose a reason for hiding this comment

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

It seems I forgot to simplify it when I migrated the code to use getBoundingClientRect.

That said, this is related to this PR - in my PR I should have just changed val <= 0 || val == null to val === nullas the code inside would be used in those cases, only the val <= 0 condition was never true. In your PR this becomes true dead code and since you even had to change this condition for the new logic to work, I think this should be removed here.

Copy link
Member Author

@timmywil timmywil Mar 13, 2017

Choose a reason for hiding this comment

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

you even had to change this condition for the new logic to work

I did? It seems to me the logic is the same.

Before:

  1. Try getBoundingClientRect
  2. If negative, zero, null, or undefined, try getCSS.
  3. If negative, null, or undefined, get inline style.

After:

  1. Try getCSS
  2. If negative, null, or undefined, get inline style.

Maybe I'm misunderstanding something, but it seems clear to me I haven't changed that condition.

That said, you may be right about this condition being dead code previously as well. I can check that and include it in this PR, but I still believe it is a separate ticket. At the very least, a separate commit.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok, did some testing in Chrome and IE9. Yup, this looks like dead code, but I do believe it was dead code before this PR as well. I'll include it here, but as a separate commit.

Copy link
Member

Choose a reason for hiding this comment

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

To sum up, since by changing getBoundingClientRect to getCSS the condition inside the if became always false while it wasn't that way before, I think the removal belongs in the same commit as the initial refactor.

Not that it's terribly important, though. :)

Copy link
Member Author

Choose a reason for hiding this comment

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

The condition val <= 0 was made more strict: val < 0.

That was a different condition. The diff might make it look that way. If you look closer, you'll notice the condition with val <= 0 was completely removed because it was checking the result from getBoundingClientRect, which was also removed. The val < 0 condition was already there checking the result from getCSS, which we now do first.

By changing what's initially tried from getBoundingClientRect to getCSS you eliminated the possibility of the value not being defined.

This was essentially true before. Again, this condition was already there after getCSS.

Copy link
Member Author

Choose a reason for hiding this comment

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

But I agree this is moot now since I've removed the condition. You approve now right?

Copy link
Member Author

Choose a reason for hiding this comment

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

diff?w=1 might make my point more clear.

Copy link
Member

Choose a reason for hiding this comment

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

Ahh, yeah, I totally mixed those two, you're right! They're so close to each other and somehow I didn't notice. 😄

LGTM then!

- getCSS already falls back to inline styles
@timmywil timmywil modified the milestone: 3.2.0 Mar 13, 2017
timmywil added a commit to timmywil/jquery that referenced this pull request Mar 13, 2017
- getCSS already falls back to inline styles

Ref jquerygh-3561
@timmywil timmywil closed this in c920ff6 Mar 13, 2017
@timmywil timmywil deleted the 3193/transform-width branch March 13, 2017 17:33
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 guess my comments mirror @mgol's observations about dead code. As for the other refactoring, I was referencing #3193 (comment) and suggesting that we introduce methods like .contentBoxWidth/.paddingBoxWidth/.borderBoxWidth/.marginBoxWidth. It can definitely wait for later.

val = elem.getBoundingClientRect()[ name ];
// Computed unit is not pixels. Stop here and return.
if ( rnumnonpx.test( val ) ) {
return val;
Copy link
Member

Choose a reason for hiding this comment

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

Out of curiosity, when does this happen?

// Check for style in case a browser which returns unreliable values
// for getComputedStyle silently falls back to the reliable elem.style
valueIsBorderBox = isBorderBox &&
( support.boxSizingReliable() || val === elem.style[ name ] );
Copy link
Member

Choose a reason for hiding this comment

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

Since you deleted the "uncomputed" block above, I think the elem.style branch can be dropped.

@mgol
Copy link
Member

mgol commented Mar 17, 2017

This PR introduced a big regression: it broke .width() on inline elements. See #3571.

@lock lock bot locked as resolved and limited conversation to collaborators Jan 18, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Development

Successfully merging this pull request may close these issues.

different result of width() and height() since jQuery 3.0

4 participants