From 0ee23f9798fb67b59da08d1513bd7d0bea7976d8 Mon Sep 17 00:00:00 2001 From: Florian Dieminger Date: Mon, 26 Jun 2023 10:32:08 +0200 Subject: [PATCH 1/4] fix(livesamples): use correct legacy url We did not use the legacy URL in LiveSampleURL.ejs --- kumascript/macros/LiveSampleURL.ejs | 2 +- .../tests/macros/EmbedLiveSample.test.ts | 27 ++++++++++--------- kumascript/tests/macros/LiveSampleURL.test.ts | 21 +++++++++------ 3 files changed, 28 insertions(+), 22 deletions(-) diff --git a/kumascript/macros/LiveSampleURL.ejs b/kumascript/macros/LiveSampleURL.ejs index 6b442d0d3aeb..925d5d5a1bf2 100644 --- a/kumascript/macros/LiveSampleURL.ejs +++ b/kumascript/macros/LiveSampleURL.ejs @@ -22,7 +22,7 @@ const samplePath = $1 ? let url; if (env.live_samples.base_url.includes('://')) { - url = new URL(env.live_samples.base_url); + url = new URL($1 ? env.live_samples.legacy_url : env.live_samples.base_url); url.pathname = samplePath; url.search = searchParams.toString(); } else { diff --git a/kumascript/tests/macros/EmbedLiveSample.test.ts b/kumascript/tests/macros/EmbedLiveSample.test.ts index 008421610043..c3a073c46229 100644 --- a/kumascript/tests/macros/EmbedLiveSample.test.ts +++ b/kumascript/tests/macros/EmbedLiveSample.test.ts @@ -4,7 +4,8 @@ import { assert, itMacro, describeMacro, beforeEachMacro } from "./utils.js"; describeMacro("EmbedLiveSample", function () { beforeEachMacro(function (macro) { macro.ctx.env.live_samples = { - base_url: "https://mdn.mozillademos.org", + base_url: "https://live.mdnplay.dev", + legacy_url: "https://mdn.mozillademos.org", }; macro.ctx.info.hasPage = jest.fn(() => true); }); @@ -15,7 +16,7 @@ describeMacro("EmbedLiveSample", function () { '

" ); }); @@ -27,7 +28,7 @@ describeMacro("EmbedLiveSample", function () { '

" ); }); @@ -38,7 +39,7 @@ describeMacro("EmbedLiveSample", function () { '

" ); }); @@ -50,7 +51,7 @@ describeMacro("EmbedLiveSample", function () { '

" ); }); @@ -61,7 +62,7 @@ describeMacro("EmbedLiveSample", function () { '

" ); }); @@ -73,7 +74,7 @@ describeMacro("EmbedLiveSample", function () { ' title="Example sample"' + ' id="frame_example"' + ' width="100%"' + - ' src="https://mdn.mozillademos.org/en-US/docs/Web/CSS/border-top-width/runner.html?id=example">' + + ' src="https://live.mdnplay.dev/en-US/docs/Web/CSS/border-top-width/runner.html?id=example">' + "" ); }); @@ -85,7 +86,7 @@ describeMacro("EmbedLiveSample", function () { ' title="Example sample"' + ' id="frame_example"' + ' width=""><script>alert("XSS");</script>"' + - ' src="https://mdn.mozillademos.org/en-US/docs/Web/CSS/border-top-width/runner.html?id=example">' + + ' src="https://live.mdnplay.dev/en-US/docs/Web/CSS/border-top-width/runner.html?id=example">' + "" ); }); @@ -97,7 +98,7 @@ describeMacro("EmbedLiveSample", function () { ' title="Images sample"' + ' id="frame_images"' + ' width="100%" height="250"' + - ' src="https://mdn.mozillademos.org/en-US/docs/Web/HTML/Element/figure/runner.html?id=images">' + + ' src="https://live.mdnplay.dev/en-US/docs/Web/HTML/Element/figure/runner.html?id=images">' + "" ); }); @@ -110,7 +111,7 @@ describeMacro("EmbedLiveSample", function () { ' title="增加关键帧 sample"' + ' id="frame_增加关键帧"' + ' width="100%" height="250"' + - ' src="https://mdn.mozillademos.org/zh-CN/docs/Web/CSS/CSS_Animations/Using_CSS_animations/runner.html?id=%E5%A2%9E%E5%8A%A0%E5%85%B3%E9%94%AE%E5%B8%A7">' + + ' src="https://live.mdnplay.dev/zh-CN/docs/Web/CSS/CSS_Animations/Using_CSS_animations/runner.html?id=%E5%A2%9E%E5%8A%A0%E5%85%B3%E9%94%AE%E5%B8%A7">' + "" ); }); @@ -126,7 +127,7 @@ describeMacro("EmbedLiveSample", function () { ' title="%E4%B8%80%E4%B8%AA%E6%A8%A1%E6%9D%BF%E9%AA%A8%E6%9E%B6 sample"' + ' id="frame_一个模板骨架"' + ' width="160" height="160"' + - ' src="https://mdn.mozillademos.org/zh-CN/docs/Web/API/Canvas_API/Tutorial/Basic_usage/runner.html?id=%E4%B8%80%E4%B8%AA%E6%A8%A1%E6%9D%BF%E9%AA%A8%E6%9E%B6">' + + ' src="https://live.mdnplay.dev/zh-CN/docs/Web/API/Canvas_API/Tutorial/Basic_usage/runner.html?id=%E4%B8%80%E4%B8%AA%E6%A8%A1%E6%9D%BF%E9%AA%A8%E6%9E%B6">' + "" ); }); @@ -138,7 +139,7 @@ describeMacro("EmbedLiveSample", function () { ' title="Images sample"' + ' id="frame_images"' + ' width="100%" height=""><script>alert("XSS");</script>"' + - ' src="https://mdn.mozillademos.org/en-US/docs/Web/HTML/Element/figure/runner.html?id=images">' + + ' src="https://live.mdnplay.dev/en-US/docs/Web/HTML/Element/figure/runner.html?id=images">' + "" ); }); @@ -147,7 +148,7 @@ describeMacro("EmbedLiveSample", function () { ' title="Examples sample"' + ' id="frame_examples"' + ' width="700px" height="700px"' + - ' src="https://mdn.mozillademos.org/en-US/docs/Web/CSS/flex-wrap/runner.html?id=examples">' + + ' src="https://live.mdnplay.dev/en-US/docs/Web/CSS/flex-wrap/runner.html?id=examples">' + ""; itMacro("Three arguments: ID, width, height (same slug)", function (macro) { macro.ctx.env.url = "/en-US/docs/Web/CSS/flex-wrap"; diff --git a/kumascript/tests/macros/LiveSampleURL.test.ts b/kumascript/tests/macros/LiveSampleURL.test.ts index 2e0e59532cc7..8a72ccca3740 100644 --- a/kumascript/tests/macros/LiveSampleURL.test.ts +++ b/kumascript/tests/macros/LiveSampleURL.test.ts @@ -7,17 +7,19 @@ describeMacro("LiveSampleURL", function () { }); itMacro("Production settings", function (macro) { macro.ctx.env.live_samples = { - base_url: "https://mdn.mozillademos.org", + base_url: "https://live.mdnplay.dev", + legacy_url: "https://mdn.mozillademos.org", }; macro.ctx.env.url = "/en-US/docs/Web/HTML/Element/p"; return assert.eventually.equal( macro.call("Example"), - "https://mdn.mozillademos.org/en-US/docs/Web/HTML/Element/p/runner.html?id=Example" + "https://live.mdnplay.dev/en-US/docs/Web/HTML/Element/p/runner.html?id=Example" ); }); itMacro("Override page URL", function (macro) { macro.ctx.env.live_samples = { - base_url: "https://mdn.mozillademos.org", + base_url: "https://live.mdnplay.dev", + legacy_url: "https://mdn.mozillademos.org", }; macro.ctx.env.url = "/en-US/docs/Learn/HTML/Forms/How_to_build_custom_form_widgets"; @@ -31,7 +33,8 @@ describeMacro("LiveSampleURL", function () { }); itMacro("Override with nonexistent page URL", async (macro) => { macro.ctx.env.live_samples = { - base_url: "https://mdn.mozillademos.org", + base_url: "https://live.mdnplay.dev", + legacy_url: "https://mdn.mozillademos.org", }; macro.ctx.info.hasPage = jest.fn(() => false); macro.ctx.info.getDescription = jest.fn((url: string) => url.toLowerCase()); @@ -44,12 +47,13 @@ describeMacro("LiveSampleURL", function () { }); itMacro("Staging settings", function (macro) { macro.ctx.env.live_samples = { - base_url: "https://files-stage.mdn.mozit.cloud", + base_url: "https://live.mdnyalp.dev", + legacy_url: "https://mdn.mozillademos.org", }; macro.ctx.env.url = "/en-US/docs/Web/CSS/background-color"; return assert.eventually.equal( macro.call("Examples"), - "https://files-stage.mdn.mozit.cloud/en-US/docs/Web/CSS/background-color/runner.html?id=Examples" + "https://live.mdnyalp.dev/en-US/docs/Web/CSS/background-color/runner.html?id=Examples" ); }); itMacro("Development default settings", function (macro) { @@ -62,12 +66,13 @@ describeMacro("LiveSampleURL", function () { }); itMacro("Unicode ID", function (macro) { macro.ctx.env.live_samples = { - base_url: "https://mdn.mozillademos.org", + base_url: "https://live.mdnplay.dev", + legacy_url: "https://mdn.mozillademos.org", }; macro.ctx.env.url = "/zh-CN/docs/Web/CSS/flex-direction"; return assert.eventually.equal( macro.call("例子"), - "https://mdn.mozillademos.org/zh-CN/docs/Web/CSS/flex-direction/runner.html?id=%E4%BE%8B%E5%AD%90" + "https://live.mdnplay.dev/zh-CN/docs/Web/CSS/flex-direction/runner.html?id=%E4%BE%8B%E5%AD%90" ); }); itMacro("Development demo settings", function (macro) { From 2d782c204c6e834516e7f5edc2566a66b47dcccf Mon Sep 17 00:00:00 2001 From: Florian Dieminger Date: Mon, 26 Jun 2023 11:06:42 +0200 Subject: [PATCH 2/4] clean up macro --- kumascript/macros/LiveSampleURL.ejs | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/kumascript/macros/LiveSampleURL.ejs b/kumascript/macros/LiveSampleURL.ejs index 925d5d5a1bf2..b773c2b6efb4 100644 --- a/kumascript/macros/LiveSampleURL.ejs +++ b/kumascript/macros/LiveSampleURL.ejs @@ -4,25 +4,27 @@ // // Parameters: // -// $0 The ID of the sample's section or containing block. -// $1 The URL from which to pull the sample; if not provided, +// $0 - The ID of the sample's section or containing block. +// $1 - (Deprecated) The URL from which to pull the sample; if not provided, // the current page is assumed. -var pagePath = env.url; +let pagePath = env.url; +let baseUrl = env.live_samples.base_url; +let samplePath = `${pagePath}/runner.html`.replace(/\/\/+/g, "/"); if ($1 && $1.length > 0) { + mdn.deprecatedParams("The second parameter of 'LiveSampleURL' is deprecated"); pagePath = $1; + baseUrl = env.live_samples.legacy_url; + samplePath = `${pagePath}/_sample_.${web.slugify(web.safeDecodeURIComponent($0))}.html`.replace(/\/\/+/g, "/"); } -const searchParams = new URLSearchParams([["id", $0], ["url", $1]].filter(([_,v]) => Boolean(v))); wiki.ensureExistence(pagePath); -const samplePath = $1 ? - `${pagePath}/_sample_.${web.slugify(web.safeDecodeURIComponent($0))}.html`.replace(/\/\/+/g, "/") - : `${pagePath}/runner.html`.replace(/\/\/+/g, "/"); +const searchParams = new URLSearchParams([["id", $0], ["url", $1]].filter(([_,v]) => Boolean(v))); let url; -if (env.live_samples.base_url.includes('://')) { - url = new URL($1 ? env.live_samples.legacy_url : env.live_samples.base_url); +if (baseUrl.includes('://')) { + url = new URL(baseUrl); url.pathname = samplePath; url.search = searchParams.toString(); } else { From 035b44b7e60519316341dc9af042aad097cc26d9 Mon Sep 17 00:00:00 2001 From: Florian Dieminger Date: Mon, 26 Jun 2023 11:45:44 +0200 Subject: [PATCH 3/4] fix test --- kumascript/macros/LiveSampleURL.ejs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/kumascript/macros/LiveSampleURL.ejs b/kumascript/macros/LiveSampleURL.ejs index b773c2b6efb4..bb11bf155bd6 100644 --- a/kumascript/macros/LiveSampleURL.ejs +++ b/kumascript/macros/LiveSampleURL.ejs @@ -4,8 +4,8 @@ // // Parameters: // -// $0 - The ID of the sample's section or containing block. -// $1 - (Deprecated) The URL from which to pull the sample; if not provided, +// $0 The ID of the sample's section or containing block. +// $1 The URL from which to pull the sample; if not provided, // the current page is assumed. let pagePath = env.url; @@ -13,7 +13,6 @@ let baseUrl = env.live_samples.base_url; let samplePath = `${pagePath}/runner.html`.replace(/\/\/+/g, "/"); if ($1 && $1.length > 0) { - mdn.deprecatedParams("The second parameter of 'LiveSampleURL' is deprecated"); pagePath = $1; baseUrl = env.live_samples.legacy_url; samplePath = `${pagePath}/_sample_.${web.slugify(web.safeDecodeURIComponent($0))}.html`.replace(/\/\/+/g, "/"); From 73a163cac9739a50ea559f94651113da6d28e8c6 Mon Sep 17 00:00:00 2001 From: Florian Dieminger Date: Mon, 26 Jun 2023 11:51:32 +0200 Subject: [PATCH 4/4] clean up more --- kumascript/macros/LiveSampleURL.ejs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/kumascript/macros/LiveSampleURL.ejs b/kumascript/macros/LiveSampleURL.ejs index bb11bf155bd6..76da538ca2c7 100644 --- a/kumascript/macros/LiveSampleURL.ejs +++ b/kumascript/macros/LiveSampleURL.ejs @@ -8,18 +8,20 @@ // $1 The URL from which to pull the sample; if not provided, // the current page is assumed. +let id = $0; +let remoteUrl = $1; let pagePath = env.url; let baseUrl = env.live_samples.base_url; let samplePath = `${pagePath}/runner.html`.replace(/\/\/+/g, "/"); -if ($1 && $1.length > 0) { - pagePath = $1; +if (remoteUrl && remoteUrl.length > 0) { + pagePath = remoteUrl; baseUrl = env.live_samples.legacy_url; - samplePath = `${pagePath}/_sample_.${web.slugify(web.safeDecodeURIComponent($0))}.html`.replace(/\/\/+/g, "/"); + samplePath = `${pagePath}/_sample_.${web.slugify(web.safeDecodeURIComponent(id))}.html`.replace(/\/\/+/g, "/"); } wiki.ensureExistence(pagePath); -const searchParams = new URLSearchParams([["id", $0], ["url", $1]].filter(([_,v]) => Boolean(v))); +const searchParams = new URLSearchParams([["id", id], ["url", remoteUrl]].filter(([_,v]) => Boolean(v))); let url; if (baseUrl.includes('://')) {