Skip to content

Commit

Permalink
webmanifest - Add support for icons in file_handlers (#9152)
Browse files Browse the repository at this point in the history
  • Loading branch information
grimsteel authored Sep 27, 2023
1 parent e0d6f54 commit 801de2c
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 39 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,26 @@
],
"shortcuts": [
{
"name": "example-shortcut",
"icons": [
{
"src": "shortcut-icon.png",
"sizes": "100x100",
"type": "image/png"
}
]
"name": "example-shortcut",
"icons": [
{
"src": "shortcut-icon.png",
"sizes": "100x100",
"type": "image/png"
}
]
}
],
"file_handlers": [
{
"name": "example-file-handler",
"icons": [
{
"src": "file-handler-icon.png",
"sizes": "100x100",
"type": "image/png"
}
]
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@
{}
]
}
]
],
"file_handlers": {}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,26 @@
],
"shortcuts": [
{
"name": "example-shortcut",
"icons": [
{
"src": "shortcut-icon.png",
"sizes": "100x100",
"type": "image/png"
}
]
"name": "example-shortcut",
"icons": [
{
"src": "shortcut-icon.png",
"sizes": "100x100",
"type": "image/png"
}
]
}
],
"file_handlers": [
{
"name": "example-file-handler",
"icons": [
{
"src": "file-handler-icon.png",
"sizes": "100x100",
"type": "image/png"
}
]
}
]
}
23 changes: 22 additions & 1 deletion packages/core/integration-tests/test/webmanifest.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ describe('webmanifest', function () {
type: 'png',
assets: ['shortcut-icon.png'],
},
{
type: 'png',
assets: ['file-handler-icon.png'],
},
]);

const manifest = await outputFS.readFile(
Expand All @@ -39,6 +43,7 @@ describe('webmanifest', function () {
assert(/screenshot\.[0-9a-f]+\.png/.test(manifest));
assert(/icon\.[0-9a-f]+\.png/.test(manifest));
assert(/shortcut-icon\.[0-9a-f]+\.png/.test(manifest));
assert(/file-handler-icon\.[0-9a-f]+\.png/.test(manifest));
});

it('should support .json', async function () {
Expand Down Expand Up @@ -67,6 +72,10 @@ describe('webmanifest', function () {
type: 'png',
assets: ['shortcut-icon.png'],
},
{
type: 'png',
assets: ['file-handler-icon.png'],
},
]);

const manifest = await outputFS.readFile(
Expand All @@ -76,9 +85,10 @@ describe('webmanifest', function () {
assert(/screenshot\.[0-9a-f]+\.png/.test(manifest));
assert(/icon\.[0-9a-f]+\.png/.test(manifest));
assert(/shortcut-icon\.[0-9a-f]+\.png/.test(manifest));
assert(/file-handler-icon\.[0-9a-f]+\.png/.test(manifest));
});

it('should throw on malformed icons and screenshots', async function () {
it('should throw on malformed icons, screenshots, shortcuts, and file handlers', async function () {
let manifestPath = path.join(
__dirname,
'/integration/webmanifest-schema/manifest.webmanifest',
Expand Down Expand Up @@ -167,6 +177,17 @@ describe('webmanifest', function () {
line: 31,
},
},
{
end: {
column: 21,
line: 35,
},
message: 'Expected type array',
start: {
column: 20,
line: 35,
},
},
],
},
],
Expand Down
47 changes: 26 additions & 21 deletions packages/transformers/webmanifest/src/WebManifestTransformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ const MANIFEST_SCHEMA: SchemaEntity = {
},
},
},
file_handlers: {
type: 'array',
items: {
type: 'object',
properties: {
icons: RESOURCES_SCHEMA,
},
},
},
},
};

Expand All @@ -54,40 +63,36 @@ export default (new Transformer({
'Invalid webmanifest',
);

for (const key of ['icons', 'screenshots']) {
const list = data[key];
function addResourceListToAsset(list, parent) {
if (list) {
invariant(Array.isArray(list));
for (let i = 0; i < list.length; i++) {
const res = list[i];
res.src = asset.addURLDependency(res.src, {
loc: {
filePath: asset.filePath,
...getJSONSourceLocation(pointers[`/${key}/${i}/src`], 'value'),
...getJSONSourceLocation(
pointers[`/${parent}/${i}/src`],
'value',
),
},
});
}
}
}

if (data.shortcuts) {
invariant(Array.isArray(data.shortcuts));
for (let i = 0; i < data.shortcuts.length; i++) {
const list = data.shortcuts[i].icons;
if (list) {
invariant(Array.isArray(list));
for (let j = 0; j < list.length; j++) {
const res = list[j];
res.src = asset.addURLDependency(res.src, {
loc: {
filePath: asset.filePath,
...getJSONSourceLocation(
pointers[`/shortcuts/${i}/icons/${j}/src`],
'value',
),
},
});
}
for (const key of ['icons', 'screenshots']) {
const list = data[key];
addResourceListToAsset(list, key);
}

for (const key of ['shortcuts', 'file_handlers']) {
const list = data[key];
if (list) {
invariant(Array.isArray(list));
for (let i = 0; i < list.length; i++) {
const iconList = list[i].icons;
addResourceListToAsset(iconList, `${key}/${i}/icons`);
}
}
}
Expand Down

0 comments on commit 801de2c

Please sign in to comment.