Skip to content

Conversation

sp193
Copy link

@sp193 sp193 commented May 5, 2019

This pull request includes a

  • Bug fix
  • New feature
  • Translation

The following changes were made

  • Fixed resizeSearch() setting width to 0 due to innerWidth() returning 0 when the element is hidden due to its parent element being hidden. If innerWidth() returns 0, then resizeSearch() shall return "100%" to default to the maximum possible width.
  • Changed _resolveWidth() returning "auto" to "100%", so that it will retain its default behaviour of utilizing all maximum width.

If this is related to an existing ticket, include a link to it as well.
Related to #3292, #3817, #3278, #291, plus other duplicates.

Credit for the fix to resizeSearch() goes to @brandonhall of #3292.

I know that this may not give exactly the same result as when select2 is initialized while it is visible, but I really cannot tell what is a betterchoice than defaulting to 100% width. This is already 7 years old, so I hope it'll be fixed someday.

Between some offered solutions like #3292 and #4898, jQuery used to have a bug that resulted in width becoming 100px.

Originally posted by me in #3817:

As for why: up to this jQuery Github PR, jQuery had a problem with computing the width of elements under specific conditions. In this example, it fails to compute the width of the element because the jQuery outerWidth (similar for innerHeight, innerWidth, height, width, outerHeight and outerWidth) function will call the width cssHook, which in turn calls getWidthOrHeight(). getWidthOrHeight() may obtain a width in %, which is then returned as it is. The width function does not check what was returned and passes it through parseFloat, which results in the 100% becoming just 100.
In newer jQuery versions, this bug was solved, but the width of such a hidden element becomes 0.

Perhaps this was why #3292 could have worked, while yet the author of #4898 disagreed (the jQuery PR was still recent back then).

As for test cases: I don't know how to test for this because it is faulty behaviour, not that the logic was wrong.
#5259 contains a test case that will exhibit this bug. You can refer to this fiddle as well: https://jsfiddle.net/NozomiTypeR/6pLzy1ak/44/

@stale
Copy link

stale bot commented Jul 4, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the status: stale label Jul 4, 2019
@sp193
Copy link
Author

sp193 commented Jul 4, 2019

Is there anything else that needs to be done? I realize that Travis is complaining about some lines being too long. If we are still interested in this patch, then I will invest effort to shorten those lines.

@stale stale bot removed the status: stale label Jul 4, 2019
kevin-brown pushed a commit that referenced this pull request Jul 9, 2019
This allows for more accurate resolution of the width when compared
to the `resolve` method. This is more relevant for jQuery 1.x, where
the `resolve` method cannot find the width of a hidden select box,
but it also applies to newer versions of jQuery where the `width()`
method provided by jQuery doesn't fully match `getComputedStyle()`.

Fixes #3278
Fixes #5502
Closes #5259
kevin-brown added a commit that referenced this pull request Jul 9, 2019
This allows for more accurate resolution of the width when compared
to the `resolve` method. This is more relevant for jQuery 1.x, where
the `resolve` method cannot find the width of a hidden select box,
but it also applies to newer versions of jQuery where the `width()`
method provided by jQuery doesn't fully match `getComputedStyle()`.

Fixes #3278
Fixes #5502
Closes #5259
MarnuLombard added a commit to Collivery/Collivery-WooCommerce that referenced this pull request Sep 25, 2019
- The width of the `el` is collapsed if a parent is overlapping it.
  * See select2/select2#5502
- Use `width:'resolve'` so that the width of `el` matches the wrapper element
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.

1 participant