Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR fixes a gnarly bug that can lead to unexpected requests for (sometimes massive) image files. This occurs for elements that have been marked as
fluid
while it or its parents are set todisplay:none;
.The root cause here is that the mechanism used to determine an element's dimensions in
imgix.calculateElementSize()
(https://github.com/imgix/imgix.js/blob/fix-displaynone/src/core.js#L240-L267) reports{width:0, height: 0}
for all hidden elements. When the function detemines that the element being measured has no height or width, it proceeds up the chain until an un-hidden element is found, usually the<body>
tag. Once a visible element is found, its dimensions are used to request an image and we end up with requests for images that match the width of the entire page--or, in a worst-case scenario, the height of the entire page.This behavior isn't inherently wrong;
imgix.calculateElementSize()
should report zeroes when measuring a hidden element. The correct fix is to avoid making this calculation entirely. This patch adds a check to immediately bail from theimgix.Fluidset.updateSrc()
method when we determine that the element we're trying to update is hidden, using the same implementation as jQuery does in its:hidden
selector.The final piece of the puzzle is to cover a the case of an
<img>
tag with an emptysrc
attribute. While we shouldn't encourage imgix.js users to include emptysrc
attributes on their tags, it hasn't been a problem before because we never bothered checking whether the element was visible or not. Now that we are, an element like<img src=""/>
will be evaluated as hidden regardless of itsdisplay
value, and imgix.js won't download or render itsdata-src
value as expected. To cope with this, we're setting all images with an emptysrc
attribute to an empty image before evaluating, to avoid this false positive.Review: @zacman85