Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix display:none; bug #28

Merged
merged 4 commits into from
May 19, 2015
Merged

Fix display:none; bug #28

merged 4 commits into from
May 19, 2015

Conversation

jayeb
Copy link
Contributor

@jayeb jayeb commented May 19, 2015

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 to display: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 the imgix.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 empty src attribute. While we shouldn't encourage imgix.js users to include empty src 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 its display value, and imgix.js won't download or render its data-src value as expected. To cope with this, we're setting all images with an empty src attribute to an empty image before evaluating, to avoid this false positive.

Review: @zacman85

@zacman85
Copy link
Contributor

Reviewed. LGTM.

@kellysutton
Copy link
Contributor

Any way to write some unit tests for this?

@jayeb
Copy link
Contributor Author

jayeb commented May 19, 2015

Good call.

@jayeb
Copy link
Contributor Author

jayeb commented May 19, 2015

Also, this PR will solve #27.

@jayeb jayeb force-pushed the fix-displaynone branch from e501c7a to 1802cea Compare May 19, 2015 22:34
@jayeb jayeb force-pushed the fix-displaynone branch from 1802cea to 0f196d0 Compare May 19, 2015 23:35
@kellysutton
Copy link
Contributor

Nice 👍

jayeb added a commit that referenced this pull request May 19, 2015
@jayeb jayeb merged commit 4ab0eb5 into master May 19, 2015
@jayeb jayeb deleted the fix-displaynone branch May 19, 2015 23:57
@jayeb jayeb mentioned this pull request May 20, 2015
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.

3 participants