Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 5 additions & 12 deletions src/dom/globals/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,21 @@ export function open(
/**
* Returns CSP nonce, if set for any script tag.
*/
export function getScriptNonce(documentOrWindow?: Document | Window): string {
return getNonceFor('script', documentOrWindow);
export function getScriptNonce(doc?: Document): string {
return getNonceFor('script', doc);
}

/**
* Returns CSP nonce, if set for any style tag.
*/
export function getStyleNonce(documentOrWindow?: Document | Window): string {
return getNonceFor('style', documentOrWindow);
export function getStyleNonce(doc?: Document): string {
return getNonceFor('style', doc);
}

function getNonceFor(
elementName: 'script' | 'style',
documentOrWindow: Document | Window = document,
doc: Document = document,
): string {
// Note: We can't use `instanceof` here because `documentOrWindow` likely
// comes from a different realm.
const doc =
'document' in documentOrWindow
? documentOrWindow.document
: documentOrWindow;

// document.querySelector can be undefined in non-browser environments.
const el = doc.querySelector?.<HTMLScriptElement | HTMLStyleElement>(
`${elementName}[nonce]`,
Expand Down