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

core(icons): defer to manifest-icon type hint for image type #6230

Merged
merged 2 commits into from
Oct 13, 2018
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
17 changes: 10 additions & 7 deletions lighthouse-core/lib/icons.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@ function pngSizedAtLeast(sizeRequirement, manifest) {
/** @type {Array<string>} */
const flattenedSizes = [];
iconValues
// filter out icons with a typehint that is not 'image/png'
.filter(icon => (!icon.value.type.value) ||
(icon.value.type.value &&
icon.value.type.value === 'image/png'))
// filter out icons that are not png
.filter(icon => icon.value.src.value &&
new URL(icon.value.src.value).pathname.endsWith('.png'))
.filter(icon => {
const typeHint = icon.value.type.value;
if (typeHint) {
// If a type hint is present, filter out icons that are not 'image/png'.
return typeHint === 'image/png';
}
// Otherwise, fall back to filtering on the icons' extension.
const src = icon.value.src.value;
return src && new URL(src).pathname.endsWith('.png');
})
.forEach(icon => {
// check that the icon has a size
if (icon.value.sizes.value) {
Expand Down
20 changes: 11 additions & 9 deletions lighthouse-core/test/lib/icons-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,22 +278,24 @@ describe('Icons helper', () => {
assert.equal(icons.pngSizedAtLeast(192, manifest.value).length, 0);
});

it('fails with an icon that has a png typehint but is not png', () => {
it('succeeds with a png icon that has query params in url', () => {
const manifestSrc = JSON.stringify({
icons: [{
src: 'path/to/image.jpg',
src: 'path/to/image.png?param=true',
sizes: '200x200',
type: 'image/png',
}],
});
const manifest = manifestParser(manifestSrc, EXAMPLE_MANIFEST_URL, EXAMPLE_DOC_URL);
assert.equal(icons.pngSizedAtLeast(192, manifest.value).length, 0);
assert.equal(icons.pngSizedAtLeast(192, manifest.value).length, 1);
});

it('succeeds with a png icon that has query params in url', () => {
// Note on tests below: we will believe your typehints until we can fetch the image and decode it.
// See https://github.com/GoogleChrome/lighthouse/issues/789
it('succeeds with an icon that has a png typehint but is not png', () => {
const manifestSrc = JSON.stringify({
icons: [{
src: 'path/to/image.png?param=true',
src: 'path/to/image.jpg',
sizes: '200x200',
type: 'image/png',
}],
Expand All @@ -302,7 +304,7 @@ describe('Icons helper', () => {
assert.equal(icons.pngSizedAtLeast(192, manifest.value).length, 1);
});

it('fails with a non-png icon that has query params in url', () => {
exterkamp marked this conversation as resolved.
Show resolved Hide resolved
it('succeeds with a non-png icon that has query params in url', () => {
const manifestSrc = JSON.stringify({
icons: [{
src: 'path/to/image.jpg?param=true',
Expand All @@ -311,10 +313,10 @@ describe('Icons helper', () => {
}],
});
const manifest = manifestParser(manifestSrc, EXAMPLE_MANIFEST_URL, EXAMPLE_DOC_URL);
assert.equal(icons.pngSizedAtLeast(192, manifest.value).length, 0);
assert.equal(icons.pngSizedAtLeast(192, manifest.value).length, 1);
});

it('fails with a non-png icon that has a .png extension in the middle', () => {
it('succeeds with a non-png icon that has a .png extension in the middle', () => {
const manifestSrc = JSON.stringify({
icons: [{
src: 'path/to/image.png.jpg',
Expand All @@ -323,7 +325,7 @@ describe('Icons helper', () => {
}],
});
const manifest = manifestParser(manifestSrc, EXAMPLE_MANIFEST_URL, EXAMPLE_DOC_URL);
assert.equal(icons.pngSizedAtLeast(192, manifest.value).length, 0);
assert.equal(icons.pngSizedAtLeast(192, manifest.value).length, 1);
});
});
});