Skip to content

core(unsized-images): skip images that are zero-sized#12054

Merged
paulirish merged 4 commits into
masterfrom
unsized-zeroisokay
Feb 10, 2021
Merged

core(unsized-images): skip images that are zero-sized#12054
paulirish merged 4 commits into
masterfrom
unsized-zeroisokay

Conversation

@paulirish

Copy link
Copy Markdown
Member

running kohls i saw these in the unsized-images results..

image

of course anything that's zero width & height isn't part of the layout (equivalent to display:none) but we hadn't really been checking that.

the boundingRect we have ends up being ideal for this calculation and includes if a parent is hidden or w/e.

sweet.

@paulirish paulirish requested a review from a team as a code owner February 5, 2021 17:41
@paulirish paulirish requested review from patrickhulce and removed request for a team February 5, 2021 17:41
@google-cla google-cla Bot added the cla: yes label Feb 5, 2021
Comment thread lighthouse-core/audits/unsized-images.js

@patrickhulce patrickhulce left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

this policy is great, but there's something worse going wrong if these are being flagged because they have an explicit CSS size of 0 already and should be ignored for that reason.

I'm concerned about the deeper issue here :/

If we don't have time to look into it now, maybe we file an issue but save this repro with what to uncomment?

Comment thread lighthouse-core/audits/unsized-images.js
@paulirish

Copy link
Copy Markdown
Member Author

this policy is great, but there's something worse going wrong if these are being flagged because they have an explicit CSS size of 0 already and should be ignored for that reason.

you mean explicit HTML size of 0, right?

image

I'm concerned about the deeper issue here :/

yeah...

here's the function which handles zero's oddly..

/**
* An img size attribute is valid for preventing CLS
* if it is a non-negative, non-zero integer.
* @param {string} attr
* @return {boolean}
*/
static isValidAttr(attr) {
const NON_NEGATIVE_INT_REGEX = /^\d+$/;
const ZERO_REGEX = /^0+$/;
return NON_NEGATIVE_INT_REGEX.test(attr) && !ZERO_REGEX.test(attr);
}

the non-negative bit comes from the spec.

the spec also says "If the two attributes are both zero, it indicates that the element is not intended for the user (e.g. it might be a part of a service to count page views)." which is nice.

but, I don't really follow why 0 as a width/height attribute size would not be a valid sized attr value.


soo.... i'll update this fn to not reject cases where attr value is "0"

@patrickhulce

Copy link
Copy Markdown
Collaborator

you mean explicit HTML size of 0, right?

No, I was actually talking about the inline CSS style="width:0px; height:0px;" but thanks for finding an even better bug on my mistake! 😆

soo.... i'll update this fn to not reject cases where attr value is "0"

SG 👍

@paulirish

Copy link
Copy Markdown
Member Author

you mean explicit HTML size of 0, right?

No, I was actually talking about the inline CSS style="width:0px; height:0px;" but thanks for finding an even better bug on my mistake! 😆

ah true. yeah.

well i've fixed the HTML one just now. 😛

small detail about CSS width/height 0.. so with this PR (and whatever change we're about to make to the gatherer (re #11289)) .. it's possible for images where 0/0 is set in inline styles... will make it through to the results.. because we'd only see the 0/0 from fetchSourceRules and soon we're not guaranteed to be doing that on all our images. but... hey i think that's okay.

Comment thread lighthouse-core/audits/unsized-images.js Outdated
Comment thread lighthouse-core/audits/unsized-images.js Outdated
Comment thread lighthouse-core/test/audits/unsized-images-test.js Outdated
expect(UnsizedImagesAudit.isValidAttr('3,000')).toEqual(false);
expect(UnsizedImagesAudit.isValidAttr('100.0')).toEqual(false);
expect(UnsizedImagesAudit.isValidAttr('2/3')).toEqual(false);
expect(UnsizedImagesAudit.isValidAttr('3,000')).toEqual(true); // interpretted as 3

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

perhaps the subject of a different future audit :)

Co-authored-by: Patrick Hulce <phulce@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants