Skip to content

feat(playground): add share data url button#1266

Open
LeoMcA wants to merge 8 commits intomainfrom
playground-copy-data-uri
Open

feat(playground): add share data url button#1266
LeoMcA wants to merge 8 commits intomainfrom
playground-copy-data-uri

Conversation

@LeoMcA
Copy link
Member

@LeoMcA LeoMcA commented Jan 30, 2026

@caugner as we briefly discussed in our 1:1

since working on devtools, I've noticed it's quite a common/nice pattern to share testcases (on slack, bugzilla, etc.) with data:text/html urls. authoring them is a bit of pain though: I asked the team if there was any better way than manually typing into the url bar... and they told me that's what they were all doing!

I've added in the simplest possible way for now: obviously references to static assets are going to break, we could remove non-syntactic line breaks to make the urls a bit shorter/prettier, etc. but we can make those improvements if the metrics show any usage/we get direct feedback about it.

I did make the effort to only urlencode non-space whitespace as otherwise the urls are very un-readable, and what's nice about the practice is you can read what the page you're about to load is before pasting it in. Firefox/Chromium/Epiphany seem to handle unencoded utf8 characters just fine when pasted in (I'll check in Safari once my mac is charged).

For an example where newlines are important:

hello
world
❤️
body {
  white-space: pre;
}
console.log(`hello
world!`);

The generated url is readable, and functional!

data:text/html;charset=utf-8,<!doctype html><style>body {%0A white-space: pre;%0A}%0A</style>hello%0Aworld%0A❤️<script>console.log(`hello%0Aworld!`);%0A</script>

@LeoMcA LeoMcA requested review from a team and caugner January 30, 2026 16:18
@github-actions
Copy link
Contributor

github-actions bot commented Jan 30, 2026

022c787 was deployed to: https://fred-pr1266.review.mdn.allizom.net/

const { css, html, js } = controller.code;
let code = `<!doctype html>`;
if (css) code += `<style>${css}</style>`;
if (html) code += html;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: What if html contains the doctype, or a full HTML structure? (I guess in this case, chances are css is empty.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have the same issue in the playground/runner currently: a user could add a full HTML structure to the HTML input.

I don't think it's really a problem, HTML parsers are able to work around it, but if we did want to fix it we should do it cohesively as a separate issue.

Copy link
Contributor

@caugner caugner Feb 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's a reason to set a more complete HTML structure.

Copy the following in the Playground:

document.body.style.backgroundColor = 'DeviceMotionEventAcceleration' in window ? 'green' : 'red';

It is green in the Playground runner, but when copying and opening the data URL, the page is white, because there is no body.

@LeoMcA LeoMcA requested a review from mdn-bot as a code owner February 10, 2026 12:37
@LeoMcA LeoMcA requested a review from caugner February 10, 2026 13:36
Copy link
Contributor

@caugner caugner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. I support this feature, and will probably use it frequently.

Before merging, let's briefly discuss it in today's meeting.

Comment on lines +44 to +62
it("should only uri-encode non-space whitespace and percent symbols", async () => {
await $(`mdn-play-editor[language="html"]`).click();
await browser.keys("@\n@ @%20 @");
await $(`mdn-play-editor[language="css"]`).click();
await browser.keys("body {\n font-size: 5em;\n}");

await $(`[data-id="share"]:not([disabled])`).click();
await $(`[data-glean-id="playground: share-data-url"]`).click();

// will cause a webdriver error about permissions, but we've set them in firefox through about:config
await expect(browser).toHaveClipboardText(
expect.stringContaining("@%0A@ @%2520 @"),
);
await expect(browser).toHaveClipboardText(
expect.stringContaining(
"<style>body {%0A font-size: 5em;%0A}</style>",
),
);
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] I would expect this to be a unit test, rather than an e2e test.

Comment on lines +20 to +21
"dom.events.testing.asyncClipboard": true,
"dom.events.testing.asyncClipboard.readText": true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] Are these here to stay,? Is there a page or bug with more info?

describe("Playground", () => {
describe("copy data url", () => {
beforeEach(async () => {
await DocPage.open("en-US/play");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] Why is there no leading slash here?

Suggested change
await DocPage.open("en-US/play");
await DocPage.open("/en-US/play");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants