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

new_audit: check images are big enough #10460

Merged
merged 21 commits into from
Mar 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
# To see the full list of contributors, see the revision history in
# source control.
Google LLC
Sebastian Kreft
sk- marked this conversation as resolved.
Show resolved Hide resolved

7 changes: 7 additions & 0 deletions lighthouse-cli/test/cli/__snapshots__/index-test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ Object {
Object {
"path": "image-aspect-ratio",
},
Object {
"path": "image-size-responsive",
},
Object {
"path": "deprecations",
},
Expand Down Expand Up @@ -748,6 +751,10 @@ Object {
"id": "image-aspect-ratio",
"weight": 1,
},
Object {
"id": "image-size-responsive",
"weight": 1,
},
],
"title": "Best Practices",
},
Expand Down
13 changes: 11 additions & 2 deletions lighthouse-cli/test/fixtures/dobetterweb/dbw_tester.html
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,18 @@ <h2>Do better web tester page</h2>
</template>

<!-- FAIL(image-aspect-ratio): image is naturally 480x318 -->
<img src="lighthouse-480x318.jpg" width="480" height="57">
<img src="lighthouse-480x318.jpg?iar1" width="120" height="15">
<!-- PASS(image-aspect-ratio) -->
<img src="lighthouse-480x318.jpg" width="480" height="318">
<img src="lighthouse-480x318.jpg?iar2" width="120" height="80">

<!-- FAIL(image-size-responsive): image is naturally 480x318 -->
<img src="lighthouse-480x318.jpg?isr1" width="4800" height="3180" style="position: absolute;">
<!-- PASS(image-size-responsive) -->
<img src="lighthouse-480x318.jpg?isr2" width="120" height="80" style="position: absolute;">
<!-- PASS(image-size-responsive) -->
<img src="lighthouse-480x318.jpg?isr3" width="960" height="636" style="image-rendering: pixelated; position: absolute;">
<!-- PASS(image-size-responsive) -->
<img src="lighthouse-480x318.jpg?isr4" srcset="lighthouse-480x318.jpg 2x" width="960" height="636" style="position: absolute;">

