support width (for size ratio)

This commit is contained in:
Kagami Sascha Rosylight 2023-03-11 21:38:28 +01:00
parent 1866d9929a
commit 6d182f919e
18 changed files with 34 additions and 13 deletions

View File

@ -64,8 +64,12 @@ async function getOEmbedPlayer($: cheerio.CheerioAPI, pageUrl: string): Promise<
return null; return null;
} }
const height = Math.min(Number(iframe.attr('height') ?? body.height), 1024); if (
if (Number.isNaN(height)) { typeof body.width !== 'number' ||
body.width <= 0 ||
typeof body.height !== 'number' ||
body.height <= 0
) {
// No proper size info // No proper size info
return null; return null;
} }
@ -87,8 +91,8 @@ async function getOEmbedPlayer($: cheerio.CheerioAPI, pageUrl: string): Promise<
return { return {
url, url,
width: null, width: body.width,
height, height: body.height,
allow: allowedFeatures allow: allowedFeatures
} }
} }

View File

@ -262,16 +262,12 @@ describe("oEmbed", () => {
}); });
} }
test('src', async () => { test('basic properties', async () => {
await setUpFastify('oembed.json'); await setUpFastify('oembed.json');
const summary = await summaly(host); const summary = await summaly(host);
expect(summary.player.url).toBe('https://example.com/'); expect(summary.player.url).toBe('https://example.com/');
}); expect(summary.player.width).toBe(500);
expect(summary.player.height).toBe(300);
test('max height', async () => {
await setUpFastify('oembed-too-tall.json');
const summary = await summaly(host);
expect(summary.player.height).toBe(1024);
}); });
test('children are ignored', async () => { test('children are ignored', async () => {

View File

@ -2,5 +2,6 @@
"version": "1.0", "version": "1.0",
"type": "rich", "type": "rich",
"html": "<div><iframe src='https://example.com/'></iframe>", "html": "<div><iframe src='https://example.com/'></iframe>",
"width": 500,
"height": 300 "height": 300
} }

View File

@ -2,5 +2,6 @@
"version": "1.0", "version": "1.0",
"type": "rich", "type": "rich",
"html": "<iframe src='https://example.com/'></iframe><iframe src='https://example.com/'></iframe>", "html": "<iframe src='https://example.com/'></iframe><iframe src='https://example.com/'></iframe>",
"width": 500,
"height": 300 "height": 300
} }

View File

@ -2,5 +2,6 @@
"version": "11.0", "version": "11.0",
"type": "rich", "type": "rich",
"html": "<iframe src='https://example.com/'></iframe>", "html": "<iframe src='https://example.com/'></iframe>",
"width": 500,
"height": 300 "height": 300
} }

View File

@ -2,5 +2,6 @@
"version": "1.0", "version": "1.0",
"type": "rich", "type": "rich",
"html": "<iframe src='http://example.com/'></iframe>", "html": "<iframe src='http://example.com/'></iframe>",
"width": 500,
"height": 300 "height": 300
} }

View File

@ -2,5 +2,6 @@
"version": "1.0", "version": "1.0",
"type": "rich", "type": "rich",
"html": "<iframe src='https://example.com/'></iframe>", "html": "<iframe src='https://example.com/'></iframe>",
"width": 500,
"height": "blobcat" "height": "blobcat"
} }

View File

@ -0,0 +1,7 @@
{
"version": "1.0",
"type": "rich",
"html": "<iframe src='https://example.com/'></iframe>",
"width": "blobcat",
"height": 300
}

View File

@ -1,5 +1,6 @@
{ {
"version": "1.0", "version": "1.0",
"type": "rich", "type": "rich",
"html": "<iframe src='https://example.com/'></iframe>" "html": "<iframe src='https://example.com/'></iframe>",
"width": 500
} }

View File

@ -1,5 +1,6 @@
{ {
"type": "rich", "type": "rich",
"html": "<iframe src='https://example.com/'></iframe>", "html": "<iframe src='https://example.com/'></iframe>",
"width": 500,
"height": 300 "height": 300
} }

View File

@ -2,5 +2,5 @@
"version": "1.0", "version": "1.0",
"type": "rich", "type": "rich",
"html": "<iframe src='https://example.com/'></iframe>", "html": "<iframe src='https://example.com/'></iframe>",
"height": 3000 "height": 300
} }

View File

@ -2,5 +2,6 @@
"version": "0.1", "version": "0.1",
"type": "rich", "type": "rich",
"html": "<iframe src='https://example.com/'></iframe>", "html": "<iframe src='https://example.com/'></iframe>",
"width": 500,
"height": 300 "height": 300
} }

View File

@ -2,5 +2,6 @@
"version": "1.0", "version": "1.0",
"type": "rich", "type": "rich",
"html": "<iframe src='https://example.com/' allow='camera'></iframe>", "html": "<iframe src='https://example.com/' allow='camera'></iframe>",
"width": 500,
"height": 300 "height": 300
} }

View File

@ -2,5 +2,6 @@
"version": "1.0", "version": "1.0",
"type": "rich", "type": "rich",
"html": "<iframe src='https://example.com/' allow='fullscreen;camera'></iframe>", "html": "<iframe src='https://example.com/' allow='fullscreen;camera'></iframe>",
"width": 500,
"height": 300 "height": 300
} }

View File

@ -2,5 +2,6 @@
"version": "1.0", "version": "1.0",
"type": "rich", "type": "rich",
"html": "<iframe src='https://example.com/' allow='fullscreen'></iframe>", "html": "<iframe src='https://example.com/' allow='fullscreen'></iframe>",
"width": 500,
"height": 300 "height": 300
} }

View File

@ -2,5 +2,6 @@
"version": "1.0", "version": "1.0",
"type": "rich", "type": "rich",
"html": "<iframe src='https://example.com/' allow='autoplay;clipboard-write;fullscreen;encrypted-media;picture-in-picture'></iframe>", "html": "<iframe src='https://example.com/' allow='autoplay;clipboard-write;fullscreen;encrypted-media;picture-in-picture'></iframe>",
"width": 500,
"height": 300 "height": 300
} }

View File

@ -2,5 +2,6 @@
"version": "1.0", "version": "1.0",
"type": "rich", "type": "rich",
"html": "<iframe src='https://example.com/'><script>alert('Hahaha I take this world')</script></iframe>", "html": "<iframe src='https://example.com/'><script>alert('Hahaha I take this world')</script></iframe>",
"width": 500,
"height": 300 "height": 300
} }

View File

@ -2,5 +2,6 @@
"version": "1.0", "version": "1.0",
"type": "rich", "type": "rich",
"html": "<iframe src='https://example.com/'></iframe>", "html": "<iframe src='https://example.com/'></iframe>",
"width": 500,
"height": 300 "height": 300
} }