Skip to content

Commit

Permalink
core(image-aspect-ratio): loosen ratio check (#5358)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickhulce committed May 29, 2018
1 parent 424c7b8 commit af71277
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lighthouse-core/audits/image-aspect-ratio.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
const Audit = require('./audit');

const URL = require('../lib/url-shim');
const THRESHOLD = 0.05;
const THRESHOLD_PX = 2;

/** @typedef {Required<LH.Artifacts.SingleImageUsage>} WellDefinedImage */

Expand All @@ -40,7 +40,9 @@ class ImageAspectRatio extends Audit {
const url = URL.elideDataURI(image.src);
const actualAspectRatio = image.naturalWidth / image.naturalHeight;
const displayedAspectRatio = image.width / image.height;
const doRatiosMatch = Math.abs(actualAspectRatio - displayedAspectRatio) < THRESHOLD;

const targetDisplayHeight = image.width / actualAspectRatio;
const doRatiosMatch = Math.abs(targetDisplayHeight - image.height) < THRESHOLD_PX;

if (!Number.isFinite(actualAspectRatio) ||
!Number.isFinite(displayedAspectRatio)) {
Expand Down
10 changes: 10 additions & 0 deletions lighthouse-core/test/audits/image-aspect-ratio-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@ describe('Images: aspect-ratio audit', () => {
},
});

testImage('is almost the right aspect ratio', {
rawValue: true,
clientSize: [412, 36],
naturalSize: [800, 69],
props: {
isCss: false,
usesObjectFit: false,
},
});

testImage('aspect ratios match', {
rawValue: true,
clientSize: [100, 100],
Expand Down

0 comments on commit af71277

Please sign in to comment.