<!-- FAIL(efficient-animated-content): animated gif found -->
<img src="lighthouse-rotating.gif" width="811" height="462">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,19 @@ const expectations = [
details: {
items: {
0: {
displayedAspectRatio: /^480 x 57/,
displayedAspectRatio: /^120 x 15/,
url: 'http://localhost:10200/dobetterweb/lighthouse-480x318.jpg?iar1',
},
length: 1,
},
},
},
'image-size-responsive': {
score: 0,
details: {
items: {
0: {
url: 'http://localhost:10200/dobetterweb/lighthouse-480x318.jpg?isr1',
},
length: 1,
},
Expand Down Expand Up @@ -385,10 +397,10 @@ const expectations = [
},
'dom-size': {
score: 1,
numericValue: 143,
numericValue: 147,
details: {
items: [
{statistic: 'Total DOM Elements', value: '143'},
{statistic: 'Total DOM Elements', value: '147'},
{statistic: 'Maximum DOM Depth', value: '4'},
{
statistic: 'Maximum Child Elements',
Expand Down
241 changes: 241 additions & 0 deletions lighthouse-core/audits/image-size-responsive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
/**
* @license Copyright 2020 The Lighthouse Authors. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
/**
* @fileoverview Checks to see if the size of the visible images used on
* the page are large enough with respect to the pixel ratio. The
* audit will list all visible images that are too small.
*/
'use strict';

const Audit = require('./audit.js');
const URL = require('../lib/url-shim.js');
const i18n = require('../lib/i18n/i18n.js');

const UIStrings = {
/** Title of a Lighthouse audit that provides detail on the size of visible images on the page. This descriptive title is shown to users when all images have correct sizes. */
title: 'Displays images with appropriate size',
/** Title of a Lighthouse audit that provides detail on the size of visible images on the page. This descriptive title is shown to users when not all images have correct sizes. */
failureTitle: 'Displays images with inappropriate size',
/** Description of a Lighthouse audit that tells the user why they should maintain an appropriate size for all images. This is displayed after a user expands the section to see more. No character length limits. 'Learn More' becomes link text to additional documentation. */
description: 'Image natural dimensions should be proportional to the display size and the ' +
'pixel ratio to maximize image clarity. [Learn more](https://web.dev/image-size-responsive).',
/** Label for a column in a data table; entries in the column will be a string representing the displayed size of the image. */
columnDisplayed: 'Displayed size',
/** Label for a column in a data table; entries in the column will be a string representing the actual size of the image. */
columnActual: 'Actual size',
/** Label for a column in a data table; entries in the column will be a string representing the expected size of the image. */
columnExpected: 'Expected size',
};

const str_ = i18n.createMessageInstanceIdFn(__filename, UIStrings);

// Factors used to allow for smaller effective density.
// A factor of 1 means the actual device pixel density will be used.
// A factor of 0.5, means half the density is required. For example if the device pixel ratio is 3,
// then the images should have at least a density of 1.5.
const SMALL_IMAGE_FACTOR = 1.0;
const LARGE_IMAGE_FACTOR = 0.75;

// An image has must have both its dimensions lower or equal to the threshold in order to be
// considered SMALL.
const SMALL_IMAGE_THRESHOLD = 64;

/** @typedef {{url: string, elidedUrl: string, displayedSize: string, actualSize: string, actualPixels: number, expectedSize: string, expectedPixels: number}} Result */

/**
* @param {{top: number, bottom: number, left: number, right: number}} imageRect
* @param {{innerWidth: number, innerHeight: number}} viewportDimensions
* @return {boolean}
*/
function isVisible(imageRect, viewportDimensions) {
return (
(imageRect.bottom - imageRect.top) * (imageRect.right - imageRect.left) > 0 &&
imageRect.top <= viewportDimensions.innerHeight &&
imageRect.bottom >= 0 &&
imageRect.left <= viewportDimensions.innerWidth &&
imageRect.right >= 0
);
}

/**
* @param {LH.Artifacts.ImageElement} image
* @return {boolean}
*/
function isCandidate(image) {
sk- marked this conversation as resolved.
Show resolved Hide resolved
if (image.displayedWidth <= 1 || image.displayedHeight <= 1) {
return false;
}
if (image.naturalWidth === 0 || image.naturalHeight === 0) {
return false;
}
if (image.mimeType === 'image/svg+xml') {
return false;
}
if (image.isCss) {
return false;
}
if (image.usesObjectFit) {
return false;
}
if (image.usesPixelArtScaling) {
return false;
}
if (image.usesSrcSetDensityDescriptor) {
return false;
}
return true;
}

/**
* @param {LH.Artifacts.ImageElement} image
* @param {number} DPR
* @return {boolean}
*/
function imageHasRightSize(image, DPR) {
const [expectedWidth, expectedHeight] =
allowedImageSize(image.displayedWidth, image.displayedHeight, DPR);
return image.naturalWidth >= expectedWidth && image.naturalHeight >= expectedHeight;
}

/**
* @param {LH.Artifacts.ImageElement} image
* @param {number} DPR
* @return {Result}
*/
function getResult(image, DPR) {
const [expectedWidth, expectedHeight] =
expectedImageSize(image.displayedWidth, image.displayedHeight, DPR);
return {
url: image.src,
elidedUrl: URL.elideDataURI(image.src),
displayedSize: `${image.displayedWidth} x ${image.displayedHeight}`,
actualSize: `${image.naturalWidth} x ${image.naturalHeight}`,
actualPixels: image.naturalWidth * image.naturalHeight,
expectedSize: `${expectedWidth} x ${expectedHeight}`,
sk- marked this conversation as resolved.
Show resolved Hide resolved
expectedPixels: expectedWidth * expectedHeight,
};
}

/**
* Compute the size an image should have given the display dimensions and pixel density in order to
* pass the audit.
*
* For smaller images, typically icons, the size must be proportional to the density.
* For larger images some tolerance is allowed as in those cases the perceived degradation is not
* that bad.
*
* @param {number} displayedWidth
* @param {number} displayedHeight
* @param {number} DPR
* @return {[number, number]}
*/
function allowedImageSize(displayedWidth, displayedHeight, DPR) {
let factor = SMALL_IMAGE_FACTOR;
if (displayedWidth > SMALL_IMAGE_THRESHOLD || displayedHeight > SMALL_IMAGE_THRESHOLD) {
factor = LARGE_IMAGE_FACTOR;
}
const width = Math.ceil(factor * DPR * displayedWidth);
const height = Math.ceil(factor * DPR * displayedHeight);
return [width, height];
}

/**
* Compute the size an image should have given the display dimensions and pixel density.
*
* @param {number} displayedWidth
* @param {number} displayedHeight
* @param {number} DPR
* @return {[number, number]}
*/
function expectedImageSize(displayedWidth, displayedHeight, DPR) {
const width = Math.ceil(DPR * displayedWidth);
const height = Math.ceil(DPR * displayedHeight);
return [width, height];
}

/**
* Remove repeated entries for the same source.
*
* It will keep the entry with the largest expected size.
*
* @param {Result[]} results
* @return {Result[]}
*/
function deduplicateResultsByUrl(results) {
results.sort((a, b) => a.url === b.url ? 0 : (a.url < b. url ? -1 : 1));
sk- marked this conversation as resolved.
Show resolved Hide resolved
const deduplicated = /** @type {Result[]} */ ([]);
for (const r of results) {
const previousResult = deduplicated[deduplicated.length - 1];
if (previousResult && previousResult.url === r.url) {
// If the URL was the same, this is a duplicate. Keep the largest image.
if (previousResult.expectedPixels < r.expectedPixels) {
deduplicated[deduplicated.length - 1] = r;
}
} else {
deduplicated.push(r);
}
}
return deduplicated;
}

/**
* Sort entries in descending order by the magnitude of the size deficit, i.e. most pressing issues listed first.
*
* @param {Result[]} results
* @return {Result[]}
*/
function sortResultsBySizeDelta(results) {
return results.sort(
(a, b) => (b.expectedPixels - b.actualPixels) - (a.expectedPixels - a.actualPixels));
}

class ImageSizeResponsive extends Audit {
/**
* @return {LH.Audit.Meta}
*/
static get meta() {
return {
id: 'image-size-responsive',
title: str_(UIStrings.title),
failureTitle: str_(UIStrings.failureTitle),
description: str_(UIStrings.description),
requiredArtifacts: ['ImageElements', 'ViewportDimensions'],
};
}

/**
* @param {LH.Artifacts} artifacts
* @return {LH.Audit.Product}
*/
static audit(artifacts) {
const DPR = artifacts.ViewportDimensions.devicePixelRatio;
const results = Array
.from(artifacts.ImageElements)
.filter(isCandidate)
.filter(image => !imageHasRightSize(image, DPR))
.filter(image => isVisible(image.clientRect, artifacts.ViewportDimensions))
.map(image => getResult(image, DPR));

/** @type {LH.Audit.Details.Table['headings']} */
const headings = [
{key: 'url', itemType: 'thumbnail', text: ''},
{key: 'elidedUrl', itemType: 'url', text: str_(i18n.UIStrings.columnURL)},
{key: 'displayedSize', itemType: 'text', text: str_(UIStrings.columnDisplayed)},
{key: 'actualSize', itemType: 'text', text: str_(UIStrings.columnActual)},
{key: 'expectedSize', itemType: 'text', text: str_(UIStrings.columnExpected)},
];

const finalResults = sortResultsBySizeDelta(deduplicateResultsByUrl(results));

return {
score: Number(results.length === 0),
details: Audit.makeTableDetails(headings, finalResults),
};
}
}

module.exports = ImageSizeResponsive;
module.exports.UIStrings = UIStrings;
2 changes: 2 additions & 0 deletions lighthouse-core/config/default-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ const defaultConfig = {
'maskable-icon',
'content-width',
'image-aspect-ratio',
'image-size-responsive',
'deprecations',
'mainthread-work-breakdown',
'bootup-time',
Expand Down Expand Up @@ -521,6 +522,7 @@ const defaultConfig = {
{id: 'password-inputs-can-be-pasted-into', weight: 1},
{id: 'errors-in-console', weight: 1},
{id: 'image-aspect-ratio', weight: 1},
{id: 'image-size-responsive', weight: 1},
],
},
'seo': {
Expand Down
18 changes: 18 additions & 0 deletions lighthouse-core/lib/i18n/locales/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,24 @@
"lighthouse-core/audits/image-aspect-ratio.js | warningCompute": {
"message": "Invalid image sizing information {url}"
},
"lighthouse-core/audits/image-size-responsive.js | columnActual": {
"message": "Actual size"
},
"lighthouse-core/audits/image-size-responsive.js | columnDisplayed": {
"message": "Displayed size"
},
"lighthouse-core/audits/image-size-responsive.js | columnExpected": {
"message": "Expected size"
},
"lighthouse-core/audits/image-size-responsive.js | description": {
"message": "Image natural dimensions should be proportional to the display size and the pixel ratio to maximize image clarity. [Learn more](https://web.dev/image-size-responsive)."
},
"lighthouse-core/audits/image-size-responsive.js | failureTitle": {
"message": "Displays images with inappropriate size"
},
"lighthouse-core/audits/image-size-responsive.js | title": {
"message": "Displays images with appropriate size"
},
"lighthouse-core/audits/installable-manifest.js | description": {
"message": "Browsers can proactively prompt users to add your app to their homescreen, which can lead to higher engagement. [Learn more](https://web.dev/installable-manifest)."
},
Expand Down
18 changes: 18 additions & 0 deletions lighthouse-core/lib/i18n/locales/en-XL.json
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,24 @@
"lighthouse-core/audits/image-aspect-ratio.js | warningCompute": {
"message": "Îńv̂ál̂íd̂ ím̂áĝé ŝíẑín̂ǵ îńf̂ór̂ḿât́îón̂ {url}"
},
"lighthouse-core/audits/image-size-responsive.js | columnActual": {
"message": "Âćt̂úâĺ ŝíẑé"
},
"lighthouse-core/audits/image-size-responsive.js | columnDisplayed": {
"message": "D̂íŝṕl̂áŷéd̂ śîźê"
},
"lighthouse-core/audits/image-size-responsive.js | columnExpected": {
"message": "Êx́p̂éĉt́êd́ ŝíẑé"
},
"lighthouse-core/audits/image-size-responsive.js | description": {
"message": "Îḿâǵê ńât́ûŕâĺ d̂ím̂én̂śîón̂ś ŝh́ôúl̂d́ b̂é p̂ŕôṕôŕt̂íôńâĺ t̂ó t̂h́ê d́îśp̂ĺâý ŝíẑé âńd̂ t́ĥé p̂íx̂él̂ ŕât́îó t̂ó m̂áx̂ím̂íẑé îḿâǵê ćl̂ár̂ít̂ý. [L̂éâŕn̂ ḿôŕê](https://web.dev/image-size-responsive)."
},
"lighthouse-core/audits/image-size-responsive.js | failureTitle": {
"message": "D̂íŝṕl̂áŷś îḿâǵêś ŵít̂h́ îńâṕp̂ŕôṕr̂íât́ê śîźê"
},
"lighthouse-core/audits/image-size-responsive.js | title": {
"message": "D̂íŝṕl̂áŷś îḿâǵêś ŵít̂h́ âṕp̂ŕôṕr̂íât́ê śîźê"
},
"lighthouse-core/audits/installable-manifest.js | description": {
"message": "B̂ŕôẃŝér̂ś ĉán̂ ṕr̂óâćt̂ív̂él̂ý p̂ŕôḿp̂t́ ûśêŕŝ t́ô ád̂d́ ŷóûŕ âṕp̂ t́ô t́ĥéîŕ ĥóm̂éŝćr̂éêń, ŵh́îćĥ ćâń l̂éâd́ t̂ó ĥíĝh́êŕ êńĝáĝém̂én̂t́. [L̂éâŕn̂ ḿôŕê](https://web.dev/installable-manifest)."
},
Expand Down
Loading