Skip to content

Commit

Permalink
core(unsized-images): ignore non-network SVGs (#13737)
Browse files Browse the repository at this point in the history
  • Loading branch information
styfle authored and adamraine committed Aug 16, 2022
1 parent 285473a commit ef35ad8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lighthouse-core/lib/url-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,9 @@ class URLShim extends URL {
}

if (url.protocol === 'data:') {
const match = url.pathname.match(/image\/(png|jpeg|svg\+xml|webp|gif|avif)(?=;)/);
const match = url.pathname.match(/^(image\/(png|jpeg|svg\+xml|webp|gif|avif))[;,]/);
if (!match) return undefined;
return match[0];
return match[1];
}

const match = url.pathname.toLowerCase().match(/\.(png|jpeg|jpg|svg|webp|gif|avif)$/);
Expand Down
15 changes: 14 additions & 1 deletion lighthouse-core/test/audits/unsized-images-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ describe('Sized images audit', () => {
expect(result.score).toEqual(1);
});

it('passes when an image is a non-network SVG', async () => {
it('passes when an image is a non-network SVG data url base64', async () => {
const result = await runAudit({
attributeWidth: '',
attributeHeight: '',
Expand All @@ -91,6 +91,19 @@ describe('Sized images audit', () => {
expect(result.score).toEqual(1);
});

it('passes when an image is a non-network SVG data url with encoded characters', async () => {
const result = await runAudit({
attributeWidth: '',
attributeHeight: '',
cssEffectiveRules: {
width: null,
height: null,
},
src: 'data:image/svg+xml,%3csvg%20xmlns=%27http://www.w3.org/2000/svg%27%20version=%271.1%27%20width=%27200%27%20height=%27200%27/%3e',
});
expect(result.score).toEqual(1);
});

describe('has empty width', () => {
it('fails when an image only has attribute height', async () => {
const result = await runAudit({
Expand Down
2 changes: 2 additions & 0 deletions lighthouse-core/test/lib/url-shim-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,10 @@ describe('URL Shim', () => {
expect(URL.guessMimeType('data:image/png;DATA')).toEqual('image/png');
expect(URL.guessMimeType('data:image/jpeg;DATA')).toEqual('image/jpeg');
expect(URL.guessMimeType('data:image/svg+xml;DATA')).toEqual('image/svg+xml');
expect(URL.guessMimeType('data:image/svg+xml,DATA')).toEqual('image/svg+xml');
expect(URL.guessMimeType('data:text/html;DATA')).toEqual(undefined);
expect(URL.guessMimeType('data:image/jpg;DATA')).toEqual(undefined);
expect(URL.guessMimeType('data:text/plain,image/png;base64,DATA')).toEqual(undefined);
});

it('uses path extension for normal files', () => {
Expand Down

0 comments on commit ef35ad8

Please sign in to comment.