From 6d182f919e34e28b8557d81e6caf07f4d2b2c5f9 Mon Sep 17 00:00:00 2001 From: Kagami Sascha Rosylight Date: Sat, 11 Mar 2023 21:38:28 +0100 Subject: [PATCH] support width (for size ratio) --- src/general.ts | 12 ++++++++---- test/index.ts | 10 +++------- test/oembed/invalid/oembed-child-iframe.json | 1 + test/oembed/invalid/oembed-double-iframes.json | 1 + test/oembed/invalid/oembed-future.json | 1 + test/oembed/invalid/oembed-insecure.json | 1 + test/oembed/invalid/oembed-invalid-height.json | 1 + test/oembed/invalid/oembed-invalid-width.json | 7 +++++++ test/oembed/invalid/oembed-no-height.json | 3 ++- test/oembed/invalid/oembed-no-version.json | 1 + .../oembed-no-width.json} | 2 +- test/oembed/invalid/oembed-old.json | 1 + test/oembed/invalid/oembed-too-powerful.json | 1 + test/oembed/invalid/oembed-too-powerful2.json | 1 + test/oembed/oembed-allow-fullscreen.json | 1 + test/oembed/oembed-allow-safelisted-features.json | 1 + test/oembed/oembed-iframe-child.json | 1 + test/oembed/oembed.json | 1 + 18 files changed, 34 insertions(+), 13 deletions(-) create mode 100644 test/oembed/invalid/oembed-invalid-width.json rename test/oembed/{oembed-too-tall.json => invalid/oembed-no-width.json} (85%) diff --git a/src/general.ts b/src/general.ts index 495998c..b93bed4 100644 --- a/src/general.ts +++ b/src/general.ts @@ -64,8 +64,12 @@ async function getOEmbedPlayer($: cheerio.CheerioAPI, pageUrl: string): Promise< return null; } - const height = Math.min(Number(iframe.attr('height') ?? body.height), 1024); - if (Number.isNaN(height)) { + if ( + typeof body.width !== 'number' || + body.width <= 0 || + typeof body.height !== 'number' || + body.height <= 0 + ) { // No proper size info return null; } @@ -87,8 +91,8 @@ async function getOEmbedPlayer($: cheerio.CheerioAPI, pageUrl: string): Promise< return { url, - width: null, - height, + width: body.width, + height: body.height, allow: allowedFeatures } } diff --git a/test/index.ts b/test/index.ts index 4d18730..c457273 100644 --- a/test/index.ts +++ b/test/index.ts @@ -262,16 +262,12 @@ describe("oEmbed", () => { }); } - test('src', async () => { + test('basic properties', async () => { await setUpFastify('oembed.json'); const summary = await summaly(host); expect(summary.player.url).toBe('https://example.com/'); - }); - - test('max height', async () => { - await setUpFastify('oembed-too-tall.json'); - const summary = await summaly(host); - expect(summary.player.height).toBe(1024); + expect(summary.player.width).toBe(500); + expect(summary.player.height).toBe(300); }); test('children are ignored', async () => { diff --git a/test/oembed/invalid/oembed-child-iframe.json b/test/oembed/invalid/oembed-child-iframe.json index 80e8570..f7bf052 100644 --- a/test/oembed/invalid/oembed-child-iframe.json +++ b/test/oembed/invalid/oembed-child-iframe.json @@ -2,5 +2,6 @@ "version": "1.0", "type": "rich", "html": "
", + "width": 500, "height": 300 } diff --git a/test/oembed/invalid/oembed-double-iframes.json b/test/oembed/invalid/oembed-double-iframes.json index 2d9837f..2e5b0c8 100644 --- a/test/oembed/invalid/oembed-double-iframes.json +++ b/test/oembed/invalid/oembed-double-iframes.json @@ -2,5 +2,6 @@ "version": "1.0", "type": "rich", "html": "", + "width": 500, "height": 300 } diff --git a/test/oembed/invalid/oembed-future.json b/test/oembed/invalid/oembed-future.json index 7978d13..db356c0 100644 --- a/test/oembed/invalid/oembed-future.json +++ b/test/oembed/invalid/oembed-future.json @@ -2,5 +2,6 @@ "version": "11.0", "type": "rich", "html": "", + "width": 500, "height": 300 } diff --git a/test/oembed/invalid/oembed-insecure.json b/test/oembed/invalid/oembed-insecure.json index 288edb3..db2ebf4 100644 --- a/test/oembed/invalid/oembed-insecure.json +++ b/test/oembed/invalid/oembed-insecure.json @@ -2,5 +2,6 @@ "version": "1.0", "type": "rich", "html": "", + "width": 500, "height": 300 } diff --git a/test/oembed/invalid/oembed-invalid-height.json b/test/oembed/invalid/oembed-invalid-height.json index 42c8cb9..1f42210 100644 --- a/test/oembed/invalid/oembed-invalid-height.json +++ b/test/oembed/invalid/oembed-invalid-height.json @@ -2,5 +2,6 @@ "version": "1.0", "type": "rich", "html": "", + "width": 500, "height": "blobcat" } diff --git a/test/oembed/invalid/oembed-invalid-width.json b/test/oembed/invalid/oembed-invalid-width.json new file mode 100644 index 0000000..ea2673c --- /dev/null +++ b/test/oembed/invalid/oembed-invalid-width.json @@ -0,0 +1,7 @@ +{ + "version": "1.0", + "type": "rich", + "html": "", + "width": "blobcat", + "height": 300 +} diff --git a/test/oembed/invalid/oembed-no-height.json b/test/oembed/invalid/oembed-no-height.json index de748b0..0658bc2 100644 --- a/test/oembed/invalid/oembed-no-height.json +++ b/test/oembed/invalid/oembed-no-height.json @@ -1,5 +1,6 @@ { "version": "1.0", "type": "rich", - "html": "" + "html": "", + "width": 500 } diff --git a/test/oembed/invalid/oembed-no-version.json b/test/oembed/invalid/oembed-no-version.json index 26f135e..d09f036 100644 --- a/test/oembed/invalid/oembed-no-version.json +++ b/test/oembed/invalid/oembed-no-version.json @@ -1,5 +1,6 @@ { "type": "rich", "html": "", + "width": 500, "height": 300 } diff --git a/test/oembed/oembed-too-tall.json b/test/oembed/invalid/oembed-no-width.json similarity index 85% rename from test/oembed/oembed-too-tall.json rename to test/oembed/invalid/oembed-no-width.json index 3f857c0..cd48cc3 100644 --- a/test/oembed/oembed-too-tall.json +++ b/test/oembed/invalid/oembed-no-width.json @@ -2,5 +2,5 @@ "version": "1.0", "type": "rich", "html": "", - "height": 3000 + "height": 300 } diff --git a/test/oembed/invalid/oembed-old.json b/test/oembed/invalid/oembed-old.json index 7db781c..dc1c1c4 100644 --- a/test/oembed/invalid/oembed-old.json +++ b/test/oembed/invalid/oembed-old.json @@ -2,5 +2,6 @@ "version": "0.1", "type": "rich", "html": "", + "width": 500, "height": 300 } diff --git a/test/oembed/invalid/oembed-too-powerful.json b/test/oembed/invalid/oembed-too-powerful.json index 111ab92..6456ff7 100644 --- a/test/oembed/invalid/oembed-too-powerful.json +++ b/test/oembed/invalid/oembed-too-powerful.json @@ -2,5 +2,6 @@ "version": "1.0", "type": "rich", "html": "", + "width": 500, "height": 300 } diff --git a/test/oembed/invalid/oembed-too-powerful2.json b/test/oembed/invalid/oembed-too-powerful2.json index 312b0e2..27c1ad1 100644 --- a/test/oembed/invalid/oembed-too-powerful2.json +++ b/test/oembed/invalid/oembed-too-powerful2.json @@ -2,5 +2,6 @@ "version": "1.0", "type": "rich", "html": "", + "width": 500, "height": 300 } diff --git a/test/oembed/oembed-allow-fullscreen.json b/test/oembed/oembed-allow-fullscreen.json index f1eb2db..286fbdd 100644 --- a/test/oembed/oembed-allow-fullscreen.json +++ b/test/oembed/oembed-allow-fullscreen.json @@ -2,5 +2,6 @@ "version": "1.0", "type": "rich", "html": "", + "width": 500, "height": 300 } diff --git a/test/oembed/oembed-allow-safelisted-features.json b/test/oembed/oembed-allow-safelisted-features.json index 6ef40a1..94cb9ef 100644 --- a/test/oembed/oembed-allow-safelisted-features.json +++ b/test/oembed/oembed-allow-safelisted-features.json @@ -2,5 +2,6 @@ "version": "1.0", "type": "rich", "html": "", + "width": 500, "height": 300 } diff --git a/test/oembed/oembed-iframe-child.json b/test/oembed/oembed-iframe-child.json index e28fd68..10ca745 100644 --- a/test/oembed/oembed-iframe-child.json +++ b/test/oembed/oembed-iframe-child.json @@ -2,5 +2,6 @@ "version": "1.0", "type": "rich", "html": "", + "width": 500, "height": 300 } diff --git a/test/oembed/oembed.json b/test/oembed/oembed.json index cd48cc3..704c37e 100644 --- a/test/oembed/oembed.json +++ b/test/oembed/oembed.json @@ -2,5 +2,6 @@ "version": "1.0", "type": "rich", "html": "", + "width": 500, "height": 300 }