Skip to content

Commit

Permalink
Merge bd8b54f into 5608cbd
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianaixba committed Nov 23, 2020
2 parents 5608cbd + bd8b54f commit 753e166
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class UsesOptimizedImages extends ByteEfficiencyAudit {
* @return {number}
*/
static estimateJPEGSizeFromDimensions(imageElement) {
// @ts-ignore - TS warns that naturalWidth and naturalHeight can be undefined, checked on L100.
const totalPixels = imageElement.naturalWidth * imageElement.naturalHeight;
// Even JPEGs with lots of detail can usually be compressed down to <1 byte per pixel
// Using 4:2:2 subsampling already gets an uncompressed bitmap to 2 bytes per pixel.
Expand Down Expand Up @@ -96,6 +97,9 @@ class UsesOptimizedImages extends ByteEfficiencyAudit {
continue;
}

// If naturalHeight or naturalWidth are undefined, information is not valid, skip.
if (!imageElement.naturalHeight || !imageElement.naturalWidth) continue;

jpegSize = UsesOptimizedImages.estimateJPEGSizeFromDimensions(imageElement);
fromProtocol = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class UsesResponsiveImages extends ByteEfficiencyAudit {
if (!usedPixels) {
const viewportWidth = ViewportDimensions.innerWidth;
const viewportHeight = ViewportDimensions.innerHeight * 2;
// @ts-ignore TS warns that naturalWidth and naturalHeight can be undefined, checked on L123.
const imageAspectRatio = image.naturalWidth / image.naturalHeight;
const viewportAspectRatio = viewportWidth / viewportHeight;
let usedViewportWidth = viewportWidth;
Expand All @@ -79,6 +80,7 @@ class UsesResponsiveImages extends ByteEfficiencyAudit {
}

const url = URL.elideDataURI(image.src);
// @ts-ignore TS warns that naturalWidth and naturalHeight can be undefined, checked on L123.
const actualPixels = image.naturalWidth * image.naturalHeight;
const wastedRatio = 1 - (usedPixels / actualPixels);
const totalBytes = image.resourceSize;
Expand Down Expand Up @@ -117,6 +119,9 @@ class UsesResponsiveImages extends ByteEfficiencyAudit {
continue;
}

// If naturalHeight or naturalWidth are undefined, information is not valid, skip.
if (!image.naturalWidth || !image.naturalHeight) continue;

const processed = UsesResponsiveImages.computeWaste(image, ViewportDimensions);
if (!processed) continue;

Expand Down
4 changes: 4 additions & 0 deletions lighthouse-core/audits/byte-efficiency/uses-webp-images.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class UsesWebPImages extends ByteEfficiencyAudit {
* @return {number}
*/
static estimateWebPSizeFromDimensions(imageElement) {
// @ts-ignore - TS warns that naturalWidth and naturalHeight can be undefined, checked on L100.
const totalPixels = imageElement.naturalWidth * imageElement.naturalHeight;
// See uses-optimized-images for the rationale behind our 2 byte-per-pixel baseline and
// JPEG compression ratio of 8:1.
Expand Down Expand Up @@ -95,6 +96,9 @@ class UsesWebPImages extends ByteEfficiencyAudit {
continue;
}

// If naturalHeight or naturalWidth are undefined, information is not valid, skip.
if (!imageElement.naturalWidth || !imageElement.naturalHeight) continue;

webpSize = UsesWebPImages.estimateWebPSizeFromDimensions(imageElement);
fromProtocol = false;
}
Expand Down
5 changes: 3 additions & 2 deletions lighthouse-core/audits/image-aspect-ratio.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,12 @@ class ImageAspectRatio extends Audit {
// - filter out images that don't have following properties:
// networkRecord, width, height, images that use `object-fit`: `cover` or `contain`
// - filter all svgs as they have no natural dimensions to audit
// - filter out images that have undefined naturalWidth or naturalHeight
return !image.isCss &&
image.mimeType &&
image.mimeType !== 'image/svg+xml' &&
image.naturalHeight > 5 &&
image.naturalWidth > 5 &&
image.naturalHeight && image.naturalHeight > 5 &&
image.naturalWidth && image.naturalWidth > 5 &&
image.displayedWidth &&
image.displayedHeight &&
!image.usesObjectFit;
Expand Down
4 changes: 3 additions & 1 deletion lighthouse-core/audits/image-size-responsive.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function isCandidate(image) {
if (image.displayedWidth <= 1 || image.displayedHeight <= 1) {
return false;
}
if (image.naturalWidth === 0 || image.naturalHeight === 0) {
if (!image.naturalWidth || !image.naturalHeight) {
return false;
}
if (image.mimeType === 'image/svg+xml') {
Expand Down Expand Up @@ -97,6 +97,7 @@ function isCandidate(image) {
function imageHasRightSize(image, DPR) {
const [expectedWidth, expectedHeight] =
allowedImageSize(image.displayedWidth, image.displayedHeight, DPR);
// @ts-ignore - TS warns that naturalWidth and naturalHeight can be undefined, checked in L220.
return image.naturalWidth >= expectedWidth && image.naturalHeight >= expectedHeight;
}

Expand All @@ -113,6 +114,7 @@ function getResult(image, DPR) {
elidedUrl: URL.elideDataURI(image.src),
displayedSize: `${image.displayedWidth} x ${image.displayedHeight}`,
actualSize: `${image.naturalWidth} x ${image.naturalHeight}`,
// @ts-ignore - TS warns that naturalWidth and naturalHeight can be undefined, checked in L220.
actualPixels: image.naturalWidth * image.naturalHeight,
expectedSize: `${expectedWidth} x ${expectedHeight}`,
expectedPixels: expectedWidth * expectedHeight,
Expand Down
7 changes: 2 additions & 5 deletions lighthouse-core/gather/gatherers/image-elements.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ function getHTMLImages(allElements) {
displayedWidth: element.width,
displayedHeight: element.height,
clientRect: getClientRect(element),
naturalWidth: canTrustNaturalDimensions ? element.naturalWidth : 0,
naturalHeight: canTrustNaturalDimensions ? element.naturalHeight : 0,
naturalWidth: canTrustNaturalDimensions ? element.naturalWidth : undefined,
naturalHeight: canTrustNaturalDimensions ? element.naturalHeight : undefined,
attributeWidth: element.getAttribute('width') || '',
attributeHeight: element.getAttribute('height') || '',
cssWidth: undefined, // this will get overwritten below
Expand Down Expand Up @@ -122,9 +122,6 @@ function getCSSImages(allElements) {
displayedWidth: element.clientWidth,
displayedHeight: element.clientHeight,
clientRect: getClientRect(element),
// CSS Images do not expose natural size, we'll determine the size later
naturalWidth: 0,
naturalHeight: 0,
attributeWidth: '',
attributeHeight: '',
cssWidth: undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ describe('Page uses responsive images', () => {
});

assert.equal(auditResult.items.length, 0);
assert.equal(auditResult.warnings.length, 1);
// Images with non valid naturalWidth or naturalHeight are ignored.
assert.equal(auditResult.warnings.length, 0);
});

it('de-dupes images', () => {
Expand Down
4 changes: 2 additions & 2 deletions types/artifacts.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,9 +405,9 @@ declare global {
/** The displayed height of the image, uses img.height when available falling back to clientHeight. See https://codepen.io/patrickhulce/pen/PXvQbM for examples. */
displayedHeight: number;
/** The natural width of the underlying image, uses img.naturalWidth. See https://codepen.io/patrickhulce/pen/PXvQbM for examples. */
naturalWidth: number;
naturalWidth?: number;
/** The natural height of the underlying image, uses img.naturalHeight. See https://codepen.io/patrickhulce/pen/PXvQbM for examples. */
naturalHeight: number;
naturalHeight?: number;
/** The raw width attribute of the image element. CSS images will be set to the empty string. */
attributeWidth: string;
/** The raw height attribute of the image element. CSS images will be set to the empty string. */
Expand Down

0 comments on commit 753e166

Please sign in to comment.