Skip to content

Commit 045c51c

Browse files
Add in file count check for data.files in canShare (#1899)
* Add in file count check for data.files in canShare * Update check and tests * Lint fix
1 parent c7683e9 commit 045c51c

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

injected/integration-test/web-compat-android.spec.js

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,34 @@ test.describe('Web Share API', () => {
7676
});
7777

7878
test.describe('navigator.canShare()', () => {
79-
test('should not let you share files', async ({ page }) => {
79+
test('should allow empty files arrays', async ({ page }) => {
8080
await navigate(page);
81-
const refuseFileShare = await page.evaluate(() => {
81+
const allowEmptyFiles = await page.evaluate(() => {
8282
return navigator.canShare({ text: 'xxx', files: [] });
8383
});
84+
expect(allowEmptyFiles).toEqual(true);
85+
});
86+
87+
test('should not let you share non-empty files arrays', async ({ page }) => {
88+
await navigate(page);
89+
const refuseFileShare = await page.evaluate(() => {
90+
// Create a mock File object
91+
const mockFile = new File([''], 'test.txt', { type: 'text/plain' });
92+
return navigator.canShare({ text: 'xxx', files: [mockFile] });
93+
});
8494
expect(refuseFileShare).toEqual(false);
8595
});
8696

97+
test('should reject non-array files values', async ({ page }) => {
98+
await navigate(page);
99+
const rejectNonArrayFiles = await page.evaluate(() => {
100+
// eslint-disable-next-line
101+
// @ts-ignore intentionally testing invalid files type
102+
return navigator.canShare({ text: 'xxx', files: 'not-an-array' });
103+
});
104+
expect(rejectNonArrayFiles).toEqual(false);
105+
});
106+
87107
test('should not let you share non-http urls', async ({ page }) => {
88108
await navigate(page);
89109
const refuseShare = await page.evaluate(() => {
@@ -218,10 +238,21 @@ test.describe('Web Share API', () => {
218238
expect(result).toBeUndefined();
219239
});
220240

221-
test('should throw when sharing files', async ({ page }) => {
241+
test('should allow sharing with empty files array', async ({ page }) => {
222242
await navigate(page);
223243
await beforeEach(page);
224244
const { result, message } = await checkShare(page, { title: 'title', files: [] });
245+
expect(message).toMatchObject({ featureName: 'webCompat', method: 'webShare', params: { title: 'title', text: '' } });
246+
expect(result).toBeUndefined();
247+
});
248+
249+
test('should throw when sharing non-empty files arrays', async ({ page }) => {
250+
await navigate(page);
251+
await beforeEach(page);
252+
const { result, message } = await checkShare(page, {
253+
title: 'title',
254+
files: [new File([''], 'test.txt', { type: 'text/plain' })],
255+
});
225256
expect(message).toBeNull();
226257
expect(result.threw.message).toContain('TypeError: Invalid share data');
227258
});

injected/src/features/web-compat.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ const MSG_DEVICE_ENUMERATION = 'deviceEnumeration';
2222
function canShare(data) {
2323
if (typeof data !== 'object') return false;
2424
if (!('url' in data) && !('title' in data) && !('text' in data)) return false; // At least one of these is required
25-
if ('files' in data) return false; // File sharing is not supported at the moment
25+
if ('files' in data) {
26+
if (!Array.isArray(data.files)) return false;
27+
if (data.files.length > 0) return false; // File sharing is not supported at the moment
28+
}
2629
if ('title' in data && typeof data.title !== 'string') return false;
2730
if ('text' in data && typeof data.text !== 'string') return false;
2831
if ('url' in data) {

0 commit comments

Comments
 (0)