GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Pass through nonce to the transformed script element by JLHwung · Pull Request #15671 · babel/babel

Pass through nonce to the transformed script element #15671

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
nicolo-ribaudo merged 6 commits into babel:main from JLHwung:pass-through-nonce
Sep 25, 2023

Uh oh!

There was an error while loading. Please reload this page.

Merged

Pass through nonce to the transformed script element #15671

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
6 commits
Select commit Hold shift + click to select a range
5206429
pass through nonce to transformed script element
JLHwung May 31, 2023
f1ca39b
add test
JLHwung May 31, 2023
9959990
make node 6 happy
JLHwung May 31, 2023
da5e835
remove semver dependency
JLHwung Jul 4, 2023
aaf9d5b
properly bind jest.it
JLHwung Jul 6, 2023
d8e0388
add describeGte
JLHwung Jul 6, 2023
File filter
Filter by extension


Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

3 changes: 2 additions & 1 deletion 3 packages/babel-standalone/package.json
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@
"@babel/preset-flow": "workspace:^",
"@babel/preset-react": "workspace:^",
"@babel/preset-typescript": "workspace:^",
"acorn": "^8.7.0"
"acorn": "^8.7.0",
"jsdom": "^22.1.0"
},
"keywords": [
"babel",
Expand Down
55 changes: 28 additions & 27 deletions 55 packages/babel-standalone/src/transformScriptTags.ts
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ type CompilationResult = {
loaded: boolean;
content: string | null;
executed: boolean;
// nonce is undefined in browsers that don't support the nonce global attribute
nonce: string | undefined;
// todo: refine plugins/presets
plugins: InputOptions["plugins"];
presets: InputOptions["presets"];
Expand Down Expand Up @@ -95,6 +97,9 @@ function run(transformFn: typeof transform, script: CompilationResult) {
if (script.type) {
scriptEl.setAttribute("type", script.type);
}
if (script.nonce) {
scriptEl.nonce = script.nonce;
}
scriptEl.text = transformCode(transformFn, script);
headEl.appendChild(scriptEl);
}
Expand Down Expand Up @@ -159,64 +164,60 @@ function loadScripts(
transformFn: typeof transform,
scripts: HTMLScriptElement[],
) {
const result: CompilationResult[] = [];
const results: CompilationResult[] = [];
const count = scripts.length;

function check() {
let script, i;

for (i = 0; i < count; i++) {
script = result[i];
for (let i = 0; i < count; i++) {
const result = results[i];

if (script.loaded && !script.executed) {
script.executed = true;
run(transformFn, script);
} else if (!script.loaded && !script.error && !script.async) {
if (result.loaded && !result.executed) {
result.executed = true;
run(transformFn, result);
} else if (!result.loaded && !result.error && !result.async) {
break;
}
}
}

scripts.forEach((script, i) => {
const scriptData = {
for (let i = 0; i < count; i++) {
const script = scripts[i];
const result: CompilationResult = {
// script.async is always true for non-JavaScript script tags
async: script.hasAttribute("async"),
type: script.getAttribute("data-type"),
nonce: script.nonce,
error: false,
executed: false,
plugins: getPluginsOrPresetsFromScript(script, "data-plugins"),
presets: getPluginsOrPresetsFromScript(script, "data-presets"),
loaded: false,
url: null,
content: null,
};
results.push(result);

if (script.src) {
result[i] = {
...scriptData,
content: null,
loaded: false,
url: script.src,
};
result.url = script.src;

load(
script.src,
content => {
result[i].loaded = true;
result[i].content = content;
result.loaded = true;
result.content = content;
check();
},
() => {
result[i].error = true;
result.error = true;
check();
},
);
} else {
result[i] = {
...scriptData,
content: script.innerHTML,
loaded: true,
url: script.getAttribute("data-module") || null,
};
result.url = script.getAttribute("data-module") || null;
result.loaded = true;
result.content = script.innerHTML;
}
});
}

check();
}
Expand Down
57 changes: 57 additions & 0 deletions 57 packages/babel-standalone/test/transform-script-tags.test.js
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import fs from "fs";
import { createRequire } from "module";
import { describeGte } from "$repo-utils";
const require = createRequire(import.meta.url);

describeGte("16.0.0")("transformScriptTags", () => {
let standaloneSource;
let JSDOM;
beforeAll(async () => {
standaloneSource = fs.readFileSync(
new URL("../babel.js", import.meta.url),
"utf8",
);
JSDOM = require("jsdom").JSDOM;
});
it("should transform script element with type 'text/babel'", () => {
const dom = new JSDOM(
`<!DOCTYPE html><head><script>${standaloneSource}</script><script type="text/babel">globalThis ?? window</script></head>`,
{ runScripts: "dangerously" },
);
return new Promise((resolve, reject) => {
dom.window.addEventListener("DOMContentLoaded", () => {
try {
const transformedScriptElement =
dom.window.document.head.children.item(2);
expect(transformedScriptElement.getAttribute("type")).toBeNull();
expect(transformedScriptElement.innerHTML).toContain(
"globalThis !== null && globalThis !== void 0 ? globalThis : window",
);
resolve();
} catch (err) {
reject(err);
}
});
});
});
it("should pass through the nonce attribute to the transformed script element", () => {
const nonceAttribute = "nonce_example";

const dom = new JSDOM(
`<!DOCTYPE html><head><script>${standaloneSource}</script><script type="text/babel" nonce="${nonceAttribute}">globalThis ?? window</script></head>`,
{ runScripts: "dangerously" },
);
return new Promise((resolve, reject) => {
dom.window.addEventListener("DOMContentLoaded", () => {
try {
const transformedScriptElement =
dom.window.document.head.children.item(2);
expect(transformedScriptElement.nonce).toBe(nonceAttribute);
resolve();
} catch (err) {
reject(err);
}
});
});
});
});
3 changes: 3 additions & 0 deletions 3 scripts/repo-utils/index.cjs
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ if (typeof jest !== "undefined") {
exports.describeESM = USE_ESM ? describe : dummy;
exports.describeBabel7 = process.env.BABEL_8_BREAKING ? dummy : describe;
exports.describeBabel8 = process.env.BABEL_8_BREAKING ? describe : dummy;
exports.describeGte = function (version) {
return semver.gte(process.version, version) ? describe : describe.skip;
};
}

exports.commonJS = function (metaUrl) {
Expand Down
1 change: 1 addition & 0 deletions 1 scripts/repo-utils/index.d.ts
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ export const itBabel7NoESM: jest.It;
export const itDummy: jest.It;
export const describeBabel7: jest.Describe;
export const describeBabel8: jest.Describe;
export function describeGte(version: string): jest.Describe;
Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.