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

webmanifest - Add support for icons in file_handlers #9152

Merged
merged 5 commits into from
Sep 27, 2023
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
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